[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
+8 -1
View File
@@ -782,6 +782,7 @@ def load_audio(
if _BACKEND == "torchcodec":
from torchcodec.decoders import AudioDecoder
try:
decoder = AudioDecoder(
source,
sample_rate=sr,
@@ -791,8 +792,14 @@ def load_audio(
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