Debian Codegraph @

eg/menu.pl

raw
1 use strict;
2 use warnings;
3 use WWW::YouTube::Download;
4
5 my $url = shift || 'http://www.youtube.com/watch?v=w1IJiAXjj7k';
6
7 my $client = WWW::YouTube::Download->new();
8 my $data = $client->prepare_download($url);
9 my $umap = $data->{video_url_map};
10 my @alternatives = @{$umap}{sort { $a <=> $b } keys %$umap};
11
12 $|++;
13 my $choice;
14 while (!defined $choice) {
15 print {*STDOUT} "Available choices:\n";
16 for my $alt (@alternatives) {
17 printf {*STDOUT} "%3d - %4s - resolution %s\n",
18 @{$alt}{qw< fmt suffix resolution >};
19 }
20 print {*STDOUT} "Your choice: ";
21 (my $input = <STDIN>) =~ s/\s+//gmxs;
22 if (($input =~ m{^(?: 0 | [1-9]\d*)$}mxs) && exists $umap->{$input}) {
23 $choice = $input;
24 }
25 else {
26 print {*STDOUT} "Invalid choice\n\n";
27 }
28 } ## end while (!defined $choice)
29
30 # Ready to download
31 my $filename = "$data->{video_id}.$umap->{$choice}{suffix}";
32 $client->download($url, {fmt => $choice, filename => $filename});
33 print "saved '$filename'\n";