-
Notifications
You must be signed in to change notification settings - Fork 0
/
YoutubeDownloader.cs
123 lines (100 loc) · 4.19 KB
/
YoutubeDownloader.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace reAudioPlayerML
{
public partial class YoutubeDownloader : Form
{
NYoutubeDL.YoutubeDL dl;
YoutubeSyncer syncer;
public YoutubeDownloader(YoutubeSyncer youtubeSyncer)
{
InitializeComponent();
syncer = youtubeSyncer;
}
private void YoutubeDownloader_Load(object sender, EventArgs e)
{
}
private void txtLink_ButtonClick(object sender, EventArgs e)
{
Process.Start("https://music.youtube.com/");
}
private void txtDirectory_ButtonClick(object sender, EventArgs e)
{
folderBrowserDialog.SelectedPath = txtDirectory.Text;
if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
{
txtDirectory.Text = folderBrowserDialog.SelectedPath;
}
}
private void txtLink_TextChanged(object sender, EventArgs e)
{
chkSync.Enabled = txtLink.Text.Contains("list=");
}
string docs = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\reAudioPlayer\\Syncs";
private void btnDownload_Click(object sender, EventArgs e)
{
if (!Directory.Exists(txtDirectory.Text))
{
MessageBox.Show("Directory Invalid!", "reAudioPlayer Downloader", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
syncer.addOptimiseWatcher(txtDirectory.Text);
dl = new NYoutubeDL.YoutubeDL();
dl.YoutubeDlPath = AppContext.BaseDirectory + "ressources\\youtube-dl.exe";
dl.Options.FilesystemOptions.Continue = true;
dl.Options.GeneralOptions.IgnoreErrors = true;
dl.Options.FilesystemOptions.NoOverwrites = true;
dl.Options.PostProcessingOptions.AddMetadata = true;
dl.Options.PostProcessingOptions.ExtractAudio = true;
dl.Options.PostProcessingOptions.AudioFormat = NYoutubeDL.Helpers.Enums.AudioFormat.mp3;
dl.Options.FilesystemOptions.Output = txtDirectory.Text + "\\%(title)s.%(ext)s";
dl.VideoUrl = txtLink.Text;
dl.Options.GeneralOptions.Update = true;
dl.Options.VideoSelectionOptions.NoPlaylist = chkSync.Enabled && (chkSync.Checked || (MessageBox.Show("Do you want to download this as a playlist?", "Apollo Downloader", MessageBoxButtons.YesNo) == DialogResult.No));
if (dl.Options.VideoSelectionOptions.NoPlaylist)
{
var arg = dl.VideoUrl.Split('?')[1].Split('&')[0];
dl.VideoUrl = dl.VideoUrl.Split('?')[0] + "?" + arg;
}
Debug.WriteLine(dl.PrepareDownload());
if (chkSync.Checked && chkSync.Enabled)
{
var args = txtLink.Text.Split('?')[1].Split('&');
string filename = "";
foreach (var arg in args)
{
if (arg.Contains("list="))
filename = arg;
}
filename = Path.Combine(docs, filename + ".conf");
File.WriteAllText(filename, $"-ciw --add-metadata --embed-thumbnail -x --audio-format mp3 -o \"{dl.Options.FilesystemOptions.Output}\"");
if (!File.Exists(filename.Replace(".conf", ".arch")))
File.Create(filename.Replace(".conf", ".arch"));
syncer.sync();
}
else
{
dl.DownloadAsync();
dl.StandardOutputEvent += Dl_StandardOutputEvent;
dl.StandardErrorEvent += Dl_StandardErrorEvent; ;
}
}
private void Dl_StandardErrorEvent(object sender, string e)
{
syncer.Dl_StandardErrorEvent(sender, e);
}
private void Dl_StandardOutputEvent(object sender, string e)
{
syncer.Dl_StandardOutputEvent(sender, e);
}
}
}