I would like to stream a video source using opencv and gstreamer on a PI 3b. From code online I got this far, and can not seem to find or debug the issue. out.IsOpened() always returns False. The video does show locally with imshow.
import cv2
cap = cv2.VideoCapture('sample-mp4-file.mp4')
# Get video dimensions
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = int(cap.get(cv2.CAP_PROP_FPS))
pipeline = 'appsrc ! videoconvert ! videoscale ! video/x-raw,width=640,height=480 ! x264enc speed-preset=veryfast tune=zerolatency bitrate=800 ! rtspclientsink location=rtsp://localhost:8554/mystream '
out = cv2.VideoWriter(pipeline, cv2.CAP_GSTREAMER, 0, fps, (frame_width, frame_height))
print(out.isOpened())
print()
while cap.isOpened():
ret, frame = cap.read()
if ret:
# Display the frame
cv2.imshow('Video Stream', frame)
out.write(frame)
print(out.isOpened())
if cv2.waitKey(int(1000/fps)) & 0xFF == ord('q'):
break
else:
break
# Release everything if job is finished
cap.release()
out.release()
With out never open nothing happens on the connection side. I never see an incoming connection. From the command line, this does work
gst-launch-1.0 filesrc location=sample-mp4-file.mp4 ! decodebin ! videoconvert ! videoscale ! video/x-raw,width=640,height=480 ! x264enc speed-preset=veryfast tune=zerolatency bitrate=800 ! rtspclientsink location=rtsp://localhost:8554/mystream
I downloaded the sample mp4 from here
To serve the rtsp stream I'm using mediamtx found here. I can then view the stream from a connected PC using VLC.
EDIT:
Currently it seems like I'm missing support for gstreamer in opencv (An error would have been nice...)
import cv2
print(cv2.getBuildInformation())
output:
Video I/O:
DC1394: NO
FFMPEG: YES
avcodec: YES (59.37.100)
avformat: YES (59.27.100)
avutil: YES (57.28.100)
swscale: YES (6.7.100)
avresample: NO
GStreamer: NO
v4l/v4l2: YES (linux/videodev2.h)
It takes forever to rebuild, so will repost tomorrow when it's done.
After quite a few attempts, I was able to compile opencv from source on the raspberry pi 3b to have gstreamer support. This solved my problem.
I used this tutorial but updated to latest version of opencv, disabled support for opencl as I could not get it to work, changed qt to version 5, and added support for GTK all done in the CMAKEFLAGS part of the script. Make sure you have a BIG swap file (mine was 5 GB). You can also increase the jobs count. I made mine 3 and it worked fine.