forked from PackJC/YoutubeToMP3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm1.cs
142 lines (116 loc) · 5.42 KB
/
Form1.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
using MediaToolkit.Model;
using MediaToolkit;
using YoutubeExplode;
using YoutubeExplode.Videos.Streams;
namespace YoutubeToMP3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private async void downloadButton_ClickAsync(object sender, EventArgs e)
{
progressBar.Value = 0;
// Check if the URL TextBox is empty or null
if (string.IsNullOrWhiteSpace(urlBox.Text))
{
MessageBox.Show("Please enter a YouTube URL.", "Input Required", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
// Validate if the URL is a YouTube URL
try
{
var uri = new Uri(urlBox.Text);
var host = uri.Host.ToLower();
if (!host.Contains("youtube.com") && !host.Contains("youtu.be"))
{
MessageBox.Show("Please enter a valid YouTube URL.", "Invalid URL", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
catch (Exception)
{
MessageBox.Show("Please enter a valid YouTube URL.", "Invalid URL", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
// Initialize YoutubeExplode client and fetch video metadata
var youtube = new YoutubeClient();
var videoId = YoutubeExplode.Videos.VideoId.Parse(urlBox.Text);
var video = await youtube.Videos.GetAsync(videoId);
var videoTitle = video.Title;
// Sanitize the video title to be a valid filename
var invalidChars = Path.GetInvalidFileNameChars();
var safeFileName = string.Join("_", videoTitle.Split(invalidChars, StringSplitOptions.RemoveEmptyEntries)).Trim();
// Use SaveFileDialog to ask the user where to save the MP3 file
using (var saveFileDialog = new SaveFileDialog())
{
saveFileDialog.Filter = "MP3 Files|*.mp3";
saveFileDialog.Title = "Save MP3 File";
saveFileDialog.FileName = safeFileName; // Suggest a filename
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
// Process the URL and download the video
try
{
// Get the stream manifest and select the best muxed stream (audio + video)
var streamManifest = await youtube.Videos.Streams.GetManifestAsync(videoId);
var streamInfo = streamManifest.GetMuxedStreams().GetWithHighestVideoQuality();
// Temporarily download the video
var videoPath = Path.Combine(Path.GetTempPath(), $"{videoId}.mp4");
// Initialize the progress bar
progressBar.Value = 0;
progressBar.Maximum = 100;
// Create an instance of IProgress<double> to report progress
var progressHandler = new Progress<double>(progress =>
{
// Update progress bar on the UI thread
Invoke((MethodInvoker)delegate
{
progressBar.Value = (int)(progress * 100);
});
});
// Download the video with progress reporting
await youtube.Videos.Streams.DownloadAsync(streamInfo, videoPath, progressHandler);
// Convert to MP3 using MediaToolkit
var inputFile = new MediaFile { Filename = videoPath };
var outputFile = new MediaFile { Filename = saveFileDialog.FileName };
using (var engine = new Engine())
{
engine.GetMetadata(inputFile);
engine.Convert(inputFile, outputFile);
}
// Clean up the temporary video file
File.Delete(videoPath);
// Update UI and show completion message
MessageBox.Show("Download complete!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
urlBox.Text = string.Empty;
progressBar.Value = 0; // Reset progress bar after completion
}
catch (Exception ex)
{
MessageBox.Show($"An error occurred: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
progressBar.Value = 0; // Reset progress bar in case of error
}
}
}
}
private void helpToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void clearButton_Click(object sender, EventArgs e)
{
urlBox.Clear();
progressBar.Value = 0;
}
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
var aboutForm = new AboutBox1();
aboutForm.Show();
}
private void checkForUpdateToolStripMenuItem_Click(object sender, EventArgs e)
{
}
}
}