Playing videos from YouTube

Just finished a little experiment to load flv’s from YouTube into your own swf files…

Every YouTube video has a unique id. You can find this id, using the public YouTube API. However, you’ll need more than this simple id to play the video’s in your own flash/flex/… application.

If you take a closer look to the source code on a YouTube page, playing a video, you will find something like this in the source code:

// <![CDATA[
var fo = new SWFObject("/player2.swf?video_id=lejN7Ulh10s&l=149&t=OEgsToPDskIn_fJRn5UCNu_UAHkEu6jJ", "movie_player", "450", "370", 7, "#FFFFFF");
fo.write("playerDiv");
// ]]>

You can see it adds some more variables after the video_id parameter. These parameters are needed to retrieve the flv itself. What you could do in flash, is retrieve the html source of a video with the given video_id, so a search in the retrieved source and retrieve the full video id. If you have a video object on the stage, you could then play the video using this object:

import mx.utils.Delegate;
function loadYoutubeVideo(id:String):Void{
	this.youtube_flv._visible = false;
	var test_xml:XML = new XML();
	test_xml.onData = Delegate.create(this, youtubeHtmlData);
	test_xml.load("http://www.youtube.com/watch?v="+id);
}
function youtubeHtmlData(src:String):Void{
	var args:String = src.substr(src.indexOf('video_id=') + 9);
	args = args.substr(0, args.indexOf("'"));
	this.ns.play("http://www.youtube.com/get_video?video_id="+args);
}
function statusChanged(infoObject:Object):Void{
	if(infoObject.code == "NetStream.Buffer.Empty"){
		scaleToVideoDimensions();
	}
}
function scaleToVideoDimensions():Void{
	this.youtube_flv._width = this.youtube_flv.width;
	this.youtube_flv._height = this.youtube_flv.height;
	this.youtube_flv._visible = true;
}
Stage.align = "TL";
Stage.scaleMode = "noScale";
this.nc = new NetConnection();
this.nc.connect(null);
this.ns = new NetStream(this.nc);
this.ns.onStatus = Delegate.create(this, statusChanged);
this.ns.onMetaData = Delegate.create(this, metaDataRecieved);
this.youtube_flv.attachVideo(this.ns);
loadYoutubeVideo("ogIqayRDr4w");

You can download the working fla source here.

I had to use the NetStream object on a video object on stage, for some reason I couldn’t get this to work with the default FLVPlayback component… Also, the width and height of the movie aren’t specified in the flv metadata from youtube, so I retrieved those values another way (onStatus event, when Buffer is empty) If you run it outside of the authoring environment, you will also get a security sandbox warning… Maybe somebody knows a workaround for this in this application?

8 Responses to “Playing videos from YouTube”


  • Very nice, I am working on finding a way to play youtube video from a SWF, thanks for posting.

    This code plays FLV files only I am hoping to find a way to modify this code so that it can play youtube videos,

    stop();
    _root.player.autoPlay = true;
    var curPlayURL:Object = new Object();
    var PlayURLWatcher:Function = function (prop, oldVal, newVal, userData) {
    trace(“set “+newVal);
    _root.player.setMedia(newVal);
    return newVal;
    };

    curPlayURL.watch(“cururl”, PlayURLWatcher, 0);
    player.addEventListener(“playheadChange”, controlListen);

    Any Thoughts?

  • Hi, thanks for the code. It seems it doesn’t work anymore and YouTube changed their HTML Data. I get this error :

    Error opening URL ‘http://www.youtube.com/get_video?video_id=/version-check.swf’

    In the HTML Data :
    var to = new SWFObject(“/version-check.swf”, “checker”, “0″, “0″, “0″, “#FFFFFF”);

    Do you know if there’s any workaround possible ?

  • Hey Mike,

    Indeed, they changed their html source. I have updated the youtubeHtmlData function:

    function youtubeHtmlData(src:String):Void{
    var args:String = src.substr(src.indexOf(‘video_id=’) + 9);
    args = args.substr(0, args.indexOf(“‘”));
    this.ns.play(“http://www.youtube.com/get_video?video_id=”+args);
    }

    Enjoy!

  • Hallo Wouter, I applied the code and It worked but only locally, when I test it on my server It doesn’t work, Do I have to do something else to make it work when I put it online?

    Sorry for my bad english, thanks for the help

  • I too can’t get the script to run on my server but it works great locally. Do you happend to know what the problem might be?

  • What is bumburbia?

  • Works really nice :) thank you for posting it, helped me alot

Leave a Reply