David Bosch - Feb 2nd, 2022
As explained in the previous lesson, a container file (a MP4 video file for exmaple) may include several elements and they can be video, audio, subtitles or other metadata elements. These elements are usually called streams or tracks.
For example, a video may have 1 video stream and 2 audio streams, each audio stream containing audio in different a language. This could be the case of a movie with 2 audio tracks, one in English and the other in French.
If more than one video or audio streams are present in a file, the video player just plays one video and one audio stream, letting the user to choose his/her preference.
Stream naming convention
When editing video or applying transformations with FFMPEG, you must refer to particular streams in the provided input files and FFMPEG has its own naming convention you must know.
This naming convention for referring to the different streams follows this schema:
i:[a|v]:n
Where:
-
i
: is the input file specified in the parameters, starting from 0 -
a or v: speficies what stream type you are referring to,
a
for audio andv
for video -
n
: is the stream you are referring to, starting from 0
Sounds complicated, but it's not, and it's easier to understand with a few examples:
-
0:a
: Refers to alla
udio streams of the first(0
) input file -
0:v
: Refers to allv
ideo streams of the first(0
) input file -
0:a:1
: Refers to the second (1
)a
udio stream of the first (0
) input input file -
0:v:0
: Refers to the first (0
)v
ideo stream of the first (0
) input file -
1:v:0
: Refers to the first (0
)v
ideo stream of the second (1
) input file
Mapping streams to output files
Now that we know how to refer to particular streams, we can select what streams from the input files we want to be included in the output file.
In FFMPEG, this is done using the -map
parameter. Let's see a few examples:
ffmpeg -i english.mp4 -i french.mp3 -map 0:v:0 -map 1:a:0 french.mp4
:
Selects the first video stream from english.mp4 (0:v:0
)
and the first audio stream from french.mp3 (1:a:0
)
and saves them into french.mp4
ffmpeg -i english.mp4 -i french.mp3 -map 0 -map 1:a:0 english-french.mp4
:
Selects all streams from english.mp4 file (0
) and the first
audio stream from french.mp4 file (1:a:0
)
creating english-french.mp4 that includes both audio streams.
This type of stream manipulation is very useful in the day to day operations, for example:
- Replacing an audio track in a video
- Adding a silent audio track to a video
- Extracting an audio track from a video into a separate file
In the following lessons, we will go through several of these examples using the -map
parameter.
Self-evaluation
In FFMPEG, 0:a
refers to
In FFMPEG, -map 1:v:0
refers to
Published on February 2nd, 2022