Never Gonna Give Up Testing: Building an RTSP Docker Container for Security Camera Automation
RTSP Docker Container Streams Rickrolls for Hassle-Free Security Camera Automation testing
Introduction
In the world of home automation and security systems, testing can quickly become tedious. My recent project involving facial recognition with security cameras highlighted this problem perfectly. After the tenth time running in and out of my camera’s field of view to trigger the system, I knew there had to be a better way to test these setups.
Inspiration struck from an unexpected place - those classic bank heist movies where criminals hack security cameras to loop old footage. Surely, I thought, someone must have created a solution for this already. To my surprise, I found very little that matched what I needed. So I built it myself: a Docker container that hosts an RTSP server capable of streaming test footage to my security system.
The Problem
Testing security camera automations traditionally requires:
- Physically triggering the camera multiple times
- Waiting for processing to complete
- Adjusting settings and repeating the process
This is not only time-consuming but also impractical for fine-tuning facial recognition systems that need consistent input.
The Solution
My solution is a simple yet effective Docker container that:
- Runs an RTSP server (MediaMTX)
- Streams video content on demand
- Can be easily integrated into existing security camera setups for testing
The beauty of this approach is that it lets you feed consistent test footage into your system without having to physically trigger your cameras repeatedly.
Technical Implementation
The implementation consists of two main components:
1. The Dockerfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# Use a lightweight Debian base image to keep the image size small
FROM debian:bookworm-slim as builder
# Install FFmpeg and clean up to reduce image size
RUN apt-get update && \
apt-get install -y ffmpeg && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*
# Use the MediaMTX base image that includes FFmpeg support
FROM bluenviron/mediamtx:latest-ffmpeg
# Copy the video file into the container
COPY videos/rickroll.mp4 /videos/rickroll.mp4
# Configure MediaMTX to use TCP only for RTSP (more firewall-friendly)
ENV MTX_PROTOCOLS=tcp
# Expose RTSP port
EXPOSE 8554
# Add and make the custom entrypoint executable
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
# Set the entrypoint script to launch MediaMTX
ENTRYPOINT ["/entrypoint.sh"]
The Dockerfile uses a multi-stage build approach:
- First stage: Uses a lightweight Debian to install FFmpeg, which will handle video conversion and streaming
- Second stage: Uses the MediaMTX image as the base (MediaMTX is a lightweight, ready-to-use RTSP/RTMP/HLS server)
- In my example I only expose the port required for RTSP but there is various protocols supported by MediaMTX which you can choose to expose (8554 1935 8888 8889 8890/udp 8189/udp)
- An entrypoint script handles the startup logic
2. The Entrypoint Script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/bin/sh
# Start MediaMTX (RTSP server) in the background
# MediaMTX provides Real-Time Streaming Protocol functionality
/mediamtx &
# Wait for MediaMTX to fully initialize
# This delay ensures the RTSP server is ready to accept connections
sleep 5
# Start FFmpeg to stream video to the RTSP server
# Parameters:
# -re = Read input at native frame rate (real-time)
# -stream_loop -1 = Loop the input file indefinitely
# -i = Input file path
# -c:v libx264 = Use H.264 video codec
# -preset ultrafast = Use fastest encoding preset (reduces quality but lowers latency)
# -tune zerolatency = Optimize for minimal latency
# -c:a copy = Copy audio stream without re-encoding
# -f rtsp = Output format is RTSP
# -rtsp_transport tcp = Use TCP (more reliable than UDP) for RTSP transport
ffmpeg -re -stream_loop -1 -i /videos/rickroll.mp4 -c:v libx264 -preset ultrafast -tune zerolatency -c:a copy -f rtsp -rtsp_transport tcp rtsp://localhost:8554/mystream
This script:
- Launches the MediaMTX server in the background
- Waits for it to initialize
- Uses FFmpeg to continuously stream the provided video file to the RTSP server
- The
-stream_loop -1flag ensures the video loops infinitely - Video is encoded with H.264 as it matches the encoding of my security system
- The
ultrafastpreset andzerolatencytune optimize for real-time streaming
The Rick Astley Factor
For my testing purposes, I used Rick Astley’s “Never Gonna Give You Up” as my test footage. There’s something poetically fitting about Rickrolling your own security system!
VLC showing RTSP feed of rick roll
Usage
To use this container in your homelab:
- Clone the repository containing the Dockerfile and entrypoint script
1 2
git clone https://github.com/TravisPooley/RTSP-Test-Server.git cd RTSP-Test-Server - Create a
videosdirectory and add your test video (or use Rick Astley for extra fun) - Build the Docker image:
1
docker build -t rtsp-server .
- Run the container:
1
docker run -d --name rtsp-test -p 8554:8554 rtsp-server
- Connect your security system to
rtsp://your-docker-host:8554/mystream
Benefits and Applications
This solution has several advantages:
- Consistency: The same exact footage is used for every test
- Efficiency: No need to physically trigger cameras
- Integration Testing: Test your entire pipeline with known inputs
- Isolation: Debug specific parts of your system without changing others
Conclusion
Building this RTSP Docker container saved me countless trips running in front of my security camera.
Whether you’re building a sophisticated home security system or just experimenting with computer vision, having a reliable way to stream test footage can save you time and sanity.
Feel free to adapt this solution to your needs, and remember: in the world of home automation testing, we’re never gonna give up making things better!
Got a security camera system that needs testing? Check out the full code for this project on GitHub.