From bc516973bc70af53430ffbb73698e9786f7b8f7f Mon Sep 17 00:00:00 2001
From: Jerry Yan <792602257@qq.com>
Date: Wed, 18 May 2022 09:22:51 +0800
Subject: [PATCH] =?UTF-8?q?=E8=A7=86=E9=A2=91=E9=80=9A=E7=94=A8=E5=8F=82?=
 =?UTF-8?q?=E6=95=B0=E6=8A=BD=E8=B1=A1?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 workflow/video.py | 59 ++++++++++++++++++++---------------------------
 1 file changed, 25 insertions(+), 34 deletions(-)

diff --git a/workflow/video.py b/workflow/video.py
index 90e5511..b582484 100644
--- a/workflow/video.py
+++ b/workflow/video.py
@@ -27,8 +27,7 @@ def multi_gpu_encode_video_with_subtitles(orig_filename: str, subtitles: list[st
     new_filename = base_ts_to_filename(base_ts)
     new_fullpath = os.path.join(VIDEO_OUTPUT_DIR, new_filename)
     if not (FFMPEG_USE_NVIDIA_GPU and FFMPEG_USE_INTEL_GPU):
-        print("[!]Please Enable Both Of Encode Acceleration")
-        print("[!]Fallback to normal")
+        print("[!]Not Enabled Both GPU")
         encode_video_with_subtitles(orig_filename, subtitles, new_fullpath)
         return [new_fullpath]
     _duration_str = get_video_real_duration(orig_filename)
@@ -47,10 +46,8 @@ def multi_gpu_encode_video_with_subtitles(orig_filename: str, subtitles: list[st
             "-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",
+            "-f", "mp4", "-b:v", VIDEO_BITRATE, "-rc:v", "vbr", "-tune:v", "hq",
+            *_common_ffmpeg_params(),
             # "-t", "10",
             new_fullpath0
         ])
@@ -61,10 +58,8 @@ def multi_gpu_encode_video_with_subtitles(orig_filename: str, subtitles: list[st
             "-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",
+            "-f", "mp4", "-b:v", VIDEO_BITRATE, "-rc:v", "vbr", "-tune:v", "hq",
+            *_common_ffmpeg_params(),
             # "-t", "10",
             new_fullpath1
         ])
@@ -85,10 +80,8 @@ def multi_gpu_encode_video_with_subtitles(orig_filename: str, subtitles: list[st
             "-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",
+            "-f", "mp4", "-b:v", VIDEO_BITRATE, "-rc:v", "vbr", "-tune:v", "hq",
+            *_common_ffmpeg_params(),
             # "-t", "10",
             new_fullpath0
         ])
@@ -99,10 +92,8 @@ def multi_gpu_encode_video_with_subtitles(orig_filename: str, subtitles: list[st
             "-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",
+            "-f", "mp4", "-b:v", VIDEO_BITRATE, "-rc:v", "vbr", "-tune:v", "hq",
+            *_common_ffmpeg_params(),
             # "-t", "10",
             new_fullpath1
         ])
@@ -124,10 +115,8 @@ def encode_video_with_subtitles(orig_filename: str, subtitles: list[str], new_fi
             "-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",
-            "-b:v", VIDEO_BITRATE, "-rc:v", "vbr", "-tune:v", "hq",
-            "-qmin", "10", "-qmax", "32", "-crf", "16",
-            "-fflags", "+genpts", "-shortest", "-movflags", "faststart",
+            "-f", "mp4", "-b:v", VIDEO_BITRATE, "-rc:v", "vbr", "-tune:v", "hq",
+            *_common_ffmpeg_params(),
             # "-t", "10",
             new_filename
         ], stdout=subprocess.PIPE)
@@ -139,10 +128,8 @@ def encode_video_with_subtitles(orig_filename: str, subtitles: list[str], new_fi
                 "-hwaccel", "qsv", "-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",
+                "-f", "mp4", "-b:v", VIDEO_BITRATE, "-rc:v", "vbr", "-tune:v", "hq",
+                *_common_ffmpeg_params(),
                 # "-t", "10",
                 new_filename
             ], stdout=subprocess.PIPE)
@@ -153,10 +140,8 @@ def encode_video_with_subtitles(orig_filename: str, subtitles: list[str], new_fi
                 "-hwaccel", "vaapi", "-i", orig_filename, "-vf",
                 ",".join("subtitles=%s" % i for i in subtitles) + ",hwupload",
                 "-c:a", "copy", "-c:v", "h264_vaapi",
-                "-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",
+                "-f", "mp4", "-b:v", VIDEO_BITRATE, "-rc:v", "vbr", "-tune:v", "hq",
+                *_common_ffmpeg_params(),
                 # "-t", "10",
                 new_filename
             ], stdout=subprocess.PIPE)
@@ -167,10 +152,8 @@ def encode_video_with_subtitles(orig_filename: str, subtitles: list[str], new_fi
             "-i", orig_filename, "-vf",
             ",".join("subtitles=%s" % i for i in subtitles),
             "-c:a", "copy", "-c:v", "h264",
-            "-f", "mp4", "-preset:v", "fast", "-profile:v", "high", "-level", "4.1",
-            "-b:v", VIDEO_BITRATE,
-            "-qmin", "10", "-qmax", "32", "-crf", "16",
-            "-fflags", "+genpts", "-shortest", "-movflags", "faststart",
+            "-f", "mp4", "-b:v", VIDEO_BITRATE,
+            *_common_ffmpeg_params(),
             # "-t", "10",
             new_filename
         ], stdout=subprocess.PIPE)
@@ -231,3 +214,11 @@ def quick_split_video(file):
         ], stdout=subprocess.PIPE)
         handle_ffmpeg_output(split_process.stdout)
         current_sec += VIDEO_CLIP_EACH_SEC
+
+
+def _common_ffmpeg_params():
+    return (
+        "-preset:v", "fast", "-profile:v", "high", "-level", "4.1",
+        "-qmin", "10", "-qmax", "48", "-crf", "26",
+        "-fflags", "+genpts", "-shortest"
+    )