-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget-arte+7.pl
188 lines (166 loc) · 5.44 KB
/
get-arte+7.pl
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
#!/usr/bin/env perl -w
use strict;
use warnings;
use Mozilla::CA;
use LWP::UserAgent;
use List::Util qw(any);
use File::Basename qw(dirname basename);
use JSON::PP qw(decode_json);
my $dryRun = 0;
my $videoQuality = 720;
while (scalar @ARGV > 0) {
if ($ARGV[0] eq '--help' or $ARGV[0] eq '-h') {
usage();
exit(0);
} elsif ($ARGV[0] eq "--dry-run") {
$dryRun = 1;
shift;
} elsif ($ARGV[0] eq "--videoQuality") {
shift;
$videoQuality = $ARGV[0];
shift;
} elsif ($ARGV[0] eq "--") {
shift;
last;
} else {
last;
}
}
my $pageUrl = shift;
my $outFile = shift;
sub usage {
print STDERR "Usage:\n";
print STDERR "\n";
print STDERR " $0 [options] <pageUrl> [<outputFile>]\n";
print STDERR "\n";
print STDERR "Options:\n";
print STDERR " --dry-run Fetch/parse URLs but do not download medias\n";
print STDERR " --videoQuality <quality> Video quality (e.g. '720', '1080', etc.)(default is '720')\n";
print STDERR "\n";
}
if (not defined $pageUrl) {
usage();
exit(0);
}
if (not defined $outFile) {
($outFile) = ($pageUrl =~ m|.*/([^/]+)/?$|);
if (not defined $outFile) {
print STDERR sprintf("Error: could compute output filename from page URL '%s'!\n", $pageUrl);
exit(1);
}
$outFile =~ s/\?.*$//i;
$outFile = sprintf("%s.mp4", $outFile);
printf("Output to '%s'\n", $outFile);
}
if (-e $outFile) {
printf("Output file '%s' already exists!\n", $outFile);
printf("Skipping download.\n");
exit(0);
}
my ($videoId) = ($pageUrl =~ m|.*/videos/([^/]+)/.*|);
if (not defined $videoId) {
print STDERR sprintf("Error: could not get videoId from page URL '%s'!\n", $pageUrl);
exit(1);
}
printf("videoId = %s\n", $videoId);
my $apiPlayerUrl = sprintf("https://api.arte.tv/api/player/v2/config/fr/%s", $videoId);
printf("apiPlayerUrl = %s\n", $apiPlayerUrl);
my $agent = new LWP::UserAgent();
my $response;
my $content;
$response = $agent->get($apiPlayerUrl);
if (!$response->is_success) {
print STDERR sprintf("Error: could not fetch '%s': %s\n", $pageUrl, $response->status_line);
exit( 1 );
}
if (! eval { $content = decode_json($response->content); 1; }) {
print STDERR sprintf("Error: could not decode JSON from '%s'!\n", $apiPlayerUrl);
exit(1);
}
my $m3u8Url = undef;
if (not defined $content->{'data'}->{'attributes'}->{'streams'}) {
print STDERR sprintf("Error: could not find stream in JSON from '%s'!\n", $apiPlayerUrl);
exit(1);
}
my $streams = $content->{'data'}->{'attributes'}->{'streams'};
for my $stream (@{$streams}) {
if (defined $stream->{'versions'}[0]->{'shortLabel'} and any { $_ eq $stream->{'versions'}[0]->{'shortLabel'} } @{['VOF', 'VF']}) {
$m3u8Url = $stream->{'url'};
last;
}
}
if (not defined $m3u8Url) {
print STDERR sprintf("Error: could not find m3u8 URL in JSON from '%s'!\n", $apiPlayerUrl);
exit(1);
}
printf("m3u8Url = %s\n", $m3u8Url);
my ($baseUrl) = dirname($m3u8Url);
printf("baseUrl = %s\n", $baseUrl);
$response = $agent->get($m3u8Url);
if (!$response->is_success) {
print STDERR sprintf("Error: could not fetch '%s': %s\n", $m3u8Url, $response->status_line);
exit(1);
}
$content = $response->content;
my ($videoUrl) = ($content =~ m|^(https://.*/medias/[^.]+_v\Q${videoQuality}\E)\.m3u8$|m);
if (not defined $videoUrl) {
print STDERR sprintf("Error: could not find video URL with quality '%s'!\n", $videoQuality);
exit(1);
}
$videoUrl = sprintf("%s.mp4", $videoUrl);
printf("videoUrl = %s\n", $videoUrl);
my ($audioUrl) = ($content =~ m|\bTYPE=AUDIO\b.*?\bURI="(https://[^"]+/medias/[^"]+)\.m3u8"|m);
if (not defined $audioUrl) {
print STDERR sprintf("Error: could not find audio URL!\n");
exit(1);
}
$audioUrl = sprintf("%s.mp4", $audioUrl);
printf("audioUrl = %s\n", $audioUrl);
my $audioFile = sprintf("%s.audio", basename($audioUrl));
my $videoFile = sprintf("%s.video", basename($videoUrl));
my (@cmd, $ret);
@cmd = ('curl', '-o', $audioFile, '-C', '-', $audioUrl);
printf("Downloading audio '%s' into file '%s'\n", $audioUrl, $audioFile);
if (!$dryRun) {
system(@cmd);
$ret = $?;
if ($ret == -1) {
print STDERR sprintf("Error: could not execute, or find, the 'curl' command!\n");
exit(1);
}
if ($ret != 0) {
print STDERR sprintf("Error: '%s' returned with error code '%s'\n", join(' ', @cmd), $ret);
exit(1);
}
}
@cmd = ('curl', '-o', $videoFile, '-C', '-', $videoUrl);
printf("Downloading video '%s' into file '%s'\n", $videoUrl, $videoFile);
if (!$dryRun) {
system(@cmd);
$ret = $?;
if( $ret == -1 ) {
print STDERR sprintf("Error: could not execute, or find, the 'curl' command!\n");
exit(1);
}
if ($ret != 0) {
print STDERR sprintf("Error: '%s' returned with error code '%s'\n", join(' ', @cmd), $ret);
exit(1);
}
}
@cmd = ('ffmpeg', '-i', $videoFile, '-i', $audioFile, '-c:v', 'copy', '-c:a', 'copy', $outFile);
printf("Merging audio '%s' and video '%s' into '%s'\n", $audioFile, $videoFile, $outFile);
if (!$dryRun) {
system(@cmd);
$ret = $?;
if( $ret == -1 ) {
print STDERR sprintf("Error: could not execute, or find, the 'ffmpeg' command!\n");
exit(1);
}
if( $ret != 0 ) {
print STDERR sprintf("Error: '%s' returned with error code '%s'\n", join(' ', @cmd), $ret);
exit(1);
}
unlink($audioFile);
unlink($videoFile);
}
exit(0);