Search code examples
pythonwindowsopencvffmpegvlc

OpenCV is able to read the stream but VLC not


I'm trying to stream my webcam frames to an UDP address. Here is my sender code.

cmd = ['ffmpeg', '-y', '-f', 'rawvideo', '-pixel_format', 'bgr24', '-video_size', f'{width}x{height}', 
       '-i', '-', '-c:v', 'mpeg4','-preset', 'ultrafast', '-tune', 'zerolatency','-b:v', '1.5M',
       '-f', 'mpegts', f'udp://@{ip_address}:{port}']
p = subprocess.Popen(cmd, stdin=subprocess.PIPE)
camera = cv2.VideoCapture(0)
while True:
    ret, frame = camera.read()
    cv2.imshow("Sender",frame)
    if not ret:
        break
    p.stdin.write(frame.tobytes())
    p.stdin.flush()
    if cv2.waitKey(1) & 0xFF == ord('q'):
            break

This Python code can make stream successfully. I can read the stream with this receiver code.

q = queue.Queue()
def receive():
    cap = cv2.VideoCapture('udp://@xxx.x.xxx.xxx:5000')
    ret, frame = cap.read()
    q.put(frame)
    while ret:
        ret, frame = cap.read()
        q.put(frame)
def display():
    while True:
        if q.empty() != True:
            frame = q.get()
            cv2.imshow('Receiver', frame)
        k = cv2.waitKey(1) & 0xff
        if k == 27:  # press 'ESC' to quit
            break
tr = threading.Thread(target=receive, daemon=True)
td = threading.Thread(target=display)
tr.start()
td.start()
td.join()

But I can not watch the stream from VLC. I'm going to Media->Open Network Stream-> udp://@xxx.x.xxx.xxx:5000 to watch stream. After some seconds, the timer that located bottom left of VLC starts to increase but there are no frames in screen, just VLC icon.

I checked firewall rules, opened all ports to UDP connections. I am using my IP address to send frames and watch them. Also, I tried other video codecs like h264, hvec, mpeg4, rawvideo. Additionally, I tried to watch stream by using Windows Media Player but it didn't work.

What should I do to fix this issue?


Solution

  • I tried to view stream from mpv and it worked, I think this issue is about a bug of VLC