Last active
May 5, 2020 19:53
-
-
Save cr-chsn1/d5c9104352921271afc66218e7d00b67 to your computer and use it in GitHub Desktop.
Script for streaming from a Raspberry Pi Camera to Twitch
This file contains 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 | |
# 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