双编码逻辑更新

This commit is contained in:
Jerry Yan 2022-04-27 07:40:56 +08:00
parent ea2de8270b
commit f1729ffc32
2 changed files with 34 additions and 28 deletions

View File

@ -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),

View File

@ -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)