From 265734262cbea230b365abb0d627d82e5928c5a5 Mon Sep 17 00:00:00 2001 From: Jerry Yan <792602257@qq.com> Date: Fri, 22 Apr 2022 09:33:04 +0800 Subject: [PATCH] =?UTF-8?q?=E5=98=A4=E7=89=B9=E5=B0=94GPU=E5=8A=A0?= =?UTF-8?q?=E9=80=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- workflow/video.py | 48 ++++++++++++++++++++++++++++++++++++----------- 1 file changed, 37 insertions(+), 11 deletions(-) diff --git a/workflow/video.py b/workflow/video.py index 56cf54a..02ca718 100644 --- a/workflow/video.py +++ b/workflow/video.py @@ -15,17 +15,43 @@ def get_video_real_duration(filename): def encode_video_with_subtitles(orig_filename: str, subtitles: list[str], new_filename: str): - encode_process = subprocess.Popen([ - FFMPEG_EXEC, "-hide_banner", "-progress", "-", "-v", "0", "-y", - "-i", orig_filename, "-vf", - ",".join("subtitles=%s" % i for i in subtitles), - "-c:a", "copy", "-c:v", "h264" if not FFMPEG_USE_GPU else ("h264_qsv" if FFMPEG_USE_INTEL_GPU else "h264_nvenc"), - "-f", "mp4", "-preset:v", "fast", "-profile:v", "high", "-level", "4.1", - "-b:v", VIDEO_BITRATE, "-rc:v", "vbr", *(["-tune:v", "hq"] if FFMPEG_USE_GPU and not FFMPEG_USE_INTEL_GPU else []), - "-qmin", "10", "-qmax", "32", "-crf", "16", - # "-t", "10", - new_filename - ], stdout=subprocess.PIPE) + if FFMPEG_USE_GPU: + if FFMPEG_USE_INTEL_GPU: + encode_process = subprocess.Popen([ + FFMPEG_EXEC, "-hide_banner", "-progress", "-", "-v", "0", "-y", + "-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", + # "-t", "10", + new_filename + ], stdout=subprocess.PIPE) + else: + encode_process = subprocess.Popen([ + FFMPEG_EXEC, "-hide_banner", "-progress", "-", "-v", "0", "-y", + "-hwaccel", "cuvid", "-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", + # "-t", "10", + new_filename + ], stdout=subprocess.PIPE) + else: + encode_process = subprocess.Popen([ + FFMPEG_EXEC, "-hide_banner", "-progress", "-", "-v", "0", "-y", + "-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, "-rc:v", "vbr", + "-qmin", "10", "-qmax", "32", "-crf", "16", + # "-t", "10", + new_filename + ], stdout=subprocess.PIPE) handle_ffmpeg_output(encode_process.stdout) return encode_process.wait()