Skip to content

Instantly share code, notes, and snippets.

@Pmmlabs
Last active March 23, 2020 16:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Pmmlabs/c9e6334f03c7a98c6804060ae7986877 to your computer and use it in GitHub Desktop.
Save Pmmlabs/c9e6334f03c7a98c6804060ae7986877 to your computer and use it in GitHub Desktop.
Twitch auto-record bash script
# Dependencies:
# jq https://stedolan.github.io/jq/
# ffmpeg https://ffmpeg.org/
# Usage:
# add to crontab schedule with command: twitch.sh [username] [oauth token]
USER="$1"
OAUTH="$2"
FFMPEG="ffmpeg" # change this if you have ffmpeg installed in other place
JQ="./jq" # change this if you have jq downloaded in other directory. By default, jq called from current directory.
CURRENT="current.m3u8"
FILE="file.m3u8"
RESP="`curl "https://api.twitch.tv/api/channels/$USER/access_token?adblock=true&need_https=true&platform=web&player_type=site&oauth_token" -H "Client-ID: $OAUTH" --compressed`"
TOKEN="`echo $RESP | $JQ '.token' | sed 's/\\\\"/"/g' | sed 's/\(^.\)//g' | sed 's/\(.$\)//g'`"
SIG="`echo $RESP | $JQ '.sig' | sed 's/\(^.\)//g' | sed 's/\(.$\)//g'`"
curl "https://usher.ttvnw.net/api/channel/hls/$USER.m3u8?sig=$SIG&allow_source=true&allow_spectre=true&player_backend=html5&expgroup=&baking_bread=false" -G --data-urlencode "token=$TOKEN" --compressed > $CURRENT
COMMAND="wc -l"
if [ "`cat $CURRENT | $COMMAND`" == "`cat $FILE | $COMMAND`" ]
then
echo "idle"
else
cat $CURRENT > $FILE
FFREPORT=file=%t.log:level=32 $FFMPEG -protocol_whitelist file,https,tls,hls,tcp -i $FILE -c copy `date +%x_%X`.ts
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment