2

I have a video file that is divided into three different sections. I want to replace the middle part of this video with another clip. However, I still want to keep the audio of the original video. Can I do that in ffmpeg?

Here are my requirements:

  1. Original Video is 60 seconds long.
  2. The part I want to replace is from 25 to 45 seconds.
  3. I want to keep the original audio from 25 to 45 seconds and just replace the visual part with some other clip.
  4. The generated video will also be 60 seconds long. However, it will have the new video from 25 to 45 seconds.

Thanks.

2

What you probably want to do is three separate steps:

  1. split the original input into three clips (0-25, 25-45, 45-60)
  2. modify the middle clip to use orginal audio and custom video (see below)
  3. concatenate all three clips

In order to get the middle clip with original audio and new video you could simply use the -map option:

ffmpeg -i original.mp4 -i newvideo.mp4 -c:v copy -c:a aac -map 0:v:0 -map 1:a:0 newmiddle.mp4
1
  • Thanks @rauberdaniel. :) Unfortunately, joining the files together gives me an error about Non-monotonous DTS in output stream. – Real Noob Feb 13 at 11:08
-1

Simple method is to use the setpts filter to add a delay to new.mp4, and then the overlay filter with the enable option to place new.mp4 over original.mp4:

ffmpeg -i original.mp4 -i new.mp4 -filter_complex "[1]setpts=PTS-STARTPTS+25/TB[fg];[0][fg]overlay=enable='between(t,25,45)'[v]" -map "[v]" -map 1:a -c:a copy output.mp4
  • This assumes new.mp4 has the same width x height as original.mp4. If not, then add the scale, scale2ref, pad, and/or crop filter.

  • Remove setpts if you want new.mp4 to play at its own timestamp of 25 seconds instead of at timestamp 0 when overlaid.

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.