Stitching mp4 in linux

⇠ Back to Blog:Hacks

Rendering a video is extremely lengthy. If you want to alter a little part of it, you certainly do not want to render the whole thing again. Here's what you can do instead.

Render the part you want to change and stitch it back to the good part.

Here is an example that happened to me: in a 1h19 video I blundered and left a trailing clip in kdenlive 2m30s in the opening. The rest was fine. The rendering took over 3 hours (during the night). So I decided to render the first 3min and stitch that to the rest of the video past this point. I picked up a frame, n°4501, which, at 25fps gives the timestamp of 180.04:

In[1]:= 4501/25.

Out[1]= 180.04

I exported the first part in a file out1.mp4 (till frame 4501). Then I extracted the "good" part (from 180.04 till the end):

laussy@wulfrun2:~/Videos$ ffmpeg -ss 180.04 -i lecture12-integrals.mp4 -c:v libx264 -c:a aac out.mp4

this took the longest but still was a bit faster than the rendering with melt, not by orders of magnitudes, but maybe twice as fast (it does this at ~0.765 times the real time of the video, so still well over 1h30 to get it done).

And then I stitched them by first listing the files to join:

file 'out1.mp4'
file 'out2.mp4'

and running

ffmpeg -f concat -i inputs.txt -c copy output.mp4

I am not sure this is the most efficient thing to do because the long part (exporting the file again) still is very lengthy. Maybe because I'm encoding again and a mere copy would be enough. This requires more testing. I didn't have time to delve into it deeper on this occasion.