修改gop后切割,避免没切到keyframe

This commit is contained in:
Jerry Yan 2022-05-31 16:45:35 +08:00
parent 2d857752ca
commit 13f18c5e6c
2 changed files with 25 additions and 43 deletions

View File

@ -23,40 +23,26 @@ def get_video_real_duration(filename):
return handle_ffmpeg_output(ffmpeg_process.stdout) return handle_ffmpeg_output(ffmpeg_process.stdout)
def multi_gpu_encode_video_with_subtitles(orig_filename: str, subtitles: list[str], base_ts: float): def encode_video_with_subtitles(orig_filename: str, subtitles: list[str], base_ts: float):
_duration_str = get_video_real_duration(orig_filename) new_filename = base_ts_to_filename(base_ts, True)
duration = duration_str_to_float(_duration_str) new_fullpath = os.path.join(VIDEO_OUTPUT_DIR, new_filename)
current_sec = 0 if FFMPEG_USE_NVIDIA_GPU:
while current_sec < duration: process = get_encode_process_use_nvenc(orig_filename, subtitles, new_fullpath)
if (current_sec + VIDEO_CLIP_OVERFLOW_SEC * 2) > duration: elif FFMPEG_USE_INTEL_GPU:
print("[-]Less than 2 overflow sec, skip") process = get_encode_process_use_intel(orig_filename, subtitles, new_fullpath)
break else:
new_filename = base_ts_to_filename(base_ts + current_sec, True) process = get_encode_process_use_cpu(orig_filename, subtitles, new_fullpath)
new_fullpath = os.path.join(VIDEO_OUTPUT_DIR, new_filename) handle_ffmpeg_output(process.stdout)
print("CUR_FN", new_filename, "BIAS_T", current_sec) process.wait()
if FFMPEG_USE_NVIDIA_GPU: return [new_fullpath]
process = get_encode_process_use_nvenc(orig_filename, subtitles, new_fullpath,
current_sec, VIDEO_CLIP_EACH_SEC + VIDEO_CLIP_OVERFLOW_SEC)
elif FFMPEG_USE_INTEL_GPU:
process = get_encode_process_use_intel(orig_filename, subtitles, new_fullpath,
current_sec, VIDEO_CLIP_EACH_SEC + VIDEO_CLIP_OVERFLOW_SEC)
else:
process = get_encode_process_use_cpu(orig_filename, subtitles, new_fullpath,
current_sec, VIDEO_CLIP_EACH_SEC + VIDEO_CLIP_OVERFLOW_SEC)
handle_ffmpeg_output(process.stdout)
process.wait()
current_sec += VIDEO_CLIP_EACH_SEC
def get_encode_process_use_nvenc(orig_filename: str, subtitles: list[str], new_filename: str, def get_encode_process_use_nvenc(orig_filename: str, subtitles: list[str], new_filename: str):
skip: float = 0, duration: float = 0):
print("[+]Use Nvidia NvEnc Acceleration") print("[+]Use Nvidia NvEnc Acceleration")
encode_process = subprocess.Popen([ encode_process = subprocess.Popen([
FFMPEG_EXEC, *_common_ffmpeg_setting(), FFMPEG_EXEC, *_common_ffmpeg_setting(),
"-ss", str(skip), "-copyts", "-t", str(duration),
"-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",
"-ss", str(skip),
"-c:a", "copy", "-c:v", "h264_nvenc", "-c:a", "copy", "-c:v", "h264_nvenc",
"-f", "mp4", "-b:v", VIDEO_BITRATE, "-rc:v", "vbr", "-tune:v", "hq", "-f", "mp4", "-b:v", VIDEO_BITRATE, "-rc:v", "vbr", "-tune:v", "hq",
*_common_ffmpeg_params(), *_common_ffmpeg_params(),
@ -66,16 +52,13 @@ def get_encode_process_use_nvenc(orig_filename: str, subtitles: list[str], new_f
return encode_process return encode_process
def get_encode_process_use_intel(orig_filename: str, subtitles: list[str], new_filename: str, def get_encode_process_use_intel(orig_filename: str, subtitles: list[str], new_filename: str):
skip: float = 0, duration: float = 0):
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, *_common_ffmpeg_setting(), FFMPEG_EXEC, *_common_ffmpeg_setting(),
"-ss", str(skip), "-copyts", "-t", str(duration),
"-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),
"-ss", str(skip),
"-c:a", "copy", "-c:v", "h264_qsv", "-c:a", "copy", "-c:v", "h264_qsv",
"-f", "mp4", "-b:v", VIDEO_BITRATE, "-rc:v", "vbr", "-tune:v", "hq", "-f", "mp4", "-b:v", VIDEO_BITRATE, "-rc:v", "vbr", "-tune:v", "hq",
*_common_ffmpeg_params(), *_common_ffmpeg_params(),
@ -86,10 +69,8 @@ def get_encode_process_use_intel(orig_filename: str, subtitles: list[str], new_f
print("[+]Use Intel VAAPI Acceleration") print("[+]Use Intel VAAPI Acceleration")
encode_process = subprocess.Popen([ encode_process = subprocess.Popen([
FFMPEG_EXEC, *_common_ffmpeg_setting(), FFMPEG_EXEC, *_common_ffmpeg_setting(),
"-ss", str(skip), "-copyts", "-t", str(duration),
"-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",
"-ss", str(skip),
"-c:a", "copy", "-c:v", "h264_vaapi", "-c:a", "copy", "-c:v", "h264_vaapi",
"-f", "mp4", "-b:v", VIDEO_BITRATE, "-rc:v", "vbr", "-tune:v", "hq", "-f", "mp4", "-b:v", VIDEO_BITRATE, "-rc:v", "vbr", "-tune:v", "hq",
*_common_ffmpeg_params(), *_common_ffmpeg_params(),
@ -99,15 +80,12 @@ def get_encode_process_use_intel(orig_filename: str, subtitles: list[str], new_f
return encode_process return encode_process
def get_encode_process_use_cpu(orig_filename: str, subtitles: list[str], new_filename: str, def get_encode_process_use_cpu(orig_filename: str, subtitles: list[str], new_filename: str):
skip: float = 0, duration: float = 0):
print("[+]Use CPU Encode") print("[+]Use CPU Encode")
encode_process = subprocess.Popen([ encode_process = subprocess.Popen([
FFMPEG_EXEC, *_common_ffmpeg_setting(), FFMPEG_EXEC, *_common_ffmpeg_setting(),
"-ss", str(skip), "-copyts", "-t", str(duration),
"-i", orig_filename, "-vf", "-i", orig_filename, "-vf",
",".join("subtitles=%s" % i for i in subtitles), ",".join("subtitles=%s" % i for i in subtitles),
"-ss", str(skip),
"-c:a", "copy", "-c:v", "h264", "-c:a", "copy", "-c:v", "h264",
"-f", "mp4", "-b:v", VIDEO_BITRATE, "-f", "mp4", "-b:v", VIDEO_BITRATE,
*_common_ffmpeg_params(), *_common_ffmpeg_params(),
@ -176,15 +154,14 @@ def quick_split_video(file):
def _common_ffmpeg_setting(): def _common_ffmpeg_setting():
return ( return (
"-y", "-hide_banner", "-progress", "-", "-loglevel", "error" "-y", "-hide_banner", "-progress", "-", "-loglevel", "error",
) )
def _common_ffmpeg_params(): def _common_ffmpeg_params():
return ( return (
"-f", "mp4", "-b:v", VIDEO_BITRATE, "-c:a", "copy", "-f", "mp4", "-b:v", VIDEO_BITRATE, "-c:a", "copy",
"-sub_charenc", "UTF-8", "-preset:v", "fast", "-profile:v", "main", "-avoid_negative_ts", "1",
"-preset:v", "fast", "-profile:v", "main", "-qmin", "12", "-qmax", "34", "-crf", "26", "-g:v", "60",
"-qmin", "12", "-qmax", "34", "-crf", "26",
"-fflags", "+genpts", "-shortest", "-movflags", "faststart" "-fflags", "+genpts", "-shortest", "-movflags", "faststart"
) )

View File

@ -2,7 +2,7 @@ import os.path
from exception.danmaku import DanmakuException from exception.danmaku import DanmakuException
from workflow.danmaku import get_file_start, diff_danmaku_files, danmaku_to_subtitle from workflow.danmaku import get_file_start, diff_danmaku_files, danmaku_to_subtitle
from workflow.video import multi_gpu_encode_video_with_subtitles from workflow.video import encode_video_with_subtitles, quick_split_video
def do_workflow(video_file, danmaku_base_file, *danmaku_files): def do_workflow(video_file, danmaku_base_file, *danmaku_files):
@ -26,9 +26,14 @@ def do_workflow(video_file, danmaku_base_file, *danmaku_files):
print("弹幕文件", danmaku_file, "异常") print("弹幕文件", danmaku_file, "异常")
continue continue
print(result) print(result)
multi_gpu_encode_video_with_subtitles(video_file, result, start_ts) file_need_split = encode_video_with_subtitles(video_file, result, start_ts)
for file in file_need_split:
quick_split_video(file)
# clean files # clean files
for file in result: for file in result:
if os.path.isfile(file): if os.path.isfile(file):
os.remove(file) os.remove(file)
for file in file_need_split:
if os.path.isfile(file):
os.remove(file)