The FLV video downloader I designed was written in PHP and some Javascript on the client side. So you’ll need to know a bit of PHP to understand how this works. It’s actually very simple, all you do is get the source from YouTube and then parse it so only the address of the .flv file is left. The following script locates the download URL and returns it. This is the core to any downloader (it’s the same concept for even Google Video downloaders and so on).
<?php
function get_video_url($id){
$url = "http://youtube.com/watch?v=".$id;
if ($contents = @file_get_contents($url)) {
if (preg_match('/video_id=\S+&.+&t=.+&f/i', $contents, $match)) {
$vars = $match[0];
$url = “http://www.youtube.com/get_video?”.$vars;
return $url;
}
}
}
?>
Line by Line Analysis
This line by line analysis of the code above should help you understand the script better.
function get_video_url($id){
This line declares a function (a repetitively usable operation) called get_video_url() so we can use it easily and efficiently. The $id is the video id from YouTube and would be a submitted value, something like FzRH3iTQPrk.
$url = "http://youtube.com/watch?v=".$id;
This line stores the youtube url will the video id in the variable $url.
if ($contents = @file_get_contents($url)) {
This line of code stored the HTML code of the YouTube page ($url) by using the file_get_contents()
function (note that some hosting services may have disabled the function). The line below it will only be executed if $contents is not empty since the if() operator is controlling it.
if (preg_match('/video_id=\S+&.+&t=.+&f/i', $contents, $match)) {
This is the core of the whole thing. It finds the required
information to get the video URL from the source code by searching the HTML code with regular expressions using the preg_match() function. This function uses regular expressions to find patterns in a variable (in this case $html) and puts the results as arrays into another variabe (in this case $match).
$vars = $match[0];
This line stores the needed information from the array $match[0] (which is the first item) into the $vars variable.
$url = "http://www.youtube.com/get_video?".$vars;
This line finally puts the information together to get the full video URL. It’s quite simple, it’s just combining the found information and http://www.youtube.com/get_video together.
return $url;
This line just returns the $url variable so we can use it later. The function can then be used by doing something like this:
echo get_video_url($url);
The rest is just closing the function and the if() operator. Try to look the function up in the PHP manual (they’re linked above).
Tips
If you want to make a downloader for other sites, try using this method:
- Get the firefox plugin: tamper data
- Go to the site with tamper data open and see what requests come in.
- Look for an xml page or an flv file.
- Look for a pattern and see how you could automate this.
- Write a script for it.
Share and Enjoy:
These icons link to social bookmarking sites where readers can share and discover new web pages.