X-Git-Url: http://p2p-next.cs.pub.ro/gitweb/?a=blobdiff_plain;f=cis%2Fapi%2Favhandling.py;h=27c9a5d0c07849bad8d9cf3f4ebd0102ec134ead;hb=c7b70f31aa7d34d184c20ac6258fec2174ea8d67;hp=85cf908ccb47e5c7eb4d7fd383020207de14399b;hpb=1ab12b6c509b6a6be95f79dc50089649a32872da;p=living-lab-site.git diff --git a/cis/api/avhandling.py b/cis/api/avhandling.py index 85cf908..27c9a5d 100644 --- a/cis/api/avhandling.py +++ b/cis/api/avhandling.py @@ -11,6 +11,7 @@ import api_exceptions import subprocess import re import os +import math class FFmpegTranscoder(base.BaseTranscoder): """ @@ -95,6 +96,8 @@ class FFmpegTranscoder(base.BaseTranscoder): log.close() + return self.output_file + class FFmpegThumbExtractor(base.BaseThumbExtractor): """ @@ -141,35 +144,55 @@ class FFmpegThumbExtractor(base.BaseThumbExtractor): 'FFmpeg created an empty file.') def get_video_duration(self): - return FFmpegAVInfo.get_video_duration(self.input_file) + return FFprobeAVInfo.get_video_duration(self.input_file) -class FFmpegAVInfo(base.BaseAVInfo): +class FFprobeAVInfo(base.BaseAVInfo): prog_bin = "ffprobe" + log_file = 'log/FFprobeAVInfo.log' + @staticmethod - def get_video_duration(input_file): - args = FFmpegAVInfo.prog_bin + ' -show_format "' \ + def get_video_duration(input_file, formated=False): + args = FFprobeAVInfo.prog_bin + ' -show_format "' \ + input_file + '"' # READ handler for process's output. p = subprocess.Popen(args, shell=True, - stdout=subprocess.PIPE, stderr=open(os.devnull, 'w')) + stdout=subprocess.PIPE, stderr=subprocess.STDOUT) pipe = p.stdout + # WRITE handler for logging. + log = open(FFprobeAVInfo.log_file, 'w') + log.write(args + '\n') + # Parse ffprobe's output. while True: line = pipe.readline() if len(line) == 0: break + log.write(line) # Search for the line which contains duration information. m = re.match(r"duration=([\d\.]+)", line) if m is not None: - return float(m.group(1)) + seconds = float(m.group(1)) + if not formated: + return seconds + else: + seconds = math.floor(seconds) + minutes = math.floor(seconds / 60) + seconds = seconds % 60 + if minutes >= 60: + hours = math.floor(minutes / 60) + minutes = minutes % 60 + + return "%02d:%02d:%02d" % (hours, minutes, seconds) + else: + return "%02d:%02d" % (minutes, seconds) exit_code = p.wait() if exit_code > 0: - raise api_exceptions.ThumbExtractionException( \ - 'FFmpeg exited with code ' + str(exit_code) + '.') + raise api_exceptions.AVInfoException( \ + 'ffprobe exited with code ' + str(exit_code) + '.')