Add audio to video using FFmpeg
Become or hire the top 3% of the developers on Toptal https://topt.al/25cXVn
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Realization
--
Chapters
00:00 Question
01:39 Accepted answer (Score 121)
03:28 Thank you
--
Full question
https://superuser.com/questions/590201/a...
Accepted answer links:
[FFmpeg Wiki]: https://trac.ffmpeg.org/wiki/Map
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#audio #video #ffmpeg
#avk47
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Realization
--
Chapters
00:00 Question
01:39 Accepted answer (Score 121)
03:28 Thank you
--
Full question
https://superuser.com/questions/590201/a...
Accepted answer links:
[FFmpeg Wiki]: https://trac.ffmpeg.org/wiki/Map
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#audio #video #ffmpeg
#avk47
ACCEPTED ANSWER
Score 135
By default, FFmpeg will only take one audio and one video stream. In your case that's taken from the first file only.
You need to map the streams correctly:
ffmpeg -i input.mp4 -i input.mp3 -c copy -map 0:v:0 -map 1:a:0 output.mp4
Some notes:
- The order mapping options determine which streams from the input are mapped to the output.
0:v:0
is the first video stream of the first file and1:a:0
is the first audio stream of the second file. Thev
/a
are not strictly necessary, but in case your input files contain multiple streams, that helps to disambiguate. More info about mapping can be found on the FFmpeg Wiki.- If your audio stream is longer than the video file, or vice-versa, you can use the
-shortest
option to have ffmpeg stop the conversion when the shorter of the two ends. -c copy
copies the audio and video streams. This means that the process will be fast and the quality will be the same. But when adding, say, WAV audio to an existing video file, it'd be better to compress that audio first. For example:ffmpeg -i input.mp4 -i input.wav -c:v copy -map 0:v:0 -map 1:a:0 -c:a aac -b:a 192k output.mp4
Here, we only copy the video stream (
-c:v copy
), and re-encode the audio stream with the ffmpeg built-in AAC encoder (-c:a aac
) at 192 kBit/s.- If your output format does not support a particular codec (e.g., when adding WAV to MP4, or AAC to AVI, etc.), re-encoding is also required.