Last active 1450529936

Revision feb882a688cd36536039b095f26d378ca5334032

jtvrtmp.py Raw
1# rtmpdump parameter generator for justin.tv/twitch.tv streams v2
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#
9# Changelog:
10# v2: Added exception catching for when a stream is offline or the specified
11# quality setting is unavailable.
12# v1: Initial release
13#
14# Copyright © 2013 Steven Smith (blha303). All Rights Reserved.
15# New BSD license
16# http://www.opensource.org/licenses/BSD-3-Clause
17
18from urllib2 import urlopen
19import json
20from sys import argv, exit
21
22def getswfaddr(channelname):
23 return urlopen("http://www.justin.tv/widgets/live_embed_player.swf?channel=" + channelname).geturl().split("&userAgent")[0]
24
25def getstreaminfo(channelname):
26 data = json.loads(urlopen("http://usher.justin.tv/find/%s.json?type=any" % channelname.lower()).read())
27 newd = {}
28 for i in data:
29 newd[i['display']] = i
30 return newd
31
32def buildcmdline(channelname, data, quality="360p"):
33 if quality == "live":
34 for i in data:
35 if "+" in i:
36 quality = i
37 try:
38 data = data[quality]
39 except KeyError:
40 return '; echo "-------------"; echo "Couldn\'t find stream info for %s, maybe the stream is offline?"; echo "-------------" #' % quality
41 if not "live-cdn" in data["connect"] and not "justintvlivefs" in data["connect"]:
42 justinlegacyparams = '-j "%s" ' % data["token"].replace('"', r'\"')
43 else:
44 justinlegacyparams = ""
45 out = '-r "%s/%s" %s--swfVfy "%s" -v -o -' % (data["connect"], data["play"], justinlegacyparams, getswfaddr(channelname))
46 return out
47
48def main():
49 if len(argv) < 2:
50 print "Usage: %s channelname [quality]"
51 exit(2)
52 if len(argv) > 2:
53 quality = argv[2]
54 else:
55 quality = "360p"
56 channelname = argv[1]
57 data = getstreaminfo(channelname)
58 print buildcmdline(channelname, data, quality=quality)
59
60if __name__ == "__main__":
61 main()
62