Login
Username:

Password:

Remember me



Lost Password?

Register now!

Sections

Who's Online
104 user(s) are online (96 user(s) are browsing Forums)

Members: 0
Guests: 104

more...

Support us!

Headlines

 
  Register To Post  

Python knowledge? ustream script
Quite a regular
Quite a regular


See User information
Following script is meant to retrieve video together with rtmpdump. But it probably does not work with python 2.5 (recommendation was 2.6 or 2.7). Anybody see why that is?

Of course you could try it with a later python to see if it still works.

Quote:

#!/usr/bin/env python
# This script finds the rtmpdump command syntax for opening a UStream stream.

import sys
import urllib2
import re


def getVideoData(url):
# Get the HTML contents
req = urllib2.Request(url)
response = urllib2.urlopen(req)
html = response.read()

# Extract the channel ID from the HTML
channelId = None
m = re.search("Channel\sID\:\s+(\d+)", html)
if (m):
channelId = m.group(1)

# Extract the channel title from the HTML
channelTitle = None
m = re.search("property\="og\:url"\s+content\="http\:\/\/www." +
"ustream\.tv\/(?:channel\/)?([^"]+)"", html)
if (m):
channelTitle = m.group(1)

amfContent = None
if (channelId):
amfUrl = ("http://cdngw.ustream.tv/Viewer/getStream/1/"
+ channelId + ".amf")
response = urllib2.urlopen(amfUrl)
amfContent = response.read()

rtmpUrl = re.search("(rtmp\:\/\/[^\x00]+)", amfContent).group(1)
f = open('tmp.txt', 'w')
f.write(amfContent)
streamName = re.search("streamName(?:\W+)([^\x00]+)",
amfContent).group(1)

return (channelId, channelTitle, rtmpUrl, streamName)


def getRtmpCommand(rtmpUrl, streamName):
result = ("rtmpdump -v -r "" + rtmpUrl + "/" + streamName + """
" -W "http://www.ustream.tv/flash/viewer.swf" --live")
return result


def main(argv=None):
# Process arguments
if argv is None:
argv = sys.argv[1:]

usage = ("Usage: ustreamRTMPDump.py <ustream channel url> [filename]\n"
"e.g. "ustreamRTMPDump.py 'http://www.ustream.tv/ffrc'"")

if (len(argv) < 1):
print usage
return

# Get RTMP information and print it
url = argv[0]
print "Opening url: " + url + "\n"

(channelId, channelTitle, rtmpUrl, streamName) = getVideoData(url)
print "Channel ID: " + channelId
print "Channel Title: " + channelTitle
print "RTMP URL: " + rtmpUrl
print "RTMP Streamname: " + streamName + "\n"

rtmpCmd = getRtmpCommand(rtmpUrl, streamName)
print "RTMP Command:\n" + rtmpCmd

if __name__ == "__main__":
main()


Here's the error it produces when you feed it a suitable URL:

Quote:

Traceback (most recent call last):
File "S:ustreamRTMPDump.py", line 76, in <module>
main()
File "S:ustreamRTMPDump.py", line 66, in main
(channelId, channelTitle, rtmpUrl, streamName) = getVideoData(url)
File "S:ustreamRTMPDump.py", line 41, in getVideoData
return (channelId, channelTitle, rtmpUrl, streamName)
UnboundLocalError: local variable 'rtmpUrl' referenced before assignment


Take care with the indentation if you copypaste the script.

Go to top
Re: Python knowledge? ustream script
Just can't stay away
Just can't stay away


See User information
Quote:
local variable 'rtmpUrl' referenced before assignment

That means you're trying to use a variable before it's initialized. Add two lines to the beginning of the getVideoData function:

def getVideoData(url):
rtmpUrl = ""
streamName = ""

Also I had to add type casting to line 77:
print "Channel ID: " + str(channelId)

Rock lobster bit me - so I'm here forever
X1000 + AmigaOS 4.1 FE
"Anyone can build a fast CPU. The trick is to build a fast system." - Seymour Cray
Go to top
Re: Python knowledge? ustream script
Quite a regular
Quite a regular


See User information
I dabbled a bit more but it's still not getting the channelId right.

Go to top

  Register To Post

 




Currently Active Users Viewing This Thread: 1 ( 0 members and 1 Anonymous Users )




Powered by XOOPS 2.0 © 2001-2024 The XOOPS Project