Last active 1450529936

Revision 3f37034f54418dd488e503abafed947ee814962a

jtvrtmp.py Raw
1# rtmpdump parameter generator for justin.tv/twitch.tv streams
2# * Usage: jtvrtmp.py channelname [quality]
3# where channelname is the channel name ([twitch/justin].tv/channelname)
4# and quality is an optional parameter with a valid quality setting
5# (360p, 480p, 720p, etc) If quality isn't present, it selects 360p.
6# * if quality is live, it picks the quality setting with a +, which
7# is a restream of the video being sent to jtv, no transcoding involved.
8# Copyright © 2013 Steven Smith (blha303). All Rights Reserved.
9# New BSD license
10# http://www.opensource.org/licenses/BSD-3-Clause
11
12from urllib2 import urlopen
13import json
14from sys import argv, exit
15
16def getswfaddr(channelname):
17 return urlopen("http://www.justin.tv/widgets/live_embed_player.swf?channel=" + channelname).geturl().split("&userAgent")[0]
18
19def getstreaminfo(channelname):
20 data = json.loads(urlopen("http://usher.justin.tv/find/%s.json?type=any" % channelname.lower()).read())
21 newd = {}
22 for i in data:
23 newd[i['display']] = i
24 return newd
25
26def buildcmdline(channelname, data, quality="360p"):
27 if quality == "live":
28 for i in data:
29 if "+" in i:
30 quality = i
31 data = data[quality]
32 if not "live-cdn" in data["connect"] and not "justintvlivefs" in data["connect"]:
33 justinlegacyparams = '-j "%s" ' % data["token"].replace('"', r'\"')
34 else:
35 justinlegacyparams = ""
36 out = '-r "%s/%s" %s--swfVfy "%s" -v -o -' % (data["connect"], data["play"], justinlegacyparams, getswfaddr(channelname))
37 return out
38
39def main():
40 if len(argv) < 2:
41 print "Usage: %s channelname [quality]"
42 exit(2)
43 if len(argv) > 2:
44 quality = argv[2]
45 else:
46 quality = "360p"
47 channelname = argv[1]
48 data = getstreaminfo(channelname)
49 print buildcmdline(channelname, data, quality=quality)
50
51if __name__ == "__main__":
52 main()
53