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: Actionscript, Download, Flex, YouTube
April 6th, 2009 at 7:34 am
I think your example doesn’t work. Have you ever tested it?
Youtube crossdomain doesn’t allow Flash remote access.
April 6th, 2009 at 9:18 am
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']"));
?>
April 6th, 2009 at 3:00 pm
you could also make use of
var urlVariables:URLVariables = new URLVariables(evt.result.toString());
in onLoadVideoInfoComplete and then simply access the token property.
April 12th, 2009 at 10:59 pm
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']));
}
April 12th, 2009 at 11:14 pm
I am happy to hear this.
April 29th, 2009 at 1:42 pm
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
April 29th, 2009 at 1:48 pm
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.
April 29th, 2009 at 1:59 pm
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”
May 14th, 2009 at 12:40 pm
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”)
August 16th, 2009 at 8:35 pm
This is not working anymore. The YouTube API must have changed.
The http://www.youtube.com/get_video.php request returns 0 bytes loaded.
November 10th, 2009 at 5:26 am
good article, thanks.