Kai Wasserb ch: YouTube: download 1080p videos when CLive is unavailable
Todays addition to the tips series has a funny history. A friend
of mine was stuck in an envrionment, where she couldn't install Perl or CLive
but PHP with some
PECL modules was
installed. She needed to download a few videos for a presentation, but didn't
know how to fetch the videos from YouTube. So I was asked to help. ;-)
After some pondering of the problem, I came up with the following script:
<?php
/*
* Copyright (C) 2011 Kai Wasserb ch <kai@dev.carbon-project.org>
*
* Small little script to help out in the absence of Perl/Clive but with
* PHP present.
*
* License: GPLv3 <http://www.gnu.org/licenses/gpl-3.0.txt>
*
*/
if(php_sapi_name() !== 'cli')
die("This script needs to be run on the CLI!\n");
if($argc !== 2 )
die("No URL given on CLI or too much parameters!\n");
// Script expects a URL like http://www.youtube.com/watch?v=[ID]
if(filter_var($argv[1], FILTER_VALIDATE_URL, FILTER_FLAG_QUERY_REQUIRED) === false)
die("Parameter given as URL does not seem to be an URL or contain a query!\n");
if(!$ch = curl_init($argv[1]))
die("Couldn't init cURL handle for ".$argv[1]." \n");
curl_setopt($ch, CURLOPT_HEADER, false );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
$htmlsource = curl_exec($ch);
$error = curl_error($ch);
curl_close($ch);
if($htmlsource === false)
die('cURL returned an error: '.$error."\n");
unset($error);
if(!preg_match('/url_encoded_fmt_stream_map=([a-zA-Z0-9%\.,\-_]+)&/',
$htmlsource, $matches))
die("No matches found for url_encoded_fmt_stream_map!\n");
$final = NULL;
foreach(explode(',', urldecode($matches[1])) as $decoded_url_map)
parse_str($decoded_url_map, $tmp);
if($tmp['quality'] === 'hd1080')
$final = $tmp;
$final['url'] = urldecode($tmp['url']);
// $final['type'] = urldecode($tmp['type']); // Not used
break; // Not really needed, but should save us some
// iterations, since 1080p seems to come always first.
if(!is_array($final) is_null($final))
die("No 1080p version of the video was found!\n");
if(!preg_match('/<meta property="og:title" content="(.*)"/', $htmlsource, $title))
die("Couldn't determine title!\n");
$ftitle = preg_replace('/[^\w\d]*/', '', $title[1]);
printf("Found hd1080-quality video for %s .\n", $argv[1]);
printf("Downloading to %s.mp4 ...", $ftitle);
if(!$ch=curl_init($final['url']))
die("\nCouldn't create cURL handle for ".$final['url']." \n");
if(!$fh = fopen(getcwd().DIRECTORY_SEPARATOR.$ftitle.'.mp4', 'wb'))
curl_close($ch);
die("\nCouldn't create ".$ftitle.".mp4 .\n");
curl_setopt($ch, CURLOPT_HEADER, false );
curl_setopt($ch, CURLOPT_FILE, $fh );
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true );
if(!curl_exec($ch))
$error = curl_error($ch);
fclose($fh);
curl_close($ch);
die("\nCouldn't download video. cURL error: ".$error."\n");
curl_close($ch);
fclose($fh);
printf(" done.\n");
?>
You can download it from my website or you can just copy the
code given above into a file. However you get the code, the execution will look
like: php -f /path/to/script.php
http://www.youtube.com/watch?v=[ID].
Now, before I get a lot of e-mails telling me, what could be improved on the
script or its other limitations (like only being able to download 1080p videos),
please remember that it was hacked together for a simple use case with no real
intent on my part to use it for anything else. It only later occurred to me,
that others might be in a similar pinch sometime. Thus I'm releasing the above
code "as is" under the GNU GPLv3. Therefore you're
free to adapt it to your needs. Please also note, that I'm not recommending
using the above script instead of CLive. If you can install CLive, do so and
don't bother with the above.
Hello, this is the fourth report for the project. First, I d like to
Together with
This is my monthly summary of my Debian related activities. If you re among the people who
This month passed by very quickly since I attended both the
Sevilla is located in the (one of the many actually) oh-boy-is-that-really-the-temperature region of Spain. It has been historically significative for its financial, artistic and cultural value.
But it s most important role has been that of hosting lots of GNOME activity. The 3rd ever GUADEC was held here, those cool 

Another year, another Debconf and now it is passed. Pictures are processes and all are now 
Boy, the last three months have been very eventful, developmentally.
First off, about 2 weeks after she turned 1, Zoe started walking in earnest.
Within about 2 weeks of that there was no stopping her. It's funny to think
she's only been walking for under 3 months and yet she's very stable on her
feet.
Secondly, she developed a mild egg allergy. Sarah was giving her some
scrambled eggs (one egg's worth) for lunch one day, and she mixed in some
cream cheese, and Zoe absolutely scoffed it all down, so she scrambled
another egg and she scoffed that down too. She then 
Debian GNU/kFreeBSD was first released with Squeeze in last february. The technology preview label indicated, among other things, that it had a number of limitations when compared with what users would expect: missing features, incomplete functionality, etc.
But it has seen many noteworthy improvements since then. Here are some that I would like to mention:
It's sometimes necessary to be able to access HTTP resources from
inside VCL. Some use cases include authentication or authorization
where a service validates a token and then tell Varnish whether to
proceed or not.
To do this, we recently implemented 
I have just bought a gamepad, the Saitek
Been hot and dry today (although the sky is now back full of
dark clouds), so I had a headache most of the morning until way
past noon. Better now though, and I found a place where I could
get Cevapi, which are really some sort of quick imbiss / fast
food here (no uve pirina though, and she didn t have any
Ajvar nor did she speak any language other than the local, but
that wasn t a problem, only a bit dry because I didn t give in
and took the offered Ketchup). Bought a 1 bottle of Kru kovac
(from Hrvatska, though) and some small plastic glasses, then.
I wonder how many people would, now, be willing to give Bosna
i Hercegovina a try as holiday region (which might have been the
intent of having a Balkan DebConf). I m sure I do.
To all attendees: the hotel will give you some kind of stamped
hardpaper card which states where you stayed on the trip, and for
how long give that to the border guards when exiting Bosnia.
Many people still install Linux from CDs. But a growing number install from USB. In an ideal world you'd be able to download one image that would let you do either, but it turns out that that's quite difficult. Shockingly enough, it's another situation where the system firmware exists to make your life difficult.