This commit is contained in:
2025-03-03 14:15:20 +08:00
parent fff20610a5
commit 1f9632761f
3 changed files with 45 additions and 1 deletions

View File

@ -1,5 +1,6 @@
import time
import uuid
from typing import Any
ENCODER_ARGS = ("-c:v", "h264_qsv", "-global_quality", "28", "-look_ahead", "1",)
PROFILE_LEVEL_ARGS = ("-profile:v", "high", "-level:v", "4")
@ -7,6 +8,8 @@ PROFILE_LEVEL_ARGS = ("-profile:v", "high", "-level:v", "4")
class FfmpegTask(object):
effects: list[str]
def __init__(self, input_file, task_type='copy', output_file=''):
self.annexb = False
if type(input_file) is str:
@ -29,6 +32,7 @@ class FfmpegTask(object):
self.luts = []
self.audios = []
self.overlays = []
self.effects = []
def __repr__(self):
_str = f'FfmpegTask(input_file={self.input_file}, task_type={self.task_type}'
@ -40,6 +44,8 @@ class FfmpegTask(object):
_str += f', overlays={self.overlays}'
if self.annexb:
_str += f', annexb={self.annexb}'
if self.effects:
_str += f', effects={self.effects}'
if self.mute:
_str += f', mute={self.mute}'
return _str + ')'
@ -83,6 +89,10 @@ class FfmpegTask(object):
self.luts.extend(luts)
self.correct_task_type()
def add_effect(self, *effects):
self.effects.extend(effects)
self.correct_task_type()
def get_output_file(self):
if self.task_type == 'copy':
return self.input_file[0]
@ -105,6 +115,8 @@ class FfmpegTask(object):
return False
if len(self.subtitles) > 0:
return False
if len(self.effects) > 0:
return False
if self.speed != 1:
return False
if self.zoom_cut is not None:
@ -120,6 +132,8 @@ class FfmpegTask(object):
return False
if len(self.subtitles) > 0:
return False
if len(self.effects) > 0:
return False
if self.speed != 1:
return False
if len(self.audios) > 1:
@ -141,7 +155,7 @@ class FfmpegTask(object):
if self.task_type == 'encode':
input_args = []
filter_args = []
output_args = ["-profile", "high", "-level", "4","-shortest", *ENCODER_ARGS]
output_args = [*PROFILE_LEVEL_ARGS,"-shortest", *ENCODER_ARGS]
if self.annexb:
output_args.append("-bsf:v")
output_args.append("h264_mp4toannexb")
@ -162,6 +176,29 @@ class FfmpegTask(object):
_f_x = pos_json.get('ltX', 0)
_x = f'{float(_f_x/_v_w) :.5f}*iw'
filter_args.append(f"[{video_output_str}]crop=x={_x}:y=0:w=ih*ih/iw:h=ih[{video_output_str}]")
for effect in self.effects:
if effect.startswith("cameraShot:"):
param = effect.split(":", 2)[1]
if param == '':
param = "3,1"
_split = param.split(",")
start = 3
duration = 1
if len(_split) >= 2:
start = int(_split[0])
duration = int(_split[1])
elif len(_split) == 1:
start = int(_split[0])
_start_out_str = "[eff_s]"
_end_out_str = "[eff_e]"
filter_args.append(f"{video_output_str}fps=fps={self.frame_rate},split{_start_out_str}{_end_out_str}")
filter_args.append(f"{_start_out_str}trim=start={0}:end={start+duration},freezeframes=first={start*self.frame_rate}:replace={start*self.frame_rate}{_start_out_str}")
filter_args.append(f"{_end_out_str}trim=start={start}{_end_out_str}")
video_output_str = "[v_eff]"
filter_args.append(f"{_start_out_str}{_end_out_str}concat=n=2:v=1:a=0{video_output_str}")
elif effect.startswith("zoom:"):
...
...
for lut in self.luts:
filter_args.append(f"[{video_output_str}]lut3d=file={lut}[{video_output_str}]")
for overlay in self.overlays: