简体   繁体   English

无法在OpenCV上播放.h264视频?

[英]Not able to play .h264 video on OpenCV?

I captured a standard video from camera of Raspberry pi. 我从Raspberry pi的相机中捕获了一个标准视频。 The codec of the file is h264. 该文件的编解码器是h264。 To play the video I do: 要播放视频,请执行以下操作:

import numpy as np
import cv2

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

while(cap.isOpened()):
    ret, frame = cap.read()

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

    cv2.imshow('frame',gray)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

Video starts But stops after sometime throwing this error: 视频开始播放,但在抛出此错误后停止播放:

Traceback (most recent call last):
  File "play_video.py", 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. 我的机器装有Ubuntu 12.04。 I played an .avi file it plays smoothly. 我播放了一个.avi文件,它播放流畅。 Is the problem with the .h264 or with the OpenCV? .h264或OpenCV是否有问题?

Your loop should check that frame is not-empty, and not that the video was opened successfully - this check should be done just once at the beginning. 您的循环应检查frame是否为空,而不是视频是否已成功打开-此检查应在开始时仅执行一次。
When the frame after last is read frame is empty and you are then trying to convert it to gray. 当最后一的帧读frame为空,然后你想将它转换为灰色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM