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:-
- YouTube! video URL, which looks this (video_id in red):
- YouTube RSS feeds [1]
- YouTube API [1]
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.



Hey guys! i just found this website and it seems to be the best and simple youtube flash video player /ripper so far. You can even put your own logo in the videos.
enjoy guys
http://www.youtubeflashplayer.com
how to play yahoo video url in flash (actionscript). i got the example for loading you tube url.so plz help me
what about using the youtube flvs for capturing the bitmap (bitmap.draw()) ? is this allowed without a proxy? don’t want to use one..
Hi!
I just started to learn flex, and I am working on developing YouTubeWidget. and It looks like this converting feature would be great help to me.
However, it doesn’t work to me. The link generated from here doesn’t work with VideoDisplay. Also, your sample also doesn’t work.
Is this out of date formula for converting URL to Flv URL?
Thank you!
Suddenly we don’t get the ‘t’ in the urlVars. does someone know what is the new way to retrieve it?
This hack, after working reliably for a year, appears to have been broken by a change in Youtube format. Can anyone else confirm this? Any workarounds yet?
Yep!
Seems to no longer work.
Would be nice to have an update, … but …
Greg
Yep!
Seems to no longer work.
Would be nice to have an update, … but …
Greg
Please! Someone help! What is new format???
help…
doesn’t work now! t urlVars = undefined
Hi It was so useful for me. But since last week the program doesn`t work. I think the U-tube change url method or thet don`t want to acess like this way. I really want to use this program again. so do U have another way to use it?? thank you for you`re kindness Bye
Yes it doesn’t work now.
I have found a solution to this issue. Hopefully the following steps will work for you as well.
 Â
var fullscreenUrl = ‘/watch_fullscreen?fs=1…
  finalURL = “http://www.youtube.com/get_video.php?” + params;
And there you go. Hope this works for you.
They changed the code on their server.
Even though I can get the t var and perform a GET on the get_video url, i get a 404 not found on the download.
But if I use a web browser and view the source, and manually extract the t variable, the download link works.
Maybe it’s something to do with cookies…
Yes, youtube’s broken the “t=” method. Not sure how to resolve this. They’re saying that downloading videos breaks the “Terms of Service” agreement.
Hmm… I’m trying to write a workaround for this right now. The AS3 chromeless API might work, as long as you don’t mind using the external interface controls for your embedded player.
Hi!
Your blog helped me to develop Youtube widget player. Thank you!
As other people said, the url format had been changed. Does anyone find the new way of getting flv url?
use this way to call the video-url. First call http://www.youtube.com/watch?video, you should return the result in AS2/3 as a text (html), then search for the text swfargs = and decode the variables in this string, then you can rebuild the right url again, I’ve got it working on my own site
download the html page in as3 (www.youtube.com/watch?v=…..), search for swfargs= and split and decode the parameters behind this string, then you can compose the video-url again, it works on my site
Hi Guys
Sorry for not responding to your comments, seems it’s time for an updated post for latest workaround.
I have been busy with many things, one of those is Chromeless AS3 Player (YouTube) Wrapper – It uses ExternalInterace or LocalConnection (both options would be there).
Once it’s ready, I would post it.
Thanks
-abdul
This technique to get the FLV URL indeed no longer work. You now have to load the swf web page and extract the parameters from there. I’ve posted the source code to do that on my blog:
http://pogopixels.com/blog/getting-the-url-of-a-youtube-flv-file-in-flash/
comment obtenir l’url flv de youtube avec un script php
Notice: Undefined offset: 1 in dm.php on line 77
This script is wrong! Anyone know working code?
Seems like YouTube is working against this obviously, all new php scrips work for a while then they stop working.