FFmpeg is an open-source software framework capable of "decode, encode, transcode, mux, demux, stream, filter, and play pretty much anything that humans and machines have created" (FFmpeg Website - “About”). The toolset supports a wide variety of codecs and formats, available on most major platforms and is highly portable itself. At its heart, FFmpeg is a command-line utility to perform many common, complex, and important tasks related to audiovisual file management, alteration, and analysis.
FFmpeg does a lot of things, and it can be difficult to get a handle on all the different use cases and options. But for a handfulcommon scenario, all you need is quick pre-made FFmpeg template so you can copy-paste them to the command line. This article will show you a few FFmpeg recipe to combine a video file with one or multiple audio file. You can also merge an audio-less video with an audio file as well.
Combine separate video and audio using FFmpeg
If you have separate audio and video file, and the video file contains no audio, you can use this command to combine them into one video file.
Please do note that the container (file extension) must accept the video and audio codec. To ensure compatibility, we recommend using MP4 or MKV as the container.
Let's suppose our video file name is INPUT_FILE.mp4 and the audio file name is AUDIO.wav. If you want to combine them AND re-encode the audio to AAC format, you can use this command :
ffmpeg -i INPUT_FILE.mp4 -i AUDIO.wav -c:v copy -c:a aac OUTPUT_FILE.mp4
-i INPUT_FILE.mp4
specify INPUT_FILE.mp4
as an input source
Similarly, -i AUDIO.wav
tells FFmpeg to take AUDIO.wav
as an input source.
-c:v copy
is a short form of -codec:v copy
which means copy the video stream from the source files to the destination file.
-c:a aac
means select all the audio stream from source files, then encode it with AAC encoder.
In case you don't want any audio conversion, just drop the aac
part in the command and replace it with copy
, so it would look like this
ffmpeg -i INPUT_FILE.mp4 -i AUDIO.aac -c:v copy -c:a copy OUTPUT_FILE.mp4
Extract video from file and combine with another audio file using FFmpeg
If your video file already contains one or more audio stream and you want to replace the default audio with another (external) audio file, you need to use FFmpeg's -map
option.
The -map
option is used to choose which streams from the input/inputs to be included in the output/outputs. The -map
option can also be used to exclude specific streams with negative mapping.
Let's suppose we have a video file with audio named video.mp4
and an audio file named audio.aac
encoded with the AAC codec. The example will replace the audio in video.mp4
with audio.aac
and output OUTPUT_FILE.mp4
.
ffmpeg -i INPUT_FILE.mp4 -i AUDIO.aac -c:v copy -c:a copy -map 0:v:0 -map 1:a:0 OUTPUT_FILE.mp4
-map 0:v:0
means that select the first input file (INPUT_FILE.mp4), then select the first (0) video stream. The first number (0) is the index of the first input file, the latter is the index number of the video stream.
-map 1:a:0
, similarly, means select from the second file (index number 1) the first audio stream (index number 0) to include in the output file.
Replace audio in files with another audio stream using FFmpeg
With the -map
option, you are able to select any stream, no matter it's video, audio, subtitle or metadata, to include in the output file.
Just like replacing audio stream with a separate audio file, you can leverage -map
option to simultaneously extract audio stream in another video file and combine with the video stream to make another file. A command line example would look like this :
ffmpeg -i INPUT_FILE1.mp4 -i INPUT_FILE2.mp4 -c:v copy -c:a copy -map 0:v:0 -map 1:a:0 OUTPUT_FILE.mp4
-map 0:v:0
means that select the first input file (INPUT_FILE.mp4, index number 0), then select the first (0) video stream. The first number (0) is the index of the first input file, the latter is the index number of the video stream.
-map 1:a:0
, similarly, means select from the second input file (INPUT_FILE2.mp4, index number 1) the first audio stream (index number 0) to include in the output file.
To re-encode the audio or video stream with a different codec, replace copy
with the name of an audio encoder, such as aac
or libvorbis
.
Combine video with multiple audio using FFmpeg
If you read the examples above, you will now have a glimpse of how -map
works.
You can put literally unlimited number of input files and leverage -map
to select separate streams from those files and include them in the output file.
Typically, a video file contains only one video stream. On the other hand, it can contains multiple audio stream as well as subtitles, metadatas.
Below is an example where we take the video stream from INPUT_FILE1.mp4
and pair it with the audio streams from INPUT_FILE2.mp4
and AUDIO2.aac
to make a new video file with multiple audio. You can clearly see how we select each input file (index 0,1,2) and decompose them with -map
.
ffmpeg -i INPUT_FILE1.mp4 -i INPUT_FILE2.mp4 -i AUDIO2.aac \
-c:v copy -c:a copy \
-map 0:v:0 -map 1:a:0 -map 2:a:0 OUTPUT_FILE.mp4
Replace audio in video file using FFmpeg
You can use the command in the third example to replace the default audio stream in a video file using FFmpeg. The process basically is selecting video and audio streams from input files and pair them together.
We will recite the command so you don't have to scroll up.
ffmpeg -i INPUT_FILE1.mp4 -i INPUT_FILE2.mp4 -c:v copy -c:a copy -map 0:v:0 -map 1:a:0 OUTPUT_FILE.mp4
Conclusion
We hope that this article helps you understand how -map
works in FFmpeg and how to use it to combine separate audio and video in multiple scenarios.
If you are interested in video processing, your work may involve downloading videos from the internet a lot. A regularly updated open-source program to help you with that is youtube-dl. We've covered a few tutorials for its command line interface such as download audio only with youtube-dl and merge video and audio with youtube-dl. You may be interested in our round-up of the best youtube-dl GUIs for you guys who want to avoid using the commands.
If you have any questions, then please feel free to ask in the comments below.