I’m using VideoCapture to load a video’s frames in batches. Eventually, my terminal starts spamming me with the following error, despite the program will running: [h264 @ 0x559351d7b040] mmco: unref short failure [h264 @ 0x559351d7b040] mmco: unref short failure [h264 @ 0x559351d7b040] mmco: unref short failure [h264 @ 0x559351d7b040] mmco: unref short failure ...
What’s causing this?
Is there any way to stop all warnings messages?
Sample Code:
vid = cv2.VideoCapture(vid_path)
# Prepare `num_processors` batches of images
for frame_num in range(start_frame, start_frame + batch_size):
vid.set(cv2.CAP_PROP_POS_FRAMES, frame_num)
ret, frame = vid.read()
if ret:
frame = Image.fromarray(frame)
frame = frame_transforms(frame)
self.batch.append(frame)
ah, you are seeking. OpenCV doesn’t do that too well. I would suggest reading frames in order, without seeking… or seeking once, since you have consecutive frames anyway.
seeking in videos is difficult and OpenCV is not a media API. files might index their video data by time stamp, or they might not. I don’t know if common container formats support indexing by frame number.
if you need to be absolutely sure, either don’t seek and vid.grab() until you’re at the right frame (possibly cheaper than read = grab + retrieve), or work with something like PyAV. there you have full control.
oh btw, common video codecs these days work with keyframes (I-frames) and following “predicted” frames referencing the previous keyframe. you can only seek to frames that are I-frames. if you try to seek to anywhere else, you’ll likely end up on the nearest I-frame after the time you wanted. it’s all very complicated and impossible to explain in a paragraph.
Hi, for one of my algorithms, I need to get previous frames in a video - how can I do this without continuously using vid.set?
(I did do some research - it seems there isn’t an obvious way)
Well, an obvious way is to store the read frames in a circular buffer/list of suitable size, and when you have a hit, look up back into it. Obviously it takes some memory to store the frames.
@crackwitz@matti.vuori
I need to get frames backwards in a video, and compare each (one at a time) with a pre-set frame, and will stop when some condition is met (this happens anywhere from ~1000-5000 times).
I’m currently just using vid.set continuously, and saving the frame received in a variable. This works, but I want to prevent the [h264 @ 0x559351d7b040] mmco: unref short failure warning message.