221

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?

Zombo
  • 1
  • 24
  • 120
  • 163
  • 2
    If 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 Dec 08 '13 at 00:33
  • no more ffmpeg for ubuntu 14.04 – Louis Oct 07 '14 at 18:03
  • 1
    @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 majicvr.com Jun 17 '19 at 14:47
  • 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 Jun 18 '19 at 15:03

4 Answers4

292

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) link broken [Updated on 7th Dec 2021]

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
  • 3,029
  • 1
  • 14
  • 7
  • 6
    Thank you! This quality is best, or I can do better? –  Sep 06 '11 at 05:32
  • 1
    try with 320kbs http://en.wikipedia.org/wiki/MP3#Bit_rate –  Sep 06 '11 at 05:36
  • 2
    @Louis You can still download it from http://ffmpeg.org/download.html – slhck Nov 22 '14 at 07:39
  • 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 Apr 04 '15 at 11:53
  • 1
    works for me `ffmpeg -i $fileNameWithExtension $fileNameOnly".mp3"` – JinSnow May 23 '19 at 15:43
  • I use Lubuntu 20_04 and avconv is not pre-installed. So I apt installed ffmpeg. – Timo Nov 01 '20 at 14:52
  • This answer would be improved if the exact command was also added on how to convert for voice quality (more compact less need for fidelity) - with a good basic voice default. – NeilG Jan 04 '23 at 03:51
83

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

ffmpeg -i in.mp4 -q:a 0 -map a out.mp3

The q option can only be used with libmp3lame and corresponds to the LAME -V option. See Encoding VBR (Variable Bit Rate) mp3 audio:

https://trac.ffmpeg.org/wiki/Encode/MP3

Zombo
  • 1
  • 24
  • 120
  • 163
  • @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 Feb 17 '13 at 21:31
  • This one finally played also the Android Music player. Thanks – michalzuber Nov 10 '15 at 07:24
  • 3
    And 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 Nov 29 '16 at 19:27
  • 1
    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 majicvr.com Jun 17 '19 at 14:48
  • @kingSlayer how do I do that on Mac? – Sam Aug 21 '22 at 21:24
17

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

ffmpeg -i video.mp4 -vn audio.mp3
caruccio
  • 398
  • 3
  • 7
  • 14
    `-vn` option explicitly drops video so the conversion is much much faster. – Josef Kufner Sep 24 '20 at 23:08
  • I use this command to convert the whole folders on Windows. `FOR %I in ("*.*") DO ffmpeg -i "%I" -movflags use_metadata_tags -vn "C:\Users\MYUSERNAME\Music\converted\%~nI.mp3"` This will be convert mp4 to mp3 while keep metadata even some of them are missing such as `comments` field but you can recover them in **MP3tag** software. – vee Jul 14 '23 at 05:40
0

You can use a graphical tool, such as FFAudioConverter, which can be installed to Linux using Flatpak or Windows.

I tried the above and many other commands, but kept having a problem with VLC reporting incorrect and also constant changing of the length of an audio track. I did not experience this problem with FFAudioConverter.

Using the tool is very simple. Just open the application, then drag and drop a file, selected files, or a directory into the application. Verify the settings are configured for your desired output, then click Convert.

enter image description here

Paul
  • 766
  • 1
  • 6
  • 22