Skip to content

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

fix(audio): honor narration-only export volume

Rudy Celekli6fc048fae846
diff --git a/app/services/task.py b/app/services/task.py
index 98fa7d52..418b5160 100644
--- a/app/services/task.py
+++ b/app/services/task.py
@@ -3,6 +3,7 @@ import math
import os
import re
import socket
+import tempfile
import threading
import time
from concurrent.futures import CancelledError, Future, ThreadPoolExecutor
@@ -1677,6 +1678,26 @@ def _run_pipeline(
sm.state.update_task(task_id, state=const.TASK_STATE_PROCESSING, progress=30)
if stop_at == "audio":
+ # Full video jobs apply gain in the final mixer. Audio-only exports have
+ # no mixer, so apply it here to an owned output (never a custom input).
+ audio_volume = 1.0 if params.voice_volume is None else float(params.voice_volume)
+ if audio_volume != 1.0:
+ descriptor, output_file = tempfile.mkstemp(
+ prefix="audio-export-", suffix=".mp3", dir=utils.task_dir(task_id)
+ )
+ os.close(descriptor)
+ export_ready = False
+ try:
+ export_ready = voice.apply_audio_volume(audio_file, output_file, audio_volume)
+ if not export_ready:
+ return _mark_task_failed(task_id, "audio", "failed to apply audio export volume")
+ finally:
+ if not export_ready:
+ try:
+ os.remove(output_file)
+ except OSError as exc:
+ logger.warning(f"failed to remove incomplete audio export: {exc}")
+ audio_file = output_file
sm.state.update_task(
task_id,
state=const.TASK_STATE_COMPLETE,
diff --git a/app/services/voice.py b/app/services/voice.py
index 3d5018d8..ef008d7c 100644
--- a/app/services/voice.py
+++ b/app/services/voice.py
@@ -782,6 +782,19 @@ def _publish_tts_ffmpeg_output(
return True
+
+def apply_audio_volume(audio_file: str, output_file: str, volume: float) -> bool:
+ """Encode narration export gain to a caller-owned destination."""
+ if not math.isfinite(volume) or volume < 0:
+ raise ValueError("audio export volume must be finite and nonnegative")
+ ensure_file_path_exists(output_file)
+ command = [
+ utils.get_ffmpeg_binary(), "-nostdin", "-v", "error", "-y",
+ "-i", audio_file, "-vn", "-af", f"volume={volume}",
+ "-codec:a", "libmp3lame", "-q:a", "4",
+ ]
+ return _publish_tts_ffmpeg_output(command, output_file, "audio export volume")
+
def _concat_audio_files(audio_files: list[str], output_file: str) -> bool:
"""
使用 PCM 解码与统一重编码合并多个音频分段。
diff --git a/test/services/test_audio_export_volume.py b/test/services/test_audio_export_volume.py
new file mode 100644
index 00000000..665e13cd
--- /dev/null
+++ b/test/services/test_audio_export_volume.py
@@ -0,0 +1,90 @@
+from unittest.mock import patch
+import wave
+
+import numpy as np
+import pytest
+from edge_tts import SubMaker
+import subprocess
+
+from app.models.schema import AudioRequest, VideoParams
+from app.services import task as tm
+from app.services.state import MemoryState
+
+
+def _write_narration(voice_file, **kwargs):
+ # Replace the provider network boundary with a real one-second PCM signal.
+ samples = np.rint(3000 * np.sin(2 * np.pi * 440 * np.arange(24000) / 24000)).astype('<i2')
+ with wave.open(voice_file, 'wb') as audio:
+ audio.setnchannels(1)
+ audio.setsampwidth(2)
+ audio.setframerate(24000)
+ audio.writeframes(samples.tobytes())
+ maker = SubMaker()
+ maker.duration = 1.0
+ return maker
+
+
+def _rms(filename):
+ decoded = subprocess.run([tm.utils.get_ffmpeg_binary(), '-nostdin', '-v', 'error', '-i', str(filename), '-f', 's16le', '-ac', '1', '-ar', '24000', 'pipe:1'], check=True, capture_output=True)
+ frames = np.frombuffer(decoded.stdout, dtype='<i2').astype(float) / 32768
+ return float(np.sqrt(np.mean(frames ** 2))), len(frames) / 24000
+
+
[email protected]('gain', [0.0, 0.5, 2.0])
+def test_audio_only_export_applies_requested_volume_to_real_pcm(tmp_path, gain):
+ params = AudioRequest(video_script='A narration.', voice_volume=gain)
+ with (patch.object(tm.sm, 'state', MemoryState()),
+ patch.object(tm.utils, 'task_dir', return_value=str(tmp_path)),
+ patch.object(tm.utils, 'check_ffmpeg_ready', return_value=True),
+ patch.object(tm, 'save_script_data'),
+ patch.object(tm, 'generate_terms', return_value=['narration']),
+ patch.object(tm.voice, 'tts', side_effect=_write_narration)):
+ result = tm.start('audio-volume', params, stop_at='audio')
+ assert 'audio_file' in result, result
+ actual, duration = _rms(result['audio_file'])
+ expected = 3000 / 32768 / np.sqrt(2) * gain
+ assert actual == pytest.approx(expected, abs=0.004)
+ assert duration == pytest.approx(1.0, abs=0.1)
+
+
+def test_full_video_keeps_unscaled_narration_for_its_existing_mixer(tmp_path):
+ params = VideoParams(video_subject='Narration', video_script='A narration.', voice_volume=2.0, subtitle_enabled=False)
+ observed = []
+ def render(*args):
+ observed.append(_rms(args[3])[0])
+ return ['video.mp4'], ['combined.mp4'], []
+ with (patch.object(tm.sm, 'state', MemoryState()),
+ patch.object(tm.utils, 'task_dir', return_value=str(tmp_path)),
+ patch.object(tm.utils, 'check_ffmpeg_ready', return_value=True),
+ patch.object(tm, 'save_script_data'),
+ patch.object(tm, 'generate_terms', return_value=['narration']),
+ patch.object(tm.voice, 'tts', side_effect=_write_narration),
+ patch.object(tm, 'generate_subtitle', return_value=''),
+ patch.object(tm, 'get_video_materials', return_value=['material.mp4']),
+ patch.object(tm, 'generate_final_videos', side_effect=render),
+ patch.object(tm.upload_post.upload_post_service, 'is_configured', return_value=False)):
+ result = tm.start('video-volume', params)
+ assert result['videos'] == ['video.mp4']
+ assert observed == pytest.approx([3000 / 32768 / np.sqrt(2)], abs=0.004)
+
+
[email protected]("filename", ["original.wav", "audio.mp3"])
+def test_custom_narration_export_preserves_input_bytes_on_retry(tmp_path, filename):
+ source = tmp_path / filename
+ _write_narration(str(source))
+ original = source.read_bytes()
+ task_dir = tmp_path
+ params = VideoParams(video_subject='Narration', video_script='A narration.', voice_volume=0.5,
+ custom_audio_file=str(source))
+ with (patch.object(tm.sm, 'state', MemoryState()),
+ patch.object(tm.utils, 'task_dir', return_value=str(task_dir)),
+ patch.object(tm.utils, 'check_ffmpeg_ready', return_value=True),
+ patch.object(tm, 'save_script_data'),
+ patch.object(tm, 'generate_terms', return_value=['narration'])):
+ results = [tm.start('custom-volume', params, stop_at='audio', allow_server_file_input=True) for _ in range(2)]
+ assert source.read_bytes() == original
+ for result in results:
+ assert result['audio_file'] != str(source)
+ actual, duration = _rms(result['audio_file'])
+ assert actual == pytest.approx(3000 / 32768 / np.sqrt(2) * 0.5, abs=0.004)
+ assert duration == pytest.approx(1.0, abs=0.1)