You're viewing a comment by Jake and its responses.

February 04, 2009, 02:28

youtube must have changed its format since you posted this, since what was being returned did not have a t get variable. The t value was stored in the javascript of the page, so i used the following regex to get it:

/, "t": "([^"]+)"/

which i found out after looking at a python script youtube-dl at http://www.arrakis.es/~rggi3/youtube-dl/ but while their downloader is very robust and object oriented, this one is quick and perl-like but gets the job done. Heres one that works as of february 2009:

use WWW::Mechanize;
use Number::Bytes::Human qw(format_bytes);

for (@ARGV) {
    s{http://|www\.|youtube\.com/|watch\?|v=|}{}g;

    $m = WWW::Mechanize->new;
    $m->get("http://www.youtube.com/watch?v=$_&gl=US&hl=en");
    ($t) = $m->content =~ /, "t": "([^"]+)"/;

    open $f, "> $_.flv";
    binmode $f;

    $m->get(
        "http://youtube.com/get_video?video_id=$_&t=$t",
        ':content_cb' => sub {
            ($c, $r) = @_;
            $b += length($c);
            if ($r->content_length) {
                printf STDERR "$_: %.2f%%: %s of %s            \r",
                    100. * $b / $r->content_length,
                    format_bytes($b),
                    format_bytes($r->content_length);
            }
            print $f $c;
        });
    
    $b = 0;
    print "\n";
    close $f;
}

I combined some things that i liked from the previous posts (such as the callback).

Reply To This Comment

(why do I need your e-mail?)

(Your twitter name, if you have one. (I'm @pkrumins, btw.))

Type first 3 letters of your name: (just to make sure you're a human)

Please preview the comment before submitting to make sure it's OK.