-
Notifications
You must be signed in to change notification settings - Fork 0
/
youtube-collector.cs
239 lines (196 loc) · 8.8 KB
/
youtube-collector.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
using YoutubeExplode;
using YoutubeExplode.Exceptions;
using YoutubeExplode.Videos.Streams;
using YoutubeExplode.Common;
using TagLib;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Processing;
using YoutubeExplode.Converter;
using System.Reflection;
namespace youtube_collector
{
public partial class Form1 : Form
{
string albumArtFilePath = "";
public Form1()
{
InitializeComponent();
// Event handlers
DownloadButton.Click += DownloadButton_Click;
ClearButton.Click += ClearButton_Click;
AlbumArtButton.Click += AlbumArtButton_Click;
}
private void AlbumArtButton_Click(object sender, EventArgs e)
{
LogRichTextBox.AppendText("Awaiting user-provided album art\n");
// Open OpenFileDialog to select album art from the user's computer
if (OpenFileDialogAlbumArt.ShowDialog() == DialogResult.OK)
{
// Store the selected album art file path
string selectedFilePath = OpenFileDialogAlbumArt.FileName;
// Update the logic to handle the selected album art file
HandleSelectedAlbumArt(selectedFilePath);
LogRichTextBox.AppendText($"Image selected: {selectedFilePath}\n");
}
}
private async void DownloadButton_Click(object sender, EventArgs e)
{
var youtube = new YoutubeClient();
// Full path to ffmpeg.exe
string ffmpegPath = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "ffmpeg", "ffmpeg.exe");
LogRichTextBox.AppendText("Locating highest quality audio stream");
try
{
// Get highest quality audio stream
var videoUrl = URLTextBox.Text;
var streamManifest = await youtube.Videos.Streams.GetManifestAsync(videoUrl);
var streamInfo = streamManifest.GetAudioOnlyStreams().GetWithHighestBitrate();
LogRichTextBox.AppendText($"Found highest quality audio stream: {streamInfo}\n");
// Download the audio stream
LogRichTextBox.AppendText($"Now downloading audio.{streamInfo.Container} and converting to mp3\n");
// Open SaveFileDialog to select the destination for the finished MP3
saveFileDialogMP3File.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyMusic);
saveFileDialogMP3File.FileName = $"{TitleTextBox.Text}.mp3";
if (saveFileDialogMP3File.ShowDialog() == DialogResult.OK)
{
string destinationPath = saveFileDialogMP3File.FileName;
// Rename the file based on the provided title
string title = TitleTextBox.Text;
string newFileName = $"{title}.mp3";
string newPath = Path.Combine(Path.GetDirectoryName(destinationPath), newFileName);
// Download the audio stream and wait for completion
await youtube.Videos.DownloadAsync(videoUrl, newPath, o => o
.SetContainer("mp3")
.SetFFmpegPath(ffmpegPath)
);
if (string.IsNullOrEmpty(albumArtFilePath))
{
await DownloadThumbnailAsync(videoUrl);
}
LogRichTextBox.AppendText($"Downloaded and saved MP3 to: {newPath}\nAwaiting metadata\n");
// Now that the download is complete, write metadata tags
WriteMetadataTags(newPath, albumArtFilePath);
System.IO.File.Delete(albumArtFilePath);
albumArtFilePath = "";
}
}
catch (VideoUnplayableException ex)
{
LogRichTextBox.AppendText($"Error: {ex.Message}\n");
}
catch (Exception ex)
{
LogRichTextBox.AppendText($"An unexpected error occurred: {ex.Message}\n");
}
}
private void WriteMetadataTags(string filePath, string albumArtPath)
{
try
{
var file = TagLib.File.Create(filePath);
// Clear existing tags
file.RemoveTags(TagTypes.AllTags);
file.Save();
file.Dispose();
file = TagLib.File.Create(filePath);
// Create an ID3v2 tag
var newTag = (TagLib.Id3v2.Tag)file.GetTag(TagTypes.Id3v2, true);
// Set metadata tags
newTag.Title = TitleTextBox.Text;
newTag.Performers = new[] { ArtistTextBox.Text };
newTag.Album = AlbumTextBox.Text;
newTag.Track = (uint)TrackNumberUpDown.Value;
// Embed album art
if (!string.IsNullOrEmpty(albumArtPath))
{
var picture = new Picture(albumArtPath);
newTag.Pictures = new IPicture[] { picture };
}
// Save changes
file.Save();
file.Dispose();
LogRichTextBox.AppendText("All metadata written to file\n");
}
catch (Exception ex)
{
LogRichTextBox.AppendText($"Error writing tags: {ex.Message}\n");
}
}
private async Task<string?> DownloadThumbnailAsync(string videoUrl)
{
var youtube = new YoutubeClient();
try
{
var video = await youtube.Videos.GetAsync(videoUrl);
var thumbnail = video.Thumbnails.TryGetWithHighestResolution();
LogRichTextBox.AppendText($"Thumbnail found: {thumbnail}\n");
if (thumbnail != null)
{
// Download and save the thumbnail to a file
string thumbnailPath = Path.Combine(Path.GetTempPath(), "thumbnail.jpg");
using (HttpClient httpClient = new())
{
var thumbnailBytes = await httpClient.GetByteArrayAsync(thumbnail.Url);
System.IO.File.WriteAllBytes(thumbnailPath, thumbnailBytes);
}
// Store the downloaded album art file path
return HandleSelectedAlbumArt(thumbnailPath);
}
else
{
LogRichTextBox.AppendText("Thumbnail not found.\n");
return null;
}
}
catch (Exception ex)
{
LogRichTextBox.AppendText($"Error downloading thumbnail: {ex.Message}\n");
return null;
}
}
private string HandleSelectedAlbumArt(string selectedFilePath)
{
// Store the selected album art file path
albumArtFilePath = selectedFilePath;
// Implement cropping logic to make the image square
CropAlbumArt(selectedFilePath);
return albumArtFilePath;
}
private void CropAlbumArt(string imagePath)
{
string croppedImagePath = "";
try
{
using (var originalImage = SixLabors.ImageSharp.Image.Load(imagePath))
{
// Determine the size of the square
int size = Math.Min(originalImage.Width, originalImage.Height);
// Calculate the coordinates for cropping centered on the actual center of the image
int x = (originalImage.Width - size) / 2;
int y = (originalImage.Height - size) / 2;
// Crop the image using ImageSharp
var croppedImage = originalImage.Clone(ctx => ctx.Crop(new SixLabors.ImageSharp.Rectangle(x, y, size, size)));
// Save the cropped image to a new file
croppedImagePath = Path.Combine(Path.GetTempPath(), "cropped_thumbnail.jpg");
croppedImage.Save(croppedImagePath);
}
// Replace the original file with the cropped one
System.IO.File.Copy(croppedImagePath, imagePath, true);
}
catch (Exception ex)
{
LogRichTextBox.AppendText($"Error cropping album art: {ex.StackTrace}\n");
}
}
private void ClearButton_Click(object sender, EventArgs e)
{
// Clear all textboxes and log
URLTextBox.Clear();
TitleTextBox.Clear();
ArtistTextBox.Clear();
AlbumTextBox.Clear();
TrackNumberUpDown.Value = 0;
LogRichTextBox.Clear();
}
}
}