<p>Probably the most simple solution is to use this command:</p>

<pre><code>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
</code></pre>

<hr>

<p><em>Notes:</em> </p>

<ol>
<li><p>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).</p></li>
<li><p>It is appropriate to use option <code>-c:1:a copy</code> (somewhere between specifying input files and <code>-map</code> options) <em>to avoid transcoding the audio</em> (of the second input file).</p></li>
<li><p>Tested with files <code>v1.avi</code>, <code>v2.avi</code> synthetically created with the command  </p>

<pre><code>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
</code></pre></li>
</ol>

<hr>

<p><em>The detailed explanation:</em></p>

<pre><code>ffmpeg -i v1.avi -i v2.avi 
</code></pre>

<p><strong>Input files</strong> are introduced with <code>-i</code> options, and are enumerated from <code>0</code>, so </p>

<ul>
<li><code>v1.avi</code> has the number <code>0</code>,</li>
<li><code>v2.avi</code> has the number <code>1</code>.</li>
</ul>

<p>After the semicolon (<code>:</code>) may be the <strong>stream specifier</strong> - <em>supposing that you have only one video and one audio streams in your input files,</em> you may simply use <code>v</code> for the video stream, and <code>a</code> for the audio stream. We will use them later — see the picture near the end.</p>

<pre><code>-lavfi
</code></pre>

<p>introduces a <em>complex filtergraph.</em> You may use the synonym <code>-filter_complex</code> instead.    </p>

<pre><code>[1:v]trim=0.5:1.5,setpts=PTS-STARTPTS[part1];
</code></pre>

<ul>
<li><p>This is the first linear chain of filters (i.e. the part up to the semicolon) of the filtergraph.</p></li>
<li><p>It consists of 2 filters: <code>trim</code> (up to the comma), and <code>setpts</code> (after it).</p>

<ul>
<li><code>trim=0.5:1.5</code> gives only half of the video (particularly, from <code>0.5</code> s to <code>1.5</code> s).</li>
<li><code>setpts=PTS-STARTPTS</code> set timestamps of the trimmed part from <code>0</code> s (to <code>1</code> s).</li>
</ul></li>
<li><p>Note the input pad <code>[1:v]</code> — it is the only <em>video</em> stream (<code>v</code>) from the input file <code>1</code> (which is the <em>second</em> input file — <code>v2.avi</code>, because input files are numbered from <code>0</code>).</p></li>
<li><p>Note the <em>output pad</em>, for which I chose the label <code>[part1]</code>.</p></li>
</ul>

<p><code>[0:v][part1]concat[video]</code></p>

<ul>
<li><p>This is the second linear chain of filters (after the semicolon) of the filtergraph. It consists of only 1 filter: <code>concat</code>.</p></li>
<li><p><code>concat</code> - the original stream <code>0:v</code> and the intermediate stream <code>[part1]</code> are concatenated.</p></li>
<li><p>I chose the label <code>[video]</code> for this other intermediate result.</p></li>
</ul>

<p><code>-map [video] -map 1:a output.avi</code></p>

<ul>
<li><p>The <code>-map</code> options map <em>selected streams</em> from the input files and / or <em>output pads</em> from the complex filter into the output file <code>output.avi</code> in the specified order:</p>

<ul>
<li>the <em>output pad</em> <code>[video]</code> from the complex filter maps to the output stream <code>0,</code></li>
<li>the <em>audio stream</em> (<code>a</code>) from the input file <code>v2.avi</code> (numbered as <code>1</code>) maps to the output stream <code>1</code>.</li>
</ul></li>
</ul>

<hr>

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

<p><a href="https://i.sstatic.net/J4Ypo.jpg" rel="nofollow noreferrer"><img src="https://i.sstatic.net/J4Ypo.jpg" alt="enter image description here"></a></p>

<p>You need specify individual streams with their <em>ordinal numbers</em> - they are again numbered from <code>0</code>.</p>

<p>For example <code>-map 1:a:2</code> will select and map the third <em>audio</em> stream (numbered as <code>2</code>) from your <em>second</em> input file (numbered as <code>1</code>) into the output file. </p>

<p>As you can see from the picture, you may use <code>-map 1:4</code> instead.</p>

CC BY-SA 4.0
2019-06-05T16:36:15.070Z
Looks like it will be working. I will check it later.
angel_30
CC BY-SA 4.0
2019-06-04T23:18:13.050Z
2019-08-08T17:39:54.997Z
2019-08-08T17:39:54.997Z
MarianD
<p>How can I merge two videos <code>v1</code> and <code>v2</code>, but the audio coming from <code>v2</code>?
Say <code>v1</code> is 1 second, and <code>v2</code> is 2 second. Basically I want to merge <code>v1</code> and half of <code>v2</code>, but the whole audio be from <code>v2</code>.</p>

<p>Please make it general about the timing.</p>

<video><ffmpeg><audio>
Merge two videos in ffmpeg with audio from one
angel_30
# Merge two videos in ffmpeg with audio from one

(Asked by: username_0 on 2019-06-03)

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.

**Tags:** video, ffmpeg, audio


---

## 1 answers

### Answer by username_1 on 2019-06-04. Score: 3 | Accepted answer

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: 

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

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.

> **Comments:**
> **username_0**: Looks like it will be working. I will check it later.  

---

