pydub returning zero byte file when I try to export part of an Mp3

620 views Asked by At

I'm sharing this for posterity, since I already figured out the answer. I'm trying to create preview snippets of MP3 files with pydub in Python, but when I ran the export the files were always zero bytes in size.

example:

from pydub import AudioSegment    

sound = AudioSegment.from_mp3("preview_temp/n/1/mp3/01_Beyond_and_Into_the_Ultimate.mp3")

# len() and slicing are in milliseconds
halfway_point = len(sound) / 2

second_half = sound[halfway_point:]
print second_half
second_half.export("preview_temp/n/1/prev/prev_test.mp3", format="mp3", bitrate="192k")

I verified the source file existed and was accessible. My inkling was this had something to do with ffmpeg. It was installed properly, but then I came across this on the pydub github:

You may use libav or ffmpeg. libav is recommended.

The pydub developer runs through installation on several platforms on the github wiki. I'm sharing it here to make it easier for folks to find, though.

2

There are 2 answers

0
tom f On BEST ANSWER

Once I installed libav, everything was a-ok. Not sure why ffmpeg wasn't outputting, but installing libav solved it.

On debian/ubuntu:

# libav
apt-get install libav-tools libavcodec-extra

####    OR    #####

# ffmpeg
apt-get install ffmpeg libavcodec-extra

On OSX, with homebrew:

# libav
brew install libav --with-libvorbis --with-sdl --with-theora

####    OR    #####

# ffmpeg
brew install ffmpeg --with-libvorbis --with-ffplay --with-theora

All from the pydub readme

0
Alireza Akhavan On

Sometimes you should mention the codec. In this case, you should add codec="libmp3lame":

second_half.export("preview_temp/n/1/prev/prev_test.mp3", format="mp3", bitrate="192k", codec="libmp3lame")