0

I would like to overlay .png image file onto a video using ffmpeg-python. The image file should appear in the video at time of 5 s until time of 5.5 s.

Here is the sample code containing creation of synthetic video and image, based on this answer

import cv2
import numpy as np
import ffmpeg

# Create synthetic MP4 video
synthetic_file = 'tmp.mp4'
ffmpeg.input('testsrc=duration=10:size=1280x720:rate=30', 
f='lavfi').output(synthetic_file).overwrite_output().run()

width, height, fps = 192, 108, 1

# Create synthetic image file
number_five=5 # image file containing number "5"
p = width//60
img = np.zeros((height, width, 4), np.uint8)
cv2.putText(img, str(number_five), (width//2-p*10*len(str(number_five)), 
height//2+p*10), cv2.FONT_HERSHEY_DUPLEX, p, (255, 255, 255, 255), p*2)
cv2.imwrite(f'{number_five:03d}.png', img)

# Overlaying part of the code
xpos = '(W-w)/2'
ypos = '(H-h)/2'
durat = 'enable=between(t,5,5.5)' # specific time interval
# durat_repr = repr('enable=between(t,5,5.5)')
overlay_video_file = 'tmp_overlay.mp4'

image_overlay = ffmpeg.input(f'{number_five:03d}.png')
v1 = ffmpeg.input(synthetic_file)
v1 = ffmpeg.filter((v1,  image_overlay), 'overlay', durat, x=xpos, y=ypos)
written = ffmpeg.output(v1, overlay_video_file).global_args('-report')
ffmpeg.run(written)

If I check the log file, I see that in the ffmpeg command instead of overlay=enable='between(t,5,5.5)' I have

overlay=enable\\\\\\\\\\\\=between(t\\,5\\,5.5)

Morover, due to this, the image apprears in the video from the very beginning, which is not what I wanted.

I would be grateful for any help.

Moonwalk
  • 123
  • 3

0 Answers0