A Python application that captures video from a webcam and streams it to an RTSP server using FFmpeg. This project enables real-time video streaming from local cameras to RTSP-compatible clients.
- Real-time streaming: Captures video from webcam and streams it via RTSP
- Low latency: Optimized FFmpeg settings for minimal delay
- Flexible configuration: Automatically detects camera properties
- Robust error handling: Graceful handling of connection issues
- Cross-platform: Works on Windows, Linux, and macOS
- Python 3.7+
- FFmpeg (must be installed and available in PATH)
- RTSP Server (e.g., MediaMTX/rtsp-simple-server) running on
localhost:8554
opencv-python- For camera capture and video processingnumpy- For numerical operations (installed with opencv-python)
git clone <repository-url>
cd rtsp_camera_streamingpython -m venv .venv
source .venv/bin/activate # On Windows: .venv\Scripts\activatepip install opencv-python numpy# Download from https://ffmpeg.org/download.html
# Add to PATH or place ffmpeg.exe in the project directorybrew install ffmpegsudo apt update
sudo apt install ffmpegDownload and run MediaMTX (formerly rtsp-simple-server):
# Download the appropriate binary for your system
# Extract and run:
./mediamtxThe server will start on localhost:8554 by default.
python main.pyThe application will:
- Detect your default webcam (device 0)
- Capture video properties (resolution, FPS)
- Start streaming to
rtsp://localhost:8554/mystream - Display streaming information in the console
Use any RTSP-compatible player to view the stream:
- VLC: Open Network Stream →
rtsp://localhost:8554/mystream - FFplay:
ffplay rtsp://localhost:8554/mystream - Web player: Configure your web-based RTSP player
You can modify the following parameters in main.py:
# Change camera device (0 = default, 1 = second camera, etc.)
cap = cv2.VideoCapture(0)
# Change RTSP destination URL
rtsp_url = 'rtsp://localhost:8554/mystream'
# Modify FFmpeg encoding settings
'-preset', 'ultrafast', # Options: ultrafast, superfast, veryfast, faster, fast, medium
'-tune', 'zerolatency', # Options: zerolatency, fastdecode, filmThe application automatically detects:
- Resolution (width × height)
- Frame rate (FPS)
- Pixel format (BGR24 for OpenCV compatibility)
- Codec: H.264 (libx264) - Standard for RTSP streaming
- Preset:
ultrafast- Optimized for low latency - Tune:
zerolatency- Minimizes encoding delay - Pixel Format:
yuv420p- Compatible with most players
- URL:
rtsp://localhost:8554/mystream - Protocol: RTSP over TCP
- Transport: Interleaved binary data
- Check if the webcam is connected and not in use by another application
- Try different camera device numbers (0, 1, 2, etc.)
- Verify camera drivers are properly installed
- Ensure FFmpeg is installed and in PATH
- Check if the RTSP server is running
- Verify the RTSP URL is correct
- Confirm MediaMTX/RTSP server is running on port 8554
- Check firewall settings
- Verify network connectivity
- Reduce resolution or frame rate
- Adjust FFmpeg preset (
superfastorveryfast) - Ensure sufficient CPU resources
Add debug information by modifying the FFmpeg command:
command = [
'ffmpeg',
'-v', 'debug', # Add verbose logging
# ... rest of the command
]Webcam → OpenCV → Raw BGR24 → FFmpeg → H.264 → RTSP Server → Client
- Camera Capture: OpenCV VideoCapture handles webcam input
- Frame Processing: Optional frame manipulation (overlays, filters)
- Encoding: FFmpeg converts raw video to H.264
- Streaming: RTSP server distributes the stream to clients
- CPU Usage: H.264 encoding is CPU-intensive
- Memory: Frame buffering uses RAM
- Network: RTSP requires stable network connection
- Latency: Ultrafast preset minimizes delay
Uncomment and modify the frame processing section:
# Optional: Process frame here (draw boxes, AI, etc.)
cv2.putText(frame, "Live", (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)Extend the application for multiple cameras:
cameras = [cv2.VideoCapture(i) for i in range(num_cameras)]
# Process each camera separatelyAdd recording capability by modifying the FFmpeg command:
command.extend([
'-f', 'rtsp',
rtsp_url,
'-f', 'mp4',
'output.mp4'
])This project is provided as-is for educational and development purposes.
Feel free to submit issues and enhancement requests!
For questions and support:
- Check the troubleshooting section
- Verify all dependencies are installed
- Test with different camera devices
- Ensure RTSP server is properly configured