From f1729ffc32acc47918700edbe03e5445dc4b8bbd Mon Sep 17 00:00:00 2001 From: Jerry Yan <792602257@qq.com> Date: Wed, 27 Apr 2022 07:40:56 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=8C=E7=BC=96=E7=A0=81=E9=80=BB=E8=BE=91?= =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- workflow/video.py | 59 +++++++++++++++++++++++++--------------------- workflow/worker.py | 3 ++- 2 files changed, 34 insertions(+), 28 deletions(-) diff --git a/workflow/video.py b/workflow/video.py index cc1e5ad..47b1d78 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", "-", "-v", "0", "-i", filename, "-c", "copy", "-f", "null", "-" + FFMPEG_EXEC, "-hide_banner", "-progress", "-", "-loglevel", "error", "-i", filename, "-c", "copy", "-f", "null", "-" ], stdout=subprocess.PIPE) return handle_ffmpeg_output(ffmpeg_process.stdout) @@ -28,16 +28,17 @@ def multi_gpu_encode_video_with_subtitles(orig_filename: str, subtitles: list[st if not (FFMPEG_USE_NVIDIA_GPU and FFMPEG_USE_INTEL_GPU): print("[!]Please Enable Both Of Encode Acceleration") print("[!]Fallback to normal") - return encode_video_with_subtitles(orig_filename, subtitles, new_filename) + encode_video_with_subtitles(orig_filename, subtitles, new_filename) + return [new_filename] _duration_str = get_video_real_duration(orig_filename) duration = duration_str_to_float(_duration_str) - if duration >= (VIDEO_CLIP_EACH_SEC * 5): + if duration > (VIDEO_CLIP_EACH_SEC * 3): # qsv 压制前2段,剩余交由nvenc压制 new_filename0 = base_ts_to_filename(base_ts) new_filename1 = base_ts_to_filename(base_ts + VIDEO_CLIP_EACH_SEC * 2) encode_process0 = subprocess.Popen([ - FFMPEG_EXEC, "-hide_banner", "-progress", "-", "-v", "0", "-y", - "-hwaccel", "qsv", "-t", str(VIDEO_CLIP_EACH_SEC * 2 + VIDEO_CLIP_OVERFLOW_SEC), "-i", orig_filename, "-vf", + 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", ",".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", @@ -45,26 +46,28 @@ def multi_gpu_encode_video_with_subtitles(orig_filename: str, subtitles: list[st "-qmin", "10", "-qmax", "32", "-crf", "16", # "-t", "10", new_filename0 - ], stdout=subprocess.PIPE) + ]) encode_process1 = subprocess.Popen([ - FFMPEG_EXEC, "-hide_banner", "-progress", "-", "-v", "0", "-y", - "-hwaccel", "cuvid", "-ss", str(VIDEO_CLIP_EACH_SEC * 2), "-i", orig_filename, "-vf", - ",".join("subtitles=%s" % i for i in subtitles) + ",hwupload_cuda", - "-c:a", "copy", "-c:v", "h264_nvenc", + FFMPEG_EXEC, "-hide_banner", "-progress", "-", "-loglevel", "error", "-y", + "-ss", str(VIDEO_CLIP_EACH_SEC * 2), "-copyts", "-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", # "-t", "10", new_filename1 - ], stdout=subprocess.PIPE) + ]) + encode_process1.wait() + encode_process0.wait() return [new_filename0, new_filename1] - elif duration >= (VIDEO_CLIP_EACH_SEC * 3): - # 至少也要能切3片,才用双GPU加速 + elif duration > (VIDEO_CLIP_EACH_SEC * 1): + # 至少也要能切2片,才用双GPU加速 new_filename0 = base_ts_to_filename(base_ts, True) new_filename1 = base_ts_to_filename(base_ts + VIDEO_CLIP_EACH_SEC) encode_process0 = subprocess.Popen([ - FFMPEG_EXEC, "-hide_banner", "-progress", "-", "-v", "0", "-y", - "-hwaccel", "qsv", "-t", str(VIDEO_CLIP_EACH_SEC + VIDEO_CLIP_OVERFLOW_SEC), "-i", orig_filename, "-vf", + 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", ",".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", @@ -72,18 +75,20 @@ def multi_gpu_encode_video_with_subtitles(orig_filename: str, subtitles: list[st "-qmin", "10", "-qmax", "32", "-crf", "16", # "-t", "10", new_filename0 - ], stdout=subprocess.PIPE) + ]) encode_process1 = subprocess.Popen([ - FFMPEG_EXEC, "-hide_banner", "-progress", "-", "-v", "0", "-y", - "-hwaccel", "cuvid", "-ss", str(VIDEO_CLIP_EACH_SEC), "-i", orig_filename, "-vf", - ",".join("subtitles=%s" % i for i in subtitles) + ",hwupload_cuda", - "-c:a", "copy", "-c:v", "h264_nvenc", + FFMPEG_EXEC, "-hide_banner", "-progress", "-", "-loglevel", "error", "-y", + "-ss", str(VIDEO_CLIP_EACH_SEC), "-copyts", "-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", # "-t", "10", new_filename1 - ], stdout=subprocess.PIPE) + ]) + encode_process1.wait() + encode_process0.wait() return [new_filename1] else: print("[-]VideoClip to short,", duration) @@ -96,8 +101,8 @@ 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", "-", "-v", "0", "-y", - "-hwaccel", "cuvid", "-i", orig_filename, "-vf", + FFMPEG_EXEC, "-hide_banner", "-progress", "-", "-loglevel", "error", "-y", + "-i", orig_filename, "-vf", ",".join("subtitles=%s" % i for i in subtitles) + ",hwupload_cuda", "-c:a", "copy", "-c:v", "h264_nvenc", "-f", "mp4", "-preset:v", "fast", "-profile:v", "high", "-level", "4.1", @@ -110,7 +115,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", "-", "-v", "0", "-y", + FFMPEG_EXEC, "-hide_banner", "-progress", "-", "-loglevel", "error", "-y", "-hwaccel", "qsv", "-i", orig_filename, "-vf", ",".join("subtitles=%s" % i for i in subtitles), "-c:a", "copy", "-c:v", "h264_qsv", @@ -123,7 +128,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", "-", "-v", "0", "-y", + FFMPEG_EXEC, "-hide_banner", "-progress", "-", "-loglevel", "error", "-y", "-hwaccel", "vaapi", "-i", orig_filename, "-vf", ",".join("subtitles=%s" % i for i in subtitles) + ",hwupload", "-c:a", "copy", "-c:v", "h264_vaapi", @@ -136,7 +141,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", "-", "-v", "0", "-y", + FFMPEG_EXEC, "-hide_banner", "-progress", "-", "-loglevel", "error", "-y", "-i", orig_filename, "-vf", ",".join("subtitles=%s" % i for i in subtitles), "-c:a", "copy", "-c:v", "h264", @@ -187,7 +192,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", "-", "-v", "0", + FFMPEG_EXEC, "-y", "-hide_banner", "-progress", "-", "-loglevel", "error", "-ss", str(current_sec), "-i", file_name, "-c", "copy", "-f", "mp4", "-t", str(VIDEO_CLIP_EACH_SEC + VIDEO_CLIP_OVERFLOW_SEC), diff --git a/workflow/worker.py b/workflow/worker.py index 8036532..6a364de 100644 --- a/workflow/worker.py +++ b/workflow/worker.py @@ -24,7 +24,8 @@ def do_workflow(video_file, danmaku_base_file, *danmaku_files): file_need_split = multi_gpu_encode_video_with_subtitles(video_file, result, start_ts) for filename in file_need_split: quick_split_video(filename) - # clean files + # clean files + for filename in file_need_split: os.remove(filename) for file in result: os.remove(file)