1

How can I merge two videos v1 and v2, but the audio coming from v2? Say 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 v2.

Please make it general about the timing.

2

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

Notes:

  1. 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).

  2. 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).

  3. Tested with files v1.avi, 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 0, so

  • v1.avi has the number 0,
  • v2.avi has the number 1.

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.

-lavfi

introduces a complex filtergraph. You may use the synonym -filter_complex instead.

[1:v]trim=0.5:1.5,setpts=PTS-STARTPTS[part1];
  • 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.5 gives only half of the video (particularly, from 0.5 s to 1.5 s).
    • setpts=PTS-STARTPTS set timestamps of the trimmed part from 0 s (to 1 s).
  • 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 0).

  • Note the output pad, for which I chose the label [part1].

[0:v][part1]concat[video]

  • This is the second linear chain of filters (after the semicolon) of the filtergraph. It consists of only 1 filter: concat.

  • 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

  • The -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:

    • the output pad [video] from the complex filter maps to the output stream 0,
    • the audio stream (a) from the input file v2.avi (numbered as 1) maps to the output stream 1.

In more complicated cases with more video or audio streams, e. g. if the second input file contains 2 video and 3 audio streams:

enter image description here

You need specify individual streams with their ordinal numbers - they are again numbered from 0.

For example -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.

  • Looks like it will be working. I will check it later. – Tina J Jun 5 at 16:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.