forked from monsieurvideo/get-flash-videos
/
get_flash_videos
executable file
·296 lines (236 loc) · 7.84 KB
/
get_flash_videos
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#!/usr/bin/perl
#
# get_flash_videos -- download all the Flash videos off a web page
#
# http://code.google.com/p/get-flash-videos/
#
# Copyright 2009, zakflash and MonsieurVideo
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain a
# copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
# Contributions are welcome and encouraged, but please take care to
# maintain the JustWorks(tm) nature of the program.
use strict;
use Getopt::Long;
use FlashVideo::URLFinder;
use FlashVideo::Downloader;
use FlashVideo::RTMPDownloader;
use FlashVideo::Utils;
use FlashVideo::GoogleVideoSearch;
use constant VERSION => "1.11";
use constant VER_INFO => <<EOF;
get_flash_videos version @{[VERSION]} (http://code.google.com/p/get-flash-videos/)
EOF
use constant USAGE => VER_INFO . <<EOF;
Usage: $0 [OPTION]... url
Options:
-d --debug Print extra debugging information.
-f --filename Filename to save the video as.
-p --play Start playing the video once enough has been downloaded.
--player Player to use for the video (default: mplayer -really-quiet %s).
-q --quiet Be quiet (only print errors).
-u --update Update to latest version.
-v --version Print version.
-y --yes Say yes to any questions (don't prompt for any information).
EOF
use constant REQ_INFO => <<EOF;
A required Perl module for downloading this video is not installed.
EOF
use constant FRIENDLY_FAILURE => <<EOF;
Couldn't extract Flash movie URL. This site may need specific support adding,
or fixing.
Please confirm the site is using Flash video and if you have Flash available
check that the URL really works(!).
Check for updates by running: $0 --update
If the latest version does not support this please open a bug (or
contribute a patch!) at http://code.google.com/p/get-flash-videos/
EOF
our %opt = (
yes => 0,
filename => '',
version => 0,
update => 0,
play => 0,
player => "mplayer -really-quiet %s",
debug => 0,
quiet => 0,
);
read_conf();
GetOptions(
"yes|y" => \$opt{yes},
"filename|f" => \$opt{filename},
"version|v" => \$opt{version},
"update|u" => \$opt{update},
"help|h" => \$opt{help},
"play|p" => \$opt{play},
"player=s" => \$opt{player},
"debug|d" => \$opt{debug},
"quiet|q" => \$opt{quiet},
);
if($opt{version}) {
die VER_INFO;
} elsif($opt{update}) {
exit update();
} elsif($opt{help}) {
die USAGE;
}
binmode STDERR, ":utf8";
my (@urls) = @ARGV;
my $url = $urls[0];
$url or die USAGE;
# Search string can either be quoted or unquoted (for ultimate laziness)
my $search;
if ( ((@urls == 1) and $url !~ m'\.') or @urls > 1) {
$search = join ' ', @urls;
}
if ($search) {
if (my @results = FlashVideo::GoogleVideoSearch::search($search)) {
if ($opt{yes} or @results == 1) {
my $message = (@results == 1) ?
"Downloading only match for '$search': '$results[0]->{name}'" :
"Downloading first match for '$search': '$results[0]->{name}'" ;
info $message;
$url = $results[0]->{url};
}
else {
print "Search for '$search' found these results:\n";
my $count = 1;
foreach my $result (@results) {
print sprintf("[%2d]", $count) . " $result->{name}\n";
$count++;
}
print "Enter the number of the video you want to download: ";
chomp(my $choice = <STDIN>);
$choice--;
if (!$results[$choice]) {
print STDERR "'$choice' is an invalid choice.\n";
exit;
}
$url = $results[$choice]->{url};
}
}
else {
print STDERR "No results found for '$search'.\n";
exit;
}
}
$url = "http://$url" if $url !~ m!^\w+:!;
# Might be downloading from a site that uses Brightcove or other similar
# Flash RTMP streaming server. These are handled differently. Need to get
# the page to determine this.
info "Downloading $url";
my $browser = FlashVideo::URLFinder::get_browser();
$browser->get($url);
# (Redirect check is for Youtube which sometimes redirects to login page
# for "mature" videos.)
if (!$browser->success and !$browser->response->is_redirect) {
error "Couldn't download '$url': " . $browser->response->status_line;
}
# Figure out what package we need to use to get either the HTTP URL or
# rtmpdump data for the video.
my($package, $possible_url) = FlashVideo::URLFinder::find_package($url, $browser);
info "Using method '" . lc((split /::/, $package)[-1]) . "' for $url";
my ($actual_url, @suggested_fnames);
eval {
($actual_url, @suggested_fnames) =
$package->find_video($browser, $possible_url);
};
if(!$actual_url) {
if($@ =~ /^Must have | requires /) {
my $error = "$@";
$error =~ s/at $0.*//;
die "$error" . REQ_INFO;
} else {
die "Error: $@" . FRIENDLY_FAILURE;
}
}
my $suggested_filename = $suggested_fnames[-1];
if (!$opt{play}) {
if (!$opt{yes} && @suggested_fnames > 1) {
print "There are different suggested filenames, please choose:\n";
my $count;
foreach my $filename (@suggested_fnames) {
$count++;
print "$count - $filename\n";
}
print "\nWhat filename would you like to use: ";
chomp(my $chosen_fname = <STDIN>);
$suggested_filename = $suggested_fnames[$chosen_fname - 1] ||
$suggested_fnames[-1];
}
}
my $save_as = $opt{filename} || $suggested_filename;
my $action = $opt{play} ? "play" : "download";
for my $data((ref($actual_url) eq 'ARRAY' ? @$actual_url : $actual_url)) {
if(ref $data eq 'HASH') {
# RTMP data
FlashVideo::RTMPDownloader->new()->$action($data) || exit 1;
} else {
# HTTP
FlashVideo::Downloader->new->$action($data, $save_as, $browser) || exit 1;
}
}
exit 0;
sub read_conf {
for my $file("/etc/get_flash_videosrc", "$ENV{HOME}/.get_flash_videosrc") {
open my $fh, "<", $file or next;
while(<$fh>) {
s/\r?\n//;
next if /^\s*(#|$)/;
my($n, $v) = split /\s*=\s*/;
$v = 1 unless defined $v;
$opt{$n} = $v;
}
}
}
sub update {
# SCRIPT_NAME is some magic set by combine-perl
die "This is a development version" unless $::SCRIPT_NAME;
my $browser = FlashVideo::URLFinder::get_browser();
$browser->get("http://get-flash-videos.googlecode.com/svn/wiki/Version.wiki");
if(!$browser->response->is_success) {
die "Unable to retrieve version data: " . $browser->response->status_line;
}
my $version = ($browser->content =~ /version: (\S+)/)[0];
my $base = ($browser->content =~ /from: (\S+)/)[0];
my $info = ($browser->content =~ /info: (\S+)/)[0];
my $url = $base . $::SCRIPT_NAME . "-" . $version;
die "Unable to parse version data" unless $version and $base;
# Split version on . and compare..
my @v = split /\./, $version;
my @V = split /\./, VERSION;
my $newer = 0;
my $i = 0;
for(@v) {
$newer = 1 if !defined $V[$i] || $_ > $V[$i];
last if $V[$i] > $v[$i];
$i++;
}
if($newer) {
info "Newer version ($version) available, downloading..";
die "Cannot update -- unable to write to $0\n" unless -w $0;
my $new_file = $0 . ".new";
$browser->mirror($url, $new_file);
if($browser->response->is_success && -f $new_file) {
rename $0, "$0.old" or die "Unable to rename $0 to $0.old: $!";
rename $new_file, $0 or die "Unable to rename $new_file to $0: $!";
chmod 0755, $0;
info "New version installed as $0";
info "(previous version backed up to $0.old).";
info $info;
exit 0;
} else {
die "Download failed: " . $browser->response->status_line;
}
} else {
die "You already have the latest version.\n";
}
}