Programming
20 May 2012 9 Comments

How to play UStream channels in VLC

Introduction

Ustream.tv doesn’t support opening streams in VLC by default, but it is possible with some effort. Most video streaming sites use the Real Time Messaging Protocol (RTMP) for transmitting video data to your flash player. Using rtmpdump we can connect to RTMP streams, and record them or play them in VLC.

1: Install rtmpdump

On Ubuntu:

$ sudo apt-get install rtmpdump

Binaries for all platforms: here.

2: Find the RTMPDump Parameters

It’s not trivial to find out the correct parameters for rtmpdump, so use this Python script:

#!/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  [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()

To use this script to get the rtmpdump command for a UStream channel, simply call it with the channel URL:

python ustreamRTMPDump.py <url>

For example:

$ python ustreamRTMPDump.py "http://www.ustream.tv/decoraheagles"
Opening url: http://www.ustream.tv/decoraheagles
 
Channel ID: 3064708
Channel Title: decoraheagles
RTMP URL: rtmp://ustreamlivefs.fplive.net/ustream3live-live/
RTMP Streamname: stream_live_1_1_3064708
 
RTMP Command:
rtmpdump -v -r "rtmp://ustreamlivefs.fplive.net/ustream3live-live//stream_live_1_1_3064708" -W "http://www.ustream.tv/flash/viewer.swf" --live

Running this command will connect to the stream and write the binary data to the terminal as characters. Not exactly what we want. But we can easily write the stream data to a file, or play it in VLC!

3: Record or Play the Stream

To play the stream in VLC, simply pipe the command into VLC by adding | vlc -.

$ rtmpdump -v -r "rtmp://ustreamlivefs.fplive.net/ustream3live-live//stream_live_1_1_3064708" 
-W "http://www.ustream.tv/flash/viewer.swf" --live | vlc -

To record the stream to a file video.flv simply add -o video.flv to the command.

$ rtmpdump -v -r "rtmp://ustreamlivefs.fplive.net/ustream3live-live//stream_live_1_1_3064708" 
-W "http://www.ustream.tv/flash/viewer.swf" --live -o video.flv

You can then play this file in VLC, even while it is being recorded:

$ vlc video.flv

Example Output

$ rtmpdump -v -r "rtmp://ustreamlivefs.fplive.net/ustream3live-live//stream_live_1_1_3064708" -W "http://www.ustream.tv/flash/viewer.swf" --live | vlc -
RTMPDump v2.4
(c) 2010 Andrej Stepanchuk, Howard Chu, The Flvstreamer Team; license: GPL
VLC media player 2.0.1 Twoflower (revision 2.0.1-0-gf432547)
WARNING: You haven't specified an output file (-o filename), using stdout
[0x8d07908] main libvlc: Running vlc with the default interface. Use 'cvlc' to use vlc without interface.
ERROR: RTMP_HashSWF: couldn't contact swfurl http://www.ustream.tv/flash/viewer.swf (HTTP error 302)
Connecting ...
INFO: Connected...
Starting Live Stream
INFO: Metadata:
INFO:   author                
INFO:   copyright             
INFO:   description           
INFO:   keywords              
INFO:   rating                
INFO:   title                 
INFO:   presetname            Custom
INFO:   creationdate          Sat May 19 23:01:07 2012
INFO:   videodevice           ATI AVStream Analog Capture
INFO:   framerate             30.00
INFO:   width                 720.00
INFO:   height                480.00
INFO:   videocodecid          avc1
INFO:   videodatarate         500.00
INFO:   avclevel              50.00
INFO:   avcprofile            77.00
INFO:   videokeyframe_frequency1.00
INFO:   audiodevice           Microphone (Realtek High Defini
INFO:   audiosamplerate       22050.00
INFO:   audiochannels         1.00
INFO:   audioinputvolume      100.00
INFO:   audiocodecid          .mp3
INFO:   audiodatarate         48.00
0.634 kB / 0.00 sec"sni-qt/24365" WARN  20:30:09.944 void StatusNotifierItemFactory::connectToSnw() Invalid interface to SNW_SERVICE 
36.430 kB / 0.00 sec[flv @ 0xb5211aa0] Estimating duration from bitrate, this may be inaccurate
214.671 kB / 1.13 sec

9 Responses to “How to play UStream channels in VLC”

  1. Alex 25 May 2012 at 2:55 pm #

    Good idea to publish that script. Unfortunately, I can’t get it working (Win X86-64), there is an error:

    File "ustreamRTMPDump.py", line 59
      print usage
                ^
     
    SyntaxError: invalid syntax

    I’m wondering if you can fix it?

    • tboerman 25 May 2012 at 3:16 pm #

      Hi,

      The code works fine here, I just tested it on an old Ubuntu installation with Python 2.6.5. What is your Python version? I haven’t tested it, but if you are using Python 3 then it is entirely possible this script doesn’t work. So be sure to use Python 2.6 or 2.7.

  2. Garrett.S 7 June 2012 at 2:14 pm #

    Hi, I have no knowledge of python whatsoever however I have gotten far enough to be able to run .py files. Could somebody walk me through what I have to put where in here preferably in baby steps please, you don’t have to explain the inner workings of python to me, literally just what URLs and such I have to put where for this to work? It’s just I have a great idea but I need to get this to work for it to work, thanks in advance. Also sorry if that’s asking too much.

    Reply
    • tboerman 7 June 2012 at 2:17 pm #

      You say you can run .py files, so all you have to do is call the script with the URL of the UStream video as a parameter:

      python ustreamRTMPDump.py "http://www.ustream.tv/decoraheagles"

      Reply
  3. Garrett.S 7 June 2012 at 4:24 pm #

    Sorry about this, I think I got slightly ahead of myself. I have the means to run the script I just don’t know how. Where do I have to put the script.py for the command line to recognise it as I keep getting invalid syntax?

    Reply
    • tboerman 7 June 2012 at 5:02 pm #

      You can put the script anywhere. Just put it in a directory, navigate to that directory, and enter the command. If you can’t figure it out you can post the error here…

      Reply
  4. Garrett.S 7 June 2012 at 5:17 pm #

    Basically this.

    Reply
    • tboerman 7 June 2012 at 5:39 pm #

      The first command will work if you put the script in C:\Python27\ustreamRTMPDump.py. You may want to check out a Python tutorial first if you run into more problems. Good luck!

      Reply
  5. Garrett.S 7 June 2012 at 8:41 pm #

    Just responding to say I got it working :) Thanks for the help, script works a treat.

    Reply