Not able to play h264 video on OpenCV?

asked 2015-02-12 06:42:49 -0500

aj_nikhil gravatar image

updated 2015-02-12 06:43:37 -0500

I captured a standard video from camera of Raspberry pi. The codec of the file is h264. To play the video I do:

import numpy as np
import cv2

cap = cv2.VideoCapture('foo.h264')

    ret, frame =

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    if cv2.waitKey(1) & 0xFF == ord('q'):


Video starts But stops after sometime throwing this error:

Traceback (most recent call last):
  File "", line 9, in <module>
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.error: /home/nikhil/Downloads/opencv-2.4/modules/imgproc/src/color.cpp:3739: error: (-215) scn == 3 || scn == 4 in function cvtColor

My machine has Ubuntu 12.04. I played an .avi file it plays smoothly. Is the problem with the .h264 or with the OpenCV?

edit retag flag offensive close merge delete



you will have to check the 'ret' value. if it is False, it means, that your image is invalid(empty), i.e you reached the end of the video.

berak gravatar imageberak ( 2015-02-12 07:14:12 -0500 )edit