-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainDownloader.cs
409 lines (396 loc) · 18.2 KB
/
MainDownloader.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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
using CSCore;
using CSCore.MediaFoundation;
using System.Linq;
using System;
using System.Collections;
using System.Net;
using VideoLibrary;
//Dont need system Diagn testing only
using System.Diagnostics;
using System.IO;
namespace YoutubePlaylistDownloader
{
class MainDownloader
{
/// <summary>
/// READ ME -------------------------------------------------
/// YouTube Playlist Downloader. Automatically Downloads and converts to .mp3 format.
/// PLaylist Links Can only be in this URL Format https://www.youtube.com/playlist?list=_PlaylistID_ for Now.
/// </summary>
/*TODO LIST
* 1) Find A Way to Download Youtube Video (Done - VideoLibrary)
* 2) Find A Way to Have Final Format be .MP3 ( Done - FFMpeg)
* A) Find a Faster Converter ( Done - CSCore)
* 3) Find A Way to Extract All Seprate Videos ID From Playlist Link (Done - .Net)
* A) Find a Way to get past the 100 Video limit. ( Done - Method PlayListIndexGenerator (New Limit 200))
* 4) Find A Way to Organize songs into Differn't Playlist Folders according to what Playlist they come From
* 5) Create Gui
* 6) Get Rid of Hard Coded Varaibles like PATH,PATHSONGLIST.
* 7) Option To Check Playlist every 5 Mins to Download new songs added.
* 8)
*/
//static string PATH = @"C:\Users\Sam\Music\Music\";
static string PATH = System.IO.Directory.GetCurrentDirectory() + @"\Music\";
static string PATHSONGLIST = PATH + @"11DownloadedSongList.txt";
//static string FFMPEGPATH;
static void Main(string[] args)
{
//FFMPEGPATH = System.IO.Directory.GetCurrentDirectory().Replace(@"\Debug", @"\ffmpeg\bin\ffmpeg.exe");
Stopwatch mywatch = new Stopwatch();
string[] PlaylistUrls = new string[2];
PlaylistUrls[0] = "https://www.youtube.com/playlist?list=PLtlQWFxwdDxWElh5JDDNSAV4h1h3kcA73";
PlaylistUrls[1] = "https://www.youtube.com/playlist?list=PLtlQWFxwdDxWt0zz33Ge-3XV60MZ2TYTv";
Console.WriteLine("1) Download a Song\n" +
"2) Download a PlayList" +
"3) Drive Music Playlist Download" +
"4) Best Trap City Music Playlist Download" +
"9) Exit");
string input = Console.ReadLine();
switch (input)
{
case "1":
Console.WriteLine("Put In Your Youtube Song with this link style link(https://www.youtube.com/watch?v=_VideoID)");
input = Console.ReadLine();
DownloadYoutubeVideo(input);
break;
case "2":
Console.WriteLine("Put In Your Youtube Playlist with this link style link(https://www.youtube.com/playlist?list=)");
input = Console.ReadLine();
mywatch.Start();
PlaylistVideoLinkExtractor(PlayListIndexGenerator(input), PlaylistIdExtractor(input), true);
break;
case "3":
mywatch.Start();
PlaylistVideoLinkExtractor(PlayListIndexGenerator(PlaylistUrls[0]), PlaylistIdExtractor(PlaylistUrls[0]), true);
break;
case "4":
mywatch.Start();
PlaylistVideoLinkExtractor(PlayListIndexGenerator(PlaylistUrls[1]), PlaylistIdExtractor(PlaylistUrls[1]), true);
break;
case "9":
Console.WriteLine("Exiting");
break;
default:
Console.WriteLine("Exiting");
break;
}
mywatch.Stop();
Console.WriteLine("All Done in = " + mywatch.Elapsed);
Console.ReadLine();
}
/// <summary>
/// Method Makes Sure Essiatial Variables are Set.
/// </summary>
private static void Intialization()
{
if (PATH == null)
throw new Exception("Intialization Failed. PATH is Null");
else if (!File.Exists(PATHSONGLIST))
{
System.IO.Directory.CreateDirectory(PATH);
File.Create(PATHSONGLIST);
}
}
/// <summary>
/// Method to 2Grab Every Youtube Videos Unique ID From Playlist.
/// </summary>
///<param name="Url">
///Takes https://www.youtube.com/watch?v=_VideoID_&index=1&list=_PlaylistID
///</param>
///<param name="PlaylistId">
///Takes String PlayListID From <see cref="PlaylistIdExtractor(string)"/>
///</param>
private static void PlaylistVideoLinkExtractor(string Url, string PlaylistId, bool Download = false)
{
Intialization();
ArrayList Playlist = new ArrayList();
foreach (string x in HttpPageRequest(Url))
{
if (x.Contains("playlist-video") & x.Contains(PlaylistId))
{
string VideoId = VideoIdExtractor(x);
if (SongListChecker(VideoId) == false)
{
Playlist.Add(VideoId);
Console.WriteLine(VideoId);
}
else
{
Console.WriteLine("Was already downloaded");
}
}
}
if (Download == true)
{
int i = 0;
//DownloadYoutubeVideos(ref Playlist);
Playlist.Reverse();
using (var tw = new StreamWriter(PATHSONGLIST, true))
{
foreach (string VideoId in Playlist)
{
if (DownloadYoutubeVideo("www.youtube.com" + VideoId) == true)
{
i++;
tw.WriteLine(VideoId);
}
}
}
Console.WriteLine("Downloaded (" + i + ") Songs");
}
}
/// <summary>
/// Method to Grab a Youtube Playlist Id From Youtube Playlist Link. Returns String PlaylistId
/// </summary>
///<param name="Url">
///<para>Can take https://www.youtube.com/playlist?list=_PlaylistID_
///or https://www.youtube.com/watch?v=_VideoID_&index=1&list=_PlaylistID_
///or https://www.youtube.com/watch?v=_VideoID_&list=_PlaylistID_&index=1</para>
/// </param>
private static string PlaylistIdExtractor(string Url)
{
try
{
if (Url == null)
throw new Exception("PlaylistIdExtract Method Was Feed A Null URL");
else if (Url.Contains("list="))
{
Url = Url.Remove(0, Url.IndexOf("list=") + 5);
if (Url.Contains("&index="))
return Url.Remove(Url.IndexOf("&index="), Url.Length);
return Url;
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
return null;
}
/// <summary>
/// Method to Generate a Youtube Playlist Url With a Index.
/// Returns String Url = https://www.youtube.com/watch?v=_VideoID_&index=1&list=_PlaylistID_.
/// </summary>
/// <param name="Url">Can only take URL https://www.youtube.com/playlist?list=_PlaylistID_</param>
private static string PlayListIndexGenerator(string Url)
{
foreach (string x in HttpPageRequest(Url))
{
if (x.Contains("pl-video-title-link"))
{
return ("https://www.youtube.com" +
VideoIdExtractor(x) +
"&index=1" +
"&list=" +
PlaylistIdExtractor(Url));
}
}
return null;
}
/// <summary>
/// Mathod Checks PATHSONGLIST to See if videoID was Already Downloaded
/// </summary>
/// <param name="VideoID">Takes a watch?v=_VideoID_</param>
/// <returns></returns>
private static bool SongListChecker(string VideoID)
{
using (var tr = new StreamReader(PATHSONGLIST))
{
string text;
while ((text = tr.ReadLine()) != null)
{
if (text.Contains(VideoID))
return true;
}
}
return false;
}
/// <summary>
/// Method Takes out VideoID from <see cref="HttpPageRequest(string)"/>. Return String VideoID.
/// </summary>
/// <param name="ContainingVideoId">A String containing a Youtube VideoID</param>
/// <returns></returns>
private static string VideoIdExtractor(string ContainingVideoID)
{
return ContainingVideoID.Substring(ContainingVideoID.IndexOf("href=\"") + 6, 20);
}
/// <summary>
/// Method Downloads a Youtube Video from The Video Url.
/// </summary>
/// <param name="Url">Takes a https://www.youtube.com/watch?v=_VideoID_ </param>
private static bool DownloadYoutubeVideo(string Url)
{
string videoFullName = "foo";
try
{
var youtube = YouTube.Default;
var video = youtube.GetVideo(Url);
videoFullName = video.FullName.Replace(" - YouTube.mp4", "");
Console.WriteLine("Downloading " + videoFullName);
//File.WriteAllBytes(PATH + videoFullName + ".mp4", video.GetBytes());
IWaveSource videoSource = CSCore.Codecs.CodecFactory.Instance.GetCodec(new Uri(video.Uri));
Tuple<IWaveSource, String> package = Tuple.Create(videoSource, videoFullName);
System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(ThreadedConvertToMp3), package);
}
catch (Exception e)
{
if (File.Exists(PATH + videoFullName + ".mp3"))
File.Delete(PATH + videoFullName + ".mp3");
Console.WriteLine(e);
return false;
}
return true;
}
/// <summary>
/// Method is For Threadpooling the ConvertToMp3 Method.
/// </summary>
/// <param name="callback">Requires A Tuple of source video.URI and FileName</param>
private static void ThreadedConvertToMp3(object callback)
{
IWaveSource source = ((Tuple<IWaveSource, String>)callback).Item1;
String videoTitle = ((Tuple<IWaveSource, String>)callback).Item2;
Console.WriteLine("Proccessing " + videoTitle);
ConvertToMp3(source, videoTitle);
}
/// <summary>
/// Method Converts IWaveSource .Mp4 to .Mp3 with 192kbs Sample Rate and Saves it to Path Using the videoTitle
/// </summary>
/// <param name="source">Takes a IWaveSource videoSource = CSCore.Codecs.CodecFactory.Instance.GetCodec(new Uri(video.Uri))</param>
/// <param name="videoTitle"> Takes the Video Title</param>
/// <returns></returns>
private static bool ConvertToMp3(IWaveSource source, string videoTitle)
{
var supportedFormats = MediaFoundationEncoder.GetEncoderMediaTypes(AudioSubTypes.MpegLayer3);
if (!supportedFormats.Any())
{
Console.WriteLine("The current platform does not support mp3 encoding.");
return true;
}
if (supportedFormats.All(
x => x.SampleRate != source.WaveFormat.SampleRate && x.Channels == source.WaveFormat.Channels))
{
int sampleRate =
supportedFormats.OrderBy(x => Math.Abs(source.WaveFormat.SampleRate - x.SampleRate))
.First(x => x.Channels == source.WaveFormat.Channels)
.SampleRate;
Console.WriteLine("Samplerate {0} -> {1}", source.WaveFormat.SampleRate, sampleRate);
Console.WriteLine("Channels {0} -> {1}", source.WaveFormat.Channels, 2);
source = source.ChangeSampleRate(sampleRate);
}
using (source)
{
using (var encoder = MediaFoundationEncoder.CreateMP3Encoder(source.WaveFormat, PATH + videoTitle + ".mp3"))
{
byte[] buffer = new byte[source.WaveFormat.BytesPerSecond];
int read;
while ((read = source.Read(buffer, 0, buffer.Length)) > 0)
{
encoder.Write(buffer, 0, read);
//Console.CursorLeft = 0;
//Console.Write("{0:P}/{1:P}", (double)source.Position / source.Length, 1);
}
}
}
File.Delete(PATH + videoTitle + ".mp4");
return false;
}
/// <summary>
/// Method to Grab HttpPageRequest. Returns a String Array Split by \n.
/// </summary>
/// <param name="Url">Takes Any Url</param>
private static string[] HttpPageRequest(string Url)
{
HttpWebRequest wb = (HttpWebRequest)WebRequest.Create(Url);
wb.Method = "GET";
wb.KeepAlive = true;
wb.Proxy = null;
wb.UserAgent = "Mozilla / 5.0(Windows NT 10.0; Win64; x64) AppleWebKit / 537.36(KHTML, like Gecko) Chrome / 68.0.3440.106 Safari / 537.36";
return new StreamReader(wb.GetResponse().GetResponseStream()).ReadToEnd().Split('\n');
}
/* Not Used Methods --------------------
/// <summary>
/// Method Ment for ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadedFfmpeg), videoFullName)
/// </summary>
/// <param name="callback">Takes string videoFullName</param>
private static void DownloadYoutubeVideos(ref ArrayList Playlist)
{
using (var cli = new VideoClient())
{
var youtube = YouTube.Default;
Video video;
string videoFullName = "foo";
using (var tw = new StreamWriter(PATHSONGLIST, true))
{
foreach (string link in Playlist)
{
try
{
video = youtube.GetVideo("https://www.youtube.com" + link);
videoFullName = video.FullName.Replace(" - YouTube.mp4", "");
//File.WriteAllBytes(PATH + videoFullName + ".mp4", cli.GetBytes(video));
IWaveSource videoSource = CSCore.Codecs.CodecFactory.Instance.GetCodec(new Uri(video.Uri));
Tuple<IWaveSource, String> package = Tuple.Create(videoSource, videoFullName);
ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadedConvertToMp3), package);
tw.WriteLine(link);
}
catch (Exception e)
{
if (File.Exists(PATH + videoFullName + ".mp3"))
File.Delete(PATH + videoFullName + ".mp3");
Console.WriteLine(e);
Playlist.Remove(link);
}
}
}
}
}
private static void ThreadedFfmpeg(object callback)
{
string t = (string)callback;
Console.WriteLine("Proccessing " + t);
Ffmpeg(t);
//ConvertToMp3(t);
}
/// <summary>
/// Method Converts .Mp4 to .Mp3 and Deletes .Mp4
/// </summary>
/// <param name="videoTitle">Just Video Name without .mp4 or PATH</param>
private static void Ffmpeg(string videoTitle)
{
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
//process.StartInfo.FileName = "ffmpeg.exe";
process.StartInfo.FileName = FFMPEGPATH;
process.StartInfo.Arguments = "-i \"" + PATH + videoTitle + ".mp4\"" +
" -qscale 0 \"" + PATH + videoTitle + ".mp3\"";
process.Start();
process.WaitForExit();
File.Delete(PATH + videoTitle + ".mp4");
}
private static void ThreadedDownloadYoutubeVideo(object callback)
{
string Url = "www.youtube.com" + (string)callback;
Console.WriteLine(Url);
string videoFullName = "foo";
try
{
var youtube = YouTube.Default;
var video = youtube.GetVideo(Url);
videoFullName = video.FullName.Replace(" - YouTube.mp4", "");
Console.WriteLine("Downloading " + videoFullName);
//File.WriteAllBytes(PATH + videoFullName + ".mp4", video.GetBytes());
IWaveSource videoSource = CSCore.Codecs.CodecFactory.Instance.GetCodec(new Uri(video.Uri));
ConvertToMp3(videoSource, videoFullName);
//Tuple<IWaveSource, String> package = Tuple.Create(videoSource, videoFullName);
//ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadedConvertToMp3), package);
}
catch (Exception e)
{
if (File.Exists(PATH + videoFullName + ".mp3"))
File.Delete(PATH + videoFullName + ".mp3");
Console.WriteLine(e);
}
}
*/
}
}