I have a txt file with annotations for each frame in my video (so "number of rows" == "number of frames"
). Now I would like to write the text of each row to its corresponding frame in the video (into some corner) – is there a way to do that using ffmpeg (or a similar free tool)?
0
votes
Generate a subtitle file (.srt/.ass) and use subtitles filter to burn it.
– Gyan
2 Answers
1
votes
import cv2
# reading text file with text. each line index is frame pos
with open('frames.txt') as f:
frames = {k: v for k, v in enumerate(f.readlines())}
cap = cv2.VideoCapture('sample.mp4')
fps = cap.get(cv2.CAP_PROP_FPS)
w = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
h = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi', fourcc, fps, (int(w), int(h)))
font = cv2.FONT_HERSHEY_SIMPLEX
while cap.isOpened():
ret, frame = cap.read()
if ret:
frame_no = cap.get(cv2.CAP_PROP_POS_FRAMES)
if frame_no in frames:
cv2.putText(frame, frames[frame_no], (10, 500), font, 4, (255, 255, 255), 2, cv2.LINE_AA)
out.write(frame)
else:
break
cap.release()
out.release()
cv2.destroyAllWindows()