From 7a18d04cc7bf4b77f126a8d8ebeb54dc77ce409f Mon Sep 17 00:00:00 2001 From: Jerry Yan <792602257@qq.com> Date: Wed, 18 May 2022 10:44:22 +0800 Subject: [PATCH] =?UTF-8?q?ffmpeg=20=E8=AE=BE=E7=BD=AE=E7=BB=9F=E4=B8=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- workflow/video.py | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/workflow/video.py b/workflow/video.py index b582484..5b352b8 100644 --- a/workflow/video.py +++ b/workflow/video.py @@ -18,7 +18,7 @@ def base_ts_to_filename(start_ts: float, is_mp4=False) -> str: def get_video_real_duration(filename): ffmpeg_process = subprocess.Popen([ - FFMPEG_EXEC, "-hide_banner", "-progress", "-", "-loglevel", "error", "-i", filename, "-c", "copy", "-f", "null", "-" + FFMPEG_EXEC, *_common_ffmpeg_setting(), "-i", filename, "-c", "copy", "-f", "null", "-" ], stdout=subprocess.PIPE) return handle_ffmpeg_output(ffmpeg_process.stdout) @@ -41,7 +41,7 @@ def multi_gpu_encode_video_with_subtitles(orig_filename: str, subtitles: list[st new_fullpath1 = os.path.join(VIDEO_OUTPUT_DIR, new_filename1) print("[+]Use Intel QSV Acceleration") encode_process0 = subprocess.Popen([ - FFMPEG_EXEC, "-hide_banner", "-progress", "-", "-loglevel", "error", "-y", + FFMPEG_EXEC, *_common_ffmpeg_setting(), "-hwaccel", "qsv", "-ss", str((_slices - 1) * VIDEO_CLIP_EACH_SEC), "-copyts", "-i", orig_filename, "-vf", ",".join("subtitles=%s" % i for i in subtitles), @@ -53,7 +53,7 @@ def multi_gpu_encode_video_with_subtitles(orig_filename: str, subtitles: list[st ]) print("[+]Use Nvidia NvEnc Acceleration") encode_process1 = subprocess.Popen([ - FFMPEG_EXEC, "-hide_banner", "-progress", "-", "-loglevel", "error", "-y", + FFMPEG_EXEC, *_common_ffmpeg_setting(), "-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), @@ -75,7 +75,7 @@ def multi_gpu_encode_video_with_subtitles(orig_filename: str, subtitles: list[st new_fullpath1 = os.path.join(VIDEO_OUTPUT_DIR, new_filename1) print("[+]Use Intel QSV Acceleration") encode_process0 = subprocess.Popen([ - FFMPEG_EXEC, "-hide_banner", "-progress", "-", "-loglevel", "error", "-y", + FFMPEG_EXEC, *_common_ffmpeg_setting(), "-hwaccel", "qsv", "-ss", str(_slices * VIDEO_CLIP_EACH_SEC), "-copyts", "-i", orig_filename, "-vf", ",".join("subtitles=%s" % i for i in subtitles), @@ -87,7 +87,7 @@ def multi_gpu_encode_video_with_subtitles(orig_filename: str, subtitles: list[st ]) print("[+]Use Nvidia NvEnc Acceleration") encode_process1 = subprocess.Popen([ - FFMPEG_EXEC, "-hide_banner", "-progress", "-", "-loglevel", "error", "-y", + FFMPEG_EXEC, *_common_ffmpeg_setting(), "-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), @@ -111,7 +111,7 @@ def encode_video_with_subtitles(orig_filename: str, subtitles: list[str], new_fi if FFMPEG_USE_NVIDIA_GPU: print("[+]Use Nvidia NvEnc Acceleration") encode_process = subprocess.Popen([ - FFMPEG_EXEC, "-hide_banner", "-progress", "-", "-loglevel", "error", "-y", + FFMPEG_EXEC, *_common_ffmpeg_setting(), "-i", orig_filename, "-vf", ",".join("subtitles=%s" % i for i in subtitles) + ",hwupload_cuda", "-c:a", "copy", "-c:v", "h264_nvenc", @@ -124,7 +124,7 @@ def encode_video_with_subtitles(orig_filename: str, subtitles: list[str], new_fi if platform.system().lower() == "windows": print("[+]Use Intel QSV Acceleration") encode_process = subprocess.Popen([ - FFMPEG_EXEC, "-hide_banner", "-progress", "-", "-loglevel", "error", "-y", + FFMPEG_EXEC, *_common_ffmpeg_setting(), "-hwaccel", "qsv", "-i", orig_filename, "-vf", ",".join("subtitles=%s" % i for i in subtitles), "-c:a", "copy", "-c:v", "h264_qsv", @@ -136,7 +136,7 @@ def encode_video_with_subtitles(orig_filename: str, subtitles: list[str], new_fi else: print("[+]Use Intel VAAPI Acceleration") encode_process = subprocess.Popen([ - FFMPEG_EXEC, "-hide_banner", "-progress", "-", "-loglevel", "error", "-y", + FFMPEG_EXEC, *_common_ffmpeg_setting(), "-hwaccel", "vaapi", "-i", orig_filename, "-vf", ",".join("subtitles=%s" % i for i in subtitles) + ",hwupload", "-c:a", "copy", "-c:v", "h264_vaapi", @@ -148,7 +148,7 @@ def encode_video_with_subtitles(orig_filename: str, subtitles: list[str], new_fi else: print("[+]Use CPU Encode") encode_process = subprocess.Popen([ - FFMPEG_EXEC, "-hide_banner", "-progress", "-", "-loglevel", "error", "-y", + FFMPEG_EXEC, *_common_ffmpeg_setting(), "-i", orig_filename, "-vf", ",".join("subtitles=%s" % i for i in subtitles), "-c:a", "copy", "-c:v", "h264", @@ -205,7 +205,7 @@ def quick_split_video(file): print("CUR_DT", current_dt) print("BIAS_T", current_sec) split_process = subprocess.Popen([ - FFMPEG_EXEC, "-y", "-hide_banner", "-progress", "-", "-loglevel", "error", + FFMPEG_EXEC, *_common_ffmpeg_setting(), "-ss", str(current_sec), "-i", file_name, "-c", "copy", "-f", "mp4", "-t", str(VIDEO_CLIP_EACH_SEC + VIDEO_CLIP_OVERFLOW_SEC), @@ -216,6 +216,12 @@ def quick_split_video(file): current_sec += VIDEO_CLIP_EACH_SEC +def _common_ffmpeg_setting(): + return ( + "-y", "-hide_banner", "-progress", "-", "-loglevel", "error", + ) + + def _common_ffmpeg_params(): return ( "-preset:v", "fast", "-profile:v", "high", "-level", "4.1",