[fix] load_audio: fall back to soundfile when torchcodec fails on WAV with trailing metadata (#24185)

This commit is contained in:
Yihao Wang
2026-05-14 15:02:49 +08:00
committed by GitHub
parent 22bfae0d1d
commit 41eb2d861b
+17 -10
View File
@@ -782,17 +782,24 @@ def load_audio(
if _BACKEND == "torchcodec":
from torchcodec.decoders import AudioDecoder
decoder = AudioDecoder(
source,
sample_rate=sr,
num_channels=1 if mono else None,
)
samples = decoder.get_all_samples()
if mono:
return samples.data.squeeze(0).numpy()
return samples.data.T.numpy()
try:
decoder = AudioDecoder(
source,
sample_rate=sr,
num_channels=1 if mono else None,
)
samples = decoder.get_all_samples()
if mono:
return samples.data.squeeze(0).numpy()
return samples.data.T.numpy()
except Exception as e:
# torchcodec's bytes-buffer IO can fail on WAV files that carry
# large trailing metadata chunks. Fall back to soundfile, which reads the PCM payload directly.
logger.warning(
f"torchcodec AudioDecoder failed ({e}); falling back to soundfile + torchaudio."
)
# Fallback: soundfile + torchaudio (ARM / no FFmpeg)
# Fallback: soundfile + torchaudio (ARM / no FFmpeg / torchcodec failure)
import soundfile as sf
import torch
import torchaudio