3

I need to replace video in a larger mp4 file with a smaller mp4 clip - but leaving the original (larger clip) audio intact.

So for example I might want the first 2 seconds of SmallClip.mp4 to replace the video in LargeClip.mp4 starting at (say) 10 seconds in to LargeClip.mp4.

The format of both clips is likely to be identical, so I'm hoping I can copy instead of decoding for speed etc (I'll have to do it a lot).

Finally, I might want to do exactly the same thing but in several places in LargeClip.mp4 - all places in the output getting the video replaced with the same input clip's video. I'd be happy to do this in multiple oerations if neccessary but if it were possible on one command so much the better :)

I'm reading up on the options for ffmpeg but I'm new to it and haven't figured it out yet. Any help much appreciated!

  • 1
    Copying is only feasible if the resume point(s) of the large MP4 is a keyframe. Otherwise, this can be done in a couple of steps. – Gyan Feb 10 '16 at 15:17
  • That's good to know - but how to output the small video clip at a certain point in in the large clip? I can't figure it out. – WoogieBoogie Feb 11 '16 at 8:59
3

You can use the concat demuxer in ffmpeg to do this. For accurate splicing, re-encoding is needed which is what the commands below do.

Step 1 Prepare the concat text file

You must first create a plaintext file, say, concat.txt, like this

file 'LargeClip.mp4'
inpoint 0.0
duration 10.0
file 'SmallClip.mp4'
inpoint 0.0
outpoint 2.0
file 'LargeClip.mp4'
inpoint 12.0
duration 5.0
file 'SmallClip.mp4'
inpoint 23.0
duration 4.0
file 'LargeClip.mp4'
inpoint 21.0

This shows the first 10 seconds of LargeClip, then the first two seconds of smallclip, then resumes the Largeclip at 12:00, shows it for 5 seconds, then splices in 23:00 to 27:00 of small clip, switches back to LargeClip at its 21:00 and keeps it till its end. These timecodes, in seconds, refer to the source file timecode, not of the assembled output.

Make sure that the sync of LargeClip isn't broken, i.e. after switching to SmallClip for 12 seconds, you want to resume LargeClip 12 seconds later from where you left it off.

Step 2 Concat the video streams

This command will skip the audio since we don't to splice the audio streams.

ffmpeg -f concat -i concat.txt -an -c:v libx264 -crf 23 -fflags +genpts spliced-vid.mp4

Step 3 Insert the audio from LargeClip

ffmpeg -i spliced-vid.mp4 -i LargeClip.mp4 -c copy -map 0:v -map 1:a Final.mp4 
| improve this answer | |
  • This looks like a wonderful answer - I will try it out and mark as an answer assuming it works. Thanks!! – WoogieBoogie Feb 11 '16 at 11:59
  • that worked thanks...with the small caveat that "duration" didn't seem to work, I replaced that with "outpoint" all the way through and it worked fine. – WoogieBoogie Feb 11 '16 at 22:20

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.