Last active 1450529936

Revision d9baf0431e4a1c6aa1f87ad4c1e0d404e4da830a

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