Twitch currently doesn’t give you a way to download VODs from their website. Just because they don’t really let you, doesn’t mean it’s impossible. There are a few hoops you need to jump through in order to do so.

When you watch a stream or VOD on Twitch, the entire video isn’t downloaded to your computer all at once. It is split up into around 1mb chunks and sent down to you. In order to tell the flash player what all the chunks are and where they are located, Twitch sends basically a playlist to the player. It’s this playlist that we need to get.

Here’s the basic request flow: Twitch VOD download request flow

1. Fetch Access Token

Url: https://api.twitch.tv/api/vods//access_token?as3=t

Returns: {"token":"{"user_id":null,"vod_id":VOD-ID,"expires":1432939520,"chansub":{"restricted_bitrates":[]},"privileged":false}","sig":"abc123"}

The “VOD-ID” is the id from a vod url. For example, 5492597 from http://www.twitch.tv/nl_kripp/v/5492597.

2. Fetch Quality Playlist

Url: http://usher.justin.tv/vod/?nauthsig=&nauth=")

Returns:

#EXTM3U
#EXT-X-MEDIA:TYPE=VIDEO,GROUP-ID="chunked",NAME="Source",AUTOSELECT=YES,DEFAULT=YES
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=2198357,CODECS="avc1.4D4029,mp4a.40.2",VIDEO="chunked"
http://vod.ak.hls.ttvnw.net/v1/AUTH_system/vods_e528/nl_kripp_14599851312_249033263/chunked/index-dvr.m3u8
#EXT-X-MEDIA:TYPE=VIDEO,GROUP-ID="high",NAME="High",AUTOSELECT=YES,DEFAULT=YES
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=1505833,CODECS="avc1.42C01F,mp4a.40.2",VIDEO="high"
http://vod.ak.hls.ttvnw.net/v1/AUTH_system/vods_e528/nl_kripp_14599851312_249033263/high/index-dvr.m3u8

You can use any url in the list but the first one is the highest quality.

3. Fetch Playlist

Url: http://vod.ak.hls.ttvnw.net/v1/AUTH_system/vods_e528/nl_kripp_14599851312_249033263/chunked/index-dvr.m3u8

Returns:

#EXTM3U
#EXT-X-VERSION:3
#EXT-X-TARGETDURATION:4
#ID3-EQUIV-TDTG:2015-05-27T04:47:39
#EXT-X-PLAYLIST-TYPE:EVENT

#EXT-X-TWITCH-ELAPSED-SECS:0.0
#EXT-X-TWITCH-TOTAL-SECS:22366.645
#EXTINF:3.536,
index-0000000014-wgO1.ts?start_offset=0&end_offset=912739
#EXTINF:4.000,
index-0000000014-wgO1.ts?start_offset=912740&end_offset=2043559
#EXTINF:4.000,
index-0000000014-wgO1.ts?start_offset=2043560&end_offset=3137907

Each of these index-* files are parts of the VOD. You can now append them to the url you just grabbed the playlist from to download each file.

Ex: http://vod.ak.hls.ttvnw.net/v1/AUTH_system/vods_e528/nl_kripp_14599851312_249033263/chunked/index-0000000014-wgO1.ts?start_offset=0&end_offset=912739

Example Code

I put together a quick script up on Github that does all this for you and appends all the pieces into one file. Just pass it a VOD like so ruby download.rb http://www.twitch.tv/nl_kripp/v/5492597

There will be more on something cool you can do with these vods in a future post. :)