Skip to content

Instantly share code, notes, and snippets.

@coderjo
Created December 12, 2011 20:33
Show Gist options
  • Save coderjo/1468962 to your computer and use it in GitHub Desktop.
Save coderjo/1468962 to your computer and use it in GitHub Desktop.
simple script to build the proper rtmpdump command line for a live ustream feed
#!/usr/bin/env python
flashVer = "WIN 10,3,183,7"
from HTMLParser import HTMLParser
from pyamf.remoting.client import RemotingService
import urllib2, sys
from time import time
# first we need to define a very basic html parser to get two meta properties
class UstreamParser(HTMLParser):
player = None
def handle_starttag(self, tag, attrs):
if tag.lower() == 'meta':
newattrs = dict()
for attr in attrs:
newattrs[attr[0]] = attr[1]
if 'property' in newattrs:
if newattrs['property'] == 'og:video':
self.player = newattrs['content']
# and we need a redirect handler to grab the redirects on the player url
# (rtmpdump can't follow redirects)
class GrabRedirectHandler(urllib2.HTTPRedirectHandler):
newurl = None
def http_error_302(self, req, fp, code, msg, headers):
self.newurl = headers['Location']
return urllib2.HTTPRedirectHandler.http_error_302(self, req, fp, code, msg, headers)
if len(sys.argv) != 2:
sys.exit("Must provide the ustream channel page url")
channelUrl = sys.argv[1]
channelName = channelUrl.rpartition('/')[2]
channelPage = urllib2.urlopen(channelUrl)
parser = UstreamParser()
parser.feed(channelPage.read())
parser.close()
playerUrl = parser.player
print "original player url: %s" % playerUrl
tmp = playerUrl.partition('?')
redir = GrabRedirectHandler()
opener = urllib2.build_opener(redir)
urllib2.install_opener(opener)
tmp2 = urllib2.urlopen(playerUrl)
tmp2.close()
playerUrl = redir.newurl
print "redirected to: %s" % playerUrl
getparams = tmp[2].split('&')
channelId = None
for param in getparams:
v = param.partition('=')
if v[0] == 'cid':
channelId = v[2]
amfUrl = "http://cdngw.ustream.tv/Viewer/getStream/1/%s.amf" % channelId
print "amf url: %s" % amfUrl
amfClient = RemotingService(amfUrl)
amfService = amfClient.getService("Viewer")
amfData = amfService.getStream()
rtmpCommandCustom = None
if 'fmsUrl' in amfData:
print "has fmsUrl: %s" % amfData['fmsUrl']
# simple command
rtmpCommandCustom = '"%s" -a "ustreamVideo/%s" -y "streams/live"' % (amfData['fmsUrl'], channelId)
elif 'cdnUrl' in amfData:
print "has cdnUrl: %s" % amfData['cdnUrl']
cdnUrl = amfData['cdnUrl']
streamName = amfData['streamName']
# more complicated command
if cdnUrl.count('edgefcs.net') > 0:
rtmpCommandCustom = '"%s" -a live -W "%s" -p "%s" -y "%s"' % (cdnUrl, playerUrl, channelUrl, streamName)
elif cdnUrl.count('llnwd.net') > 0:
rtmpCommandCustom = '"%s/%s" -W "%s" -p "%s"' % (cdnUrl, streamName, playerUrl, channelUrl)
rtmpCommand = 'rtmpdump -v -f "%s" -r %s -o %s_%d.flv' % (flashVer, rtmpCommandCustom, channelName, time())
print "\nrtmp command:\n%s" % rtmpCommand
@coderjoe
Copy link

Are you, by chance, the same "thecoderjoe" from justin.tv who recently contacted me? If so cheers and well met. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment