Basically, I want to take two rectangles ouf of sixth minute of the video and overlay them at an earlier point in the video.

If I use ffmpeg three times like this:

ffmpeg -i a.mp4 -filter_complex "[0:v]trim=start_frame=9000:end_frame=9001,crop=1280:100:0:0,null,setpts=PTS-STARTPTS[v]" -map [v] -c:v libx264 -c:a null -crf 15 -preset slow -y -t 0.1 still1.mp4
ffmpeg -i a.mp4 -filter_complex "[0:v]trim=start_frame=9000:end_frame=9001,crop=100:100:600:500,null,setpts=PTS-STARTPTS[v]" -map [v] -c:v libx264 -c:a null -crf 15 -preset slow -y -t 0.1 still2.mp4
ffmpeg -i a.mp4 -i still1.mp4 -i still2.mp4 -filter_complex \
"[0:v][1:v]overlay=0:0:enable='between(t,60,61)'[v0]; \
[v0][2:v]overlay=600:500:enable='between(t,60,61)'[v1]" \
-map [v1] -c:v libx264 -crf 17 -preset slow -y -t 362 p.mp4

The consumption of RAM as measured by top stays around 3 GB. However, when I try it in one command, it quickly eats up 13GB of my free memory and the process quickly gets killed by OOM killer:

ffmpeg -i a.mp4 -filter_complex \
"[0:v]trim=start_frame=9000:end_frame=9001,crop=1280:100:0:0,setpts=PTS-STARTPTS[still1]; \
[0:v]trim=start_frame=9000:end_frame=9001,crop=100:100:600:500,setpts=PTS-STARTPTS[still2]; \
[0:v][still1]overlay=0:0:enable='between(t,61,62)'[v0]; \
[v0][still2]overlay=600:500:enable='between(t,61,62)'[v1]" \
-map [v1] -c:v libx264 -crf 17 -preset slow -y  p.mp4

I suspect ffmpeg keeps all the frames decoded in memory when trimming, but is there any way around this? It sounds wastefull, I am interested in two little frames, that should not eat up GB of memory!

1 Answer 1


Use better command: ffmpeg -i a.mp4 -i a.mp4 -i a.mp4 -filter_complex \ "[0:v]trim=start_frame=9000:end_frame=9001,crop=1280:100:0:0,setpts=PTS-STARTPTS[still1]; \ [1:v]trim=start_frame=9000:end_frame=9001,crop=100:100:600:500,setpts=PTS-STARTPTS[still2]; \ [2:v][still1]overlay=0:0:enable='between(t,61,62)'[v0]; \ [v0][still2]overlay=600:500:enable='between(t,61,62)'[v1]" \ -map [v1] -c:v libx264 -crf 17 -preset slow -y p.mp4

This way ffmpeg will not keep frames it still needs in memory.

  • So the problem was to input the file just once and not three times? Interesting, will keep that in mind the next time, thanks for this (I cannot actually test this anymore but will keep that in mind for the next time:-)).
    – sup
    Jun 27, 2019 at 17:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.