Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.
TypeScript ai-artartificial-intelligencegenerative-artimage-generationimg2imginpaintinglatent-diffusionlinuxmacosoutpaintingstable-diffusiontxt2imgwindows
fix(audio): select first stream for int16 extraction
agammann4c3c59066721
diff --git a/invokeai/app/util/video_audio.py b/invokeai/app/util/video_audio.pyindex 074546c539..f7cc6eeb1a 100644--- a/invokeai/app/util/video_audio.py+++ b/invokeai/app/util/video_audio.py@@ -35,7 +35,7 @@ class AudioExtractionError(RuntimeError):def extract_audio_pcm(video_path: Path, *, float_pcm: bool = False) -> tuple[np.ndarray, int] | None:- """Decode ``video_path``'s audio track to float32 stereo PCM at its native sample rate.+ """Decode ``video_path``'s first audio stream to float32 stereo PCM at its native sample rate.Returns ``(samples, sample_rate)`` with ``samples`` shaped ``(2, n)`` in [-1, 1], or``None`` when the container has no audio stream. Mono sources are upmixed to stereo by@@ -64,7 +64,8 @@ def extract_audio_pcm(video_path: Path, *, float_pcm: bool = False) -> tuple[np."error","-i",str(video_path),- *(["-map", "0:a:0?"] if float_pcm else []),+ "-map",+ "0:a:0?","-vn","-ac","2",diff --git a/tests/app/util/test_video_audio_float.py b/tests/app/util/test_video_audio_float.pyindex c5052ac0a5..044ce7d693 100644--- a/tests/app/util/test_video_audio_float.py+++ b/tests/app/util/test_video_audio_float.py@@ -1,4 +1,4 @@-"""The float-PCM branch of extract_audio_pcm (Ref2VA reference soundtracks).+"""PCM extraction uses the first audio stream for both decode formats.The float branch pins ``-map 0:a:0`` and reads the rate off the container banner; the twomust describe the same stream, including on multi-track containers where ffmpeg's default@@ -72,21 +72,31 @@ def test_float_branch_returns_float_samples_at_the_probed_rate(tmp_dir):assert np.mean((pcm * 32768.0) % 1.0 > 1e-6) > 0.5-def test_multi_track_container_decodes_the_stream_the_probe_describes(tmp_dir):[email protected]("float_pcm", [False, True])+def test_multi_track_container_decodes_the_first_audio_stream(tmp_dir, float_pcm):# Track 0 at 44100 Hz, track 1 at 48000 Hz: without the -map pin, ffmpeg's default- # selection can decode a different stream than the banner-order probe reports, and the- # soundtrack conditions ~9% off-speed - silently.+ # selection picks the wrong soundtrack and, for float PCM, disagrees with the probe.video = _make_video(tmp_dir, [(_sine(44100, 2.0, 440.0), 44100), (_sine(48000, 2.0, 220.0), 48000)])- extracted = extract_audio_pcm(video, float_pcm=True)+ extracted = extract_audio_pcm(video, float_pcm=float_pcm)assert extracted is not Nonepcm, rate = extractedassert rate == 44100# The sample COUNT must match the probed rate's timeline (2 s +/- codec padding).assert pcm.shape[1] == pytest.approx(2.0 * rate, rel=0.05)+ spectrum = np.abs(np.fft.rfft(pcm[0]))+ frequency = np.fft.rfftfreq(pcm.shape[1], 1.0 / rate)[np.argmax(spectrum)]+ assert frequency == pytest.approx(440.0, abs=5.0)++[email protected]("float_pcm", [False, True])+def test_container_without_audio_returns_none(tmp_dir, float_pcm):+ video = _make_video(tmp_dir, [])++ assert extract_audio_pcm(video, float_pcm=float_pcm) is None-def test_int16_branch_unchanged(tmp_dir):+def test_int16_branch_decodes_single_audio_stream(tmp_dir):video = _make_video(tmp_dir, [(_sine(44100, 1.0, 440.0), 44100)])extracted = extract_audio_pcm(video)