audio: torchaudio.load() for file-like object fails for mp3 files
🐛 Describe the bug
Description
This error occurs when trying to read a file-like object that contains an MP3 audio. This error does not occur for file-like objects that contain WAV audio.
Stack Trace
formats: can't determine type of file `'
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
File "/home/rob/code/english_toolkit/.venv/lib/python3.8/site-packages/torchaudio/backend/sox_io_backend.py", line 149, in load
return torchaudio._torchaudio.load_audio_fileobj(
RuntimeError: Error loading audio file: failed to open file <in memory buffer>
Reproducible Snippets
To reproduce with MP3:
with requests.get("https://filesamples.com/samples/audio/mp3/sample3.mp3", stream=True) as response:
y,sr = torchaudio.load(response.raw)
To verify this is not an issue for WAV"
with requests.get("https://www2.cs.uic.edu/~i101/SoundFiles/gettysburg10.wav", stream=True) as response:
y,sr = torchaudio.load(response.raw)
Relevant Documentation
These snippets copy exactly the torchaudio load filelike object documentation
Versions
torchaudio version: 0.11.0
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 25
Starting 0.12, the MP3 decoder is switched to libavcodec. The error indicates that there is no installation of FFmpeg 4. Please install ffmpeg 4.
conda install 'ffmpeg<5'
should do in most cases.Hi @rbracco
To use MP3 with file-like object, you need to pass
format="mp3"
argument.I’m having trouble loading mp3 even when specifying the format:
Solution:
make sure the ffmpeg codec libraries is linked to libtorchaudio
Details:
I spent two days to solve this problem, my situation is: ffmpeg: 5.1.1, torchaudio: 0.13.1, python: 3.9 I tried all advices above and did not work, then I searched in torchaudio official doc, and found this about the load function:
ldd ${python_lib}/dist-packages/torchaudio/lib/libtorchaudio_ffmpeg.so
apt install libavcodec-dev ...
Question:
though it works in torchaudio.load() function, i still failed to find ‘mp3’ in torchaudio.utils.sox_utils.list_read_formats() outputs:
I have same problem(ffmpeg=4.3, torchaudio=0.12)。I found /lib64/libavcodec.so.57 under /lib64. but /data/home/yiljiang/anaconda3/envs/mert/bin/…/lib/libavcodec.so.58 under conda env directory.
So I set
And then problem solved
On Google Colab, the following should install a supported FFmpeg.
‘’’ !add-apt-repository -y ppa:savoury1/ffmpeg4 !apt-get -qq install -y ffmpeg ‘’’
…Follow up: Nope. Even after installing
conda
on Colab, still getting the same error.even after restarting the runtime and coming back, same error persists.
So, …
Solution
Forget torchaudio. Just do a
then
load with librosa and recast to
torch.tensor()
✅Failed to load mp3 with ffmpeg==4.2.7 and torchaudio==2.0.1
For ffmpeg 4.2.8 (devel-package installed), I was able to achieve mp3 support on torchaudio 0.13.1 by explicit setting USE_FFMPEG=1 when building torchaudio from the sources
None of the previous advice was working for me (ffmpeg=4.3, torchaudio=0.12.1). I upgraded ffmpeg to version 5.1.2, and now I can load mp3 files.