Compare commits
3 Commits
56bdad7ad1
...
744fe28421
Author | SHA1 | Date | |
---|---|---|---|
744fe28421 | |||
cf43e6d549 | |||
dcf5f5630d |
@ -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', ''):
|
||||
|
@ -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")
|
||||
@ -180,7 +181,7 @@ class FfmpegTask(object):
|
||||
_v_w = pos_json.get('imgWidth', 1)
|
||||
_f_x = pos_json.get('ltX', 0)
|
||||
_f_x2 = pos_json.get('rbX', 0)
|
||||
_x = f'{float((_f_x2 - _f_x)/(2 * _v_w)) :.4f}*iw'
|
||||
_x = f'{float((_f_x2 + _f_x)/(2 * _v_w)) :.4f}*iw-ih*ih/(2*iw)'
|
||||
filter_args.append(f"{video_output_str}crop=x={_x}:y=0:w=ih*ih/iw:h=ih[v_cut{effect_index}]")
|
||||
video_output_str = f"[v_cut{effect_index}]"
|
||||
effect_index += 1
|
||||
@ -242,19 +243,18 @@ 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:
|
||||
filter_args.append(f"{audio_output_str}[{input_index}:a]amix[a]")
|
||||
audio_output_str = "[a]"
|
||||
audio_track_index += 1
|
||||
filter_args.append(f"{audio_output_str}[{input_index}:a]amix=duration=shortest:dropout_transition=0:normalize=0[a]")
|
||||
audio_output_str = "[a]"
|
||||
if audio_output_str:
|
||||
output_args.append("-map")
|
||||
output_args.append(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")
|
||||
@ -293,7 +295,7 @@ class FfmpegTask(object):
|
||||
input_args.append("-i")
|
||||
input_args.append(audio.replace("\\", "/"))
|
||||
audio_track_index += 1
|
||||
filter_args.append(f"{audio_output_str}[{input_index}:a]amix[a]")
|
||||
filter_args.append(f"{audio_output_str}[{input_index}:a]amix=duration=shortest:dropout_transition=0:normalize=0[a]")
|
||||
audio_output_str = "[a]"
|
||||
if audio_output_str:
|
||||
output_args.append("-map")
|
||||
|
@ -5,7 +5,7 @@ import subprocess
|
||||
from datetime import datetime
|
||||
from typing import Optional, IO
|
||||
|
||||
from entity.ffmpeg import FfmpegTask, ENCODER_ARGS, VIDEO_ARGS, AUDIO_ARGS
|
||||
from entity.ffmpeg import FfmpegTask, ENCODER_ARGS, VIDEO_ARGS, AUDIO_ARGS, MUTE_AUDIO_INPUT
|
||||
from telemetry import get_tracer
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
@ -16,8 +16,12 @@ 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",
|
||||
"-f", "mpegts", file+".ts"])
|
||||
ffmpeg_process = subprocess.run(["ffmpeg", "-y", "-hide_banner", "-i", file,
|
||||
*MUTE_AUDIO_INPUT,
|
||||
"-map", "0:v", "-map", "1:a",
|
||||
"-c:v", "copy", "-bsf:v", "h264_mp4toannexb", "-shortest",
|
||||
"-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)
|
||||
span.set_attribute("ffmpeg.code", ffmpeg_process.returncode)
|
||||
@ -34,8 +38,13 @@ def re_encode_and_annexb(file):
|
||||
if not os.path.exists(file):
|
||||
return file
|
||||
logger.info("ReEncodeAndAnnexb: %s", file)
|
||||
ffmpeg_process = subprocess.run(["ffmpeg", "-y", "-hide_banner", "-i", file, *VIDEO_ARGS, *AUDIO_ARGS, *ENCODER_ARGS, "-bsf:v", "h264_mp4toannexb",
|
||||
"-f", "mpegts", file +".ts"])
|
||||
ffmpeg_process = subprocess.run(["ffmpeg", "-y", "-hide_banner", "-i", file,
|
||||
*MUTE_AUDIO_INPUT,
|
||||
"-map", "0:v", "-map", "1:a",
|
||||
*VIDEO_ARGS, "-bsf:v", "h264_mp4toannexb",
|
||||
*AUDIO_ARGS, "-bsf:a", "setts=pts=DTS",
|
||||
*ENCODER_ARGS, "-shortest",
|
||||
"-f", "mpegts", file + ".ts"])
|
||||
span.set_attribute("ffmpeg.args", json.dumps(ffmpeg_process.args))
|
||||
logger.info("ReEncodeAndAnnexb: %s, returned: %s", file, ffmpeg_process.returncode)
|
||||
span.set_attribute("ffmpeg.code", ffmpeg_process.returncode)
|
||||
@ -128,6 +137,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::
|
||||
|
Loading…
x
Reference in New Issue
Block a user