Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot read mp3 with file-like object #439

Open
BakerBunker opened this issue Jul 12, 2024 · 7 comments
Open

Cannot read mp3 with file-like object #439

BakerBunker opened this issue Jul 12, 2024 · 7 comments

Comments

@BakerBunker
Copy link

BakerBunker commented Jul 12, 2024

I tried to read a mp3 with file handle, but soundfile failed to read.

import soundfile as sf

ret = sf.read("052316.mp3")
print(ret)

try:
    sf.read(open("052316.mp3", "rb"))
except Exception as e:
    print(e)

it shows

(array([[0., 0.],
       [0., 0.],
       [0., 0.],
       ...,
       [0., 0.],
       [0., 0.],
       [0., 0.]]), 44100)
Error opening <_io.BufferedReader name='052316.mp3'>: Format not recognised.

and here is the file
052316.mp3.zip

@bastibe
Copy link
Owner

bastibe commented Jul 15, 2024

Since no file name is available with open, you need to provide a format and subtype manually.

@BakerBunker
Copy link
Author

Is it possible to try to get the file name with name property?

https://github.com/python/cpython/blob/8303d32ff55945c5b38eeeaf1b1811dbcf8aa9be/Lib/typing.py#L3472-L3475

@bastibe
Copy link
Owner

bastibe commented Jul 15, 2024

@BakerBunker
Copy link
Author

According to PEP519, the __fspath__ method is only for the path-like object, for file-like objects created by open and remote file-like objects created by smart_open, the filename should accessed by name property.

@bastibe
Copy link
Owner

bastibe commented Jul 16, 2024

It should be reading the format from the name property: https://github.com/bastibe/python-soundfile/blob/master/soundfile.py#L1466

@BakerBunker
Copy link
Author

BakerBunker commented Jul 16, 2024

It's strange, I get an error when I open the above file with sf.read(open("052316.mp3",'rb')), but not when I open this one, is it because the metadata is different?
111097.mp3.zip

file 052316.mp3
> Audio file with ID3 version 2.4.0
file 111097.mp3
> Audio file with ID3 version 2.4.0, contains: MPEG ADTS, layer III, v1, 320 kbps, 44.1 kHz, Stereo

@bastibe
Copy link
Owner

bastibe commented Jul 17, 2024

Perhaps the file is broken in some way? Libsndfile is a bit more stringent in its MP3 implementation than some other libs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants