Spaces:
Running
Running
Fix additive audio duration handling
Browse files- app/operations/mix_audio.py +22 -4
app/operations/mix_audio.py
CHANGED
|
@@ -53,6 +53,13 @@ def _audio_bitrate(params: dict[str, Any]) -> str:
|
|
| 53 |
return value
|
| 54 |
|
| 55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
async def add_audio(
|
| 57 |
ffmpeg: FFmpegService,
|
| 58 |
inputs: Sequence[InputMedia],
|
|
@@ -62,14 +69,20 @@ async def add_audio(
|
|
| 62 |
require_inputs(inputs, 2)
|
| 63 |
tracks = _track_params(params, len(inputs) - 1)
|
| 64 |
bitrate = _audio_bitrate(params)
|
|
|
|
| 65 |
output = output_path(output_dir, "audio-mix", "mp4")
|
| 66 |
|
| 67 |
-
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
for offset, track in enumerate(tracks, start=1):
|
| 70 |
delay_ms = round(track["start"] * 1000)
|
| 71 |
filters.append(
|
| 72 |
-
f"[{offset}:a]volume={track['volume']:.6g},adelay={delay_ms}:all=1
|
|
|
|
| 73 |
)
|
| 74 |
mix_inputs.append(f"[a{offset}]")
|
| 75 |
|
|
@@ -105,5 +118,10 @@ async def add_audio(
|
|
| 105 |
args,
|
| 106 |
output,
|
| 107 |
"video.add_audio",
|
| 108 |
-
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
)
|
|
|
|
| 53 |
return value
|
| 54 |
|
| 55 |
|
| 56 |
+
def _video_duration(inputs: Sequence[InputMedia]) -> float:
|
| 57 |
+
duration = inputs[0].duration
|
| 58 |
+
if duration is None or duration <= 0:
|
| 59 |
+
raise InputError("The source video duration is required for additive audio mixing")
|
| 60 |
+
return duration
|
| 61 |
+
|
| 62 |
+
|
| 63 |
async def add_audio(
|
| 64 |
ffmpeg: FFmpegService,
|
| 65 |
inputs: Sequence[InputMedia],
|
|
|
|
| 69 |
require_inputs(inputs, 2)
|
| 70 |
tracks = _track_params(params, len(inputs) - 1)
|
| 71 |
bitrate = _audio_bitrate(params)
|
| 72 |
+
duration = _video_duration(inputs)
|
| 73 |
output = output_path(output_dir, "audio-mix", "mp4")
|
| 74 |
|
| 75 |
+
duration_value = f"{duration:.6f}"
|
| 76 |
+
filters: list[str] = [
|
| 77 |
+
f"[0:a]apad=whole_dur={duration_value},atrim=duration={duration_value}[base]"
|
| 78 |
+
]
|
| 79 |
+
mix_inputs = ["[base]"]
|
| 80 |
+
|
| 81 |
for offset, track in enumerate(tracks, start=1):
|
| 82 |
delay_ms = round(track["start"] * 1000)
|
| 83 |
filters.append(
|
| 84 |
+
f"[{offset}:a]volume={track['volume']:.6g},adelay={delay_ms}:all=1,"
|
| 85 |
+
f"apad=whole_dur={duration_value},atrim=duration={duration_value}[a{offset}]"
|
| 86 |
)
|
| 87 |
mix_inputs.append(f"[a{offset}]")
|
| 88 |
|
|
|
|
| 118 |
args,
|
| 119 |
output,
|
| 120 |
"video.add_audio",
|
| 121 |
+
{
|
| 122 |
+
"audio_tracks": tracks,
|
| 123 |
+
"preserve_video": True,
|
| 124 |
+
"mix_mode": "additive",
|
| 125 |
+
"duration_seconds": duration,
|
| 126 |
+
},
|
| 127 |
)
|