主动判断是否有音频

This commit is contained in:
Jerry Yan 2025-03-06 21:37:17 +08:00
parent 56bdad7ad1
commit dcf5f5630d
3 changed files with 35 additions and 9 deletions

View File

@ -45,7 +45,6 @@ def parse_ffmpeg_task(task_info, template_info):
output_file = "out_" + str(time.time()) + ".mp4"
task = FfmpegTask(tasks, output_file=output_file)
overall = template_info.get("overall_template")
task.mute = False
task.center_cut = template_info.get("crop_mode", None)
task.frame_rate = template_info.get("frame_rate", 25)
if overall.get('source', ''):

View File

@ -167,6 +167,7 @@ class FfmpegTask(object):
output_args.append("1")
video_output_str = "[0:v]"
audio_output_str = ""
audio_track_index = 0
effect_index = 0
for input_file in self.input_file:
input_args.append("-i")
@ -242,17 +243,16 @@ class FfmpegTask(object):
input_index = input_args.count("-i")
input_args += MUTE_AUDIO_INPUT
filter_args.append(f"[{input_index}:a]acopy[a]")
audio_track_index += 1
audio_output_str = "[a]"
else:
audio_output_str = "[0:a]"
audio_track_index += 1
for audio in self.audios:
input_index = input_args.count("-i")
input_args.append("-i")
input_args.append(audio.replace("\\", "/"))
if audio_output_str == "":
filter_args.append(f"[{input_index}:a]acopy[a]")
audio_output_str = "[a]"
else:
audio_track_index += 1
filter_args.append(f"{audio_output_str}[{input_index}:a]amix[a]")
audio_output_str = "[a]"
if audio_output_str:
@ -276,6 +276,8 @@ class FfmpegTask(object):
elif isinstance(input_file, FfmpegTask):
f.write("file '" + input_file.get_output_file() + "'\n")
input_args += ["-f", "concat", "-safe", "0", "-i", _tmp_file]
from util.ffmpeg import probe_video_audio
self.mute = not probe_video_audio(_tmp_file, "concat")
output_args.append("-map")
output_args.append("0:v")
output_args.append("-c:v")

View File

@ -16,7 +16,11 @@ def to_annexb(file):
if not os.path.exists(file):
return file
logger.info("ToAnnexb: %s", file)
ffmpeg_process = subprocess.run(["ffmpeg", "-y", "-hide_banner", "-i", file, "-c", "copy", "-bsf:v", "h264_mp4toannexb",
ffmpeg_process = subprocess.run(["ffmpeg", "-y", "-hide_banner", "-i", file,
"-f", "lavfi", "-i", "anullsrc=cl=stereo:r=48000",
"-map", "0:v", "-map", "1:a",
"-c:v", "copy", "-bsf:v", "h264_mp4toannexb",
"-c:a", "aac", "-b:a", "128k", "-ar", "48000", "-ac", "2",
"-f", "mpegts", file+".ts"])
span.set_attribute("ffmpeg.args", json.dumps(ffmpeg_process.args))
logger.info("ToAnnexb: %s, returned: %s", file, ffmpeg_process.returncode)
@ -128,6 +132,27 @@ def probe_video_info(video_file):
return int(width), int(height), float(duration)
def probe_video_audio(video_file, type=None):
tracer = get_tracer(__name__)
with tracer.start_as_current_span("probe_video_audio") as span:
span.set_attribute("video.file", video_file)
args = ["ffprobe", "-hide_banner", "-v", "error", "-select_streams", "a", "-show_entries", "stream=index", "-of", "csv=p=0"]
if type == 'concat':
args.append("-safe")
args.append("0")
args.append("-f")
args.append("concat")
args.append(video_file)
result = subprocess.run(args, stderr=subprocess.STDOUT, **subprocess_args(True))
span.set_attribute("ffprobe.args", json.dumps(result.args))
span.set_attribute("ffprobe.code", result.returncode)
logger.info("probe_video_audio: %s", result.stdout.decode('utf-8').strip())
if result.returncode != 0:
return False
if result.stdout.decode('utf-8').strip() == '':
return False
return True
# Create a set of arguments which make a ``subprocess.Popen`` (and
# variants) call work with or without Pyinstaller, ``--noconsole`` or
# not, on Windows and Linux. Typical use::