How can I convert wav files to mp3 files in Python ? The only answers I found say to use "pydub" but it's not working at all for me :
from pydub import AudioSegment
AudioSegment.from_wav("myfile.wav").export("myfile.mp3", format="mp3")
Result with the error :
CouldntEncodeError: Encoding failed. ffmpeg/avlib returned error code: 1
Command:['ffmpeg', '-y', '-f', 'wav', '-i', 'C:\\Users\\user\\AppData\\Local\\Temp\\tmp4bj663tf', '-f', 'mp3',
'C:\\Users\\user\\AppData\\Local\\Temp\\tmp0kadz5q2']
Output from ffmpeg/avlib:
ffmpeg version 4.3.1 Copyright (c) 2000-2020 the FFmpeg developers
built with gcc 10.2.1 (GCC) 20200726
configuration: --disable-static --enable-shared --enable-gpl --enable-version3 --enable-sdl2 --enable-fontconfig --enable-gnutls --enable-iconv --enable-libass --enable-libdav1d --enable-libbluray --enable-libfreetype --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libsrt --enable-libtheora --enable-libtwolame --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libzimg --enable-lzma --enable-zlib --enable-gmp --enable-libvidstab --enable-libvmaf --enable-libvorbis --enable-libvo-amrwbenc --enable-libmysofa --enable-libspeex --enable-libxvid --enable-libaom --enable-libgsm --enable-librav1e --disable-w32threads --enable-libmfx --enable-ffnvcodec --enable-cuda-llvm --enable-cuvid --enable-d3d11va --enable-nvenc --enable-nvdec --enable-dxva2 --enable-avisynth --enable-libopenmpt --enable-amf
libavutil 56. 51.100 / 56. 51.100
libavcodec 58. 91.100 / 58. 91.100
libavformat 58. 45.100 / 58. 45.100
libavdevice 58. 10.100 / 58. 10.100
libavfilter 7. 85.100 / 7. 85.100
libswscale 5. 7.100 / 5. 7.100
libswresample 3. 7.100 / 3. 7.100
libpostproc 55. 7.100 / 55. 7.100
Guessed Channel Layout for Input Stream #0.0 : mono
Input #0, wav, from 'C:\Users\user\AppData\Local\Temp\tmp4bj663tf':
Duration: 00:49:30.40, bitrate: 800 kb/s
Stream #0:0: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 50000 Hz, mono, s16, 800 kb/s
Stream mapping:
Stream #0:0 -> #0:0 (pcm_s16le (native) -> mp3 (mp3_mf))
Press [q] to stop, [?] for help
[mp3_mf @ 00000217000ebdc0] MFT name: 'MP3 Encoder ACM Wrapper MFT'
[mp3_mf @ 00000217000ebdc0] unsupported input sample rate set
Error initializing output stream 0:0 -- Error while opening encoder for output stream #0:0 - maybe incorrect parameters such as bit_rate, rate, width or height
Conversion failed!
Paths to files are correct (else I have a different error) and I've tried with frequencies from 50k to 5k for the input signal, same error.
Searching for another way to convert or a fix for this.
CodePudding user response:
The error message suggests that the issue is related to the sample rate of the input audio file. The error message states that the sample rate of the input audio file is not supported by the encoder.
One solution would be to use the set_frame_rate function from pydub, which can change the sample rate of an audio file, before exporting it to mp3. You can try something like this:
from pydub import AudioSegment
audio = AudioSegment.from_wav("myfile.wav")
audio = audio.set_frame_rate(44100)
audio.export("myfile.mp3", format="mp3")
It's also possible that the ffmpeg library pydub uses doesn't support the sample rate of your wav file. You can try specifying the ffmpeg path explicitly when you create an AudioSegment object and see if that fixes the issue:
from pydub import AudioSegment
AudioSegment.converter = r"path\to\ffmpeg.exe"
audio = AudioSegment.from_wav("myfile.wav")
audio.export("myfile.mp3", format="mp3")
If none of the above solution is working, you may try other libraries like moviepy or soundfile to perform the conversion, or use command line tools such as ffmpeg directly.