Answers.Body: <p>A command of the form below will do it:</p>
  
  <pre><code>ffmpeg -i main.mkv -i intro.mkv
     -filter_complex "overlay=eof_action=pass[v]"
     -map "[v]" -map 0:a -c:v libx264 -crf 18 -c:a copy out.mkv
  </code></pre>
  
  <p>If your ffmpeg build is recent, you'll need to add another option:</p>
  
  <p><code>overlay=eof_action=pass:repeatlast=0</code></p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-10-14T19:48:06.600Z
Answers.Text: That works great, thanks.
  Now I need to figure out the fadeout...
Answers.UserDisplayName: DJL
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-10-14T18:56:41.510Z
Answers.LastActivityDate: 2017-10-14T18:56:41.510Z
Answers.UserDisplayName: Gyan
Body: <p>I'm trying to replace the first few seconds of a video with a different one, while retaining the audio from the first.</p>
  
  <p>This didn't work for me (the last frame of the overlay is retained after it finishes) <a href="https://stackoverflow.com/questions/35269387/ffmpeg-overlay-one-video-onto-another-video">https://stackoverflow.com/questions/35269387/ffmpeg-overlay-one-video-onto-another-video</a></p>
  
  <p>I also tried doing it in multiple steps:</p>
  
  <pre><code>INTRO_TIME=$(/usr/bin/ffmpeg -i intro.mkv 2&gt;&amp;1 | grep Duration | cut -d ' ' -f 4 | sed s/,//  )
  rm temp1.mkv temp2.mkv
  /usr/bin/ffmpeg -i $1 -i intro.mkv -filter_complex "overlay" -shortest -c:a flac -c:v libx264 -crf 18 -t ${INTRO_TIME} temp1.mkv
  /usr/bin/ffmpeg -accurate_seek -ss ${INTRO_TIME} -i $1 -c:a flac -c:v libx264 temp2.mkv 
  DIR=$(pwd);
  /usr/bin/ffmpeg -f concat -safe 0 -i &lt;(echo file ${DIR}/temp1.mkv; echo file ${DIR}/temp2.mkv) -c copy $2
  </code></pre>
  
  <p>But this appears to be corrupted after the intro ends (the audio plays but the video doesn't. N.B. both temp files look ok)</p>
  
  <p>Ideally I'd like the last frame of the intro to be frozen and then faded out to reveal the original video underneath.</p>
  
  <p>Also, the original video(s) can be quite long, so if there's a way to do it without re-encoding the bulk of the original video, even better. (I realise re-encoding may be necessary for the duration of the intro, and maybe a few seconds beyond that)</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-10-14T17:36:42.527Z
Comments.Text: You need to use `overlay=eof_action=pass` to keep the overlay input from freezing on top after its runtime, and drop the `shortest=1`. Re-encoding just the affected portion of the main video is possible but it is tedious to get it right.
Comments.UserDisplayName: Gyan
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-10-14T18:15:37.247Z
Comments.Text: sorry, can you put that into a full command? I tried a few things but can't figure out how to applyit
Comments.UserDisplayName: DJL
Tags: <ffmpeg>
Title: Overlay intro video
UserDisplayName: DJL
ThreadText: # Overlay intro video
  
  (Asked by: username_0 on 2017-10-14)
  
  I'm trying to replace the first few seconds of a video with a different one, while retaining the audio from the first.
  
  This didn't work for me (the last frame of the overlay is retained after it finishes) <https://stackoverflow.com/questions/35269387/ffmpeg-overlay-one-video-onto-another-video>
  
  I also tried doing it in multiple steps:
  
  ```
  INTRO_TIME=$(/usr/bin/ffmpeg -i intro.mkv 2>&1 | grep Duration | cut -d ' ' -f 4 | sed s/,//  )
  rm temp1.mkv temp2.mkv
  /usr/bin/ffmpeg -i $1 -i intro.mkv -filter_complex "overlay" -shortest -c:a flac -c:v libx264 -crf 18 -t ${INTRO_TIME} temp1.mkv
  /usr/bin/ffmpeg -accurate_seek -ss ${INTRO_TIME} -i $1 -c:a flac -c:v libx264 temp2.mkv 
  DIR=$(pwd);
  /usr/bin/ffmpeg -f concat -safe 0 -i <(echo file ${DIR}/temp1.mkv; echo file ${DIR}/temp2.mkv) -c copy $2
  
  ```
  
  But this appears to be corrupted after the intro ends (the audio plays but the video doesn't. N.B. both temp files look ok)
  
  Ideally I'd like the last frame of the intro to be frozen and then faded out to reveal the original video underneath.
  
  Also, the original video(s) can be quite long, so if there's a way to do it without re-encoding the bulk of the original video, even better. (I realise re-encoding may be necessary for the duration of the intro, and maybe a few seconds beyond that)
  
  **Tags:** ffmpeg
  
  > **Comments:**
  > **username_1**: You need to use `overlay=eof_action=pass` to keep the overlay input from freezing on top after its runtime, and drop the `shortest=1`. Re-encoding just the affected portion of the main video is possible but it is tedious to get it right.  
  > **username_0**: sorry, can you put that into a full command? I tried a few things but can't figure out how to applyit  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2017-10-14. Score: 1 | Accepted answer
  
  A command of the form below will do it:
  
  ```
  ffmpeg -i main.mkv -i intro.mkv
     -filter_complex "overlay=eof_action=pass[v]"
     -map "[v]" -map 0:a -c:v libx264 -crf 18 -c:a copy out.mkv
  
  ```
  
  If your ffmpeg build is recent, you'll need to add another option:
  
  `overlay=eof_action=pass:repeatlast=0`
  
  > **Comments:**
  > **username_0**: That works great, thanks. Now I need to figure out the fadeout...  
  
  ---
  
  