Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: YouTube Video Downloader

by BigJoe (Curate)
on Sep 01, 2006 at 03:28 UTC ( [id://570715]=note: print w/replies, xml ) Need Help??


in reply to YouTube Video Downloader

I reposted your code because I needed to pass on the file name to the ffmpeg program. BTW this require ffmpeg. I pulled this from a Ubuntu blog (http://www.jadmadi.net/2006/06/22/ubuntu-howto-converting-flv-to-mpg/)

I had just setup a shell script for this and I would just run it on all the files in the directory but this seems a little easier.
#!/usr/bin/perl use warnings; use LWP::Simple; use LWP::UserAgent; # shamelessly reversed engineered from a python script :-) print "startring first page retrieval\n"; my $urlin = shift || "http://youtube.com/watch?v=EkTpUxh8Vxc"; my $content = get( $urlin ) or die "$!\n"; print "done first page retrieval\n"; #print $content; # regex for 2 key text strings which identify the video file # the second one $2 is unique for each download attempt $content =~ /player2\.swf\?video_id=([^&]+)&.*t=([^&]+)&/ ; print $1, "\n" , $2, "\n"; my $infile = $1.'.flv'; #add a .flv extension my $filename = $1; #http://www.youtube.com/get_video?video_id=p_YMigZmUuk&t=OEgsToPDskLRl +9-iKyfQVcNT8xes2OIT my $get_url = 'http://www.youtube.com/get_video?video_id='.$1.'&t='.$2 +; print "gettin video file $get_url\n"; # don't buffer the prints to make the status update $| = 1; open(IN,"> $infile") or die "$_\n"; my $ua = LWP::UserAgent->new(); my $received_size = 0; my $url = $get_url; print "Fetching $url\n"; my $request_time = time; my $last_update = 0; my $response = $ua->get($url, ':content_cb' => \&callback, ':read_size_hint' => 8192, ); print "\n"; close IN; #play the flv file with mplayer #system( "mplayer $infile" ); my $return_val = `ffmpeg -i $filename.flv -ab 56 -ar 22050 -b 500 -s +320x240 $filename.mpg`; ############################################# sub callback { my ($data, $response, $protocol) = @_; my $total_size = $response->header('Content-Length') || 0; $received_size += length $data; # write the $data to a filehandle or whatever should happen # with it here. print IN $data; my $time_now = time; # this to make the status only update once per second. return unless $time_now > $last_update or $received_size == $total_s +ize; $last_update = $time_now; print "\rReceived $received_size bytes"; printf " (%i%%)", (100/$total_size)*$received_size if $total_size; printf " %6.1f/bps", $received_size/(($time_now-$request_time)||1) if $received_size; } __END__


--BigJoe

Learn patience, you must.
Young PerlMonk, craves Not these things.
Use the source Luke.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://570715]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (7)
As of 2024-04-23 20:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found