This is a very crude hack I just whipped together to save You-Tube videos. It uses
Gtk2::WebKit, and a hack to pull the cached flv files out of the /tmp directory. My hack to detect files is very crude, and is full of possible pitfalls, but it works as a simple demo. What happens is that Gtk2::Webkit will respect the javascript, and the cws flash loader file, and automatically load the flash video. The problem comes in detecting the writing of the cached video file in /tmp.
Anyways... it is getting harder and harder to automatically save videos from You-Tube servers, but this works. Gtk2::WebKit::Download would seem to be a better way to do it more directly, but it dosn't seem to respect the javascript or flash loader.... but someone else may know a way.
The Webkit browser will delete the cache files automatically upon exit, so I copy it to the pwd.
UPDATE: As an after thought, I did very little testing of this on big video downloads. So my method of saving the video as soon as the cache file is detected, may get you some truncated videos, as that cache file probably grows as buffering occurs. The answer of course, is maybe to just have a save button
that lets you save the video after you have seen it fully load once.... or maybe tap into the progress activity somehow, as in Perl Web Browser using Gtk2 and WebKit..... all I wanted to mention is that this script works as is for small videos, and the javascript and cws loader involved, make it quite a complex beast to tame. :-)
#!/usr/bin/perl
use strict;
use warnings;
use Gtk2 -init;
use Gtk2::WebKit;
use File::Copy;
my $url = 'http://www.youtube.com/watch?v=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";
# 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 );
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 find_save{
my $timer = Glib::Timeout->add (1000, sub {
# check for flash file every second
# to allow flash loader to load file
print "test for file\n";
opendir(my $dir, $tmpdir) or die("ack: $!");
my @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 "success\n";
foreach my $flash(@files){
print "$flash\n";
copy("$tmpdir/$flash","$pwd/$0-$flash".'.flv')
or warn "Can not copy$!\n";
}
return 0; # end timer
}else{ return 1 } # keep timer going
});
}