Spaces:
Running
Running
Validate audio mix bitrate
Browse files- app/operations/mix_audio.py +10 -1
app/operations/mix_audio.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
from __future__ import annotations
|
| 2 |
|
| 3 |
import json
|
|
|
|
| 4 |
from collections.abc import Sequence
|
| 5 |
from pathlib import Path
|
| 6 |
from typing import Any
|
|
@@ -45,6 +46,13 @@ def _track_params(params: dict[str, Any], count: int) -> list[dict[str, float]]:
|
|
| 45 |
return tracks
|
| 46 |
|
| 47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
async def add_audio(
|
| 49 |
ffmpeg: FFmpegService,
|
| 50 |
inputs: Sequence[InputMedia],
|
|
@@ -53,6 +61,7 @@ async def add_audio(
|
|
| 53 |
) -> OperationResult:
|
| 54 |
require_inputs(inputs, 2)
|
| 55 |
tracks = _track_params(params, len(inputs) - 1)
|
|
|
|
| 56 |
output = output_path(output_dir, "audio-mix", "mp4")
|
| 57 |
|
| 58 |
filters: list[str] = []
|
|
@@ -85,7 +94,7 @@ async def add_audio(
|
|
| 85 |
"-c:a",
|
| 86 |
"aac",
|
| 87 |
"-b:a",
|
| 88 |
-
|
| 89 |
"-movflags",
|
| 90 |
"+faststart",
|
| 91 |
output,
|
|
|
|
| 1 |
from __future__ import annotations
|
| 2 |
|
| 3 |
import json
|
| 4 |
+
import re
|
| 5 |
from collections.abc import Sequence
|
| 6 |
from pathlib import Path
|
| 7 |
from typing import Any
|
|
|
|
| 46 |
return tracks
|
| 47 |
|
| 48 |
|
| 49 |
+
def _audio_bitrate(params: dict[str, Any]) -> str:
|
| 50 |
+
value = str(params.get("audio_bitrate", "192k")).strip().lower()
|
| 51 |
+
if not re.fullmatch(r"(?:32|48|64|96|128|160|192|224|256|320)k", value):
|
| 52 |
+
raise InputError("audio_bitrate must be a supported AAC bitrate")
|
| 53 |
+
return value
|
| 54 |
+
|
| 55 |
+
|
| 56 |
async def add_audio(
|
| 57 |
ffmpeg: FFmpegService,
|
| 58 |
inputs: Sequence[InputMedia],
|
|
|
|
| 61 |
) -> OperationResult:
|
| 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 |
filters: list[str] = []
|
|
|
|
| 94 |
"-c:a",
|
| 95 |
"aac",
|
| 96 |
"-b:a",
|
| 97 |
+
bitrate,
|
| 98 |
"-movflags",
|
| 99 |
"+faststart",
|
| 100 |
output,
|