Here is an improved version that will grab an entire video by testing for stat file size increases. It may truncate videos if on a slow dialup line, because I use a 3 second timer in the file stat.... it works on slow dsl though :-)
#!/usr/bin/perl use strict; use warnings; use Gtk2 -init; use Gtk2::WebKit; use File::Copy; my $url = 'http://www.youtube.com/watch?v=EAtBki0PsC0'; #'9uDgJ9_H0gg +'; my $pwd = `pwd`; chomp $pwd; #where the flash will be cached temporarily by the browser my $tmpdir = Glib::get_tmp_dir(); print "$tmpdir\n"; # stat to monitor buffering my $old_size = 0 ; # find names like FlashGRsDw9 opendir(my $dir, $tmpdir) or die("ack: $!"); my @files = grep /^Flash[A-Za-z0-9]{6}/, readdir $dir; closedir $dir; print "initial files in /tmp-> ", @files, "\n"; my $view = Gtk2::WebKit::WebView->new; $view->signal_connect( 'notify::progress' => \&notify_progress, undef +); $view->signal_connect( 'load_finished' => \&load_finished, undef ); $view->signal_connect( 'notify::load-status' => \&notify_load_status, +undef ); my $sw = Gtk2::ScrolledWindow->new; $sw->add($view); my $win = Gtk2::Window->new; $win->set_default_size(800, 600); $win->signal_connect(destroy => sub { Gtk2->main_quit }); $win->add($sw); $win->show_all; $view->open($url ); Gtk2->main; sub notify_progress{ my $load_progress = $view->get('progress'); print "$load_progress\n"; } sub load_finished{ print "load complete\n"; find_save(); return 0; } sub notify_load_status{ print $view->get('load_status'),"\n"; } sub find_save{ # this is just to detect the cache filename my $timer = Glib::Timeout->add (1000, sub { # check for file every second # to allow flash loader to load file print "test for file\n"; my @files; opendir(my $dir, $tmpdir) or die("ack: $!"); @files = grep /^Flash[A-Za-z0-9]{6}/, readdir $dir; closedir $dir; print "files-> ", @files, "\n"; print scalar @files, "\n"; if( scalar @files >= 1 ){ print "found file success\n"; watch_save( $files[0] ); return 0; # end timer }else{ return 1 } # keep timer going }); } sub watch_save{ my $file = shift; my $watch = "$tmpdir/$file"; print "watching file-> $watch\n"; # this is the problemsome point, if you are on a slow # network, greater than 3 second delays may occur and trip a false EOF # but it works on my slow dsl my $timer = Glib::Timeout->add (3000, sub{ my $new_size = (stat $watch )[7]; $new_size ||= 0; print "new size-> $new_size\n"; #see perldoc -f stat #print lstat $file,"\n";; if( $old_size != $new_size ){ print "1 still buffering $old_size $new_size \n", ; $old_size = $new_size; return 1; #keep timer going }else{ #done unless there is a network hang print "flash done writing $pwd/$0-$file".'.flv',"\n"; copy("$watch" ,"$pwd/$0-$file".'.flv') or warn "Can not copy $!\n"; return 0; #end timer } }); }

I'm not really a human, but I play one on earth.
Old Perl Programmer Haiku

In reply to Re: Another YouTube Video Downloader using GtkWebkit by zentara
in thread Another YouTube Video Downloader using GtkWebkit by zentara

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.