BlowfishCMS
A small content management system for OpenBSD
version 0.45.6 · ISC · 2026-12-03 · releases
76 lines · 4.3 KB · download this file
#!/bin/sh
# live-upload: takes OBS's stream on this computer and sends it, piece by
# piece, to your site's Live add-on (macOS, Linux, BSD; needs ffmpeg and
# curl). Leave it running and use Start Streaming in OBS (Stream settings:
# Service Custom, Server rtmp://127.0.0.1:1935/live, any stream key):
#
# sh live-upload.sh https://example.org [PORT]
#
# It asks for the stream key (made on the Live add-on's Stream screen) and
# keeps it only while it runs, in a file only you can read, so it isn't on a
# command line. Each time OBS stops streaming, the stream comes off the site
# and live-upload waits for the next. Ctrl+C stops it.
set -u
site=${1:?"usage: sh live-upload.sh https://your-site [PORT]"} port=${2:-1935}
command -v ffmpeg >/dev/null || { echo 'live-upload needs ffmpeg (macOS: brew install ffmpeg; Linux: your package manager).' >&2; exit 1; }
command -v curl >/dev/null || { echo 'live-upload needs curl.' >&2; exit 1; }
in=${site%/}/x/live/in/
work=$(mktemp -d) || exit 1
chmod 700 "$work"
printf 'Stream key: '; stty -echo 2>/dev/null; read -r key; stty echo 2>/dev/null; echo
printf 'X-Stream-Key: %s\n' "$(printf '%s' "$key" | tr -d '[:space:]')" >"$work/header"; key=''
live='' pid=''
off() { [ -n "$live" ] && curl -fsS -X DELETE -H @"$work/header" "${in}live.m3u8" >/dev/null 2>&1 && echo 'The stream is off the site.'; live=''; }
stop() { [ -z "$pid" ] || kill "$pid" 2>/dev/null; off; rm -rf "$work"; exit 0; }
trap stop INT TERM
send() { # send NAME FILE
if out=$(curl -sS -w '\n%{http_code}' -T "$2" -H @"$work/header" -H 'Content-Type: application/octet-stream' "$in$1" 2>&1); then
code=$(printf '%s' "$out" | tail -n 1)
[ "$code" = 200 ] && return 0
msg=$(printf '%s' "$out" | sed '$d')
echo " $1 wasn't sent: $msg"
case $msg in *'stream key'*) echo "Stopped: check the stream key on the Live add-on's Stream screen."; stop ;; esac
else echo " $1 wasn't sent: $out"; fi
return 1
}
while :; do
# ffmpeg waits for OBS, cuts its stream into 2-second pieces as it comes, and ends when OBS stops.
rm -rf "$work/hls"; mkdir "$work/hls"; : >"$work/sent"; : >"$work/last"; was='' fails=0
(cd "$work/hls" && exec ffmpeg -nostdin -hide_banner -loglevel error -listen 1 -i "rtmp://127.0.0.1:$port/live/obs" \
-c copy -f hls -hls_time 2 -hls_list_size 6 -hls_flags delete_segments+independent_segments+temp_file \
-hls_segment_type fmp4 -hls_fmp4_init_filename init.mp4 -hls_segment_filename 'seg%d.m4s' live.m3u8 2>"$work/ffmpeg.log") &
pid=$!
echo "Waiting for OBS: Start Streaming (to rtmp://127.0.0.1:$port/live). Ctrl+C stops."
while kill -0 "$pid" 2>/dev/null; do
list=$work/hls/live.m3u8
if [ -f "$list" ] && cp "$list" "$work/list" 2>/dev/null; then
{ sed -n 's/.*URI="\([^"]*\)".*/\1/p' "$work/list"; grep -v '^#' "$work/list" | tr -d '\r' | sed '/^$/d'; } >"$work/names"
ok=1 fail=''
while IFS= read -r n; do
f=$work/hls/$n
[ -f "$f" ] || { ok=''; continue; }
# shellcheck disable=SC2012 # its date and time, to see a rewritten init piece
id="$n $(wc -c <"$f" | tr -d ' ') $(ls -l "$f" | awk '{print $6 $7 $8}')"
grep -qxF "$id" "$work/sent" && continue
# The first refusal ends the round: the rest wait for the next try.
if send "$n" "$f"; then grep -v "^$n " "$work/sent" >"$work/sent.new"; echo "$id" >>"$work/sent.new"; mv "$work/sent.new" "$work/sent"; else ok=''; fail=1; break; fi
done <"$work/names"
if [ -n "$ok" ] && ! cmp -s "$work/list" "$work/last"; then
if send live.m3u8 "$work/list"; then [ -n "$live" ] || echo 'Live.'; live=1; was=1; cp "$work/list" "$work/last"; else fail=1; fi
fi
# When the site refuses or can't be reached, try less and less often (up to every 30 seconds), never in a tight loop.
if [ -z "$fail" ]; then fails=0; else
fails=$((fails + 1))
[ "$fails" != 5 ] || echo 'The site keeps refusing pieces: trying less often. See the messages above.'
wait_s=2; i=1; while [ "$i" -lt "$fails" ] && [ "$wait_s" -lt 30 ]; do wait_s=$((wait_s * 2)); i=$((i + 1)); done
[ "$wait_s" -le 30 ] || wait_s=30
sleep "$wait_s"
fi
fi
sleep 1
done
wait "$pid"; code=$?; pid=''
off
# FFmpeg ends with an error when OBS stops; only one that never streamed is worth a word.
[ "$code" = 0 ] || [ -n "$was" ] || { echo "ffmpeg stopped: $(tail -n 2 "$work/ffmpeg.log" | tr '\n' ' ')"; echo "Is something else using port $port? Another live-upload, or a streaming program?"; sleep 3; }
done