From 30d006c1bbb97f722850d451e1ed11f46f45fe0e Mon Sep 17 00:00:00 2001 From: Jerry Yan <792602257@qq.com> Date: Wed, 11 May 2022 15:28:08 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=8C=E5=8D=A1=E5=8A=A0=E6=8C=81=EF=BC=88?= =?UTF-8?q?=E8=99=BD=E7=84=B6=E4=B8=8D=E4=B8=80=E5=AE=9A=E7=94=A8=EF=BC=89?= =?UTF-8?q?=EF=BC=8C=E9=81=BF=E5=85=8D=E7=94=9F=E6=88=90=E9=9F=B3=E8=A7=86?= =?UTF-8?q?=E9=A2=91=E9=95=BF=E7=9F=AD=E4=B8=8D=E4=B8=80=E7=9A=84=E6=96=87?= =?UTF-8?q?=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- workflow/video.py | 41 ++++++++++++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 11 deletions(-) diff --git a/workflow/video.py b/workflow/video.py index 02dae47..87c2fa1 100644 --- a/workflow/video.py +++ b/workflow/video.py @@ -32,58 +32,72 @@ def multi_gpu_encode_video_with_subtitles(orig_filename: str, subtitles: list[st return [new_filename] _duration_str = get_video_real_duration(orig_filename) duration = duration_str_to_float(_duration_str) - if duration > (VIDEO_CLIP_EACH_SEC * 3): + if duration > (VIDEO_CLIP_EACH_SEC * 5): # qsv 压制前2段,剩余交由nvenc压制 - new_filename0 = base_ts_to_filename(base_ts) - new_filename1 = base_ts_to_filename(base_ts + VIDEO_CLIP_EACH_SEC * 2) + _slices = int(duration / VIDEO_CLIP_EACH_SEC) + new_filename0 = base_ts_to_filename(base_ts + (_slices - 1) * VIDEO_CLIP_EACH_SEC) + new_filename1 = base_ts_to_filename(base_ts) + print("[+]Use Intel QSV Acceleration") encode_process0 = subprocess.Popen([ FFMPEG_EXEC, "-hide_banner", "-progress", "-", "-loglevel", "error", "-y", - "-hwaccel", "qsv", "-t", str(VIDEO_CLIP_EACH_SEC * 2 + (VIDEO_CLIP_OVERFLOW_SEC * 0.8)), "-i", orig_filename, "-vf", + "-hwaccel", "qsv", "-ss", str((_slices - 1) * VIDEO_CLIP_EACH_SEC), + "-copyts", "-i", orig_filename, "-vf", ",".join("subtitles=%s" % i for i in subtitles), "-c:a", "copy", "-c:v", "h264_qsv", "-f", "mp4", "-preset:v", "fast", "-profile:v", "high", "-level", "4.1", "-b:v", VIDEO_BITRATE, "-rc:v", "vbr", "-tune:v", "hq", "-qmin", "10", "-qmax", "32", "-crf", "16", + "-fflags", "+genpts", "-shortest", "-movflags", "faststart", # "-t", "10", new_filename0 ]) + print("[+]Use Nvidia NvEnc Acceleration") encode_process1 = subprocess.Popen([ FFMPEG_EXEC, "-hide_banner", "-progress", "-", "-loglevel", "error", "-y", - "-ss", str(VIDEO_CLIP_EACH_SEC * 2), "-copyts", "-i", orig_filename, "-vf", + "-t", str((_slices - 1) * VIDEO_CLIP_EACH_SEC + (VIDEO_CLIP_OVERFLOW_SEC * 0.8)), + "-i", orig_filename, "-vf", ",".join("subtitles=%s" % i for i in subtitles), "-c:a", "copy", "-c:v", "h264_nvenc", "-ss", str(VIDEO_CLIP_EACH_SEC * 2), "-f", "mp4", "-preset:v", "fast", "-profile:v", "high", "-level", "4.1", "-b:v", VIDEO_BITRATE, "-rc:v", "vbr", "-tune:v", "hq", "-qmin", "10", "-qmax", "32", "-crf", "16", + "-fflags", "+genpts", "-shortest", "-movflags", "faststart", # "-t", "10", new_filename1 ]) - encode_process1.wait() encode_process0.wait() + encode_process1.wait() return [new_filename0, new_filename1] - elif duration > (VIDEO_CLIP_EACH_SEC * 1): + elif duration > (VIDEO_CLIP_EACH_SEC * 3): # 至少也要能切2片,才用双GPU加速 - new_filename0 = base_ts_to_filename(base_ts, True) - new_filename1 = base_ts_to_filename(base_ts + VIDEO_CLIP_EACH_SEC) + _slices = int(duration / VIDEO_CLIP_EACH_SEC) + new_filename0 = base_ts_to_filename(base_ts + _slices * VIDEO_CLIP_EACH_SEC, True) + new_filename1 = base_ts_to_filename(base_ts) + print("[+]Use Intel QSV Acceleration") encode_process0 = subprocess.Popen([ FFMPEG_EXEC, "-hide_banner", "-progress", "-", "-loglevel", "error", "-y", - "-hwaccel", "qsv", "-t", str(VIDEO_CLIP_EACH_SEC + (VIDEO_CLIP_OVERFLOW_SEC * 0.8)), "-i", orig_filename, "-vf", + "-hwaccel", "qsv", "-ss", str(_slices * VIDEO_CLIP_EACH_SEC), + "-copyts", "-i", orig_filename, "-vf", ",".join("subtitles=%s" % i for i in subtitles), "-c:a", "copy", "-c:v", "h264_qsv", "-f", "mp4", "-preset:v", "fast", "-profile:v", "high", "-level", "4.1", "-b:v", VIDEO_BITRATE, "-rc:v", "vbr", "-tune:v", "hq", "-qmin", "10", "-qmax", "32", "-crf", "16", + "-fflags", "+genpts", "-shortest", "-movflags", "faststart", # "-t", "10", new_filename0 ]) + print("[+]Use Nvidia NvEnc Acceleration") encode_process1 = subprocess.Popen([ FFMPEG_EXEC, "-hide_banner", "-progress", "-", "-loglevel", "error", "-y", - "-ss", str(VIDEO_CLIP_EACH_SEC), "-copyts", "-i", orig_filename, "-vf", + "-t", str(_slices * VIDEO_CLIP_EACH_SEC + (VIDEO_CLIP_OVERFLOW_SEC * 0.8)), + "-i", orig_filename, "-vf", ",".join("subtitles=%s" % i for i in subtitles), "-c:a", "copy", "-c:v", "h264_nvenc", "-ss", str(VIDEO_CLIP_EACH_SEC), "-f", "mp4", "-preset:v", "fast", "-profile:v", "high", "-level", "4.1", "-b:v", VIDEO_BITRATE, "-rc:v", "vbr", "-tune:v", "hq", "-qmin", "10", "-qmax", "32", "-crf", "16", + "-fflags", "+genpts", "-shortest", "-movflags", "faststart", # "-t", "10", new_filename1 ]) @@ -108,6 +122,7 @@ def encode_video_with_subtitles(orig_filename: str, subtitles: list[str], new_fi "-f", "mp4", "-preset:v", "fast", "-profile:v", "high", "-level", "4.1", "-b:v", VIDEO_BITRATE, "-rc:v", "vbr", "-tune:v", "hq", "-qmin", "10", "-qmax", "32", "-crf", "16", + "-fflags", "+genpts", "-shortest", "-movflags", "faststart", # "-t", "10", new_filename ], stdout=subprocess.PIPE) @@ -122,6 +137,7 @@ def encode_video_with_subtitles(orig_filename: str, subtitles: list[str], new_fi "-f", "mp4", "-preset:v", "fast", "-profile:v", "high", "-level", "4.1", "-b:v", VIDEO_BITRATE, "-rc:v", "vbr", "-tune:v", "hq", "-qmin", "10", "-qmax", "32", "-crf", "16", + "-fflags", "+genpts", "-shortest", "-movflags", "faststart", # "-t", "10", new_filename ], stdout=subprocess.PIPE) @@ -135,6 +151,7 @@ def encode_video_with_subtitles(orig_filename: str, subtitles: list[str], new_fi "-f", "mp4", "-preset:v", "fast", "-profile:v", "high", "-level", "4.1", "-b:v", VIDEO_BITRATE, "-rc:v", "vbr", "-tune:v", "hq", "-qmin", "10", "-qmax", "32", "-crf", "16", + "-fflags", "+genpts", "-shortest", "-movflags", "faststart", # "-t", "10", new_filename ], stdout=subprocess.PIPE) @@ -148,6 +165,7 @@ def encode_video_with_subtitles(orig_filename: str, subtitles: list[str], new_fi "-f", "mp4", "-preset:v", "fast", "-profile:v", "high", "-level", "4.1", "-b:v", VIDEO_BITRATE, "-rc:v", "vbr", "-qmin", "10", "-qmax", "32", "-crf", "16", + "-fflags", "+genpts", "-shortest", "-movflags", "faststart", # "-t", "10", new_filename ], stdout=subprocess.PIPE) @@ -200,6 +218,7 @@ def quick_split_video(file): "-ss", str(current_sec), "-i", file_name, "-c", "copy", "-f", "mp4", "-t", str(VIDEO_CLIP_EACH_SEC + VIDEO_CLIP_OVERFLOW_SEC), + "-fflags", "+genpts", "-shortest", "-movflags", "faststart", "{}.mp4".format(current_dt) ], stdout=subprocess.PIPE) handle_ffmpeg_output(split_process.stdout)