269

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?

4
  • 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. Commented Dec 8, 2013 at 0:33
  • no more ffmpeg for ubuntu 14.04 Commented Oct 7, 2014 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 Commented Jun 17, 2019 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. Commented Jun 18, 2019 at 15:03

5 Answers 5

349

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.

7
  • 6
    Thank you! This quality is best, or I can do better? Commented Sep 6, 2011 at 5:32
  • 2
    try with 320kbs en.wikipedia.org/wiki/MP3#Bit_rate Commented Sep 6, 2011 at 5:36
  • 2
    @Louis You can still download it from ffmpeg.org/download.html Commented Nov 22, 2014 at 7:39
  • 1
    @Louis - same for 14.10, but it is apparently returning in 15.04: askubuntu.com/questions/432542/… Commented Apr 4, 2015 at 11:53
  • 1
    works for me ffmpeg -i $fileNameWithExtension $fileNameOnly".mp3" Commented May 23, 2019 at 15:43
95

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

5
  • @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)) Commented Feb 17, 2013 at 21:31
  • This one finally played also the Android Music player. Thanks Commented Nov 10, 2015 at 7: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; Commented Nov 29, 2016 at 19:27
  • 2
    How is the resulting .mp3 different from the vanilla command ffmpeg -i vid.mp4 audio.mp3 in @Naga Harish M 's answer? Less lossy? Commented Jun 17, 2019 at 14:48
  • @kingSlayer how do I do that on Mac? Commented Aug 21, 2022 at 21:24
23

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

ffmpeg -i video.mp4 -vn audio.mp3
2
  • 18
    -vn option explicitly drops video so the conversion is much much faster. Commented Sep 24, 2020 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. Commented Jul 14, 2023 at 5: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

2
  • Can it take bulk? multiple videos? Commented Jul 17, 2022 at 14:18
  • 2
    @j0h Yes. From the linked GitHub description: "Convert many files or whole directories very fast". Commented Jul 17, 2022 at 14:36
0

For those Windows users, I have created a batch script to Extract but not Convert a video file to its related MP3 or M4A audio file, depending on the video encoding.

This method does not lose or change the original audio quality because there is no Re-encoding so, it's really fast!

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

:: Check if a path is provided as a command line argument
IF "%~1"=="" (
    SET "WORKING_DIR=%CD%"
) ELSE (
    SET "WORKING_DIR=%~1"
)

:: Change to the working directory
PUSHD "%WORKING_DIR%"

:: Extracts the audio track including all metatags from the video file
FOR %%I in ("*.mp4") DO (
    :: Retrieve the input video file audio codec
    FOR /F "tokens=*" %%J IN ('"ffprobe -v error -select_streams a:0 -show_entries stream=codec_name -of default=noprint_wrappers=1:nokey=1 "%%I""') DO SET "codec=%%J"
    IF "!codec!"=="aac" (
        ffmpeg -hide_banner -i "%%I" -movflags use_metadata_tags -map 0:a -c copy "%%~nI.m4a"
    ) ELSE (
        ffmpeg -hide_banner -i "%%I" -movflags use_metadata_tags -map 0:a -c copy "%%~nI.mp3"
    )
)

:: Return to the original directory
POPD

EXIT /B

Example:

VideoToAudio.cmd "D:\Source\MySummer Videos\"

If you don't define the running path (omit the parameter) and just run VideoToAudio.cmd, it starts to convert all mp4 files inside the current path.

Note: You must have the "ffmpeg.exe" and "ffprobe.exe" files in the running path or beside the VideoToAudio.cmd script file.

You can download FFMPEG tools from here: Get Windows Executable Files

You must log in to answer this question.