kenorb

Script for finding UStream stream using rtmpdump

Nov 13th, 2012
1,823
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.05 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # This script finds the rtmpdump command syntax for opening a UStream stream.
  3. # Sites:
  4. #   https://github.com/kenorb/UStream-recorder
  5. #   http://code.google.com/p/ustream-recorder
  6.  
  7. import sys
  8. import urllib2
  9. import re
  10.  
  11.  
  12. def getVideoData(url):
  13.     # Get the HTML contents
  14.     req = urllib2.Request(url)
  15.     response = urllib2.urlopen(req)
  16.     html = response.read()
  17.  
  18.     # Extract the channel ID from the HTML
  19.     channelId = re.search("ustream.vars.channelId=(\d+)", html).group(1)
  20.  
  21.     # Extract the channel title from the HTML
  22.     channelTitle = None
  23.     m = re.search("ustream.vars.channelTitle=\"([^\"]+)\"", html)
  24.     if (m):
  25.       channelTitle = m.group(1)
  26.  
  27.     if (not channelTitle):
  28.       m = re.search("property\=\"og\:url\"\s+content\=\"http\:\/\/www." +
  29.         "ustream\.tv\/(?:channel\/)?([^\"]+)\"", html)
  30.       if (m):
  31.         channelTitle = m.group(1)
  32.  
  33.     amfContent = None
  34.     if (channelId):
  35.         amfUrl = "http://cdngw.ustream.tv/Viewer/getStream/1/%s.amf" % channelId
  36.         response = urllib2.urlopen(amfUrl)
  37.         amfContent = response.read()
  38.  
  39.         streams = []
  40.         rtmpUrl, streamName = re.search("\x00\x06cdnUrl\x02\x00.(rtmp:[^\x00]+)\x00\x0astreamName\x02\x00.([^\x00]+)", amfContent).groups()
  41.         streams.append({"url":rtmpUrl, "name":streamName})
  42.  
  43.         altPtn = re.compile("\x00\x0dcdnStreamName\x02\x00.([^\x00]+)\x00\x0ccdnStreamUrl\x02\x00.(rtmp:[^\x00]+)")
  44.         for m in re.finditer(altPtn, amfContent):
  45.           altName, altUrl = m.groups()
  46.           if (altUrl == rtmpUrl and altName == streamName): continue
  47.           streams.append({"url":altUrl, "name":altName})
  48.  
  49.         #f = open('tmp.txt', 'wb')
  50.         #f.write(amfContent)
  51.  
  52.     return (channelId, channelTitle, streams)
  53.  
  54.  
  55. def getRtmpCommand(rtmpUrl, streamName):
  56.     result = "rtmpdump -v -r \"%s/%s\" -W \"http://www.ustream.tv/flash/viewer.swf\" --live" % (rtmpUrl, streamName)
  57.     return result
  58.  
  59.  
  60. def main(argv=None):
  61.     # Process arguments
  62.     if argv is None:
  63.         argv = sys.argv[1:]
  64.  
  65.     usage = "Usage: ustreamRTMPDump.py "
  66.     usage += "\ne.g. ustreamRTMPDump.py \"http://www.ustream.tv/ffrc\""
  67.  
  68.     if (len(argv) < 1):
  69.         print usage
  70.         return
  71.  
  72.     # Get RTMP information and print it
  73.     url = argv[0]
  74.     print "Opening url: %s\n" % url
  75.  
  76.     (channelId, channelTitle, streams) = getVideoData(url)
  77.     print "Channel ID: %s" % channelId
  78.     print "Channel Title: %s" % channelTitle
  79.     for stream in streams:
  80.       print "RTMP URL: %s\nRTMP Streamname: %s" % (stream["url"], stream["name"])
  81.       print ""
  82.       rtmpCmd = getRtmpCommand(streams[0]["url"], streams[0]["name"])
  83.       print "RTMP Command: %s\n" % rtmpCmd
  84.  
  85.     # Example streams...
  86.     # HQ: stream_live_1_1_6540154
  87.     # 480p: stream_live_8_1_6540154
  88.     # 360p: stream_live_4_1_6540154
  89.     # 240p: stream_live_6_1_6540154
  90.     # HQ: ustream-sj2_63@53274
  91.     # 480p: ustream-sj2_677@19148
  92.     # 360p: ustream-sj2_195@20448
  93.     # 240p: ustream-sj2_44@7605
  94.  
  95. if __name__ == "__main__":
  96.     main()
Advertisement
Add Comment
Please, Sign In to add comment