Skip to content

利用 AI 大模型和自动化工作流,根据主题或关键词一键生成高清短视频。Generate HD short videos from a topic or keyword with an automated AI workflow.

Reject background music that contains no decoded audio samples

Rudy Celekli94e553658f06
diff --git a/app/services/bgm.py b/app/services/bgm.py
index b1bd01f4..4e3ec079 100644
--- a/app/services/bgm.py
+++ b/app/services/bgm.py
@@ -163,6 +163,13 @@ def _validate_audio(file_path: str, timeout_seconds: int = 30) -> None:
file_path,
"-map",
"0:a:0",
+ # Normalize timestamps from decoded samples. A header-only
+ # WAV can exit successfully without producing any audio.
+ "-af",
+ "asetpts=N/SR/TB",
+ "-progress",
+ "pipe:1",
+ "-nostats",
"-f",
"null",
"-",
@@ -177,6 +184,16 @@ def _validate_audio(file_path: str, timeout_seconds: int = 30) -> None:
raise BgmServiceError("failed to run FFmpeg for background music validation") from exc
if decoded.returncode != 0:
raise BgmUploadError("uploaded file must contain a decodable audio stream")
+ decoded_audio = False
+ for line in decoded.stdout.splitlines():
+ key, separator, value = line.partition(b"=")
+ if separator and key == b"out_time_us":
+ try:
+ decoded_audio = decoded_audio or int(value) > 0
+ except ValueError:
+ continue
+ if not decoded_audio:
+ raise BgmUploadError("uploaded file must contain nonempty decodable audio")
def validate_audio_file(file_path: str, timeout_seconds: int = 120) -> None:
diff --git a/test/services/test_bgm.py b/test/services/test_bgm.py
index 3f908c55..ef909638 100644
--- a/test/services/test_bgm.py
+++ b/test/services/test_bgm.py
@@ -265,7 +265,7 @@ class TestBackgroundMusicService(unittest.TestCase):
)
def test_audio_validation_uses_only_configured_ffmpeg(self):
- completed = SimpleNamespace(returncode=0)
+ completed = SimpleNamespace(returncode=0, stdout=b"out_time_us=100000\nprogress=end\n")
with (
patch.object(
bgm.utils,
diff --git a/test/services/test_bgm_decoded_audio.py b/test/services/test_bgm_decoded_audio.py
new file mode 100644
index 00000000..a6792462
--- /dev/null
+++ b/test/services/test_bgm_decoded_audio.py
@@ -0,0 +1,38 @@
+import io
+import wave
+from unittest.mock import patch
+
+import pytest
+
+from app.services import bgm
+
+
+def wav_bytes(frames):
+ output = io.BytesIO()
+ with wave.open(output, "wb") as audio:
+ audio.setnchannels(1)
+ audio.setsampwidth(2)
+ audio.setframerate(16000)
+ audio.writeframes(b"\0\0" * frames)
+ return output.getvalue()
+
+
+def test_header_only_wav_rejected_by_real_ffmpeg(tmp_path):
+ target = tmp_path / "empty.wav"
+ target.write_bytes(wav_bytes(0))
+ assert target.stat().st_size > 0
+ with pytest.raises(bgm.BgmUploadError, match="audio"):
+ bgm.validate_audio_file(str(target))
+
+
+def test_empty_upload_rejected_and_staging_removed(tmp_path):
+ with patch.object(bgm, "uploaded_bgm_dir", return_value=str(tmp_path)):
+ with pytest.raises(bgm.BgmUploadError, match="audio"):
+ bgm.save_bgm_upload("empty.wav", io.BytesIO(wav_bytes(0)))
+ assert list(tmp_path.iterdir()) == []
+
+
+def test_silence_with_positive_frames_remains_valid(tmp_path):
+ target = tmp_path / "valid-silence.wav"
+ target.write_bytes(wav_bytes(1600))
+ bgm.validate_audio_file(str(target))