Skip to content

About

RTSP streaming service using laptop's main camera, FFmpeg, and MediaMTX/rtsp-simple-server.

Resources

Stars

1 star

Watchers

0 watching

Forks

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 

Repository files navigation

RTSP Camera Streaming

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.

Features

  • 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

Requirements

System Dependencies

  • Python 3.7+
  • FFmpeg (must be installed and available in PATH)
  • RTSP Server (e.g., MediaMTX/rtsp-simple-server) running on localhost:8554

Python Dependencies

  • opencv-python - For camera capture and video processing
  • numpy - For numerical operations (installed with opencv-python)

Installation

1. Clone the Repository

git clone <repository-url>
cd rtsp_camera_streaming

2. Set Up Virtual Environment

python -m venv .venv
source .venv/bin/activate  # On Windows: .venv\Scripts\activate

3. Install Dependencies

pip install opencv-python numpy

4. Install FFmpeg

Windows

# Download from https://ffmpeg.org/download.html
# Add to PATH or place ffmpeg.exe in the project directory

macOS

brew install ffmpeg

Linux (Ubuntu/Debian)

sudo apt update
sudo apt install ffmpeg

5. Set Up RTSP Server

Download and run MediaMTX (formerly rtsp-simple-server):

# Download the appropriate binary for your system
# Extract and run:
./mediamtx

The server will start on localhost:8554 by default.

Usage

Basic Usage

python main.py

The application will:

  1. Detect your default webcam (device 0)
  2. Capture video properties (resolution, FPS)
  3. Start streaming to rtsp://localhost:8554/mystream
  4. Display streaming information in the console

Viewing the Stream

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

Customization

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, film

Configuration

Camera Settings

The application automatically detects:

  • Resolution (width × height)
  • Frame rate (FPS)
  • Pixel format (BGR24 for OpenCV compatibility)

FFmpeg Encoding Parameters

  • 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

RTSP Stream Properties

  • URL: rtsp://localhost:8554/mystream
  • Protocol: RTSP over TCP
  • Transport: Interleaved binary data

Troubleshooting

Common Issues

"Failed to grab frame"

  • 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

"FFmpeg process disconnected"

  • Ensure FFmpeg is installed and in PATH
  • Check if the RTSP server is running
  • Verify the RTSP URL is correct

Connection Issues

  • Confirm MediaMTX/RTSP server is running on port 8554
  • Check firewall settings
  • Verify network connectivity

Performance Issues

  • Reduce resolution or frame rate
  • Adjust FFmpeg preset (superfast or veryfast)
  • Ensure sufficient CPU resources

Debug Mode

Add debug information by modifying the FFmpeg command:

command = [
    'ffmpeg',
    '-v', 'debug',  # Add verbose logging
    # ... rest of the command
]

Architecture

Data Flow

Webcam → OpenCV → Raw BGR24 → FFmpeg → H.264 → RTSP Server → Client

Components

  1. Camera Capture: OpenCV VideoCapture handles webcam input
  2. Frame Processing: Optional frame manipulation (overlays, filters)
  3. Encoding: FFmpeg converts raw video to H.264
  4. Streaming: RTSP server distributes the stream to clients

Performance Considerations

  • CPU Usage: H.264 encoding is CPU-intensive
  • Memory: Frame buffering uses RAM
  • Network: RTSP requires stable network connection
  • Latency: Ultrafast preset minimizes delay

Development

Adding Frame Processing

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)

Multiple Cameras

Extend the application for multiple cameras:

cameras = [cv2.VideoCapture(i) for i in range(num_cameras)]
# Process each camera separately

Recording

Add recording capability by modifying the FFmpeg command:

command.extend([
    '-f', 'rtsp',
    rtsp_url,
    '-f', 'mp4',
    'output.mp4'
])

License

This project is provided as-is for educational and development purposes.

Contributing

Feel free to submit issues and enhancement requests!

Support

For questions and support:

  1. Check the troubleshooting section
  2. Verify all dependencies are installed
  3. Test with different camera devices
  4. Ensure RTSP server is properly configured

About

RTSP streaming service using laptop's main camera, FFmpeg, and MediaMTX/rtsp-simple-server.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages