0

opencv: for some videos, after code: cv2.VideoCapture(video_name), many red warning occurs like non-existing PPS 0 referenced, decode_slice_header error or no frame. Here is my full code:

def get_video_frame(video_name, save_dir, interval):
    cap = cv2.VideoCapture(video_name)
    frame_all = cap.get(cv2.CAP_PROP_FRAME_COUNT)
    index = 0

    while index <= frame_all:
        success, frame = cap.read()
        index += 1
        if success and index % interval == 0:
            cv2.imencode('.jpg', frame)[1].tofile(os.path.join(save_dir, index, '.jpg'))
    cap.release()

However, for some other videos, no these red warning. I read answers of some similar questions(like opencv read error:[h264 @ 0x8f915e0] error while decoding MB 53 20, bytestream -7 and OpenCV FFMPEG RTSP Camera Feed Errors), it cannot solve my problems.

It seems not the frames cannot be read fast enough, Because these warnings occurs just after cv2.VideoCapture(video_name), I observe this in debug mode.

  • 1
    Does the operation still work? It may be some of your videos are ill-formed. It happens. – Tim Roberts Dec 12 '21 at 04:26
  • Yes, I agree your reply. There should be something wrong with those videos with these red warnings. Do you know how filter there red warning, don't show then in the run window? In addition, I add 2 links in my description, they are a little similar to my questions. – LIN Fengfeng Dec 12 '21 at 04:41
  • 1
    Well, what happens? If you get an exception, you can catch it and move to the next video. – Tim Roberts Dec 12 '21 at 07:36
  • Though many red warning occur when cv2.VideoCapture some videos, the video still have some frames which are normal and can be save to jpg images. So, it will be good not to avoid the video and filter the warning. Could you kindly help give suggestions? – LIN Fengfeng Dec 12 '21 at 09:29
  • There's really no magic here. If `cap.read()` gets an exception, you can catch that and loop back to try it again, but if there are files that OpenCV2 just can't read, you can't force it to read them. I don't know what you mean by "red warnings". If these are just console prints that don't generate an exception, then there's almost nothing you can do. – Tim Roberts Dec 12 '21 at 20:18
  • Search for redirecting stdout and stderr to somewhere else – Micka Dec 12 '21 at 22:24
  • Does this help while cap.isOpened(): instead of while index <= frame_all: – toyota Supra May 07 '22 at 13:17

0 Answers0