Debian Codegraph @

script/youtube-download

raw
1 #!perl
2
3 # PODNAME: youtube-download.pl
4
5 use strict;
6 use warnings;
7 use Getopt::Long qw(GetOptions :config bundling);
8 use Pod::Usage qw(pod2usage);
9 use WWW::YouTube::Download;
10 use Encode qw(find_encoding decode_utf8);
11 use Time::HiRes;
12
13 my $encode = 'utf8';
14 my $overwrite = 0;
15 my $verbose = -t STDOUT ? 1 : 0; # don't be verbose on dump terminal
16 my $interval = 1; # sec
17 my $proxy = undef;
18 my $interactive = undef;
19 my $progress = 0;
20 GetOptions(
21 'C|no-colors!' => \my $disable_colors,
22 'U|url!' => \my $playback_url,
23 'o|output=s' => \my $output,
24 'F|fmt=i', => \my $fmt,
25 's|skip' => \my $skip,
26 'n|dry-run' => \my $dry_run,
27 'user-agent=s' => \my $user_agent,
28 'v|verbose!' => \$verbose,
29 'i|interval=i' => \$interval,
30 'a|interactive' => \$interactive, # 'a' for 'ask', 'i' was taken
31 'e|encode=s' => \$encode,
32 'f|force!' => \$overwrite,
33 'p|proxy=s' => \$proxy,
34 'P|progress!' => \$progress,
35 'q|quiet!' => sub { $verbose = 0 },
36 'h|help' => sub { help() },
37 'm|man' => sub { pod2usage(-verbose => 2) },
38 'V|version!' => sub { show_version() },
39 ) or help();
40 challeng_load_argv_from_fh() unless @ARGV;
41 help() unless @ARGV;
42
43 my $encoder = find_encoding($encode) or throw("not supported encoding: $encode");
44 $output = $encoder->decode($output) if $output;
45
46 my $client = WWW::YouTube::Download->new;
47
48 if ($proxy) {
49 $client->{ua}->proxy(['http','https'] => $proxy);
50 print "--> Using proxy $proxy\n";
51 }
52
53 if ($user_agent) {
54 $client->{ua}->agent($user_agent);
55 print "--> Using user-agent-string: \"".$client->{ua}->agent()."\"\n";
56 }
57
58 main: {
59 while (@ARGV) {
60 my $video_id = shift @ARGV;
61 my $meta_data = $client->prepare_download($video_id);
62 chatty("--> Working on $meta_data->{video_id}");
63
64 ASK:
65 if($interactive){
66 print "YouTube tells us these formats are available:\n";
67 print " format-tag \tresolution \tsuffix\n";
68 print "-----------------------------------------------\n";
69 for(sort keys %{ $$meta_data{video_url_map} }){
70 my $ref = ${ $$meta_data{video_url_map} }{$_};
71 print " $ref->{fmt} \t\t$ref->{resolution} \t$ref->{suffix}\n";
72 }
73 print "Your selection (currently: ". ($fmt ? $fmt : '"best"/default' )."): ";
74 $fmt = <>; chomp($fmt);
75 }
76
77 if ($fmt && !$client->_is_supported_fmt($video_id, $fmt)) {
78 if($interactive){
79 print "[$meta_data->{video_id}] this video is not offered in fmt: $fmt\n";
80 goto ASK if $interactive;
81 }
82 throw("[$meta_data->{video_id}] this video is not offered in fmt: $fmt");
83 }
84
85 if ($playback_url) {
86 my $p_url = $client->playback_url($video_id, { fmt => $fmt });
87 chatty("Playback URL: $p_url");
88 next;
89 }
90
91 # multibyte fixes
92 my $filename = $client->_format_filename($output, {
93 video_id => $meta_data->{video_id},
94 user => $meta_data->{user},
95 resolution => $meta_data->{resolution},
96 # title => decode_utf8($meta_data->{title}),
97 title => $meta_data->{title}, # todo: as of Aug 2020, I've encountered "wide character" errors here, fix was to remove decode_utf8(), todo is investigate why
98 suffix => $fmt ? $meta_data->{video_url_map}{$fmt}{suffix} : $meta_data->{suffix},
99 fmt => $fmt || $meta_data->{fmt},
100 });
101 $filename = filename_normalize($filename);
102 $filename = $encoder->encode($filename, sub { sprintf 'U+%x', shift });
103
104 if ($dry_run) {
105 print "$filename\n";
106 next;
107 }
108
109 if ($skip && -e $filename) {
110 print "Skipping existing file: $filename\n";
111 next;
112 }
113
114 eval {
115 $client->download($video_id, {
116 filename => $filename,
117 fmt => $fmt,
118 verbose => $verbose,
119 overwrite => $overwrite,
120 progress => $progress,
121 });
122 };
123 if (my $e = $@) {
124 unlink $filename if -e $filename && !-s $filename;
125 throw("[$meta_data->{video_id}] $e");
126 }
127
128 chatty(pcolor(['green'], 'Download successful!'));
129
130 Time::HiRes::sleep($interval) if @ARGV;
131 }
132 }
133
134 exit;
135
136 sub challeng_load_argv_from_fh {
137 return unless $0 ne '-' && !-t STDIN;
138
139 # e.g. $ youtube-download < video_list
140 while (defined (my $line = <STDIN>)) {
141 chomp $line;
142 $line =~ s/#.*$//; # comment
143 $line =~ s/^\s+|\s+$//g; # trim spaces
144 push @ARGV, $line;
145 }
146 }
147
148 sub filename_normalize {
149 my $filename = shift;
150 $filename =~ s#[[:cntrl:]]##smg; # remove all control characters
151 $filename =~ s#^\s+|\s+$##g; # trim spaces
152 $filename =~ s#^\.+##; # remove multiple leading dots
153 $filename =~ tr#"/\\:*?<>|#'\-\-\-_____#; # NTFS and FAT unsupported characters
154 return $filename;
155 }
156
157 sub throw {
158 die pcolor(['red'], 'ERROR: ', @_), "\n";
159 }
160
161 sub chatty {
162 print @_, "\n";
163 }
164
165 sub pcolor {
166 my ($color, @msg) = @_;
167
168 if ($^O eq 'MSWin32' || $disable_colors || !-t STDOUT) {
169 return @msg;
170 }
171
172 eval { require Term::ANSIColor };
173 return @msg if $@; # module not available
174 return Term::ANSIColor::colored($color, @msg);
175 }
176
177 sub show_version {
178 print "youtube-download (WWW::YouTube::Download) version $WWW::YouTube::Download::VERSION\n";
179 exit;
180 }
181
182 sub help {
183 print << 'HELP';
184 Usage:
185 youtube-download [options] video_id_or_video_url ...
186
187 Options:
188 -C, --no-colors Disable colored output
189 -o, --output Output filename, supports `{$value}` format
190 -e, --encode File system encoding (e.g. cp932)
191 -F, --fmt Video quality (SEE ALSO Wikipedia)
192 -f, --force Force overwrite output file
193 -s, --skip Skip download when output file exists
194 -i, --interval Download interval
195 -p, --proxy Use the stated proxy
196 -n, --dry-run Do not download any videos, print target filenames
197 -P, --progress Show progress bar if you have Term::ProgressBar
198 -v, --verbose Turns on chatty output (default: enabled)
199 -q, --quiet Turns off progress
200 -U, --url Display playback URL for a video
201 -h, --help Display help
202 -m, --man Display man page
203 -V, --version Display version
204 --user-agent Supply your own user-agent string
205
206 supported `{$value}` format are:
207 {video_id} / {user} / {title} / {fmt} / {suffix} / {resolution}
208
209 Examples:
210 $ youtube-download -o "[{video_id}] {title}.{suffix}"
211 $ youtube-download -p socks://127.0.0.1:4321/ -o "{title} by {user} (youtube {video_id}).{suffix}"
212
213 HELP
214 exit 1;
215 }
216
217 =pod
218
219 =encoding UTF-8
220
221 =head1 NAME
222
223 youtube-download.pl - youtube-download - Download video(s) from YouTube
224
225 =head1 VERSION
226
227 version 0.65
228
229 =head1 SYNOPSIS
230
231 $ youtube-download bT8yLWy4B5w
232 $ youtube-download http://www.youtube.com/watch?v=bT8yLWy4B5w
233 $ youtube-download < video_list_file
234
235 =head1 OPTIONS
236
237 =over
238
239 =item -C, --no-colors
240
241 Force disable colored output
242
243 =item -o, --output
244
245 output filename, supports `{$value}` format (default: {video_id}.{suffix})
246
247 =item -i, --interval
248
249 Download interval (default: 1 (sec))
250
251 =item -p
252
253 Use the given proxy. Requires LWP::Protocol::socks to be installed for socks proxies. See examples for syntax. (default: no proxy)
254
255 =item -e, --encode
256
257 File system encoding (default: utf8)
258
259 =item -s, --skip
260
261 Skip downloading a video, if target file exists.
262
263 =item -f, --force
264
265 Force overwrite output file (default: disabled)
266
267 =item -n, --dry-run
268
269 Do not download any videos, but print their target filenames,
270 as defined by -o option. This option still sends query to
271 Google servers to fetch details about given video.
272
273 =item -F, --fmt
274
275 Video quality (SEE ALSO Wikipedia)
276
277 =item -v, --verbose
278
279 Turns on chatty output (default: enabled)
280
281 =item -U, --url
282
283 Show playback URL of a video, but do not download it
284
285 =item -P, --progress
286
287 Show progress bar if you have Term::ProgressBar
288
289 =item -q, --quiet
290
291 Turns off the most output
292
293 =item -h, --help
294
295 Display help
296
297 =item -m, --man
298
299 Display help page
300
301 =item -V, --version
302
303 Display version
304
305 =item --user-agent
306
307 Supply your own user agent string
308
309 =back
310
311 =head2 supported `{$value}` format
312
313 {video_id} / {user} / {title} / {fmt} / {suffix} / {resolution}
314
315 Example:
316 $ youtube-download -o "[{video_id}] {title}.{suffix}"
317 $ youtube-download -p socks://127.0.0.1:4321/ -o "{title} by {user} (youtube {video_id}).{suffix}"
318
319 =head1 AUTHOR
320
321 xaicron <xaicron {@} cpan.org>
322
323 =head1 COPYRIGHT AND LICENSE
324
325 This software is copyright (c) 2013 by Yuji Shimada.
326
327 This is free software; you can redistribute it and/or modify it under
328 the same terms as the Perl 5 programming language system itself.
329
330 =cut
331
332 __END__
333
334 # ABSTRACT: youtube-download - Download video(s) from YouTube
335