利用 AI 大模型和自动化工作流,根据主题或关键词一键生成高清短视频。Generate HD short videos from a topic or keyword with an automated AI workflow.
Python ai-video-generatorcontent-creationffmpeginstagram-reelsllmpythonshort-videosubtitlestext-to-speechtiktokvideo-automationvideo-workflowworkflow-automationyoutube-shorts
fix(video): fade background music after extending its playback
Rudy Celekli4432b896b877
diff --git a/app/services/video.py b/app/services/video.pyindex 107af43a..7de00bef 100644--- a/app/services/video.py+++ b/app/services/video.py@@ -1793,15 +1793,14 @@ def generate_video(bgm_mix_succeeded = Trueif bgm_file:try:- bgm_effects = [- afx.MultiplyVolume(params.bgm_volume),- afx.AudioFadeOut(3),- ]+ bgm_effects = [afx.MultiplyVolume(params.bgm_volume)]# 服务内解析的随机/自定义音乐可能比成片短,需要循环铺满;任务层# 通过 override 传入的文件表示提供商已经完成时长适配。这里依据# 文件来源决定是否循环,避免今后每增加一个提供商都修改名称白名单。if bgm_file_override is None:bgm_effects.append(afx.AudioLoop(duration=video_clip.duration))+ # Fade the complete playback timeline, not each source loop.+ bgm_effects.append(afx.AudioFadeOut(3))bgm_source_clip = clip_stack.enter_context(AudioFileClip(bgm_file))bgm_clip = bgm_source_clip.with_effects(bgm_effects)audio_clip = CompositeAudioClip([audio_clip, bgm_clip])diff --git a/test/services/test_bgm_final_fade.py b/test/services/test_bgm_final_fade.pynew file mode 100644index 00000000..a195abb3--- /dev/null+++ b/test/services/test_bgm_final_fade.py@@ -0,0 +1,34 @@+import shutil+import subprocess++import numpy as np+import pytest++from app.models.schema import TaskVideoRequest+from app.services import video+++def test_looped_background_music_fades_only_at_final_video_end(tmp_path):+ if not shutil.which("ffmpeg"):+ pytest.skip("native FFmpeg required")+ footage, speech, music, output = (tmp_path / name for name in ("input.mp4", "speech.wav", "music.wav", "final.mp4"))+ base = ["ffmpeg", "-nostdin", "-v", "error", "-y", "-threads", "1"]+ subprocess.run([*base, "-f", "lavfi", "-i", "color=c=red:s=64x64:r=10:d=8", "-c:v", "libx264", "-pix_fmt", "yuv420p", str(footage)], check=True)+ subprocess.run([*base, "-f", "lavfi", "-i", "anullsrc=r=44100:cl=mono", "-t", "8", str(speech)], check=True)+ subprocess.run([*base, "-f", "lavfi", "-i", "sine=frequency=440:duration=2", str(music)], check=True)+ request = TaskVideoRequest(video_subject="Music", subtitle_enabled=False, bgm_type="custom", bgm_file=str(music), bgm_volume=1.0, n_threads=1)+ from unittest.mock import patch+ with patch.object(video, "get_bgm_file", return_value=str(music)):+ assert video.generate_video(str(footage), str(speech), "", str(output), request)+ decoded = subprocess.run([*base, "-i", str(output), "-map", "0:a:0", "-f", "f32le", "-acodec", "pcm_f32le", "-ac", "1", "-ar", "44100", "-"], capture_output=True, check=True)+ samples = np.frombuffer(decoded.stdout, dtype="<f4")+ def rms(start, end):+ window = samples[int(start * 44100):int(end * 44100)]+ return float(np.sqrt(np.mean(window * window)))+ initial = rms(0.2, 0.4)+ assert initial > 0.02+ # An interior point close to the first source's end must remain at full gain.+ assert rms(1.7, 1.9) > initial * 0.85+ assert rms(3.7, 3.9) > initial * 0.85+ # Only the final three seconds fade; the endpoint is quieter than its start.+ assert rms(7.7, 7.9) < initial * 0.15