使用OpenCV读取网络摄像头流时触发Gstreamer异常求助
Hey there, let's work through this issue with reading your MJPG camera stream using OpenCV on Ubuntu 16.04. I've dealt with similar problems before, so here are some targeted steps to diagnose and fix it:
1. First, Verify the Stream is Accessible
Before blaming OpenCV, make sure the MJPG stream itself is working correctly:
- Open the URL
http://xxx.xxx.xx.xx:5555/mjpg/101.jpgin a browser (Firefox handles MJPG streams well by default) — if you don't see a live feed, the issue is with the camera/network setup, not OpenCV. - Alternatively, use
curlto test connectivity:
You should see a stream of binary data (JPG chunks) in the output. If you get connection errors or empty responses, troubleshoot your network or camera configuration first.curl -v http://xxx.xxx.xx.xx:5555/mjpg/101.jpg
2. Confirm OpenCV Has GStreamer/FFmpeg Support
Ubuntu 16.04's default OpenCV packages often don't enable GStreamer or FFmpeg support fully, even if you've installed the GStreamer libraries. Let's check:
Run this command in your Python environment:
import cv2 print(cv2.getBuildInformation())
Look for the GStreamer section in the output — if it says NO, you'll need to recompile OpenCV with GStreamer enabled (skip to step 4 if this is the case). If it says YES, move to step 3.
3. Force a Specific Backend for VideoCapture
OpenCV might be picking the wrong backend to read the MJPG stream. Try explicitly specifying either the FFmpeg or GStreamer backend:
Option A: Use FFmpeg Backend
FFmpeg is usually reliable for HTTP MJPG streams:
import cv2 camera_number101 = "http://xxx.xxx.xx.xx:5555/mjpg/101.jpg" # Explicitly use FFmpeg backend cap101 = cv2.VideoCapture(camera_number101, cv2.CAP_FFMPEG) # Check if the stream opened successfully if not cap101.isOpened(): print("Failed to open camera stream!") exit() # Read frames in a loop while True: ret101, image_np101 = cap101.read() if not ret101: print("Failed to grab frame — stream might be disconnected") break # Display the frame (optional) cv2.imshow("Camera 101 Feed", image_np101) # Exit on 'q' key press if cv2.waitKey(1) & 0xFF == ord('q'): break # Cleanup cap101.release() cv2.destroyAllWindows()
Option B: Use GStreamer Backend with Custom Pipeline
If FFmpeg doesn't work, try constructing a GStreamer pipeline tailored for HTTP MJPG streams:
import cv2 # GStreamer pipeline for HTTP MJPG stream pipeline = 'souphttpsrc location=http://xxx.xxx.xx.xx:5555/mjpg/101.jpg ! multipartdemux ! jpegdec ! videoconvert ! appsink' cap101 = cv2.VideoCapture(pipeline, cv2.CAP_GSTREAMER) if not cap101.isOpened(): print("Failed to initialize GStreamer pipeline!") exit() # Rest of the frame reading loop is the same as option A while True: ret101, image_np101 = cap101.read() if not ret101: print("Frame grab failed") break cv2.imshow("Camera 101 (GStreamer)", image_np101) if cv2.waitKey(1) & 0xFF == ord('q'): break cap101.release() cv2.destroyAllWindows()
4. Recompile OpenCV with GStreamer Support (If Needed)
If OpenCV wasn't built with GStreamer support, follow these steps on Ubuntu 16.04:
- Install all required dependencies:
sudo apt-get update sudo apt-get install build-essential cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev sudo apt-get install libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev - Clone the OpenCV repository (use a version compatible with Ubuntu 16.04, like 3.4.x):
git clone https://github.com/opencv/opencv.git cd opencv git checkout 3.4.16 mkdir build && cd build - Configure CMake with GStreamer enabled:
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_GSTREAMER=ON -D WITH_FFMPEG=ON .. - Compile and install:
make -j$(nproc) sudo make install
After recompiling, restart your Python environment and recheck the build information to confirm GStreamer is enabled.
内容的提问来源于stack exchange,提问作者porzoo

