jtvrtmp.py
· 2.2 KiB · Python
Raw
#!/usr/bin/env python2
# rtmpdump parameter generator for justin.tv/twitch.tv streams v4
# * Usage: jtvrtmp.py channelname [quality]
# where channelname is the channel name ([twitch/justin].tv/channelname)
# and quality is an optional parameter with a valid quality setting
# (360p, 480p, 720p, etc) If quality isn't present, it selects 360p.
# * if quality is live, it picks the quality setting with a +, which
# is a restream of the video being sent to jtv, no transcoding involved.
#
# Changelog:
# v4: Add shebang line
# v3: Removed unicode copyright symbol
# v2: Added exception catching for when a stream is offline or the specified
# quality setting is unavailable.
# v1: Initial release
#
# Copyright 2013 Steven Smith (blha303). All Rights Reserved.
# New BSD license
# http://www.opensource.org/licenses/BSD-3-Clause
from urllib2 import urlopen
import json
from sys import argv, exit
def getswfaddr(channelname):
return urlopen("http://www.justin.tv/widgets/live_embed_player.swf?channel=" + channelname).geturl().split("&userAgent")[0]
def getstreaminfo(channelname):
data = json.loads(urlopen("http://usher.justin.tv/find/%s.json?type=any" % channelname.lower()).read())
newd = {}
for i in data:
newd[i['display']] = i
return newd
def buildcmdline(channelname, data, quality="360p"):
if quality == "live":
for i in data:
if "+" in i:
quality = i
try:
data = data[quality]
except KeyError:
return '; echo "-------------"; echo "Couldn\'t find stream info for %s, maybe the stream is offline?"; echo "-------------" #' % quality
if not "live-cdn" in data["connect"] and not "justintvlivefs" in data["connect"]:
justinlegacyparams = '-j "%s" ' % data["token"].replace('"', r'\"')
else:
justinlegacyparams = ""
out = '-r "%s/%s" %s--swfVfy "%s" -v -o -' % (data["connect"], data["play"], justinlegacyparams, getswfaddr(channelname))
return out
def main():
if len(argv) < 2:
print "Usage: %s channelname [quality]"
exit(2)
if len(argv) > 2:
quality = argv[2]
else:
quality = "360p"
channelname = argv[1]
data = getstreaminfo(channelname)
print buildcmdline(channelname, data, quality=quality)
if __name__ == "__main__":
main()
| 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 | |
| 22 | from urllib2 import urlopen |
| 23 | import json |
| 24 | from sys import argv, exit |
| 25 | |
| 26 | def getswfaddr(channelname): |
| 27 | return urlopen("http://www.justin.tv/widgets/live_embed_player.swf?channel=" + channelname).geturl().split("&userAgent")[0] |
| 28 | |
| 29 | def 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 | |
| 36 | def 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 | |
| 52 | def 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 | |
| 64 | if __name__ == "__main__": |
| 65 | main() |
| 66 |
jtvrtmp.sh
· 486 B · Bash
Raw
#!/bin/bash
# Quality setting in this file doesn't work at the moment. I'm open to suggestions.
# Copyright 2013 Steven Smith (blha303). All Rights Reserved.
# New BSD license
# http://www.opensource.org/licenses/BSD-3-Clause
die () {
echo >&2 "$@"
exit 1
}
(( $# == 1 )) || die "Usage: jtvrtmp.sh channelname [quality]"
(( $# == 2 )) || echo "rtmpdump $(python jtvrtmp.py $1) | vlc -" | sh; exit
(( $# == 3 )) || echo "rtmpdump $(python jtvrtmp.py $1 $2) | vlc -" | sh; exit
| 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 | |
| 7 | die () { |
| 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 |