bastibe / python-soundfile

SoundFile is an audio library based on libsndfile, CFFI, and NumPy
BSD 3-Clause "New" or "Revised" License
701 stars 108 forks source link

Fail to write mp3 file in ubuntu #348

Open itsdapi opened 1 year ago

itsdapi commented 1 year ago

my code is

import numpy as np
import soundfile as sf

print(sf.__libsndfile_version__)
sf.write('stereo_file2.mp3', np.random.randn(10, 2), 44100)

and the out put

1.1.0
Traceback (most recent call last):
  File "sftest.py", line 5, in <module>
    sf.write('stereo_file2.mp3', np.random.randn(10, 2), 44100)
  File "/home/dapi/.local/lib/python3.8/site-packages/soundfile.py", line 340, in write
    with SoundFile(file, 'w', samplerate, channels,
  File "/home/dapi/.local/lib/python3.8/site-packages/soundfile.py", line 655, in __init__
    self._file = self._open(file, mode_int, closefd)
  File "/home/dapi/.local/lib/python3.8/site-packages/soundfile.py", line 1213, in _open
    raise LibsndfileError(err, prefix="Error opening {0!r}: ".format(self.name))
soundfile.LibsndfileError: Error opening 'stereo_file2.mp3': File contains data in an unimplemented format.

Since the apt can only get libsndfile1 version 1.0.28, so i Cmake it to update to 1.1.0 like

./configuration
make
sudo make install

The Soundfile version is 0.11.0b6 ubuntu server 20.04

It did generate an mp3 file but it was corrupted.

itsdapi commented 1 year ago

i tried

sf.write('stereo_file.mp3', np.random.randn(10, 2), 44100, format='MP3', subtype='MPEG_LAYER_III')

but it was the same result

bastibe commented 1 year ago

On Linux, soundfile uses your system-installed libsndfile. What version is your libsndfile, and does it support MP3 yet?

itsdapi commented 1 year ago

On Linux, soundfile uses your system-installed libsndfile. What version is your libsndfile, and does it support MP3 yet?

yeah i have checked it is 1.1.0

itsdapi commented 1 year ago

On Linux, soundfile uses your system-installed libsndfile. What version is your libsndfile, and does it support MP3 yet?

But when i list all avaliable formats and suntypes, MP3 not showing up.

bastibe commented 1 year ago

It might be that your libsndfile is the current version, but MP3 support is not enabled. Did you compile it with liblame and mpg123? Have a look at this build script to compile a working version: https://github.com/bastibe/libsndfile-binaries/blob/master/mac_build.sh

itsdapi commented 1 year ago

It might be that your libsndfile is the current version, but MP3 support is not enabled. Did you compile it with liblame and mpg123? Have a look at this build script to compile a working version: https://github.com/bastibe/libsndfile-binaries/blob/master/mac_build.sh

no i just download the tar.xz file and run the configure file, and sudo make install I will check it later

bastibe commented 1 year ago

That's probably the reason then. The default compilation probably doesn't include MP3 support, or didn't find the required dependencies.