#!/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' => \¬ify_progress, undef ); $view->signal_connect( 'load_finished' => \&load_finished, undef ); $view->signal_connect( 'notify::load-status' => \¬ify_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 } }); }