使用handbrake进行视频压制

This commit is contained in:
2023-02-08 23:21:39 +08:00
parent c3993965a9
commit 4745e1a927
5 changed files with 120 additions and 197 deletions

View File

@ -1,11 +1,12 @@
import json
import os
import subprocess
import warnings
from datetime import datetime, timedelta
from typing import IO
from config import FFMPEG_EXEC, FFMPEG_USE_HEVC, VIDEO_BITRATE, FFMPEG_USE_NVIDIA_GPU, VIDEO_CLIP_EACH_SEC, \
VIDEO_CLIP_OVERFLOW_SEC, \
FFMPEG_USE_INTEL_GPU, VIDEO_OUTPUT_DIR, VIDEO_GOP, FFMPEG_USE_VAAPI
from config import VIDEO_CLIP_EACH_SEC, VIDEO_CLIP_OVERFLOW_SEC, VIDEO_OUTPUT_DIR, \
FFMPEG_EXEC, HANDBRAKE_EXEC, HANDBRAKE_PRESET_FILE, HANDBRAKE_PRESET, HANDBRAKE_ENCOPT
from . import LOGGER
@ -25,143 +26,78 @@ def get_video_real_duration(filename):
def encode_video_with_subtitles(orig_filename: str, subtitles: list[str], base_ts: float):
new_filename = base_ts_to_filename(base_ts, False)
new_fullpath = os.path.join(VIDEO_OUTPUT_DIR, new_filename)
if FFMPEG_USE_HEVC:
if FFMPEG_USE_NVIDIA_GPU:
process = get_encode_hevc_process_use_nvenc(orig_filename, subtitles, new_fullpath)
elif FFMPEG_USE_VAAPI:
process = get_encode_hevc_process_use_vaapi(orig_filename, subtitles, new_fullpath)
elif FFMPEG_USE_INTEL_GPU:
process = get_encode_hevc_process_use_intel(orig_filename, subtitles, new_fullpath)
else:
process = get_encode_hevc_process_use_cpu(orig_filename, subtitles, new_fullpath)
else:
if FFMPEG_USE_NVIDIA_GPU:
process = get_encode_process_use_nvenc(orig_filename, subtitles, new_fullpath)
elif FFMPEG_USE_VAAPI:
process = get_encode_process_use_vaapi(orig_filename, subtitles, new_fullpath)
elif FFMPEG_USE_INTEL_GPU:
process = get_encode_process_use_intel(orig_filename, subtitles, new_fullpath)
else:
process = get_encode_process_use_cpu(orig_filename, subtitles, new_fullpath)
handle_ffmpeg_output(process.stdout)
process.wait()
return [new_fullpath]
create_dt = datetime.fromtimestamp(base_ts)
_duration_str = get_video_real_duration(orig_filename)
duration = duration_str_to_float(_duration_str)
current_sec = 0
_video_parts = []
while current_sec < duration:
if (current_sec + VIDEO_CLIP_OVERFLOW_SEC * 2) > duration:
print("[-]Less than 2 overflow sec, skip")
break
current_dt = (create_dt + timedelta(seconds=current_sec)).strftime("%Y%m%d_%H%M_")
process = get_encode_process_use_handbrake(orig_filename,
subtitles,
os.path.join(VIDEO_OUTPUT_DIR, "{}.mp4".format(current_dt)),
current_sec,
VIDEO_CLIP_EACH_SEC + VIDEO_CLIP_OVERFLOW_SEC)
handle_handbrake_output(process.stdout)
process.wait()
_video_parts.append({
"base_path": VIDEO_OUTPUT_DIR,
"file": "{}.mp4".format(current_dt),
})
current_sec += VIDEO_CLIP_EACH_SEC
def get_encode_process_use_nvenc(orig_filename: str, subtitles: list[str], new_filename: str):
print("[+]Use Nvidia NvEnc Acceleration")
def get_encode_process_use_handbrake(orig_filename: str, subtitles: list[str], new_filename: str, start_time: int, stop_time: int):
print("[+]Use HandBrakeCli")
encode_process = subprocess.Popen([
FFMPEG_EXEC, *_common_ffmpeg_setting(),
"-i", orig_filename, "-vf",
",".join("subtitles=%s" % i for i in subtitles) + ",hwupload_cuda",
"-c:v", "h264_nvenc",
*_common_ffmpeg_params(),
# "-t", "10",
HANDBRAKE_EXEC, *_common_handbrake_setting(),
"--preset-import-file", HANDBRAKE_PRESET_FILE, "--preset", HANDBRAKE_PRESET,
"--start-at", "seconds:{}".format(start_time), "--stop-at", "seconds:{}".format(stop_time),
"-i", orig_filename, "-x", HANDBRAKE_ENCOPT,
"--ssa-file", ",".join(i for i in subtitles),
"--ssa-offset", "{}000".format(start_time),
"--ssa-burn", ",".join("%d" % (i+1) for i in range(len(subtitles))),
"-o",
new_filename
], stdout=subprocess.PIPE)
return encode_process
def get_encode_process_use_intel(orig_filename: str, subtitles: list[str], new_filename: str):
print("[+]Use Intel QSV Acceleration")
encode_process = subprocess.Popen([
FFMPEG_EXEC, *_common_ffmpeg_setting(),
"-hwaccel", "qsv", "-i", orig_filename, "-vf",
",".join("subtitles=%s" % i for i in subtitles),
"-c:v", "h264_qsv",
*_common_ffmpeg_params(),
# "-t", "10",
new_filename
], stdout=subprocess.PIPE)
return encode_process
def get_encode_process_use_vaapi(orig_filename: str, subtitles: list[str], new_filename: str):
print("[+]Use VAAPI Acceleration")
encode_process = subprocess.Popen([
FFMPEG_EXEC, *_common_ffmpeg_setting(),
"-hwaccel", "vaapi", "-hwaccel_output_format", "vaapi", "-i", orig_filename, "-vf",
"hwmap=mode=read+write+direct,format=nv12," +
"".join("subtitles=%s," % i for i in subtitles) + "hwmap",
"-c:v", "h264_vaapi",
*_common_ffmpeg_params(),
# "-t", "10",
new_filename
], stdout=subprocess.PIPE)
return encode_process
def get_encode_process_use_cpu(orig_filename: str, subtitles: list[str], new_filename: str):
print("[+]Use CPU Encode")
encode_process = subprocess.Popen([
FFMPEG_EXEC, *_common_ffmpeg_setting(),
"-i", orig_filename, "-vf",
",".join("subtitles=%s" % i for i in subtitles),
"-c:v", "h264",
*_common_ffmpeg_params(),
# "-t", "10",
new_filename
], stdout=subprocess.PIPE)
return encode_process
def get_encode_hevc_process_use_nvenc(orig_filename: str, subtitles: list[str], new_filename: str):
print("[+]Use Nvidia NvEnc Acceleration")
encode_process = subprocess.Popen([
FFMPEG_EXEC, *_common_ffmpeg_setting(),
"-i", orig_filename, "-vf",
"".join("subtitles=%s," % i for i in subtitles) + ",hwupload_cuda",
"-c:v", "hevc_nvenc",
*_common_ffmpeg_params(),
# "-t", "10",
new_filename
], stdout=subprocess.PIPE)
return encode_process
def get_encode_hevc_process_use_vaapi(orig_filename: str, subtitles: list[str], new_filename: str):
print("[+]Use VAAPI Acceleration")
encode_process = subprocess.Popen([
FFMPEG_EXEC, *_common_ffmpeg_setting(),
"-hwaccel", "vaapi", "-hwaccel_output_format", "vaapi", "-i", orig_filename, "-vf",
"hwmap=mode=read+write+direct,format=nv12," +
"".join("subtitles=%s," % i for i in subtitles) + "hwmap",
"-c:v", "hevc_vaapi",
*_common_ffmpeg_params(),
# "-t", "10",
new_filename
], stdout=subprocess.PIPE)
return encode_process
def get_encode_hevc_process_use_intel(orig_filename: str, subtitles: list[str], new_filename: str):
print("[+]Use Intel QSV Acceleration")
encode_process = subprocess.Popen([
FFMPEG_EXEC, *_common_ffmpeg_setting(),
"-hwaccel", "qsv", "-i", orig_filename, "-vf",
",".join("subtitles=%s" % i for i in subtitles),
"-c:v", "hevc_qsv",
*_common_ffmpeg_params(),
# "-t", "10",
new_filename
], stdout=subprocess.PIPE)
return encode_process
def get_encode_hevc_process_use_cpu(orig_filename: str, subtitles: list[str], new_filename: str):
print("[+]Use CPU Encode")
encode_process = subprocess.Popen([
FFMPEG_EXEC, *_common_ffmpeg_setting(),
"-i", orig_filename, "-vf",
",".join("subtitles=%s" % i for i in subtitles),
"-c:v", "hevc",
*_common_ffmpeg_params(),
# "-t", "10",
new_filename
], stdout=subprocess.PIPE)
return encode_process
def handle_handbrake_output(stdout: IO[bytes]):
out_time = "0:0:0.0"
speed = "0"
if stdout is None:
print("[!]STDOUT is null")
return
json_body = ""
json_start = False
_i = 0
while True:
line = stdout.readline()
if line == b"":
break
if json_start:
json_body += line.strip().decode("UTF-8")
if line.startswith(b"}"):
json_start = False
status_payload = json.loads(json_body)
if status_payload["State"] == "WORKING":
out_time = "ETA: {Hours:02d}:{Minutes:02d}:{Seconds:02d}".format_map(status_payload["Working"])
speed = "{Rate:.2f}FPS".format_map(status_payload["Working"])
_i += 1
if _i % 300 == 150:
LOGGER.debug("[>]Speed:{}@{}".format(out_time, speed))
elif status_payload["State"] == "WORKDONE":
break
continue
if line.startswith(b"Progress:"):
json_start = True
json_body = "{"
LOGGER.debug("[ ]Speed:{}@{}".format(out_time, speed))
def handle_ffmpeg_output(stdout: IO[bytes]) -> str:
@ -195,6 +131,7 @@ def duration_str_to_float(duration_str) -> float:
def quick_split_video(file):
warnings.warn("已过时", DeprecationWarning)
if not os.path.isfile(file):
raise FileNotFoundError(file)
file_name = os.path.split(file)[-1]
@ -235,10 +172,9 @@ def _common_ffmpeg_setting():
)
def _common_ffmpeg_params():
def _common_handbrake_setting():
return (
"-f", "mp4", "-b:v", VIDEO_BITRATE, "-c:a", "aac",
"-preset:v", "fast", "-profile:v", "main", "-avoid_negative_ts", "1",
"-qmin", "18", "-qmax", "38", "-g:v", str(VIDEO_GOP),
"-fflags", "+genpts", "-shortest"
"--json",
"--crop-mode", "none", "--no-comb-detect", "--no-bwdif", "--no-decomb", "--no-detelecine", "--no-hqdn3d",
"--no-nlmeans", "--no-chroma-smooth", "--no-unsharp", "--no-lapsharp", "--no-deblock", "--align-av"
)

View File

@ -26,15 +26,9 @@ def do_workflow(video_file, danmaku_base_file, *danmaku_files):
print("弹幕文件", danmaku_file, "异常")
continue
print(result)
file_need_split = encode_video_with_subtitles(video_file, result, start_ts)
_video_parts = []
for file in file_need_split:
_video_parts += quick_split_video(file)
_video_parts = encode_video_with_subtitles(video_file, result, start_ts)
# clean files
for file in result:
if os.path.isfile(file):
os.remove(file)
for file in file_need_split:
if os.path.isfile(file):
os.remove(file)
return _video_parts