264

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

5 Answers5

344

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.

94

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
22

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

ffmpeg -i video.mp4 -vn audio.mp3
caruccio
  • 448
  • 4
  • 7
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
  • 786
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

Bobbiz
  • 11