简体   繁体   English

可以OpenCV解码H264 - MPEG-4 AVC(第10部分)

[英]Can OpenCV decode H264 - MPEG-4 AVC (part 10)

I am trying to use OpenCV (python bindings) to connect to a UDP multicast and recover individual received frames for post-processing. 我正在尝试使用OpenCV(python绑定)连接到UDP多播并恢复单个接收的帧以进行后处理。

I can connect to my multicast via VLC, and VLC displays the broadcast with no issues at all. 我可以通过VLC连接到我的多播,VLC显示广播完全没有问题。 VLC reports that the codec it uses for decoding is H264 - MPEG-4 AVC (part 10). VLC报告它用于解码的编解码器是H264 - MPEG-4 AVC(第10部分)。

When I try to decode using OpenCV, I do see my video stream, but many frames appear fragmented. 当我尝试使用OpenCV进行解码时,我确实看到了我的视频流,但是很多帧看起来都是碎片化的。 The frames appear as if the last line of pixels just got repeated to fill in the rest of the image (sometimes 75% or more of the whole image). 帧看起来好像最后一行像素被重复以填充图像的其余部分(有时是整个图像的75%或更多)。 OpenCV reports decoding errors (error while decoding MB ...., bytestream ). OpenCV报告解码错误(解码MB ....,字节流时出错)。

Is there any way to force OpenCV to use whatever codec VLC is using? 有没有办法强制OpenCV使用VLC正在使用的任何编解码器? I tried to specify the specific codec to use in my code for OpenCV but it seems to have no effect. 我试图在我的OpenCV代码中指定要使用的特定编解码器,但它似乎没有任何效果。

The code I am using is below: 我使用的代码如下:

import numpy as np
import cv2
from cv2 import cv

cap = cv2.VideoCapture()
cap.set(cv.CV_CAP_PROP_FOURCC, cv.CV_FOURCC('A','V','C','1')) 
cwi=cap.open(r'myurlandport')

counter = 0

while(cap.isOpened()):

    ret, frame = cap.read()


    counter += 1

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

cap.release()
cv2.destroyAllWindows()

Last time I checked (OpenCV 2.4.9) ffmpeg build used in OpenCV did not utilize UDP protocol. 上次我检查(OpenCV 2.4.9)在OpenCV中使用的ffmpeg构建没有使用UDP协议。 It doesn't buffer received packets for later use. 它不缓冲收到的数据包供以后使用。 More info here: http://code.opencv.org/issues/2235 更多信息: http//code.opencv.org/issues/2235

EDIT: In order to force TCP mode, edit opencv\\sources\\modules\\highgui\\src\\cap_ffmpeg_impl.hpp line 538 编辑:为了强制TCP模式,编辑opencv \\ sources \\ modules \\ highgui \\ src \\ cap_ffmpeg_impl.hpp第538行

int err=avformat_open_input(&ic, _filename, NULL, NULL);

adding tcp: 添加tcp:

 AVDictionary *d = NULL;
 av_dict_set(&d, "rtsp_transport", "tcp", 0);
 int err=avformat_open_input(&ic, _filename, NULL, &d);

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

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