-
Notifications
You must be signed in to change notification settings - Fork 0
/
wwdc-download
executable file
·201 lines (192 loc) · 5.4 KB
/
wwdc-download
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
#!/usr/bin/perl -s
#
# right-click on streaming video, select "Copy video address", and feed it to this script
#
# Example URL:
# 2016: http://devstreaming.apple.com/videos/wwdc/2015/106z3yjwpfymnauri96m/106/hls_vod_mvp.m3u8
# 2017: http://devstreaming.apple.com/videos/wwdc/2016/102w0bsn0ge83qfv7za/102/hls_vod_mvp.m3u8
#
# Not sure if this works outside WWDC.
#
# You need to be logged in to http://developer.apple.com
#
# hacked together by Reid / @clith - [email protected]
#
my $base;
my $m3u;
my $ts;
my $ffmpegCmd = "ffmpeg -i 'concat:%s' -c copy -bsf:a aac_adtstoasc '%s'";
my $limit="400k";
my $limitOpt = "--limit-rate ${limit}";
my $baseCurlCmd = "curl -s";
# $baseCurlCmd .= " $limitOpt";
my $maxDownloadRetryCount = 4;
my $dateString;
local $SIG{HUP} = sub {
chomp($dateString = `date`);
print "\n\n[$dateString] Caught user ^C, deleting ${ts} and exiting..\n";
unlink($ts);
exit(0);
};
sub findBaseAudioVideo($) {
my $url = shift;
my $base = $url;
$base =~ s#/[^/]*?$##;
print "url = $url\n";
print "base = $base\n";
open CURL, "curl -s \"$url\"|" || die "$!: Can't open URL $url";
my $audio = "";
my $highestBandwidth=0;
my $video = "";
while(<CURL>) {
chomp;
if(m#GROUP-ID="program_audio".*URI="(.+)"#) {
$audio = $1;
next;
}
if(m#EXT-X-STREAM-INF.*\bBANDWIDTH=(\d+)#) {
if($1 > $highestBandwidth) {
$highestBandwidth = $1;
$video = <CURL>;
next;
}
}
}
close(CURL);
print("base = $base\n");
print("audio = $audio\n");
print("video = $video\n");
return ($base, $audio, $video)
}
#print "## Limiting downloads to ${limit}B/s\n";
foreach (@ARGV) {
unless(m#^(.*)/(\d+)/([\w.]+)$#) {
unless(m#^(.*)/(m3u8)/([\w.]+)$#) {
unless(m#^(.*)/([\w.]+)$#) {
warn "Can't read url $_\n";
next;
}
else {
print "$1 -- $2\n";
# $base = "$1/$2";
$session = "m3u8";
# $m3u = $3;
}
} else {
print "$1 -- $2 -- $3\n";
# $base = "$1/$2";
$session = "$2";
# $m3u = $3;
}
} else {
# $base = "$1/$2";
$session = "$2";
# $m3u = $3;
}
# $curlCmd = "${baseCurlCmd} --referer $_";
print("1 session=${session}\n");
$session="101" if $session eq "m3u8";
# print("2 session=${session} [$base / $m3u]\n");
print("2 session=${session}\n");
# my @videoList = split ' ', `$curlCmd "$base/$m3u"`;
# my @videoList = split ' ', `$curlCmd "$_"`;
# my $vidbase = "${base}/${vidSize}";
# my $vidm3u = "${vidbase}/${vidSize}.m3u8";
# my $stream_m3u = `$curlCmd "$_"`;
# my $vidm3u = `$curlCmd "$_"|grep -v '^#'| sort -r -s | head -1`;
mkdir $session unless -d $session;
chdir $session || warn "was not able to chdir: $!\n";
chomp(my $date = `date`);
print "\n# Starting $session [$tsfiles[0] - ${lastTs}] on ${date}\n";
print "$_\n";
my ($base, $audio, $video) = findBaseAudioVideo($_);
foreach my $vidm3u ("$video", "$audio") {
chomp($vidm3u);
print("vidm3u = $vidm3u\n");
$vidbase = ${vidm3u};
$vidbase =~ s#/[^/]*$##;
print("tslist-cmd: $curlCmd \"$base/$vidm3u\" | grep -v '^#'");
my $tslist = `$curlCmd "$base/$vidm3u"|grep -v '^#'`;
my @tsfiles = split ' ', $tslist;
my $lastTs = $tsfiles[$#tsfiles -1];
# skip already-compiled session videos
my @allFiles=<*>;
print("3 session=${session}\n");
my @sessionVideoList = grep(/${session}./, @allFiles);
if(scalar @sessionVideoList > 0) {
print "Skipping \"@sessionVideoList\"\n---\n";
my $cmd = "echo grep \"${session}\"";
system($cmd);
next;
} else {
print "Downloading \"$session\"\n";
}
my $stopped = 0;
my $complete = 0;
my $column = 1;
my $lastTs = "";
foreach $ts (@tsfiles) {
my $tsurl = "$base/$ts";
print("tsurl = $tsurl\n");
my $shouldSkip=0;
if(-r $ts) {
print " [$ts] ";
$shouldSkip=1;
$complete = 1;
} else {
print " $ts ";
$complete = 0;
}
$column++;
if($column > 10) {
$column=1;
print "\n";
}
next if $shouldSkip;
for (my $downloadAttemptCount = 1; $downloadAttemptCount < $maxDownloadRetryCount; $downloadAttemptCount++) {
my $systemValue = system("$curlCmd -O \"$tsurl\"");
# print "$curlCmd -O \"$tsurl\"\n";
if($systemValue) {
my $sig = $? & 127;
$lastTs = $tsurl;
chomp($dateString = `date`);
printf("\n\n[$dateString] Error: failed (signal %d) to download $ts (try ${downloadAttemptCount} of $maxDownloadRetryCount) [$systemValue]\n", $sig);
print " Exited with value ", $? >> 8, "\n";
print " $ts \n";
$columns=2;
if($sig == 2) {
print "^C\n";
print "\n[$dateString] Cleaning up and exiting; deleting $ts..\n";
unlink $ts;
exit(0);
}
} else {
$complete = 1;
last;
}
}
}
if($complete) {
my $episode=$session;
$episode =~ s/^(\d)/${1}x/;
my $cmd = sprintf($ffmpegCmd, join("|", @tsfiles), "../${episode}..${hdExt}.mp4");
my $printCmd = sprintf($ffmpegCmd, "<$#tsfiles files>", "../${episode}..${hdExt}.mp4");
chomp($dateString = `date`);
print "\n\n[$dateString] Creating mp4\n[$dateString] $printCmd\n\n";
if(system($cmd)) {
warn "Creation of ${episode} mp4 failed: $!\n";
} else {
chomp($dateString = `date`);
print "\n[$dateString] Creation of ${episode} mp4 complete\n\n";
}
} else {
chomp($dateString = `date`);
print "\n\n[$dateString] Error: failed to download $ts (try #${downloadAttemptCount} of $maxDownloadRetryCount)\n";
print "$lastTs\n";
print "[$dateString] Halting all downloads.\n";
unlink $ts if $ts ne "";
last;
}
chdir "..";
}
}