Skip to content

Instantly share code, notes, and snippets.

@bruxisma
Last active August 29, 2015 14:17
Show Gist options
  • Save bruxisma/de3e0a38b2390b00404a to your computer and use it in GitHub Desktop.
Save bruxisma/de3e0a38b2390b00404a to your computer and use it in GitHub Desktop.
Download Twitch Broadcasts
#!/bin/sh
# This is a shell script to download a 'past broadcast' from twitch.tv and
# convert it to an MP4. Takes a channel and a number parameter, which is the
# which is the number in the url http://twitch.tv/<channel>/b/<number>
#
# If no parameter is provided, a list of up to 10 video ids combined with their
# dates and the game played will be printed.
#
# This requires the following tools:
# curl
# ffmpeg
# jq
#
# This script was written to work on OS X. It may work on other platforms.
# Feel free to modify this to take additional arguments like 'limits' and such
TMPDIR=`mktemp -d -t spasm`
OUTPUT=$2
trap "rm -rf ${TMPDIR}" EXIT
trap "exit" SIGINT
function broadcasts {
url="https://api.twitch.tv/kraken/channels/$1/videos?broadcasts=true"
filter='.videos|.[]|"\(.game), \(._id|ltrimstr("a")), \(.recorded_at)"'
curl -s -X GET $url | jq -r "$filter"
exit
}
function download {
: ${OUTPUT:="output"}
url="http://download.twitchapps.com/listparts.php?id=a$1&playlist=true"
pushd $TMPDIR > /dev/null
curl -s $url | grep "flv\$" | xargs -n 1 curl --progress-bar -O
ls -A1 | grep "flv$" | xargs printf "file './%s'\n" > list.txt
ffmpeg -v 0 -f concat -i list.txt -c copy ${OUTPUT}.mp4
popd > /dev/null
mv ${TMPDIR}/${OUTPUT}.mp4 ./
echo "Finished"
exit
}
if [[ $# -eq 0 ]]; then
echo "usage: spasm <channel|broadcast-id> [output-name]" && exit
fi
case $1 in
''|*[!0-9]*) broadcasts $1;;
*) download $1;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment