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' => \&notify_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 }); }

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

In reply to 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.