Monday, June 17, 2013

Twitch.tv without Adobe Flash

How to watch streams in your favourite video player.

Current problems with Flash under Linux:

  • new versions of Adobe plugins will support only Chrome Pepper - for other browsers only older plugins will be available
  • closed source ™
  • sometimes you cannot exit full screen mode
  • sometimes just hangs with 100% CPU usage
  • sometimes it just crash

There is some alternative - shumway but currently it doesn't support YouTube so my guess is that Twitch isn't either. Additionally when page is using SWFObject we will see only "install Adobe Flash" text.

Downloading

Listing flv files for given archive link (past broadcast), archive.py:

#!/usr/bin/python3

from urllib.request import urlopen
import json, sys, re

decoder = json.decoder.JSONDecoder()

for arg in sys.argv[1:]:
 m = re.match(r"^.*/([^/]+)/b/([0-9]+)$", arg)
 video_id = int(m.group(2))
 with urlopen("http://api.justin.tv/api/broadcast/by_archive/%d.json" % video_id) as f:
  resp = decoder.decode(f.read().decode())
  for i in resp:
   print(i["video_file_url"])

Usage:

$ ./archive.py http://www.twitch.tv/some_channel/b/4173086
http://store30.media30.justin.tv/archives/2013-6-15/live_user_some_channel_13713088.flv
http://store38.media38.justin.tv/archives/2013-6-15/live_user_some_channel_13713106.flv
http://store6.media6.justin.tv/archives/2013-6-15/live_user_some_channel_13713124.flv

Only remaining thing is to download those files.

Watching

We will be needing livestreamer and rtmpdump. To not litter in system i've used python virtualenv. Steps below are assuming that rtmpdump is not installed in system.

So:

cd /path/to/project
git clone git://git.ffmpeg.org/rtmpdump
virtualenv env
make -C rtmpdump/
source env/bin/activate
pip install livestreamer

Next step could be writing script for starting player:

#!/bin/bash

d="$(dirname $0)"

source "${d}"/l/bin/activate
LD_LIBRARY_PATH="${d}"/rtmpdump/librtmp/ livestreamer -p /path/to/player -r "${d}"/rtmpdump/rtmpdump "${@}"

... and we can start watching:

./run.sh http://www.twitch.tv/dreamhacksc2 720p

you can also save stream to file:

./run.sh http://www.twitch.tv/dreamhacksc2 720p -o file.flv