Last active
June 24, 2025 11:56
-
-
Save halimb/171918320492bbb3f0b381f972d56203 to your computer and use it in GitHub Desktop.
Webcam RTSP streaming script (using MediaMTX)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
MEDIAMTX_DIR="$HOME/mediamtx" | |
MEDIAMTX_VERSION="v1.11.3" | |
CONFIG_FILE="/tmp/mediamtx.yml" | |
setup_mediamtx() { | |
if [ ! -f "$MEDIAMTX_DIR/mediamtx" ]; then | |
mkdir -p "$MEDIAMTX_DIR" && cd "$MEDIAMTX_DIR" | |
echo "Setting up MediaMTX $MEDIAMTX_VERSION..." | |
echo "Downloading MediaMTX..." | |
wget -q -O - "https://github.com/bluenviron/mediamtx/releases/download/$MEDIAMTX_VERSION/mediamtx_${MEDIAMTX_VERSION}_linux_amd64.tar.gz" | tar -xz -C . | |
cd - | |
fi | |
if [ ! -f "$CONFIG_FILE" ]; then | |
echo "Creating new MediaMTX configuration in $CONFIG_FILE..." | |
cat > "$CONFIG_FILE" << EOF | |
logLevel: info | |
paths: | |
webcam: | |
source: publisher | |
hlsDisable: yes | |
EOF | |
elif ! grep -q "webcam:" "$CONFIG_FILE"; then | |
echo "Adding webcam path to configuration in $CONFIG_FILE..." | |
awk '/paths:/{print;print " webcam:";print " source: publisher";next}1' "$CONFIG_FILE" > "$CONFIG_FILE.tmp" | |
sed -i 's/hlsDisable: no/hlsDisable: yes/' "$CONFIG_FILE.tmp" | |
mv "$CONFIG_FILE.tmp" "$CONFIG_FILE" | |
fi | |
} | |
start_streaming() { | |
cd "$MEDIAMTX_DIR" | |
echo "Starting MediaMTX server with config from $CONFIG_FILE..." | |
./mediamtx "$CONFIG_FILE" & | |
MEDIAMTX_PID=$! | |
sleep 2 | |
echo "Starting FFmpeg stream from webcam... RTSP URL: rtsp://localhost:8554/webcam" | |
ffmpeg -f v4l2 -framerate 8 -video_size 1280x720 -i /dev/video0 \ | |
-c:v libx264 -preset ultrafast -tune zerolatency -b:v 1000k \ | |
-f rtsp -rtsp_transport tcp -stimeout 600000000 rtsp://localhost:8554/webcam 2>/dev/null | |
kill $MEDIAMTX_PID | |
echo "Streaming ended." | |
cd - | |
} | |
setup_mediamtx | |
start_streaming |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment