You've already forked my-video-workflow
支持vaapi、支持定义使用哪种弹幕转换工具
This commit is contained in:
@ -7,7 +7,8 @@ from typing import Union
|
||||
|
||||
from bs4 import BeautifulSoup
|
||||
|
||||
from config import DANMAKU_FACTORY_EXEC, VIDEO_RESOLUTION, DANMAKU_SPEED, DEFAULT_FONT_NAME, DANMAKU_FONT_SIZE
|
||||
from config import DANMAKU_EXEC, VIDEO_RESOLUTION, DANMAKU_SPEED, DANMAKU_FONT_NAME, DANMAKU_FONT_SIZE, \
|
||||
DANMAKU_USE_DANMU2ASS, DANMAKU_USE_DANMAKUFACTORY
|
||||
from exception.danmaku import NoDanmakuException, DanmakuFormatErrorException
|
||||
from util.file import check_file_exist
|
||||
|
||||
@ -34,15 +35,33 @@ def diff_danmaku_files(base_file: Union[os.PathLike[str], str], file: Union[os.P
|
||||
|
||||
def danmaku_to_subtitle(file: Union[os.PathLike[str], str], time_shift: float):
|
||||
new_subtitle_name = md5(file.encode("utf-8")).hexdigest() + ".ass"
|
||||
process = subprocess.Popen((
|
||||
DANMAKU_FACTORY_EXEC, "--ignore-warnings",
|
||||
if DANMAKU_USE_DANMAKUFACTORY:
|
||||
process = danmaku_to_subtitle_use_danmaku_factory(file, time_shift, new_subtitle_name)
|
||||
elif DANMAKU_USE_DANMU2ASS:
|
||||
process = danmaku_to_subtitle_use_danmu2ass(file, time_shift, new_subtitle_name)
|
||||
else:
|
||||
return
|
||||
process.wait()
|
||||
return new_subtitle_name
|
||||
|
||||
|
||||
def danmaku_to_subtitle_use_danmaku_factory(file: Union[os.PathLike[str], str], time_shift: float, new_subtitle_name: str):
|
||||
return subprocess.Popen((
|
||||
DANMAKU_EXEC, "--ignore-warnings",
|
||||
"-r", str(VIDEO_RESOLUTION), "-s", str(DANMAKU_SPEED), "-f", "5",
|
||||
"-S", str(DANMAKU_FONT_SIZE), "-N", str(DEFAULT_FONT_NAME), "--showmsgbox", "FALSE",
|
||||
"-S", str(DANMAKU_FONT_SIZE), "-N", str(DANMAKU_FONT_NAME), "--showmsgbox", "FALSE",
|
||||
"-O", "255", "-L", "1", "-D", "0",
|
||||
"-o", "ass", new_subtitle_name, "-i", file, "-t", str(time_shift)
|
||||
))
|
||||
process.wait()
|
||||
return new_subtitle_name
|
||||
|
||||
|
||||
def danmaku_to_subtitle_use_danmu2ass(file: Union[os.PathLike[str], str], time_shift: float, new_subtitle_name: str):
|
||||
(_w, _h) = VIDEO_RESOLUTION.split("x")
|
||||
return subprocess.Popen((
|
||||
DANMAKU_EXEC, "--force", "-a", "1", "-d", str(DANMAKU_SPEED), "--font", str(DANMAKU_FONT_NAME),
|
||||
"--font-size", str(DANMAKU_FONT_SIZE), "--lane-size", str(DANMAKU_FONT_SIZE), "--width", _w, "--height", _h,
|
||||
"-o", new_subtitle_name, "-p", "1", "--time-offset", str(time_shift), "--width-ratio", "1", file
|
||||
))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
@ -3,8 +3,9 @@ import subprocess
|
||||
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_CRF, VIDEO_GOP
|
||||
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_CRF, VIDEO_GOP, FFMPEG_USE_VAAPI
|
||||
from . import LOGGER
|
||||
|
||||
|
||||
@ -29,6 +30,8 @@ def encode_video_with_subtitles(orig_filename: str, subtitles: list[str], base_t
|
||||
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:
|
||||
@ -36,6 +39,8 @@ def encode_video_with_subtitles(orig_filename: str, subtitles: list[str], base_t
|
||||
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:
|
||||
@ -73,6 +78,20 @@ def get_encode_process_use_intel(orig_filename: str, subtitles: list[str], new_f
|
||||
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",
|
||||
",".join("subtitles=%s" % i for i in subtitles),
|
||||
"-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([
|
||||
@ -101,6 +120,20 @@ def get_encode_hevc_process_use_nvenc(orig_filename: str, subtitles: list[str],
|
||||
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",
|
||||
",".join("subtitles=%s" % i for i in subtitles),
|
||||
"-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([
|
||||
|
Reference in New Issue
Block a user