-
Notifications
You must be signed in to change notification settings - Fork 0
/
Download.cs
365 lines (301 loc) · 12.3 KB
/
Download.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
using YoutubeExplode.Videos.Streams;
using YoutubeExplode.Playlists;
using System.Text.Json;
using System.Text.Encodings.Web;
using System.Text.Unicode;
using System.Text.RegularExpressions;
using System.Diagnostics;
using YoutubeExplode.Videos;
using YoutubeExplode;
namespace YTPlayListDownloader.Collectors;
class Download : Collector
{
public Download(string url) : base(url)
{
}
private void AnnotateMp3Tag(string filePath, string vTitle, string? comment)
{
string pattern = @"\[(.*?)\]";
var matches = Regex.Matches(vTitle, pattern);
try
{
TagLib.Id3v2.Tag.DefaultVersion = 4;
TagLib.Id3v2.Tag.ForceDefaultVersion = true;
using TagLib.File mp3 = TagLib.File.Create(filePath);
if (matches.Any())
{
var contributors = matches.Select(match => match.Groups[1].Value);
mp3.Tag.Performers = contributors.ToArray();
}
if (comment != null)
{
mp3.Tag.Comment = comment;
}
mp3.Save();
}
catch (System.Exception e)
{
Console.WriteLine(e);
}
}
/// <summary>
/// <para>
/// youtube現在好像有可能會卡這類影片的自動下載,可以考慮直接跳過(現有的相關檔案要備份R)
/// </para>
/// <para>
/// 作者似乎也能設定擋下載 (YoutubeExplode.Exceptions.VideoUnplayableException: Video 'videoId' is unplayable. Reason: 'Playback on other websites has been disabled by the video owner')
/// </para>
/// </summary>
// {"id": "ry3Tupx4BL4","title": "[ヨルシカ] パレード"}
// {"id": "87moOXPTtSk","title": "[-inai-][可不] 死のうとしたのにな"}
// {"id": "4QXCPuwBz2E","title": "[ツユ] あの世行きのバスに乗ってさらば"}
// {"id": "0_pfGRDugxg","title": "[Rap Battle!] Light Yagami vs Monika"}
private bool IsSpecialVideo(VideoId videoId)
{
string[] suicide = new string[] { "ry3Tupx4BL4", "87moOXPTtSk", "4QXCPuwBz2E", "0_pfGRDugxg" };
string[] unplayable = new string[] { "Ej0DA0BgbRU" };
return unplayable.Contains(videoId.ToString());
}
private bool IsNeedReStereo(VideoId videoId)
{
string[] special = new string[] { "QJq6GAZYH18" };
return special.Contains(videoId.ToString());
}
private bool IsNeedSeek(VideoId videoId)
{
string[] special = new string[] { "iASoRE5k0Vw", "QJq6GAZYH18" };
return special.Contains(videoId.ToString());
}
private async Task<bool> DownloadVideo(string queryPlayListId, PlaylistVideo Video, Videos? CustomVideoTitles)
{
var vinfo = await yt.Videos.GetAsync(Video.Url);
var vtitle = vinfo.Title;
var vId = vinfo.Id.ToString();
var PlaylistId = Video.PlaylistId;
Stream youtubeStream = new MemoryStream();
var CustomVideo = CustomVideoTitles?.items.FirstOrDefault(v => v.Id == vinfo.Id);
vtitle = RemoveSpecialChar(CustomVideo?.Title ?? vtitle);
var filePath = $@"./{vtitle.Split("]").Last().Trim()}.mp3";
// 可能會炸的檢查
if (IsSpecialVideo(vId))
{
return false;
}
try
{
var videotManifest = await yt.Videos.Streams.GetManifestAsync(vId);
var videoInfo = videotManifest.GetAudioOnlyStreams()
.GetWithHighestBitrate();
using var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Add("Accept", "image/jpg");
var youtubeCover = await vinfo.getPictureStream(CustomVideo.CoverUrl);
if (IsNeedReStereo(vId) && IsNeedSeek(vId))
{
// reference -> https://www.c-sharpcorner.com/blogs/timespan-in-c-sharp
if (vId == "QJq6GAZYH18")
{
var start = new TimeSpan(0, 0, 40);
youtubeStream =
await (await (await videoInfo.GetReStereoStream(1)).SeekTo(start)).AnnotateTag(CustomVideo,
queryPlayListId, youtubeCover);
Console.WriteLine($"{filePath.Split('/').Last()} ReStereo ☑");
}
}
else if (IsNeedSeek(vId))
{
TimeSpan? start = vId switch
{
"iASoRE5k0Vw" => new TimeSpan(0, 0, 11),
"QJq6GAZYH18" => new TimeSpan(0, 0, 40),
_ => null
};
youtubeStream =
await (await videoInfo.SeekTo(start)).AnnotateTag(CustomVideo, queryPlayListId, youtubeCover);
Console.WriteLine($"{filePath.Split('/').Last()} Seek ☑");
}
else
{
youtubeStream =
await (await videoInfo.GetStream()).AnnotateTag(CustomVideo, queryPlayListId, youtubeCover);
}
using var fileStream = File.Create(filePath);
await youtubeStream.CopyToAsync(fileStream);
return true;
}
catch (System.Exception)
{
throw;
}
}
private async Task DownlaodList(string queryPlayListId, Queue<PlaylistVideo> videos)
{
string jsonFile =
await File.ReadAllTextAsync(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "./customTitle.json"));
var jsonContent = JsonSerializer.Deserialize<Videos>(
jsonFile,
new JsonSerializerOptions { PropertyNameCaseInsensitive = true }
);
Stopwatch watch = new Stopwatch();
var count = 0;
var playListLength = videos.Count;
// https://stackoverflow.com/questions/10806951/how-to-limit-the-amount-of-concurrent-async-i-o-operations
var throttler = new SemaphoreSlim(initialCount: 5);
List<Task> DownloadTasks = new List<Task>();
if (videos.Count == 0)
{
return;
}
while (videos.Count > 0)
{
var video = videos.Peek();
var vinfo = await yt.Videos.GetAsync(video.Url);
var vtitle = vinfo.Title;
var vId = vinfo.Id;
var Thumbnails = vinfo.Thumbnails.OrderBy(thumbnail => thumbnail.Resolution.Width).ToList();
var url = Thumbnails.LastOrDefault()!.Url;
try
{
count++;
var v = jsonContent?.items.FirstOrDefault(v => v.Id == vinfo.Id);
vtitle = RemoveSpecialChar(v?.Title ?? vtitle);
var filePath = $@"./{vtitle.Split("]").Last().Trim()}.mp3";
if (File.Exists(filePath))
{
videos.Dequeue();
var message = $"{filePath.Split('/').Last()} ☑ {watch.Elapsed}";
Console.WriteLine(message);
}
else
{
if (v == null)
{
jsonContent?.items.Add(new Video(vId, vtitle, null, url));
}
await throttler.WaitAsync();
DownloadTasks.Add(Task.Run(async () =>
{
try
{
Stopwatch watch = Stopwatch.StartNew();
watch.Restart();
var res = await DownloadVideo(queryPlayListId, video, jsonContent);
watch.Stop();
if (res)
{
var message = $"{filePath.Split('/').Last()} Download ☑ {watch.Elapsed}";
Console.WriteLine(message);
}
else
{
Console.WriteLine($"{filePath.Split('/').Last()} ☑ {watch.Elapsed}");
Console.WriteLine(
$"\tDue to uncontrollable factors such as YouTube policies, downloading is temporarily unavailable.");
Console.WriteLine(
$"\tPlease make backups in advance, or seek alternative services for downloading.");
}
}
catch (System.Exception e)
{
Console.WriteLine($"\n{e}");
Console.WriteLine("Boom!");
throw;
}
finally
{
throttler.Release();
}
}));
videos.Dequeue();
}
}
catch (System.Exception e)
{
Console.WriteLine($"\n{e}");
Console.WriteLine("Boom!");
continue;
}
}
Task allTask = Task.WhenAll(DownloadTasks);
try
{
await allTask;
}
catch (System.Exception e)
{
Console.WriteLine($"\n{e}");
Console.WriteLine("Boom!");
throw;
}
if (jsonContent != null)
{
jsonContent.items = jsonContent.items.ToList().OrderBy(v => v.Title).ToHashSet();
var finalJson = JsonSerializer.Serialize<Videos>(
jsonContent,
new JsonSerializerOptions
{ WriteIndented = true, Encoder = JavaScriptEncoder.Create(UnicodeRanges.All) }
);
// current directory is in download folder
// return to root directory for overwrite customTitle.json
Directory.SetCurrentDirectory($"../");
await File.WriteAllTextAsync("./customTitle.json", finalJson);
}
}
// for update Video's Front Cover
private async Task UpdateVideoFrontCover()
{
string jsonFile =
await File.ReadAllTextAsync(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "./customTitle.json"));
var jsonContent = JsonSerializer.Deserialize<Videos>(
jsonFile,
new JsonSerializerOptions { PropertyNameCaseInsensitive = true }
);
jsonContent.items = jsonContent.items.Select((video, index) =>
{
try
{
var yt = new YoutubeClient();
var vinfo = yt.Videos.GetAsync($"https://www.youtube.com/watch?v={video.Id}").Result;
var Thumbnails = vinfo.Thumbnails.OrderBy(thumbnail => thumbnail.Resolution.Width).ToList();
var CoverUrl = Thumbnails.LastOrDefault()!.Url;
Console.WriteLine($"{index + 1} {video.Title} {CoverUrl}");
video.CoverUrl = CoverUrl;
return video;
}
catch (System.Exception)
{
video.CoverUrl = "";
return video;
}
}).ToHashSet();
if (jsonContent != null)
{
jsonContent.items = jsonContent.items.ToList().OrderBy(v => v.Title).ToHashSet();
var finalJson = JsonSerializer.Serialize<Videos>(
jsonContent,
new JsonSerializerOptions
{ WriteIndented = true, Encoder = JavaScriptEncoder.Create(UnicodeRanges.All) }
);
await File.WriteAllTextAsync("./customTitle.json", finalJson);
}
}
//reference -> https://csharpkh.blogspot.com/2017/10/c-async-void-async-task.html
public override async Task Invoke()
{
Console.OutputEncoding = System.Text.Encoding.UTF8;
// await UpdateVideoFrontCover();
Stopwatch watch = new Stopwatch();
watch.Start();
var playListInfo = await GetPlayListInfo(url);
var videoQueue = new Queue<PlaylistVideo>(playListInfo.videos);
string name = $"YT-{playListInfo.title}";
if (!Directory.Exists($"./{name}"))
{
Directory.CreateDirectory($"./{name}");
}
Directory.SetCurrentDirectory($"./{name}");
await DownlaodList(playListInfo.id, videoQueue);
watch.Stop();
Console.WriteLine($"執行時間: {watch.Elapsed}");
// var explodeCount = await Downlaod(playListInfo.videos, playListInfo.videos.Count);
}
}