6

I'm trying to export a file as mp3 in pydub, but I get this error:

Automatic encoder selection failed for output stream #0:0. Default encoder for format mp3 is probably disabled. Please choose an encoder manually

How do I select an encoder manually, what is the default encoder, and how could I enable it?

PS: My Pydub opens mp3 files without any problem. I'm using Windows and Libav.

Daniel Möller
  • 84,878
  • 18
  • 192
  • 214
  • Possible duplicate of https://stackoverflow.com/questions/20443850/error-when-exporting-with-pydub-how-to-install-mp3-codecs-for-pydub#20445742. Have you tried encoding as a `wav` file to ensure that your file is encodable? Check to see that your mp3 codec is installed. – NuclearPeon May 18 '17 at 22:14
  • Yes, it exports as wav. And it loads mp3. – Daniel Möller May 18 '17 at 22:16
  • 1
    Playing mp3s requires a decoder, whereas writing mp3s requires an encoder. What OS are you using and what is your mp3 encoder? Are you using `ffmpeg`, `gstreamer`, `ubuntu-restricted-codecs`, etc. You can download and compile the source for Lame (mp3 encoder) on Mac/Linux/Windows here: http://lame.sourceforge.net/download.php . – NuclearPeon May 18 '17 at 23:09
  • I don't think I have an encoder.... – Daniel Möller May 18 '17 at 23:29
  • 1
    Here's a useful link. Audacity is an audio editor and if you try to export mp3s without the encoder it brings you to this page. I post this because it has useful links about the nature of encoders and links to liblame that does not require compiling from source. http://manual.audacityteam.org/man/faq_installation_and_plug_ins.html#lame If you're on Linux, installing the codec is easy. Otherwise, check out the files here: http://lame.buanzo.org/#lamewindl – NuclearPeon May 18 '17 at 23:37

3 Answers3

6

The other solution did not work for me. The problem for me was that the ffmpeg version that came installed with Anaconda did not seem to be compiled with an encoder. So instead of:

DEA.L. mp3 MP3 (MPEG audio layer 3) (decoders: mp3 mp3float mp3_at ) (encoders: libmp3lame )

I saw:

DEA.L. mp3 MP3 (MPEG audio layer 3) (decoders: mp3 mp3float mp3_at )

Without the (encoders: ...) part.

My solution was to do this:

  • ffmpeg -codecs | grep mp3, to check if there is any encoder (there isn't!).
  • conda uninstall ffmpeg
  • Open new terminal window.
  • brew install ffmpeg --with-libmp3lame
  • ffmpeg -codecs | grep mp3, to check if there is any encoder (now there is!).
Emiel
  • 343
  • 6
  • 14
  • for some reason, just conda uninstall FFmpeg on my Mac 10.14.5 (Mojave) worked without having to install FFmpeg through brew. – anjchang Sep 24 '19 at 06:55
3

you can find which codecs are available with ffmpeg -codecs or avconv -codecs and on the line with mp3 you'll see something like:

DEA.L. mp3                  MP3 (MPEG audio layer 3) (decoders: mp3 mp3float mp3_at ) (encoders: libmp3lame )

D means ffmpeg can decode
E means it can encode
A means it's an audio codec
L means it's a lossy encoding

but the most important part is the encoders: … portion

I think you'll need to pick one of the encoders listed and tell pydub to use it (I'm not sure why, this isn't required on my machine - but it probably depends on your ffmpeg installation)

from pydub import AudioSegment
sound = AudioSegment.from_file(…)
sound.export("/path/to/output.mp3", codec="libmp3lame")
Jiaaro
  • 74,485
  • 42
  • 169
  • 190
  • I don't think I have ffmpeg....I followed an alternative installation mentioned in pydub documentaion using libav.... – Daniel Möller May 19 '17 at 04:09
  • @Daniel Updated my answer to include the libav command as well – Jiaaro May 22 '17 at 14:18
  • 1
    if you do not have an mp3 encoder installed with libav maybe try installing ffmpeg instead in hopes that it bundles an mp3 encoder? I do not use windows so I, unfortunately, don't know much about your options wrt getting support for specific formats – Jiaaro Jun 01 '17 at 23:38
3

I had the same problem as Emiel, where the ffmpeg version available in Anaconda did have an mp3 encoder. I solved the problem another way, by installing this version of ffmpeg from the conda-forge channel. I used:

conda install -n <anaconda-env> -c conda-forge ffmpeg 

Now using ffmpeg -codecs | grep mp3 the mp3 encoders show up:

 DEA.L. mp3      MP3 (MPEG audio layer 3) (decoders: mp3float mp3 ) (encoders: libmp3lame libshine )