python-soundfile
python-soundfile copied to clipboard
Fail to write mp3 file in ubuntu
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.
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
On Linux, soundfile uses your system-installed libsndfile. What version is your libsndfile, and does it support MP3 yet?
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
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.
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
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
That's probably the reason then. The default compilation probably doesn't include MP3 support, or didn't find the required dependencies.