Home > Flash and Actionscript, Macromedia Flex > Constructing YouTube FLV URL on client-side without any server-side script

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.

  1. July 23rd, 2008 at 09:44 | #1

    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

  2. abdulla
    September 4th, 2008 at 17:15 | #2

    how to play yahoo video url in flash (actionscript). i got the example for loading you tube url.so plz help me

  3. maloola
    September 9th, 2008 at 20:26 | #3

    what about using the youtube flvs for capturing the bitmap (bitmap.draw()) ? is this allowed without a proxy? don’t want to use one..

  4. Mincheol Lee
    September 30th, 2008 at 07:55 | #4

    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!

  5. rony
    October 23rd, 2008 at 21:37 | #5

    Suddenly we don’t get the ‘t’ in the urlVars. does someone know what is the new way to retrieve it?

  6. Andy Milburn
    October 24th, 2008 at 01:37 | #6

    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?

  7. Greg
    October 24th, 2008 at 01:48 | #7

    Yep!
    Seems to no longer work.
    Would be nice to have an update, … but …
    Greg

  8. October 24th, 2008 at 01:49 | #8

    Yep!
    Seems to no longer work.
    Would be nice to have an update, … but …
    Greg

  9. Seraphim
    October 24th, 2008 at 23:28 | #9

    Please! Someone help! What is new format???

  10. ural
    October 25th, 2008 at 02:48 | #10

    help…
    doesn’t work now! t urlVars = undefined

  11. kay
    October 26th, 2008 at 13:24 | #11

    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 :-)

  12. gaurav
    October 27th, 2008 at 14:17 | #12

    Yes it doesn’t work now.

  13. Brian
    October 27th, 2008 at 23:58 | #13

    I have found a solution to this issue. Hopefully the following steps will work for you as well.

    1. Get the URL to the clip you wish to load. This URL must be of the form http://www.youtube.com/watch?v=dMH0bHeiRNg. If you are getting the URLs from the standardfeeds url (http://gdata.youtube.com/feeds/api/standardfeeds/), the URL to the clip is the media:player item
    2. Load this URL and scan the source of the page for the “watch_fullscreen” URL. It will look something like this:
        
      var fullscreenUrl = ‘/watch_fullscreen?fs=1…
    3. Take everything after “watch_fullscreen?” and save it to a variable (we’ll use params)
    4. Generate your FLV url as follows:

        finalURL = “http://www.youtube.com/get_video.php?” + params;

      And there you go. Hope this works for you.

  14. jean-marc
    October 28th, 2008 at 06:32 | #14

    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…

  15. Yes
    October 29th, 2008 at 01:56 | #15

    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.

  16. owen
    October 29th, 2008 at 04:24 | #16

    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.

  17. mincheol Lee
    October 29th, 2008 at 08:40 | #17

    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?

  18. October 30th, 2008 at 04:03 | #18

    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

  19. October 30th, 2008 at 04:06 | #19

    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

  20. October 30th, 2008 at 12:09 | #20

    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

  21. November 4th, 2008 at 02:28 | #21

    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/

  22. January 16th, 2009 at 02:32 | #22

    comment obtenir l’url flv de youtube avec un script php

  23. Bora
    February 11th, 2009 at 12:03 | #23

    Notice: Undefined offset: 1 in dm.php on line 77
    This script is wrong! Anyone know working code?

  24. May 19th, 2009 at 15:35 | #24

    Seems like YouTube is working against this obviously, all new php scrips work for a while then they stop working.

Comment pages
1 2 3 273