def ffmpeg_probe_frame_count(self): """ probe self.file and return frame count """ instance = Popen(["ffprobe", self.file.filename], stdout=subprocess.PIPE, stderr=subprocess.PIPE) output = "" for line in instance.stderr: output += line.decode("utf8") # call sleep, see https://stackoverflow.com/questions/34599578/using-popen-in-a-thread-blocks-every-incoming-flask-socketio-request eventlet.sleep() fps_reg = re.findall(r"([0-9]*\.?[0-9]+) fps|tbr", output) if fps_reg is None: return -1 fps = float(" ".join(fps_reg)) duration = duration_to_seconds( re.findall(r"Duration: (.*?),", output)[0]) # calculate the amount of frames for the calculation of progress frame_count = int(math.ceil(duration * float(fps))) return frame_count
def ffmpeg_probe_frame_count(self): """ probe self.file and return frame count """ instance = Popen(["ffprobe", self.file.filename], stdout=subprocess.PIPE, stderr=subprocess.PIPE) output = "" for line in instance.stderr: output += line.decode("utf8") # call sleep, see https://stackoverflow.com/questions/34599578/using-popen-in-a-thread-blocks-every-incoming-flask-socketio-request eventlet.sleep() fps_reg = re.findall(r"([0-9]*\.?[0-9]+) fps|tbr", output) if fps_reg is None: return -1 fps = float(" ".join(fps_reg)) duration = duration_to_seconds(re.findall(r"Duration: (.*?),", output)[0]) # calculate the amount of frames for the calculation of progress frame_count = int(math.ceil(duration * float(fps))) return frame_count
def run_ffmpeg(self, cmd, frame_count): """ run ffmpeg with given cmd arguments and a frame count :param cmd: the command line dictionary containing all the arguments :param frame_count: the amount of frames of this video, necessary for the progress calculation :return: """ instance = Popen(map(str, cmd), stdout=subprocess.PIPE, stderr=subprocess.PIPE) reader = io.TextIOWrapper(instance.stderr, encoding="utf8") # these two variables are just needed for when the processing fails, see below last_lines = deque( maxlen=5) # parameter determines how many lines to keep # oddly ffmpeg writes to stderr instead of stdout for line in reader: # kill ffmpeg when not being active anymore if not self.active: instance.kill() # call sleep, see https://stackoverflow.com/questions/34599578/using-popen-in-a-thread-blocks-every-incoming-flask-socketio-request eventlet.sleep() # append current line to last_lines last_lines.append(line) match = PROGRESS_PATTERN.match(line) # first few lines have no match if match: frame = int(match.group( 1)) # current frame, needed for calculation of progress fps = float( match.group(2)) # needed for calculation of remaining time size = int(match.group(4)) # current size in kB time = duration_to_seconds(match.group( 5)) # time already passed for converting, in seconds bitrate = float(match.group(6)) # in kbits/s progress = round((frame / float(frame_count)) * 100, 1) # in % frames_remaining = frame_count - frame # needed for eta eta = frames_remaining / fps if fps != 0 else -1 # in seconds yield { "return_code": -1, "ffmpeg_eta": eta, "ffmpeg_progress": progress, "ffmpeg_bitrate": bitrate, "ffmpeg_time": time, "ffmpeg_size": size, "ffmpeg_fps": fps } return_code = instance.wait() if return_code != 0: yield {"return_code": return_code, "last_lines": last_lines}
def run_ffmpeg(self, cmd, frame_count): """ run ffmpeg with given cmd arguments and a frame count :param cmd: the command line dictionary containing all the arguments :param frame_count: the amount of frames of this video, necessary for the progress calculation :return: """ instance = Popen(map(str, cmd), stdout=subprocess.PIPE, stderr=subprocess.PIPE) reader = io.TextIOWrapper(instance.stderr, encoding="utf8") # these two variables are just needed for when the processing fails, see below last_lines = deque(maxlen=5) # parameter determines how many lines to keep # oddly ffmpeg writes to stderr instead of stdout for line in reader: # kill ffmpeg when not being active anymore if not self.active: instance.kill() # call sleep, see https://stackoverflow.com/questions/34599578/using-popen-in-a-thread-blocks-every-incoming-flask-socketio-request eventlet.sleep() # append current line to last_lines last_lines.append(line) match = PROGRESS_PATTERN.match(line) # first few lines have no match if match: frame = int(match.group(1)) # current frame, needed for calculation of progress fps = float(match.group(2)) # needed for calculation of remaining time size = int(match.group(4)) # current size in kB time = duration_to_seconds(match.group(5)) # time already passed for converting, in seconds bitrate = float(match.group(6)) # in kbits/s progress = round((frame / float(frame_count)) * 100, 1) # in % frames_remaining = frame_count - frame # needed for eta eta = frames_remaining / fps if fps != 0 else -1 # in seconds yield {"return_code": -1, "ffmpeg_eta": eta, "ffmpeg_progress": progress, "ffmpeg_bitrate": bitrate, "ffmpeg_time": time, "ffmpeg_size": size, "ffmpeg_fps": fps} return_code = instance.wait() if return_code != 0: yield {"return_code": return_code, "last_lines": last_lines}