Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract an audio part from a mp4 using ffmpeg?

Tags:

ffmpeg

audio

I tried to use the following command :

ffmpeg -i video.mp4 -acodec copy -ss 00:30:00 -to 00:60:00 extract.mp3

but it fails and says

[mp3 @ 0x3bfade0] Invalid audio stream. Exactly one MP3 audio stream is required.
Could not write header for output file #0 (incorrect codec parameters ?): Invalid argument

I believe it's because I need to extract the mp3 data first and provide it to the command, but is not there a way to do this in one command and avoid creating the full mp3 file that I don't need ?

like image 588
vdegenne Avatar asked Apr 13 '19 18:04

vdegenne


People also ask

How do I get audio from a video using FFmpeg?

First, the -i flag specifies the input video file name. Second, using -map 0:a selects all audio streams we found before. Next, -acodec copy copies the stream without re-encoding. Finally, audio.

How do I copy audio from FFmpeg?

-map 0 tells ffmpeg to all streams from input 0 (the first input) in the output file. -c:a copy -c:s copy will copy over any audio and subtitle streams present.

What FFmpeg command-line syntax will pull the audio from a video file and save it as an mp3?

You can of course select any ffmpeg parameters for audio encoding that you like, to set things like bitrate and so on. Use -acodec libmp3lame and change the extension from . ogg to . mp3 for mp3 encoding.

Does FFmpeg work with mp4?

You can use FFmpeg to concatenate mp4 files very easily! There are many ways to do this including variations such as (1) concatenating only the audio (2) concatenating only video (3) concatenating all the files in a directory (4) concatenating files that do not have the same height, width, etc.


1 Answers

Your audio track is likely AAC, not MP3.

So either

ffmpeg -i video.mp4 -acodec copy -ss 00:30:00 -to 00:60:00 extract.aac

or

ffmpeg -i video.mp4 -vn -ss 00:30:00 -to 00:60:00 extract.mp3

Save it to a generic MP4 if you need a failsafe command.

ffmpeg -i video.mp4 -map 0:a:0 -acodec copy -ss 00:30:00 -to 00:60:00 extract.mp4
like image 171
Gyan Avatar answered Oct 13 '22 15:10

Gyan