-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathVideoDownloader.cs
390 lines (329 loc) · 13.8 KB
/
VideoDownloader.cs
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
namespace openu_video_fetcher
{
public class UrlFileCache
{
public UrlFileCache(DirectoryInfo input)
{
parentDir = input;
}
public string GetContent(string playlistName, string subDir = "")
{
try
{
return RetrieveCachedFile(playlistName, subDir);
}
catch
{
return null;
}
}
public FileInfo GetFile(string key, string subDir = "")
{
return new FileInfo(Path.Combine(getDstDir(subDir), key));
}
public void PutBinary(string key, byte[] content, string subDir = "")
{
try
{
var dstDir = getDstDir(subDir);
if (!Directory.Exists(dstDir))
parentDir.CreateSubdirectory(subDir);
File.WriteAllBytes(Path.Combine(dstDir, key), content);
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
public void PutContent(string key, string content, string subDir = "")
{
try
{
var dstDir = getDstDir(subDir);
if (!Directory.Exists(dstDir))
parentDir.CreateSubdirectory(subDir);
File.WriteAllText(Path.Combine(dstDir, key), content);
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
private string RetrieveCachedFile(string key, string subDir)
{
var dstDir = getDstDir(subDir);
return File.ReadAllText(Path.Combine(dstDir, key));
}
private string getDstDir(string subDir)
{
return Path.Combine(parentDir.FullName, subDir);
}
DirectoryInfo parentDir;
internal bool HasFile(string key, string subDir = "")
{
var dstDir = Path.Combine(parentDir.FullName, subDir);
try
{
return File.Exists(Path.Combine(dstDir, key));
}
catch
{
return false;
}
}
internal string GetSubDir(string v)
{
var fullPath = Path.Combine(parentDir.FullName, v);
if (!Directory.Exists(fullPath))
parentDir.CreateSubdirectory(v);
return fullPath;
}
}
public class VideoDownloader
{
UrlFileCache cache;
OpenuVideoData data;
ChunksFile chunks;
List<string> chunkNames;
string baseUrl;
string wantedQuality = "40000";
string wantedQuality2nd = "800000";
public VideoDownloader(UrlFileCache bank, OpenuVideoData data, BackgroundWorker changesReporter)
{
this.cache = bank;
this.data = data;
durations = new ConcurrentBag<TimeSpan>();
this.changesReporter = changesReporter;
}
string requestPlaylist(OpenuVideoData data)
{
var playlistFile = cache.GetContent(data.formatPlaylistFile(), data.VideoId);
if (playlistFile != null)
return playlistFile;
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(data.PlaylistUrl);
req.AllowAutoRedirect = true;
var response = (HttpWebResponse) req.GetResponse();
if (response.StatusCode != HttpStatusCode.OK)
{
return null;
}
playlistFile = new StreamReader(response.GetResponseStream()).ReadToEnd();
cache.PutContent(data.formatPlaylistFile(), playlistFile, data.VideoId);
return playlistFile;
}
public void Download()
{
string playlistM3U8 = requestPlaylist(data);
parsePlaylist(playlistM3U8);
baseUrl = chunks.Url.Remove(chunks.Url.LastIndexOf("/"));
new Thread(printProgress).Start();
Parallel.ForEach(chunkNames, new ParallelOptions { MaxDegreeOfParallelism = 10 },
chunkName => downloadChunk(chunkName));
var inputFile = cache.GetFile(data.formatFFmpegFile(), data.VideoId);
var outputFile = data.formatOutputFile();
if (File.Exists(outputFile))
{
try
{
File.Delete(outputFile);
}
catch
{
Console.WriteLine("Unable to combine video - '{0}', because the output file '{1}' exists", data.VideoId, outputFile);
return;
}
}
var p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.FileName = Path.Combine("ffmpeg", "ffmpeg.exe");
p.StartInfo.Arguments = string.Format("-i \"{0}\" -c copy -bsf:a aac_adtstoasc {1}", inputFile, outputFile);
p.StartInfo.WorkingDirectory = inputFile.Directory.FullName;
p.Start();
var errors = p.StandardError;
var output = p.StandardOutput;
Console.WriteLine(errors.ReadToEnd());
Console.WriteLine(errors.ReadToEnd());
p.WaitForExit();
if (success)
cache.PutContent("done", "", data.VideoId);
}
private void printProgress()
{
Stopwatch totalRuntime = new Stopwatch();
totalRuntime.Start();
int remainingUnits = totalUnits;
int downloaded = this.downloaded;
while (remainingUnits > 0)
{
TimeSpan overallDuration = new TimeSpan();
foreach (var dur in durations)
overallDuration = overallDuration.Add(dur);
double average = 0;
long remainingMilli = 0;
if (downloaded != 0)
{
average = ((double)overallDuration.TotalSeconds) / downloaded;
var timePerUnit = (long)((double)totalRuntime.ElapsedMilliseconds / downloaded);
remainingMilli = timePerUnit * remainingUnits;
}
var remainingTime = TimeSpan.FromMilliseconds(remainingMilli);
Console.WriteLine("Progress: remaining - {0}, {1} / {2}, active {3}, avg = {4}",
remainingTime, preExistingUnits + downloaded, totalUnits, active, average);
Thread.Sleep((int)TimeSpan.FromSeconds(1).TotalMilliseconds);
downloaded = this.downloaded;
changesReporter.ReportProgress((downloaded + preExistingUnits) * 100 / totalUnits, new VideoProgress() { RemainingTime = remainingTime});
remainingUnits = totalUnits - downloaded - preExistingUnits;
}
}
private void parsePlaylist(string playlistM3U8)
{
chunks = getChunksFile(playlistM3U8);
var allChunkNames = parseChunksFile(chunks.Content);
chunkNames = new List<string>();
foreach (var chunkName in allChunkNames)
{
if (!cache.HasFile(chunkName, data.VideoId))
chunkNames.Add(chunkName);
}
Console.WriteLine("There exists {0}/{1} of the needed chunks, downloading {2} chunks.",
allChunkNames.Count - chunkNames.Count, allChunkNames.Count, chunkNames.Count);
totalUnits = allChunkNames.Count;
preExistingUnits = totalUnits - chunkNames.Count;
}
private ChunksFile getChunksFile(string playlistM3U8)
{
ChunksFile chunks = new ChunksFile();
chunks.Url = cache.GetContent(data.formatChunksFileUrl(wantedQuality), data.VideoId);
chunks.Content = cache.GetContent(data.formatChunksFileContent(wantedQuality), data.VideoId);
if (chunks.Content == null || chunks.Url == null)
chunks = fetchAndCacheChunksFile(playlistM3U8);
return chunks;
}
int totalUnits = 0;
int downloaded = 0;
int active = 0;
int preExistingUnits = 0;
bool success = true;
ConcurrentBag<TimeSpan> durations;
private BackgroundWorker changesReporter;
private void downloadChunk(string chunkName)
{
Stopwatch w = new Stopwatch();
w.Start();
Interlocked.Increment(ref active);
int tryNum = 0;
bool success = false;
do
try
{
var req = WebRequest.Create(baseUrl + "/" + chunkName);
var resp = req.GetResponse();
var chunkData = new BinaryReader(resp.GetResponseStream()).ReadBytes((int)resp.ContentLength);
cache.PutBinary(chunkName, chunkData, data.VideoId);
success = true;
}
catch
{
tryNum++;
Console.WriteLine("Failed downloading '{0}' on {1}.", chunkName, data.VideoId);
}
while (tryNum < 3 && success == false);
if (!success)
{
// TODO
success = false;
}
Interlocked.Increment(ref downloaded);
Interlocked.Decrement(ref active);
w.Stop();
durations.Add(w.Elapsed);
}
private List<string> parseChunksFile(string chunksFile)
{
List<string> chunkNames = new List<string>();
var ext = ".ts";
var chunkMetaFinder = new Regex("#EXTINF:");
using (StringReader sr = new StringReader(chunksFile))
{
// This is needed because the resources names inside the chunks file
// might not be legal file names, that means ffmpeg conversion won't work.
using (StringWriter wr = new StringWriter())
{
string line;
while ((line = sr.ReadLine()) != null)
{
wr.WriteLine(line);
if (chunkMetaFinder.IsMatch(line))
{
var rawChunkName = sr.ReadLine();
var chunkFileName = rawChunkName.Remove(rawChunkName.IndexOf(ext)) + ext;
wr.WriteLine(chunkFileName);
chunkNames.Add(chunkFileName);
}
}
wr.Close();
cache.PutContent(data.formatFFmpegFile(), wr.ToString(), data.VideoId);
}
}
return chunkNames;
}
struct ChunksFile
{
public string Url { get; set; }
public string Content { get; set; }
}
private ChunksFile fetchAndCacheChunksFile(string playlistM3U8)
{
string chunksUrl = getChunksFileUrl(playlistM3U8);
var chunksReq = (HttpWebRequest)WebRequest.Create(chunksUrl);
var resp = chunksReq.GetResponse();
string chunksFile = new StreamReader(resp.GetResponseStream()).ReadToEnd();
cache.PutContent(data.formatChunksFileUrl(wantedQuality), chunksUrl, data.VideoId);
cache.PutContent(data.formatChunksFileContent(wantedQuality), chunksFile, data.VideoId);
return new ChunksFile { Url = chunksUrl, Content = chunksFile };
}
private string getChunksFileUrl(string playlistM3U8)
{
SortedDictionary<int, string> qualityToChunkLists = new SortedDictionary<int, string>();
var qualityTester = new Regex(String.Format(":BANDWIDTH=([0-9]*)", wantedQuality));
var lines = playlistM3U8.Split('\n');
for (int i = 0; i < lines.Length; i++)
{
if (qualityTester.IsMatch(lines[i]))
{
string _qualityS = qualityTester.Match(lines[i]).Groups[1].Value;
if (_qualityS == wantedQuality)
return lines[i + 1];
int quality = 0;
int.TryParse(_qualityS, out quality);
string chunkList = lines[i + 1];
qualityToChunkLists[quality] = chunkList;
}
}
if (qualityToChunkLists.ContainsKey(int.Parse(wantedQuality2nd)))
return qualityToChunkLists[int.Parse(wantedQuality2nd)];
// Can't find wanted qualities... going for 2nd best
KeyValuePair<int, string>[] sortedKeys = new KeyValuePair<int, string>[qualityToChunkLists.Count];
qualityToChunkLists.CopyTo(sortedKeys, 0);
// There may not be a 2nd best...
if (sortedKeys.Length > 1)
return sortedKeys[sortedKeys.Length - 2].Value;
// This is an error. TODO: throw exception.
if (sortedKeys.Length == 0)
return null;
return sortedKeys[0].Value;
}
}
}