Skip to content

Instantly share code, notes, and snippets.

@cr-chsn1
Last active May 5, 2020 19:53
Show Gist options
  • Save cr-chsn1/d5c9104352921271afc66218e7d00b67 to your computer and use it in GitHub Desktop.
Save cr-chsn1/d5c9104352921271afc66218e7d00b67 to your computer and use it in GitHub Desktop.
Script for streaming from a Raspberry Pi Camera to Twitch
#!/bin/bash
# Note: You need ffmpeg to use this script.
# If not installed, type 'sudo apt install ffmpeg'.
# Twitch Stream-Key
STREAMKEY=live_
# Twitch Ingest: https://stream.twitch.tv/ingests/
ENDPOINTS=(rtmp://live-ber.twitch.tv/app rtmp://live-ams.twitch.tv/app rtmp://live-fra02.twitch.tv/app)
# Stream settings
WIDTH=1920
HEIGHT=1080
FRAMERATE=30
KEYFRAME=60
BITRATE=6000000
I=0
while true
do
# Getting the current endpoint
EPIDX=$(( $I % ${#ENDPOINTS[@]} ))
CURENDPOINT=${ENDPOINTS[$EPIDX]}
echo "Using $CURENDPOINT as Twitch endpoint"
# Starting the stream
raspivid -n -t 0 -w $WIDTH -h $HEIGHT -fps $FRAMERATE -b $BITRATE -g $KEYFRAME -o - | ffmpeg -f lavfi -i anullsrc -c:a aac -r $FRAMERATE -i - -g $KEYFRAME -strict experimental -threads 4 -vcodec copy -map 0:a -map 1:v -b:v $BITRATE -preset ultrafast -f flv "${CURENDPOINT}/${STREAMKEY}"
# Cooldown when stream was aborted
echo
echo "Stream exited unexpected. Wait 60 seconds and restart..."
echo
sleep 60
# Choosing next endpoint
I=$(( $I + 1 ))
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment