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)