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 image

Versions

torchaudio version: 0.11.0

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 25

Most upvoted comments

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:

Python 3.8.13 (default, Mar 28 2022, 11:38:47)
[GCC 7.5.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import torchaudio
>>> torchaudio.get_audio_backend()
'sox_io'
>>> wav, sr = torchaudio.load("test.mp3", format="mp3")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/path/wav2vec/lib/python3.8/site-packages/torchaudio/backend/sox_io_backend.py", line 227, in load
    return _fallback_load(filepath, frame_offset, num_frames, normalize, channels_first, format)
  File "/path/wav2vec/lib/python3.8/site-packages/torchaudio/backend/sox_io_backend.py", line 29, in _fail_load
    raise RuntimeError("Failed to load audio from {}".format(filepath))
RuntimeError: Failed to load audio from test.mp3
>>>

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:

To load MP3, FLAC, OGG/VORBIS, OPUS and other codecs libsox does not handle natively, your installation of torchaudio has to be linked to libsox and corresponding codec libraries such as libmad or libmp3lame etc.

  1. try the ldd cmd: ldd ${python_lib}/dist-packages/torchaudio/lib/libtorchaudio_ffmpeg.so
  2. check if all libraries are linked to torchaudio
libavdevice.so.58 => /usr/lib/x86_64-linux-gnu/libavdevice.so.58 (0x00007fb903e38000)
libavfilter.so.7 => /usr/lib/x86_64-linux-gnu/libavfilter.so.7 (0x00007fb903796000)
libavformat.so.58 => /usr/lib/x86_64-linux-gnu/libavformat.so.58 (0x00007fb9032ed000)
libavcodec.so.58 => /usr/lib/x86_64-linux-gnu/libavcodec.so.58 (0x00007fb901ce7000)
libavutil.so.56 => /usr/lib/x86_64-linux-gnu/libavutil.so.56 (0x00007fb901815000)
  1. if not, install with apt/yum 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:

>>> import torchaudio
>>> torchaudio.utils.sox_utils.list_read_formats()
['aifc', 'aiffc', 'aiff', 'aif', 'al', 'au', 'snd', 'avr', 'cdda', 'cdr', 'cvsd', 'cvs', 'cvu', 'dat', 'dvms', 'vms', 'f4', 'f32', 'f8', 'f64', 'gsrt', 'hcom', 'htk', 'ima', 'la', 'lu', 'maud', 'null', 'prc', 'raw', 's1', 's8', 'sb', 's2', 's16', 'sw', 's3', 's24', 's4', 's32', 'sl', 'sf', 'ircam', 'sln', 'smp', 'sndr', 'sndt', 'sox', 'sph', 'nist', '8svx', 'txw', 'u1', 'u8', 'ub', 'sou', 'fssd', 'u2', 'u16', 'uw', 'u3', 'u24', 'u4', 'u32', 'ul', 'voc', 'vox', 'wav', 'wavpcm', 'amb', 'wve', 'xa', 'amr-nb', 'anb', 'amr-wb', 'awb', 'flac', 'gsm', 'lpc10', 'lpc', 'opus', 'vorbis', 'ogg']

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

# use libavcodec.so.58 under /data/home/yl/anaconda3/envs/mert/bin/../lib/ first
export LD_LIBRARY_PATH=/data/home/yl/envs/mert/bin/../lib/:$LD_LIBRARY_PATH

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.

%%bash 
MINICONDA_INSTALLER_SCRIPT=Miniconda3-4.5.4-Linux-x86_64.sh
MINICONDA_PREFIX=/usr/local
wget https://repo.continuum.io/miniconda/$MINICONDA_INSTALLER_SCRIPT
chmod +x $MINICONDA_INSTALLER_SCRIPT
./$MINICONDA_INSTALLER_SCRIPT -b -f -p $MINICONDA_PREFIX
conda update -n base conda
yes | conda install ffmpeg=4.3 -c conda-forge
yes | conda uninstall torch torchvision torchaudio 
yes | conda install pytorch torchvision torchaudio cudatoolkit=11.3 -c pytorch

...etc...

even after restarting the runtime and coming back, same error persists.

So, …

Solution

Forget torchaudio. Just do a

!sudo apt install ffmpeg

then

import librosa

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.