output handle progress=end line

This commit is contained in:
Jerry Yan 2022-04-19 10:24:32 +08:00
parent 0865122c43
commit a9387f7b1d

View File

@ -1,5 +1,4 @@
import os import os
import re
import subprocess import subprocess
from datetime import datetime, timedelta from datetime import datetime, timedelta
from typing import IO 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): def get_video_real_duration(filename):
ffmpeg_process = subprocess.Popen([ ffmpeg_process = subprocess.Popen([
"ffmpeg", "-hide_banner", "-i", filename, "-c", "copy", "-f", "null", "-" "ffmpeg", "-hide_banner", "-progress", "-", "-v", "0", "-i", filename, "-c", "copy", "-f", "null", "-"
], stdout=subprocess.PIPE, stderr=subprocess.PIPE) ], stdout=subprocess.PIPE)
result = "0:0:0.0" return handle_ffmpeg_output(ffmpeg_process.stdout)
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
def encode_video_with_subtitles(orig_filename: str, subtitles: list[str], new_filename: str): 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() 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: while True:
line = stderr.readline() line = stderr.readline()
if line == b"": if line == b"":
break break
if line.strip() == b"progress=end":
# 处理完毕
break
if PROD_ENV: if PROD_ENV:
# 正式环境不要输出一大堆的东西了 # 正式环境不要输出一大堆的东西了
continue continue
if line.startswith(b"out_time="): if line.startswith(b"out_time="):
cur_time = line.replace(b"out_time=", b"").decode() cur_time = line.replace(b"out_time=", b"").decode()
print("CurTime", cur_time.strip()) print("CurTime", cur_time.strip())
out_time = cur_time.strip()
if line.startswith(b"speed="): if line.startswith(b"speed="):
speed = line.replace(b"speed=", b"").decode() speed = line.replace(b"speed=", b"").decode()
print("Speed", speed.strip()) print("Speed", speed.strip())
return out_time
def duration_str_to_float(duration_str) -> float: def duration_str_to_float(duration_str) -> float: