ffmpeg 设置统一

This commit is contained in:
Jerry Yan 2022-05-18 10:44:22 +08:00
parent bc516973bc
commit 7a18d04cc7

View File

@ -18,7 +18,7 @@ def base_ts_to_filename(start_ts: float, is_mp4=False) -> str:
def get_video_real_duration(filename): def get_video_real_duration(filename):
ffmpeg_process = subprocess.Popen([ ffmpeg_process = subprocess.Popen([
FFMPEG_EXEC, "-hide_banner", "-progress", "-", "-loglevel", "error", "-i", filename, "-c", "copy", "-f", "null", "-" FFMPEG_EXEC, *_common_ffmpeg_setting(), "-i", filename, "-c", "copy", "-f", "null", "-"
], stdout=subprocess.PIPE) ], stdout=subprocess.PIPE)
return handle_ffmpeg_output(ffmpeg_process.stdout) return handle_ffmpeg_output(ffmpeg_process.stdout)
@ -41,7 +41,7 @@ def multi_gpu_encode_video_with_subtitles(orig_filename: str, subtitles: list[st
new_fullpath1 = os.path.join(VIDEO_OUTPUT_DIR, new_filename1) new_fullpath1 = os.path.join(VIDEO_OUTPUT_DIR, new_filename1)
print("[+]Use Intel QSV Acceleration") print("[+]Use Intel QSV Acceleration")
encode_process0 = subprocess.Popen([ encode_process0 = subprocess.Popen([
FFMPEG_EXEC, "-hide_banner", "-progress", "-", "-loglevel", "error", "-y", FFMPEG_EXEC, *_common_ffmpeg_setting(),
"-hwaccel", "qsv", "-ss", str((_slices - 1) * VIDEO_CLIP_EACH_SEC), "-hwaccel", "qsv", "-ss", str((_slices - 1) * VIDEO_CLIP_EACH_SEC),
"-copyts", "-i", orig_filename, "-vf", "-copyts", "-i", orig_filename, "-vf",
",".join("subtitles=%s" % i for i in subtitles), ",".join("subtitles=%s" % i for i in subtitles),
@ -53,7 +53,7 @@ def multi_gpu_encode_video_with_subtitles(orig_filename: str, subtitles: list[st
]) ])
print("[+]Use Nvidia NvEnc Acceleration") print("[+]Use Nvidia NvEnc Acceleration")
encode_process1 = subprocess.Popen([ encode_process1 = subprocess.Popen([
FFMPEG_EXEC, "-hide_banner", "-progress", "-", "-loglevel", "error", "-y", FFMPEG_EXEC, *_common_ffmpeg_setting(),
"-t", str((_slices - 1) * VIDEO_CLIP_EACH_SEC + (VIDEO_CLIP_OVERFLOW_SEC * 0.8)), "-t", str((_slices - 1) * VIDEO_CLIP_EACH_SEC + (VIDEO_CLIP_OVERFLOW_SEC * 0.8)),
"-i", orig_filename, "-vf", "-i", orig_filename, "-vf",
",".join("subtitles=%s" % i for i in subtitles), ",".join("subtitles=%s" % i for i in subtitles),
@ -75,7 +75,7 @@ def multi_gpu_encode_video_with_subtitles(orig_filename: str, subtitles: list[st
new_fullpath1 = os.path.join(VIDEO_OUTPUT_DIR, new_filename1) new_fullpath1 = os.path.join(VIDEO_OUTPUT_DIR, new_filename1)
print("[+]Use Intel QSV Acceleration") print("[+]Use Intel QSV Acceleration")
encode_process0 = subprocess.Popen([ encode_process0 = subprocess.Popen([
FFMPEG_EXEC, "-hide_banner", "-progress", "-", "-loglevel", "error", "-y", FFMPEG_EXEC, *_common_ffmpeg_setting(),
"-hwaccel", "qsv", "-ss", str(_slices * VIDEO_CLIP_EACH_SEC), "-hwaccel", "qsv", "-ss", str(_slices * VIDEO_CLIP_EACH_SEC),
"-copyts", "-i", orig_filename, "-vf", "-copyts", "-i", orig_filename, "-vf",
",".join("subtitles=%s" % i for i in subtitles), ",".join("subtitles=%s" % i for i in subtitles),
@ -87,7 +87,7 @@ def multi_gpu_encode_video_with_subtitles(orig_filename: str, subtitles: list[st
]) ])
print("[+]Use Nvidia NvEnc Acceleration") print("[+]Use Nvidia NvEnc Acceleration")
encode_process1 = subprocess.Popen([ encode_process1 = subprocess.Popen([
FFMPEG_EXEC, "-hide_banner", "-progress", "-", "-loglevel", "error", "-y", FFMPEG_EXEC, *_common_ffmpeg_setting(),
"-t", str(_slices * VIDEO_CLIP_EACH_SEC + (VIDEO_CLIP_OVERFLOW_SEC * 0.8)), "-t", str(_slices * VIDEO_CLIP_EACH_SEC + (VIDEO_CLIP_OVERFLOW_SEC * 0.8)),
"-i", orig_filename, "-vf", "-i", orig_filename, "-vf",
",".join("subtitles=%s" % i for i in subtitles), ",".join("subtitles=%s" % i for i in subtitles),
@ -111,7 +111,7 @@ def encode_video_with_subtitles(orig_filename: str, subtitles: list[str], new_fi
if FFMPEG_USE_NVIDIA_GPU: if FFMPEG_USE_NVIDIA_GPU:
print("[+]Use Nvidia NvEnc Acceleration") print("[+]Use Nvidia NvEnc Acceleration")
encode_process = subprocess.Popen([ encode_process = subprocess.Popen([
FFMPEG_EXEC, "-hide_banner", "-progress", "-", "-loglevel", "error", "-y", FFMPEG_EXEC, *_common_ffmpeg_setting(),
"-i", orig_filename, "-vf", "-i", orig_filename, "-vf",
",".join("subtitles=%s" % i for i in subtitles) + ",hwupload_cuda", ",".join("subtitles=%s" % i for i in subtitles) + ",hwupload_cuda",
"-c:a", "copy", "-c:v", "h264_nvenc", "-c:a", "copy", "-c:v", "h264_nvenc",
@ -124,7 +124,7 @@ def encode_video_with_subtitles(orig_filename: str, subtitles: list[str], new_fi
if platform.system().lower() == "windows": if platform.system().lower() == "windows":
print("[+]Use Intel QSV Acceleration") print("[+]Use Intel QSV Acceleration")
encode_process = subprocess.Popen([ encode_process = subprocess.Popen([
FFMPEG_EXEC, "-hide_banner", "-progress", "-", "-loglevel", "error", "-y", FFMPEG_EXEC, *_common_ffmpeg_setting(),
"-hwaccel", "qsv", "-i", orig_filename, "-vf", "-hwaccel", "qsv", "-i", orig_filename, "-vf",
",".join("subtitles=%s" % i for i in subtitles), ",".join("subtitles=%s" % i for i in subtitles),
"-c:a", "copy", "-c:v", "h264_qsv", "-c:a", "copy", "-c:v", "h264_qsv",
@ -136,7 +136,7 @@ def encode_video_with_subtitles(orig_filename: str, subtitles: list[str], new_fi
else: else:
print("[+]Use Intel VAAPI Acceleration") print("[+]Use Intel VAAPI Acceleration")
encode_process = subprocess.Popen([ encode_process = subprocess.Popen([
FFMPEG_EXEC, "-hide_banner", "-progress", "-", "-loglevel", "error", "-y", FFMPEG_EXEC, *_common_ffmpeg_setting(),
"-hwaccel", "vaapi", "-i", orig_filename, "-vf", "-hwaccel", "vaapi", "-i", orig_filename, "-vf",
",".join("subtitles=%s" % i for i in subtitles) + ",hwupload", ",".join("subtitles=%s" % i for i in subtitles) + ",hwupload",
"-c:a", "copy", "-c:v", "h264_vaapi", "-c:a", "copy", "-c:v", "h264_vaapi",
@ -148,7 +148,7 @@ def encode_video_with_subtitles(orig_filename: str, subtitles: list[str], new_fi
else: else:
print("[+]Use CPU Encode") print("[+]Use CPU Encode")
encode_process = subprocess.Popen([ encode_process = subprocess.Popen([
FFMPEG_EXEC, "-hide_banner", "-progress", "-", "-loglevel", "error", "-y", FFMPEG_EXEC, *_common_ffmpeg_setting(),
"-i", orig_filename, "-vf", "-i", orig_filename, "-vf",
",".join("subtitles=%s" % i for i in subtitles), ",".join("subtitles=%s" % i for i in subtitles),
"-c:a", "copy", "-c:v", "h264", "-c:a", "copy", "-c:v", "h264",
@ -205,7 +205,7 @@ def quick_split_video(file):
print("CUR_DT", current_dt) print("CUR_DT", current_dt)
print("BIAS_T", current_sec) print("BIAS_T", current_sec)
split_process = subprocess.Popen([ split_process = subprocess.Popen([
FFMPEG_EXEC, "-y", "-hide_banner", "-progress", "-", "-loglevel", "error", FFMPEG_EXEC, *_common_ffmpeg_setting(),
"-ss", str(current_sec), "-ss", str(current_sec),
"-i", file_name, "-c", "copy", "-f", "mp4", "-i", file_name, "-c", "copy", "-f", "mp4",
"-t", str(VIDEO_CLIP_EACH_SEC + VIDEO_CLIP_OVERFLOW_SEC), "-t", str(VIDEO_CLIP_EACH_SEC + VIDEO_CLIP_OVERFLOW_SEC),
@ -216,6 +216,12 @@ def quick_split_video(file):
current_sec += VIDEO_CLIP_EACH_SEC current_sec += VIDEO_CLIP_EACH_SEC
def _common_ffmpeg_setting():
return (
"-y", "-hide_banner", "-progress", "-", "-loglevel", "error",
)
def _common_ffmpeg_params(): def _common_ffmpeg_params():
return ( return (
"-preset:v", "fast", "-profile:v", "high", "-level", "4.1", "-preset:v", "fast", "-profile:v", "high", "-level", "4.1",