4

Is it possible to CLI ffmpeg to replace a specific frame at a specified interval with another image? I know how to extract all frames from a video, and re-stitch as another video, but I am looking to avoid this process, if possible.

My goal:

  • Given a video file input.mp4
  • Given a PNG file, image.png and given its known to occur at exactly a specific timestamp within input.mp4
  • create out.mp4 with image.png replacing that position of input.mp4
user1361529
  • 2,667
  • 29
  • 61

2 Answers2

5

The basic command is

ffmpeg -i video -i image \
       -filter_complex \
         "[1]setpts=4.40/TB[im];[0][im]overlay=eof_action=pass" -c:a copy out.mp4

where 4.40 is the timestamp of the frame to be replaced.

pepoluan
  • 6,132
  • 4
  • 46
  • 76
Gyan
  • 85,394
  • 9
  • 169
  • 201
0

Note that images default to a framerate of 25fps. Thus, for timestamps that are not multiples of (1/25=)0.04s, the framerate must be specified (eg, to replace frame at timestamp 3.5035 in a 29.97fps video):

ffmpeg -i input.vid -itsoffset 3.5035 -framerate 30000/1001 -i frame.png -filter_complex "[0:v:0][1]overlay=eof_action=pass" output.vid

This technique works just as well for replacing multiple sequential frames (eg, to replace frames starting at 107s in a 12.5fps video):

ffmpeg -i input.mp4 -itsoffset 107 -framerate 25/2 -i '107+%06d.png' -filter_complex "[0:v:0][1]overlay=eof_action=pass" output.mp4

This only works for videos with constant framerates (CFR). For VFR video, I have a separate question.

Arnon Weinberg
  • 871
  • 8
  • 20