树莓派3B下OpenCV+GStreamer RTSP推流至VLC失败问题排查
树莓派3B上OpenCV+GStreamer推流问题:out.isOpened()始终返回False
在树莓派3B上尝试使用OpenCV和GStreamer实现视频推流,参考网上代码编写后遇到问题:代码中out.isOpened()始终返回False,但本地通过imshow可以正常显示视频,推流端无任何连接请求。
使用的Python代码
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()
可正常运行的GStreamer命令
直接在命令行执行以下命令可实现正常推流:
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
测试环境说明
测试所用MP4文件和RTSP流服务(mediamtx)均正常,正常推流时可通过PC端VLC观看视频。
排查结果
执行以下代码检查OpenCV编译配置:
import cv2 print(cv2.getBuildInformation())
输出显示OpenCV未启用GStreamer支持:
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)
当前进展
目前正在重新编译OpenCV以添加GStreamer支持,完成后将更新进展。
内容的提问来源于stack exchange,提问作者Christo Labuschagne
相关产品推荐
相关产品推荐

