question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't read mp3 from BytesIO: File contains data in an unknown format.

See original GitHub issue

Describe the bug An MP3 file that can be loaded just fine from disk fails to be loaded when offered as an io.BytesIO object.

If this is a known restriction, it could be documented better.

To Reproduce

import librosa
import io

# This works fine.
print(librosa.load('/tmp/test.mp3'))

# This doesn't.
with open('/tmp/test.mp3', 'rb') as f:
    data = f.read()
print(librosa.load(io.BytesIO(data)))

Actual output

/.../.venv/lib/python3.8/site-packages/librosa/core/audio.py:162: UserWarning: PySoundFile failed. Trying audioread instead.
  warnings.warn("PySoundFile failed. Trying audioread instead.")
(array([0., 0., 0., ..., 0., 0., 0.], dtype=float32), 22050)
Traceback (most recent call last):
  File "/tmp/test.py", line 10, in <module>
    print(librosa.load(io.BytesIO(data)))
  File "/.../.venv/lib/python3.8/site-packages/librosa/core/audio.py", line 165, in load
    raise (exc)
  File "/.../.venv/lib/python3.8/site-packages/librosa/core/audio.py", line 146, in load
    with sf.SoundFile(path) as sf_desc:
  File "/.../.venv/lib/python3.8/site-packages/soundfile.py", line 629, in __init__
    self._file = self._open(file, mode_int, closefd)
  File "/.../.venv/lib/python3.8/site-packages/soundfile.py", line 1183, in _open
    _error_check(_snd.sf_error(file_ptr),
  File "/.../.venv/lib/python3.8/site-packages/soundfile.py", line 1357, in _error_check
    raise RuntimeError(prefix + _ffi.string(err_str).decode('utf-8', 'replace'))
RuntimeError: Error opening <_io.BytesIO object at 0x7ff3086254f0>: File contains data in an unknown format.

Software versions

Linux-5.8.14-arch1-1-x86_64-with-glibc2.2.5
Python 3.8.6 (default, Sep 30 2020, 04:00:38) 
[GCC 10.2.0]
NumPy 1.18.2
SciPy 1.5.4
librosa 0.8.0
INSTALLED VERSIONS
------------------
python: 3.8.6 (default, Sep 30 2020, 04:00:38) 
[GCC 10.2.0]

librosa: 0.8.0

audioread: 2.1.9
numpy: 1.18.2
scipy: 1.5.4
sklearn: 0.23.2
joblib: 0.17.0
decorator: 4.4.2
soundfile: 0.10.3
resampy: 0.2.2
numba: 0.51.2

numpydoc: None
sphinx: None
sphinx_rtd_theme: None
sphinxcontrib.versioning: None
sphinx-gallery: None
pytest: None
pytest-mpl: None
pytest-cov: None
matplotlib: 3.2.1
presets: None

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
bmcfeecommented, Nov 21, 2020

The problem here is that mp3 is not supported by soundfile (yet), so librosa will fall back to audioread. Audioread doesn’t generally support BytesIO input (because it in turn wraps several possible decoder backends, like ffmpeg, gstreamer, etc).

I’d be open to expanding the documentation here, but don’t have the bandwidth to handle it myself right now.

0reactions
ttencatecommented, Mar 7, 2022
Read more comments on GitHub >

github_iconTop Results From Across the Web

Librosa cannot load from BytesIO - Stack Overflow
It breaks with the following error: RuntimeError: Error opening <_io.BytesIO object at 0x7f509ed1cf90>: File contains data in an unknown format.
Read more >
Librosa not loading mp3 files from dataset. - Reddit
I am getting an error saying : Error opening 'D:/fma_small/000/000002.mp3': File contains data in an unknown format. I did go through their ...
Read more >
Error opening 'female_present.wav': File contains data in an ...
As indicated in the title, the sound module doesn't play my file and says it's of an unknown format, despite being a .wav....
Read more >
File contains data in an unknown format" when trying to play ...
The first thing to do is to identify the file format. Try this page: File Identifier - Identify unknown files instantly. You should...
Read more >
Advanced I/O Use Cases — librosa 0.10.0.dev0 documentation
Reading audio files using soundfile is similar to the method in librosa. One important difference is that the read data is of shape...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found