How can I merge two videos
v2, but the audio coming from
v1 is 1 second, and
v2 is 2 second. Basically I want to merge
v1 and half of
v2, but the whole audio be from
Please make it general about the timing.
Probably the most simple solution is to use this command:
ffmpeg -i v1.avi -i v2.avi \ -lavfi [1:v]trim=0.5:1.5,setpts=PTS-STARTPTS[part1];[0:v][part1]concat[video] \ -map [video] -map 1:a output.avi
This command is sufficient only in an ideal case, when there are not needed another filters for conversions of incompatible video streams (e. g. because of different frame sizes).
It is appropriate to use option
-c:1:a copy (somewhere between specifying input files and
-map options) to avoid transcoding the audio (of the second input file).
Tested with files
v2.avi synthetically created with the command
ffmpeg -f lavfi -i testsrc2=d=1 -f lavfi -i anoisesrc=d=1 -f lavfi -i testsrc=d=2 -f lavfi -i sine=d=2 -map 0:0 -map 1 v1.avi -map 2 -map 3 v2.avi
The detailed explanation:
ffmpeg -i v1.avi -i v2.avi
Input files are introduced with
-i options, and are enumerated from
v1.avihas the number
v2.avihas the number
After the semicolon (
:) may be the stream specifier - supposing that you have only one video and one audio streams in your input files, you may simply use
v for the video stream, and
a for the audio stream. We will use them later — see the picture near the end.
introduces a complex filtergraph. You may use the synonym
This is the first linear chain of filters (i.e. the part up to the semicolon) of the filtergraph.
It consists of 2 filters:
trim (up to the comma), and
setpts (after it).
trim=0.5:1.5gives only half of the video (particularly, from
setpts=PTS-STARTPTSset timestamps of the trimmed part from
Note the input pad
[1:v] — it is the only video stream (
v) from the input file
1 (which is the second input file —
v2.avi, because input files are numbered from
Note the output pad, for which I chose the label
This is the second linear chain of filters (after the semicolon) of the filtergraph. It consists of only 1 filter:
concat - the original stream
0:v and the intermediate stream
[part1] are concatenated.
I chose the label
[video] for this other intermediate result.
-map [video] -map 1:a output.avi
-map options map selected streams from the input files and / or output pads from the complex filter into the output file
output.avi in the specified order:
[video]from the complex filter maps to the output stream
a) from the input file
1) maps to the output stream
In more complicated cases with more video or audio streams, e. g. if the second input file contains 2 video and 3 audio streams:
You need specify individual streams with their ordinal numbers - they are again numbered from
-map 1:a:2 will select and map the third audio stream (numbered as
2) from your second input file (numbered as
1) into the output file.
As you can see from the picture, you may use
-map 1:4 instead.