2022-05-17 17:56:27 +08:00

234 lines
11 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import os
import platform
import subprocess
from datetime import datetime, timedelta
from typing import IO
from config import FFMPEG_EXEC, VIDEO_BITRATE, FFMPEG_USE_NVIDIA_GPU, VIDEO_CLIP_EACH_SEC, VIDEO_CLIP_OVERFLOW_SEC, \
FFMPEG_USE_INTEL_GPU, VIDEO_OUTPUT_DIR
def base_ts_to_filename(start_ts: float, is_mp4=False) -> str:
base_start = datetime.fromtimestamp(start_ts)
if is_mp4:
return base_start.strftime("%Y%m%d_%H%M.mp4")
else:
return base_start.strftime("%Y%m%d_%H%M.flv")
def get_video_real_duration(filename):
ffmpeg_process = subprocess.Popen([
FFMPEG_EXEC, "-hide_banner", "-progress", "-", "-loglevel", "error", "-i", filename, "-c", "copy", "-f", "null", "-"
], stdout=subprocess.PIPE)
return handle_ffmpeg_output(ffmpeg_process.stdout)
def multi_gpu_encode_video_with_subtitles(orig_filename: str, subtitles: list[str], base_ts: float):
new_filename = base_ts_to_filename(base_ts)
new_fullpath = os.path.join(VIDEO_OUTPUT_DIR, new_filename)
if not (FFMPEG_USE_NVIDIA_GPU and FFMPEG_USE_INTEL_GPU):
print("[!]Please Enable Both Of Encode Acceleration")
print("[!]Fallback to normal")
encode_video_with_subtitles(orig_filename, subtitles, new_fullpath)
return [new_fullpath]
_duration_str = get_video_real_duration(orig_filename)
duration = duration_str_to_float(_duration_str)
if duration > (VIDEO_CLIP_EACH_SEC * 5):
# qsv 压制前2段剩余交由nvenc压制
_slices = int(duration / VIDEO_CLIP_EACH_SEC)
new_filename0 = base_ts_to_filename(base_ts + (_slices - 1) * VIDEO_CLIP_EACH_SEC)
new_filename1 = base_ts_to_filename(base_ts)
new_fullpath0 = os.path.join(VIDEO_OUTPUT_DIR, new_filename0)
new_fullpath1 = os.path.join(VIDEO_OUTPUT_DIR, new_filename1)
print("[+]Use Intel QSV Acceleration")
encode_process0 = subprocess.Popen([
FFMPEG_EXEC, "-hide_banner", "-progress", "-", "-loglevel", "error", "-y",
"-hwaccel", "qsv", "-ss", str((_slices - 1) * VIDEO_CLIP_EACH_SEC),
"-copyts", "-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",
"-b:v", VIDEO_BITRATE, "-rc:v", "vbr", "-tune:v", "hq",
"-qmin", "10", "-qmax", "32", "-crf", "16",
"-fflags", "+genpts", "-shortest", "-movflags", "faststart",
# "-t", "10",
new_fullpath0
])
print("[+]Use Nvidia NvEnc Acceleration")
encode_process1 = subprocess.Popen([
FFMPEG_EXEC, "-hide_banner", "-progress", "-", "-loglevel", "error", "-y",
"-t", str((_slices - 1) * 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_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",
"-fflags", "+genpts", "-shortest", "-movflags", "faststart",
# "-t", "10",
new_fullpath1
])
encode_process0.wait()
encode_process1.wait()
return [new_filename0, new_filename1]
elif duration > (VIDEO_CLIP_EACH_SEC * 3):
# 至少也要能切2片才用双GPU加速
_slices = int(duration / VIDEO_CLIP_EACH_SEC)
new_filename0 = base_ts_to_filename(base_ts + _slices * VIDEO_CLIP_EACH_SEC, True)
new_filename1 = base_ts_to_filename(base_ts)
new_fullpath0 = os.path.join(VIDEO_OUTPUT_DIR, new_filename0)
new_fullpath1 = os.path.join(VIDEO_OUTPUT_DIR, new_filename1)
print("[+]Use Intel QSV Acceleration")
encode_process0 = subprocess.Popen([
FFMPEG_EXEC, "-hide_banner", "-progress", "-", "-loglevel", "error", "-y",
"-hwaccel", "qsv", "-ss", str(_slices * VIDEO_CLIP_EACH_SEC),
"-copyts", "-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",
"-b:v", VIDEO_BITRATE, "-rc:v", "vbr", "-tune:v", "hq",
"-qmin", "10", "-qmax", "32", "-crf", "16",
"-fflags", "+genpts", "-shortest", "-movflags", "faststart",
# "-t", "10",
new_fullpath0
])
print("[+]Use Nvidia NvEnc Acceleration")
encode_process1 = subprocess.Popen([
FFMPEG_EXEC, "-hide_banner", "-progress", "-", "-loglevel", "error", "-y",
"-t", str(_slices * 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_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",
"-fflags", "+genpts", "-shortest", "-movflags", "faststart",
# "-t", "10",
new_fullpath1
])
encode_process1.wait()
encode_process0.wait()
return [new_filename1]
else:
print("[-]VideoClip to short,", duration)
print("[-]Fallback to normal")
encode_video_with_subtitles(orig_filename, subtitles, new_fullpath)
return [new_fullpath]
def encode_video_with_subtitles(orig_filename: str, subtitles: list[str], new_filename: str):
if FFMPEG_USE_NVIDIA_GPU:
print("[+]Use Nvidia NvEnc Acceleration")
encode_process = subprocess.Popen([
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",
"-b:v", VIDEO_BITRATE, "-rc:v", "vbr", "-tune:v", "hq",
"-qmin", "10", "-qmax", "32", "-crf", "16",
"-fflags", "+genpts", "-shortest", "-movflags", "faststart",
# "-t", "10",
new_filename
], stdout=subprocess.PIPE)
elif FFMPEG_USE_INTEL_GPU:
if platform.system().lower() == "windows":
print("[+]Use Intel QSV Acceleration")
encode_process = subprocess.Popen([
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",
"-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",
"-fflags", "+genpts", "-shortest", "-movflags", "faststart",
# "-t", "10",
new_filename
], stdout=subprocess.PIPE)
else:
print("[+]Use Intel VAAPI Acceleration")
encode_process = subprocess.Popen([
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",
"-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",
"-fflags", "+genpts", "-shortest", "-movflags", "faststart",
# "-t", "10",
new_filename
], stdout=subprocess.PIPE)
else:
print("[+]Use CPU Encode")
encode_process = subprocess.Popen([
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",
"-f", "mp4", "-preset:v", "fast", "-profile:v", "high", "-level", "4.1",
"-b:v", VIDEO_BITRATE,
"-qmin", "10", "-qmax", "32", "-crf", "16",
"-fflags", "+genpts", "-shortest", "-movflags", "faststart",
# "-t", "10",
new_filename
], stdout=subprocess.PIPE)
handle_ffmpeg_output(encode_process.stdout)
return encode_process.wait()
def handle_ffmpeg_output(stderr: IO[bytes]) -> str:
out_time = "0:0:0.0"
speed = "0"
_i = 0
while True:
line = stderr.readline()
if line == b"":
break
if line.strip() == b"progress=end":
# 处理完毕
break
if line.startswith(b"out_time="):
out_time = line.replace(b"out_time=", b"").decode().strip()
if line.startswith(b"speed="):
speed = line.replace(b"speed=", b"").decode().strip()
_i += 1
if _i % 120 == 90:
print("[>]Speed:", out_time, "@", speed)
print("[ ]Speed:", out_time, "@", speed)
return out_time
def duration_str_to_float(duration_str) -> float:
_duration = datetime.strptime(duration_str, "%H:%M:%S.%f") - datetime(1900, 1, 1)
return _duration.total_seconds()
def quick_split_video(file):
if not os.path.isfile(file):
raise FileNotFoundError(file)
file_name = os.path.split(file)[-1]
_create_dt = os.path.splitext(file_name)[0]
create_dt = datetime.strptime(_create_dt, "%Y%m%d_%H%M")
_duration_str = get_video_real_duration(file)
duration = duration_str_to_float(_duration_str)
current_sec = 0
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_")
print("CUR_DT", current_dt)
print("BIAS_T", current_sec)
split_process = subprocess.Popen([
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),
"-fflags", "+genpts", "-shortest", "-movflags", "faststart",
"{}.mp4".format(current_dt)
], stdout=subprocess.PIPE)
handle_ffmpeg_output(split_process.stdout)
current_sec += VIDEO_CLIP_EACH_SEC