Download a movie from YouTube with Flex
Saturday, January 3rd, 2009If 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