直接拼接逻辑

This commit is contained in:
2025-01-11 17:25:58 +08:00
parent da6579a9ed
commit ce469dacf2
7 changed files with 191 additions and 21 deletions

View File

@ -1,10 +1,14 @@
import time
import uuid
class FfmpegTask(object):
def __init__(self, input_file, task_type='copy', output_file=''):
self.annexb = False
if type(input_file) is str:
if input_file.endswith(".ts"):
self.annexb = True
self.input_file = [input_file]
elif type(input_file) is list:
self.input_file = input_file
@ -19,7 +23,6 @@ class FfmpegTask(object):
self.luts = []
self.audios = []
self.overlays = []
self.annexb = False
def __repr__(self):
_str = f'FfmpegTask(input_file={self.input_file}, task_type={self.task_type}'
@ -125,7 +128,10 @@ class FfmpegTask(object):
if self.task_type == 'encode':
input_args = []
filter_args = []
output_args = ["-shortest", "-c:v h264_qsv"]
output_args = ["-shortest", "-c:v", "h264_qsv"]
if self.annexb:
output_args.append("-bsf:v")
output_args.append("h264_mp4toannexb")
video_output_str = "[0:v]"
audio_output_str = "[0:v]"
video_input_count = 0
@ -172,11 +178,43 @@ class FfmpegTask(object):
output_args.append(audio_output_str)
return args + input_args + ["-filter_complex", ";".join(filter_args)] + output_args + [self.get_output_file()]
elif self.task_type == 'concat':
# 无法通过 annexb 合并的
input_args = []
output_args = ["-shortest", "-c:v", "h264_qsv", "-r", "25"]
output_args = ["-shortest"]
if self.check_annexb() and len(self.audios) <= 1:
# output_args
if len(self.audios) > 0:
input_args.append("-an")
_tmp_file = "tmp_concat_"+str(time.time())+".txt"
with open(_tmp_file, "w", encoding="utf-8") as f:
for input_file in self.input_file:
if type(input_file) is str:
f.write("file '"+input_file+"'\n")
elif isinstance(input_file, FfmpegTask):
f.write("file '" + input_file.get_output_file() + "'\n")
input_args.append("-f")
input_args.append("concat")
input_args.append("-safe")
input_args.append("0")
input_args.append("-i")
input_args.append(_tmp_file)
output_args.append("-c:v")
output_args.append("copy")
if len(self.audios) > 0:
input_args.append("-i")
input_args.append(self.audios[0])
output_args.append("-c:a")
output_args.append("copy")
output_args.append("-f")
output_args.append("mp4")
return args + input_args + output_args + [self.get_output_file()]
output_args.append("-c:v")
output_args.append("h264_qsv")
output_args.append("-r")
output_args.append("25")
filter_args = []
video_output_str = "[0:v]"
audio_output_str = "[0:v]"
audio_output_str = "[0:a]"
video_input_count = 0
audio_input_count = 0
for input_file in self.input_file:
@ -217,12 +255,31 @@ class FfmpegTask(object):
if len(self.input_file) == 1:
if type(self.input_file[0]) is str:
if self.input_file[0] == self.get_output_file():
return None
return []
return args + ["-i", self.input_file[0]] + ["-c", "copy", self.get_output_file()]
def set_output_file(self, file=None):
if file is None:
if self.output_file == '':
self.output_file = "rand_" + str(uuid.uuid4()) + ".mp4"
if self.annexb:
self.output_file = "rand_" + str(uuid.uuid4()) + ".ts"
else:
self.output_file = "rand_" + str(uuid.uuid4()) + ".mp4"
else:
self.output_file = file
self.output_file = file
def check_annexb(self):
for input_file in self.input_file:
if type(input_file) is str:
if self.task_type == 'encode':
return self.annexb
elif self.task_type == 'concat':
return False
elif self.task_type == 'copy':
return self.annexb
else:
return False
elif isinstance(input_file, FfmpegTask):
if not input_file.check_annexb():
return False
return True