Download a movie from YouTube with Flex

If you search in Google “download a youtube video” you will find a lot of tools that help you to download a video from YouTube but if you want to make your own application in Flex that download a YouTube movie or you just want to use the VideoDisplay component from Flex to play a YouTube movie this is the post you must read. So this app will be totaly client no use of server side. We will do all the work in Flex.

So let’s start!

First let’s study how we can download a movie and after that we will write our code in Flex. For example if we want to download this movie from YouTube (gread commercial my favorite one :D), all we have to do is to paste in our browser this link  http://youtube.com/get_video.php?video_id=670X2MCWzK0&t=OEgsToPDskIOEdLV0E46VwO32hMEmKaT

Let’s break the link:

1.  http://youtube.com/get_video.php
2.  video_id=670X2MCWzK0
3.  t=OEgsToPDskIOEdLV0E46VwO32hMEmKaT

we can see that this link has three parts. A link and two parameters. First parameter the “video_id” is the same with the “v” parameter from first link so we can get this easy but how do we get the second parameter “t” ? Let me present you the magic link :D http://youtube.com/get_video_info?video_id=670X2MCWzK0. Maybe you will ask how do I know about this link, the answere is simple I have used a very simple flash decompiler and decompile the YouTube player and that’s where I have find the link. The “video_id” in that link is the same with the “v” parameter. So with thatk link and video_id we can get all the information we need to download a movie. So the steps are:

1. From a YouTube link we extract the value of “v” parameter this is the “video_id”. (In our example 670X2MCWzK0)
2. Get video information (title, thumbnail, token) with youtube.com/get_video_info.php and video_id
3. Get video with youtube.com/get_video.php, video_id and t (t means token)

Step 1. Extract video_id from YouTube URL

private function parseURL(url:String):String
{

     var reg:RegExp = /youtube\.com\/watch\?v=([^&\/]+)/;
     var res:Object = reg.exec(url); 

     return res[1];

Step 2. Get video information

private const GET_VIDEO :String = “http://youtube.com/get_video.php”;
private const GET_VIDEO_INFO :String = “http://youtube.com/get_video_info.php”;

private function download():void
{

     var service :HTTPService = new HTTPService();
     videoID = parseURL(siteUrl.text); // siteUrl is a mx:TextInput
     
     service.url = GET_VIDEO_INFO + “?video_id=” + videoID;
     service.resultFormat = ‘object’;

     phiBusy.showBusy();

     service.addEventListener(ResultEvent.RESULT, onLoadVideoInfoComplete);
     service.send();

}

Step 3. Get Video

private function onLoadVideoInfoComplete(evt:ResultEvent):void
{
     phiBusy.removeBusy();

     var reg:RegExp = /token=([^&\/]+)/;
     var token :Object = reg.exec(evt.result);

     downloadVideo(token[1]);
}

private function downloadVideo(token:String):void
{
     navigateToURL(new URLRequest(GET_VIDEO + “?video_id=”+ videoID +”&t=”+ token));
}

And we are done. I have used navigateToURL to open a new window and download the video but if you provide that link to a VideoDisplay component the video will play.

View the example | View Source 

 

Tags: , , ,

12 Responses to “Download a movie from YouTube with Flex”

  1. Mark Jones Says:

    I think your example doesn’t work. Have you ever tested it?
    Youtube crossdomain doesn’t allow Flash remote access.

  2. Alexandru Ghiura Says:

    It works. All you need to do is not access http://youtube.com/get_video_info?video_id=670X2MCWzK0 directly access a php file mygetvideo.php?video_id=670X2MCWzK0 for ex and all the code you need to write in php is

    < ?
    echo implode("", file("http://youtube.com/get_video_info?video_id=$_GET['video_id']"));
    ?>

  3. zOrder Says:

    you could also make use of

    var urlVariables:URLVariables = new URLVariables(evt.result.toString());

    in onLoadVideoInfoComplete and then simply access the token property.

  4. Robin van Emden Says:

    Thanks a lot! All I had to do to make this work for a Flash AS3 project do is change your Download function slightly, snippet:

    private function download(siteUrl):void {
    loader = new URLLoader();
    videoID=parseURL(siteUrl);
    var serviceurl:String=mygetvideoUrl+”?video_id=”+videoID;
    var request:URLRequest=new URLRequest(serviceurl);
    request.method=URLRequestMethod.GET;
    loader.addEventListener(Event.COMPLETE, onDataLoad);
    loader.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
    loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError);
    loader.load(request);
    }

    private function onDataLoad(e:Event):void {
    var raw:String=URLLoader(e.target).data;
    var urlVariables:URLVariables=new URLVariables(raw);
    downloadVideo(escape(urlVariables['token']));
    }

  5. Alexandru Ghiura Says:

    I am happy to hear this.

  6. Pelerin Says:

    Salut, tu dupa nume pari a fi roman, am si eu o nelamurire de ce pe localhost pot afla $token si pe host imi arata ceva dar nu merge cu cel de pe host

  7. Alexandru Ghiura Says:

    Salut, sincer si eu am vazut ca acest algoritm nu mai functioneaza numai de pe local dar nu am avut inca timp sa studiez problema de acea nu iti pot da un raspuns pentru ca nici eu nu il stiu. Dar daca voi gasi unul il voi posta aici.

  8. Pelerin Says:

    OK ms , pe unele hosturi nu-l vede deloc
    nu stiu daca e chiar buna linia pe care o folosesc
    $videoid = ‘videoid’;
    parse_str(file_get_contents(”http://youtube.com/get_video_info?video_id=$videoid”));
    echo “$token”

  9. Pelerin Says:

    Salut, am gasit un script care merge sa descarci videoclipurile, imi arata si acel algoritm $token, dar tot e ceva ciudat cu el pe un calculator il vede bine si de pe altul cu acelasi browser nu mai este cel corect.
    scriptul de descarcat functioneaza bine ,dar mie imi trebuie acel token pentru altceva:

    $videoid= ‘id video youtube’;
    $content= file_get_contents(”http://youtube.com/get_video_info?video_id=$videoid”);
    parse_str($content);
    $url = “http://www.youtube.com/get_video.php?video_id=” . $videoid . “&t=” . $token. “”;
    $headers = get_headers($url);
    foreach($headers as $h){
    if(strpos($h,”googlevideo.com”)!=false){
    $url = substr($h,10);
    break;
    }
    }
    header(”location:$url”)

  10. AB Says:

    This is not working anymore. The YouTube API must have changed.

    The http://www.youtube.com/get_video.php request returns 0 bytes loaded.

  11. syler Says:

    good article, thanks.

  12. Januar Says:

    thanks for your info.,!! nice article., :)


Leave a Reply