How can I convert MP4 video to MP3 audio with FFmpeg?

125

47

I need to extract an MP3 audio track from an MP4 video with ffmpeg. I can do this for .flv -> mp3, but I don't know the command line parameters for mp4->mp3. For example, flv -> mp3:

ffmpeg -i video.flv -acodec copy audio.mp3

What parameters should I use for mp4 -> mp3?

user916350

Posted 2011-09-06T05:19:04.603

Reputation:

no more ffmpeg for ubuntu 14.04 – Louis – 2014-10-07T18:03:19.343

@ThomWiggers is it necessarily lossy? I don't know how mp4s are encoded, but as long as you can separate video and audio, it shouldn't have to be – Nathan – 2019-06-17T14:47:24.050

The original version of this question was not clear how the audio was encoded – that got changed in edits. Remuxing does not have to be lossy. – Thom Wiggers – 2019-06-18T15:03:51.060

2If you don't really need an MP3, I would not convert the audio: MP4->MP3 is a lossy transformation, you will lose extra source information. – Thom Wiggers – 2013-12-08T00:33:34.737

Answers

159

The basic command is:

ffmpeg -i filename.mp4 filename.mp3

or

ffmpeg -i video.mp4 -b:a 192K -vn music.mp3

Check this URL: MP4 Video to MP3 File Using ffmpeg (Ubuntu 9.10 Karmic Koala)

Note: Ubuntu does not supply FFmpeg, but the fork named Libav. The syntax is the same – just use avconv instead of ffmpeg for the above examples.

Naga Harish M

Posted 2011-09-06T05:19:04.603

Reputation: 1 699

no more ffmpeg for ubuntu 14.04 – Louis – 2014-10-07T18:03:34.880

2

@Louis You can still download it from http://ffmpeg.org/download.html

– slhck – 2014-11-22T07:39:53.910

1

@Louis - same for 14.10, but it is apparently returning in 15.04: http://askubuntu.com/questions/432542/is-ffmpeg-missing-from-the-official-repositories-in-14-04

– Wilf – 2015-04-04T11:53:10.240

works for me ffmpeg -i $fileNameWithExtension $fileNameOnly".mp3" – JinSnow – 2019-05-23T15:43:35.850

4Thank you! This quality is best, or I can do better? – None – 2011-09-06T05:32:54.460

try with 320kbs http://en.wikipedia.org/wiki/MP3#Bit_rate

– None – 2011-09-06T05:36:56.723

56

The better way to encode MP3 is to use -q:a for variable bit rate.

ffmpeg -i k.mp4 -q:a 0 -map a k.mp3

The q option can only be used with libmp3lame and corresponds to the LAME -V option. See:

Steven Penny

Posted 2011-09-06T05:19:04.603

Reputation: 7 294

This one finally played also the Android Music player. Thanks – michalzuber – 2015-11-10T07:24:27.350

1And to convert whole directory (including filenames with spaces) with the above command: for i in *.mp4; do ffmpeg -i "$i" -q:a 0 -map a "$(basename "${i/.mp4}").mp3"; done; – kingSlayer – 2016-11-29T19:27:02.013

How is the resulting .mp3 different from the vanilla command ffmpeg -i vid.mp4 audio.mp3 in @Naga Harish M 's answer? Less lossy? – Nathan – 2019-06-17T14:48:54.333

@slhck ...that's a good point, but I've just tested on a video file, and leaving out -vn just copies the audio stream. I suppose ffmpeg must have some way of detecting the difference (I just checked a file with ffprobe, and the video was stream 1 rather than the usual 0, and had some metadata: comment : Cover (front)) – evilsoup – 2013-02-17T21:31:33.563

0

I got it working from youtube mp4 videos with follwing command:

ffmpeg -i video.mp4 -vn audio.mp3

caruccio

Posted 2011-09-06T05:19:04.603

Reputation: 118