diff --git a/workflow/video.py b/workflow/video.py index 5d50eba..2636f80 100644 --- a/workflow/video.py +++ b/workflow/video.py @@ -1,5 +1,4 @@ import os -import re import subprocess from datetime import datetime, timedelta from typing import IO @@ -9,15 +8,9 @@ from config import FFMPEG_EXEC, VIDEO_BITRATE, FFMPEG_USE_GPU, VIDEO_CLIP_EACH_S def get_video_real_duration(filename): ffmpeg_process = subprocess.Popen([ - "ffmpeg", "-hide_banner", "-i", filename, "-c", "copy", "-f", "null", "-" - ], stdout=subprocess.PIPE, stderr=subprocess.PIPE) - result = "0:0:0.0" - for line in ffmpeg_process.stderr.readlines(): - match_result = re.findall("(?<= time=).+?(?= )", line.decode()) - if len(match_result) == 0: - continue - result = match_result.pop() - return result + "ffmpeg", "-hide_banner", "-progress", "-", "-v", "0", "-i", filename, "-c", "copy", "-f", "null", "-" + ], stdout=subprocess.PIPE) + return handle_ffmpeg_output(ffmpeg_process.stdout) def encode_video_with_subtitles(orig_filename: str, subtitles: list[str], new_filename: str): @@ -36,21 +29,27 @@ def encode_video_with_subtitles(orig_filename: str, subtitles: list[str], new_fi return encode_process.wait() -def handle_ffmpeg_output(stderr: IO[bytes]) -> None: +def handle_ffmpeg_output(stderr: IO[bytes]) -> str: + out_time = "0:0:0.0" while True: line = stderr.readline() if line == b"": break + if line.strip() == b"progress=end": + # 处理完毕 + break if PROD_ENV: # 正式环境不要输出一大堆的东西了 continue if line.startswith(b"out_time="): cur_time = line.replace(b"out_time=", b"").decode() print("CurTime", cur_time.strip()) + out_time = cur_time.strip() if line.startswith(b"speed="): speed = line.replace(b"speed=", b"").decode() print("Speed", speed.strip()) + return out_time def duration_str_to_float(duration_str) -> float: