Constructing YouTube FLV URL on client-side without any server-side script

It seems, there is a easier way of constructing YouTube! FLV URL on client-side without using any server-side script.

We need get the YouTube video_id from any of following places:-

Then, construct a URL like this: http://www.youtube.com/v/V3BsjxDZJM0

Above URL is used in YouTube embed-code. It is basically a server-side redirection that points to YouTube Player (.swf). Above redirects (server-side) to following URL:-
http://www.youtube.com/p.swf?video_id=V3BsjxDZJM0&eurl=&iurl=http%3A//sjc-
static12.sjc.youtube.com/vi/V3BsjxDZJM0/2.jpg&t=OEgsToPDskKzlTMEFZ1jOh40Xc3qOxzQ

As you can see in above URL, it contains “t” param (in red), which is what we need along with video_id to construct YouTube video (FLV) URL, YouTube FLV URL would look like this:-
http://www.youtube.com/get_video.php?video_id=V3BsjxDZJM0&
t=OEgsToPDskKzlTMEFZ1jOh40Xc3qOxzQ

You can check out code below to figure out things your own. You can also check out the example-app.

Flex 2.0/ActionScript 3.0 example:-


<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
creationComplete="onAppCreationComplete ()"
horizontalAlign="left"
width="800"
height="350">
<mx:Script>
<![CDATA[
import flash.net.*;
import flash.events.*;
import flash.display.*;
private var loader:Loader;
private var abortId:uint;
private function onAppCreationComplete ():void
{
loader = new Loader ();
}
private function startLoading ():void
{
var req:URLRequest = new URLRequest ("http://www.youtube.com/v/3IcwG0jUFxU");
loader.contentLoaderInfo.addEventListener(Event.INIT, handlerLoaderInit);
loader.load(req);
logMessage ("Loading YouTube URL..");
}
private function handlerLoaderInit (event:Event):void
{
logMessage ("Loaded, processing: " + loader.contentLoaderInfo.url);
var urlVars:URLVariables = new URLVariables ();
urlVars.decode (loader.contentLoaderInfo.url.split("?")[1]);
logMessage ("Processed:-");
logMessage ("\t\t video_id:" + urlVars.video_id);
logMessage ("\t\t t param:" + urlVars.t);
logMessage ("\t\t thumbnail-url:" + urlVars.iurl);
var flvURL:String = constructFLVURL (urlVars.video_id, urlVars.t);
logMessage ("YouTube FLV URL: " + flvURL);
playVideo (flvURL);
logMessage ("Started Playing Video...");
loader.unload();
}
private function constructFLVURL (video_id:String, t:String):String
{
var str:String = "http://www.youtube.com/get_video.php?";
str += "video_id=" + video_id;
str += "&t=" + t;
return str;
}
private function playVideo (url:String):void
{
player.source = url;
player.play();
}
private function logMessage (message:String):void
{
outputText.text += message + "\n";
trace (message);
}
]]>
</mx:Script>
<mx:HBox width="100%">
<mx:Label text="YouTube Video URL:"/>
<mx:TextInput id="urlText" width="300"/>
<mx:Button label="Submit" click="startLoading ()" id="submitButton"/>
</mx:HBox>
<mx:HBox width="100%">
<mx:VBox height="100%">
<mx:Label text="VideoDisplay:"/>
<mx:VideoDisplay width="320" height="240" id="player"/>
</mx:VBox>
<mx:VBox height="100%" width="100%">
<mx:Label text="Output:-"/>
<mx:TextArea id="outputText" width="100%" height="100%"/>
</mx:VBox>
</mx:HBox>
</mx:Application>

ActionScript 2.0 code-snippet:-


createEmptyMovieClip ("mc", getNextHighestDepth ());
var _mcl:MovieClipLoader = new MovieClipLoader ();
_mclListener = new Object ();
_mclListener.onLoadStart = function (target:MovieClip)
{
trace ("The URL is: " + target._url);
var _lv:LoadVars = new LoadVars ();
_lv.decode (target._url.split ("?")[1]);
trace ("t param: " + _lv.t);
trace ("video_id: " + _lv.video_id);
trace ("thumbnail: " + _lv.iurl);
trace ("flv url: " + constructFLVURL (_lv.video_id, _lv.t));
_mcl.unloadClip (target);
};
function constructFLVURL (videoId:String, t:String):String
{
var str:String = "http://www.youtube.com/get_video.php?";
str += "video_id=" + videoId;
str += "&t=" + t;
return str;
}
_mcl.addListener (_mclListener);
_mcl.loadClip ("http://www.youtube.com/v/3IcwG0jUFxU", mc);

 [1] Flash clients would require server-side proxy because YouTube changed crossdomain.xml sometimes back.