imageio / imageio-ffmpeg

FFMPEG wrapper for Python
BSD 2-Clause "Simplified" License
221 stars 50 forks source link

write text to video #110

Open sugizo opened 3 months ago

sugizo commented 3 months ago

objective write text to video, e.g. using cv2.putText

code

from imageio_ffmpeg import read_frames, write_frames
import cv2
input_file = 'http://195.196.36.242/mjpg/video.mjpg'
output_file = 'rtsp.mp4'
reader = read_frames(input_file)
meta = reader.__next__()
writer = write_frames(output_file,
                      size = meta['size'], fps = meta['fps'],
                      input_params = ['-y', ],
                      output_params = ['-vf', 'scale = 720 : -2', ] )
writer.send(None)
i = 0
text = 'stifix'
font = cv2.FONT_HERSHEY_SIMPLEX
org = (00, 185)
fontScale = 1
color = (0, 0, 128)
thickness = 2
for frame in reader:
    img = np.frombuffer(frame, np.uint8).reshape(meta['size'][1],
                                                 meta['size'][0], -1)
    #put_text = cv2.putText(img, text, org, font, fontScale, color, thickness, cv2.LINE_AA, False) # not work
    put_text = np.ascontiguousarray(cv2.putText(img, text, org, font, fontScale, color, thickness, cv2.LINE_AA, False) ) # not work
    writer.send(put_text)
    if i == 9: # 100 = 6 secs
        break
    i += 1
writer.close()

result no error occured but output video have no text written

expected result video output have text written

best regards

almarklein commented 3 months ago

I'd write the frames to image files using imageio and see if the text is visible there. If not, it's likely related to the putText cal.