Skip to content

Instantly share code, notes, and snippets.

@livibetter
Created May 1, 2016 01:05
Show Gist options
  • Save livibetter/61682aba4926826a234dd5e0a2ecf1b5 to your computer and use it in GitHub Desktop.
Save livibetter/61682aba4926826a234dd5e0a2ecf1b5 to your computer and use it in GitHub Desktop.
streaming script for Twitch and YouTube
#!/bin/bash
# Written by Yu-Jie Lin in 2016
# Adopted from
# https://wiki.archlinux.org/index.php/Streaming_using_twitch.tv
# With guides
# http://help.twitch.tv/customer/portal/articles/1253460-broadcast-requirements
# https://trac.ffmpeg.org/wiki/StreamingGuide
# https://support.google.com/youtube/answer/2853702
LOG_OPTS='-loglevel warning -stats'
SIZE=1280x720 # input resolution
FPS=10 # target FPS
PRESET=ultrafast # one of the many FFMPEG preset
THREADS=0 # max 6
CBR=2500k # constant bitrate (should be between 1000k - 3000k)
AUDIO_RATE=48000
AUDIO_BR=128k
GOP=$((2 * FPS)) # i-frame interval, should be double of FPS,
GOPMIN="$FPS" # min i-frame interval, should be equal to fps,
VINPUT="-f x11grab -s $SIZE -r $FPS -i :0.0"
AINPUT="-f alsa -i pulse -f flv -ac 2 -ar $AUDIO_RATE"
VRATES="-b:v $CBR -minrate $CBR -maxrate $CBR -bufsize $CBR"
VCODEC="-vcodec libx264 -preset $PRESET -g $GOP -keyint_min $GOPMIN $VRATES -pix_fmt yuv420p"
ACODEC="-acodec libfdk_aac -b:a $AUDIO_BR"
case "$1" in
twitch)
SERVER=live-tpe
#SERVER="live-fra" # twitch server in frankfurt, see http://bashtech.net/twitch/ingest.php for list
STREAM_KEY="$(<$XDG_CONFIG_HOME/twitch-key)"
OUTPUT="rtmp://$SERVER.twitch.tv/app/$STREAM_KEY"
;;
youtube)
STREAM_KEY="$(<$XDG_CONFIG_HOME/youtube-key)"
OUTPUT="rtmp://a.rtmp.youtube.com/live2/$STREAM_KEY"
;;
null)
OUTPUT=/dev/null
;;
test)
OUTPUT=/tmp/test.mp4
;;
*)
echo 'no service specified.' >&2
exit 1
esac
ffmpeg \
$LOG_OPTS \
$VINPUT \
$AINPUT \
$VCODEC \
$ACODEC \
"$OUTPUT"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment