From ad06ee4854ec820aa6df21e97e64b6997ee03065 Mon Sep 17 00:00:00 2001 From: ComputerTech Date: Sat, 4 Apr 2026 13:14:44 +0100 Subject: [PATCH] feat: add setup_mediamtx.sh install script --- setup_mediamtx.sh | 146 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 146 insertions(+) create mode 100755 setup_mediamtx.sh diff --git a/setup_mediamtx.sh b/setup_mediamtx.sh new file mode 100755 index 0000000..1f6bfb8 --- /dev/null +++ b/setup_mediamtx.sh @@ -0,0 +1,146 @@ +#!/usr/bin/env bash +# setup_mediamtx.sh — Install and configure MediaMTX for TechDJ +# Run on the remote server as a user with sudo access: +# bash setup_mediamtx.sh + +set -e + +LISTENER_PORT=5001 +SRT_PORT=8890 +HLS_PORT=8888 +SRT_PATH=aussie_dj +MEDIAMTX_CONF=/usr/local/etc/mediamtx/mediamtx.yml + +echo "==> Detecting architecture..." +ARCH=$(uname -m) +case "$ARCH" in + x86_64) ARCH_LABEL="amd64" ;; + aarch64) ARCH_LABEL="arm64" ;; + armv7l) ARCH_LABEL="armv7" ;; + *) echo "ERROR: Unsupported architecture: $ARCH"; exit 1 ;; +esac +echo " Architecture: $ARCH_LABEL" + +echo "==> Fetching latest MediaMTX version..." +LATEST=$(curl -fsSL https://api.github.com/repos/bluenviron/mediamtx/releases/latest \ + | grep '"tag_name"' | head -1 | sed 's/.*"v\([^"]*\)".*/\1/') +if [ -z "$LATEST" ]; then + echo "ERROR: Could not fetch latest version. Check internet connectivity." + exit 1 +fi +echo " Latest version: v$LATEST" + +# Check if already installed and up to date +if command -v mediamtx &>/dev/null; then + INSTALLED=$(mediamtx --version 2>&1 | grep -oP '\d+\.\d+\.\d+' | head -1 || true) + if [ "$INSTALLED" = "$LATEST" ]; then + echo "==> MediaMTX v$LATEST already installed, skipping download." + else + echo "==> Updating MediaMTX from v$INSTALLED to v$LATEST..." + INSTALL=1 + fi +else + INSTALL=1 +fi + +if [ "${INSTALL:-0}" = "1" ]; then + TARBALL="mediamtx_v${LATEST}_linux_${ARCH_LABEL}.tar.gz" + URL="https://github.com/bluenviron/mediamtx/releases/download/v${LATEST}/${TARBALL}" + echo "==> Downloading $TARBALL..." + curl -fsSL "$URL" -o "/tmp/$TARBALL" + echo "==> Installing to /usr/local/bin..." + sudo tar -xzf "/tmp/$TARBALL" -C /usr/local/bin mediamtx + rm "/tmp/$TARBALL" + echo " Installed: $(mediamtx --version 2>&1 | head -1)" +fi + +echo "==> Writing MediaMTX config to $MEDIAMTX_CONF..." +sudo mkdir -p "$(dirname "$MEDIAMTX_CONF")" +sudo tee "$MEDIAMTX_CONF" > /dev/null << YAMLEOF +############################################################################### +# MediaMTX config for TechDJ +# Generated by setup_mediamtx.sh +############################################################################### + +# Logging +logLevel: info +logDestinations: [stdout] + +# Disable unused protocols +rtsp: + disabled: false # keep enabled — mediamtx needs it internally + +rtmp: + disabled: true + +webrtc: + disabled: true + +# HLS output — listeners access the stream here +hls: + address: :${HLS_PORT} + # Keep a short segment list so listeners tune in quickly + segmentDuration: 2s + partDuration: 200ms + segmentMaxAge: 30s + +# SRT input — DJ streams here via OBS +srt: + address: :${SRT_PORT} + +# Paths +paths: + ${SRT_PATH}: + runOnPublish: >- + curl -s -X POST http://127.0.0.1:${LISTENER_PORT}/api/webhook + -H "Content-Type: application/json" + -d "{\"event\":\"publish\",\"path\":\"${SRT_PATH}\",\"source\":{\"remoteAddr\":\"\$MTX_QUERY\"}}" + runOnUnpublish: >- + curl -s -X POST http://127.0.0.1:${LISTENER_PORT}/api/webhook + -H "Content-Type: application/json" + -d "{\"event\":\"unpublish\",\"path\":\"${SRT_PATH}\"}" +YAMLEOF +echo " Config written." + +echo "==> Writing systemd service..." +sudo tee /etc/systemd/system/mediamtx.service > /dev/null << 'SVCEOF' +[Unit] +Description=MediaMTX Real-time Media Server +After=network.target + +[Service] +ExecStart=/usr/local/bin/mediamtx /usr/local/etc/mediamtx/mediamtx.yml +Restart=always +RestartSec=5 +StandardOutput=journal +StandardError=journal + +[Install] +WantedBy=multi-user.target +SVCEOF + +echo "==> Enabling and starting mediamtx service..." +sudo systemctl daemon-reload +sudo systemctl enable mediamtx +sudo systemctl restart mediamtx + +echo "==> Opening firewall ports..." +if command -v ufw &>/dev/null; then + sudo ufw allow "${SRT_PORT}/udp" && echo " UFW: opened ${SRT_PORT}/udp (SRT)" + sudo ufw allow "${HLS_PORT}/tcp" && echo " UFW: opened ${HLS_PORT}/tcp (HLS)" +else + echo " UFW not found — open ports ${SRT_PORT}/udp and ${HLS_PORT}/tcp manually." +fi + +echo "" +echo "==> Waiting for service to start..." +sleep 2 +sudo systemctl status mediamtx --no-pager | head -20 + +echo "" +echo "================================================================" +echo " MediaMTX setup complete!" +echo " SRT ingest : srt://$(hostname -f):${SRT_PORT}?streamid=${SRT_PATH}" +echo " HLS output : http://$(hostname -f):${HLS_PORT}/${SRT_PATH}/index.m3u8" +echo " Webhook : http://127.0.0.1:${LISTENER_PORT}/api/webhook" +echo "================================================================"