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

I am trying to use OpenCV (python bindings) to connect to a UDP multicast network and restore individual received frames for further processing.

I can connect to my multicast via VLC, and the VLC displays the broadcast without any problems. VLC reports that the codec that it uses for decoding is H264 - MPEG-4 AVC (part 10).

When I try to decode using OpenCV, I see my video stream, but many frames look fragmented. The frames look as if the last row of pixels had just been repeated to fill the rest of the image (sometimes 75% or more of the whole image). OpenCV reports decoding errors (error while decoding MB ...., bytestream).

Is there a way to get OpenCV to use any VLC codec? I tried to specify a specific codec to use in my code for OpenCV, but it does not seem to have any effect.

The code I'm 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()
+4
source share
1 answer

The last time I checked (OpenCV 2.4.9), the ffmpeg assembly used in OpenCV did not use the UDP protocol. It does not buffer received packets for later use. More details here: http://code.opencv.org/issues/2235

EDIT: To force TCP mode, edit opencv \ sources \ modules \ highgui \ src \ cap_ffmpeg_impl.hpp line 538

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

adding tcp:

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

Source: https://habr.com/ru/post/1547500/


All Articles