[fix] load_audio: fall back to soundfile when torchcodec fails on WAV with trailing metadata (#24185)
This commit is contained in:
@@ -782,17 +782,24 @@ def load_audio(
|
|||||||
if _BACKEND == "torchcodec":
|
if _BACKEND == "torchcodec":
|
||||||
from torchcodec.decoders import AudioDecoder
|
from torchcodec.decoders import AudioDecoder
|
||||||
|
|
||||||
decoder = AudioDecoder(
|
try:
|
||||||
source,
|
decoder = AudioDecoder(
|
||||||
sample_rate=sr,
|
source,
|
||||||
num_channels=1 if mono else None,
|
sample_rate=sr,
|
||||||
)
|
num_channels=1 if mono else None,
|
||||||
samples = decoder.get_all_samples()
|
)
|
||||||
if mono:
|
samples = decoder.get_all_samples()
|
||||||
return samples.data.squeeze(0).numpy()
|
if mono:
|
||||||
return samples.data.T.numpy()
|
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 soundfile as sf
|
||||||
import torch
|
import torch
|
||||||
import torchaudio
|
import torchaudio
|
||||||
|
|||||||
Reference in New Issue
Block a user