Last active 1450529936

Revision 36d66dbce28b60828c32c42c3f1d7f96569ca4df

jtvrtmp.py Raw
1#!/usr/bin/env python2
2
3# rtmpdump parameter generator for justin.tv/twitch.tv streams v4
4# * Usage: jtvrtmp.py channelname [quality]
5# where channelname is the channel name ([twitch/justin].tv/channelname)
6# and quality is an optional parameter with a valid quality setting
7# (360p, 480p, 720p, etc) If quality isn't present, it selects 360p.
8# * if quality is live, it picks the quality setting with a +, which
9# is a restream of the video being sent to jtv, no transcoding involved.
10#
11# Changelog:
12# v4: Add shebang line
13# v3: Removed unicode copyright symbol
14# v2: Added exception catching for when a stream is offline or the specified
15# quality setting is unavailable.
16# v1: Initial release
17#
18# Copyright 2013 Steven Smith (blha303). All Rights Reserved.
19# New BSD license
20# http://www.opensource.org/licenses/BSD-3-Clause
21
22from urllib2 import urlopen
23import json
24from sys import argv, exit
25
26def getswfaddr(channelname):
27 return urlopen("http://www.justin.tv/widgets/live_embed_player.swf?channel=" + channelname).geturl().split("&userAgent")[0]
28
29def getstreaminfo(channelname):
30 data = json.loads(urlopen("http://usher.justin.tv/find/%s.json?type=any" % channelname.lower()).read())
31 newd = {}
32 for i in data:
33 newd[i['display']] = i
34 return newd
35
36def buildcmdline(channelname, data, quality="360p"):
37 if quality == "live":
38 for i in data:
39 if "+" in i:
40 quality = i
41 try:
42 data = data[quality]
43 except KeyError:
44 return '; echo "-------------"; echo "Couldn\'t find stream info for %s, maybe the stream is offline?"; echo "-------------" #' % quality
45 if not "live-cdn" in data["connect"] and not "justintvlivefs" in data["connect"]:
46 justinlegacyparams = '-j "%s" ' % data["token"].replace('"', r'\"')
47 else:
48 justinlegacyparams = ""
49 out = '-r "%s/%s" %s--swfVfy "%s" -v -o -' % (data["connect"], data["play"], justinlegacyparams, getswfaddr(channelname))
50 return out
51
52def main():
53 if len(argv) < 2:
54 print "Usage: %s channelname [quality]"
55 exit(2)
56 if len(argv) > 2:
57 quality = argv[2]
58 else:
59 quality = "360p"
60 channelname = argv[1]
61 data = getstreaminfo(channelname)
62 print buildcmdline(channelname, data, quality=quality)
63
64if __name__ == "__main__":
65 main()
66
jtvrtmp.sh Raw
1#!/bin/bash
2# Quality setting in this file doesn't work at the moment. I'm open to suggestions.
3# Copyright 2013 Steven Smith (blha303). All Rights Reserved.
4# New BSD license
5# http://www.opensource.org/licenses/BSD-3-Clause
6
7die () {
8 echo >&2 "$@"
9 exit 1
10}
11
12(( $# == 1 )) || die "Usage: jtvrtmp.sh channelname [quality]"
13(( $# == 2 )) || echo "rtmpdump $(python jtvrtmp.py $1) | vlc -" | sh; exit
14(( $# == 3 )) || echo "rtmpdump $(python jtvrtmp.py $1 $2) | vlc -" | sh; exit