Windows Forms: Download Youtube Video in C#

This post shows you How to download video from Youtube using YoutubeExtractor in C# Windows Forms Application.

The YoutubeExtractor is a library for .NET, written in c# code that allows to download videos from YouTube and extract their audio track.

To play the demo, you shoud create a new windows froms application project, then right-click on your project, and select the Manage NuGet Packages from your visual studio.

Next, search youtube extractor and install it to your project.

The YoutubeExtractor library supports multiple target platforms, such as

+ .NET Framework 3.5 and higher

+ Windows Phone 8

+ WinRT

+ Xamarin.Android

+ Xamarin.iOS

Note that Windows Phone 8, WinRT, Xamarin.Android and Xamarin.iOS only support the extraction of the download URLs

Drag and drop TextBox, Combobox, ProgresBar, Label and Button controls from your Visual Studio toolbox to your Windows Forms Application, then design a simple UI that allows you to enter the youtube url into the textbox. Next, select the video resolution from the combobox, then click download button to download the youtube video.

youtube extractor in c#

Add the load event handler to your form allows you to initialize the combobox select index.

private void Form1_Load(object sender, EventArgs e)
{
    cboResolution.SelectedIndex = 0;
}

You can add the click event handler to the Download button allows you to download the youtube video as shown below.

private void btnDownload_Click(object sender, EventArgs e)
{
    progressBar.Minimum = 0;
    progressBar.Maximum = 100;
    //Get url video
    IEnumerable<VideoInfo> videos = DownloadUrlResolver.GetDownloadUrls(txtUrl.Text);
    VideoInfo video = videos.First(p => p.VideoType == VideoType.Mp4 && p.Resolution == Convert.ToInt32(cboResolution.Text));
    if (video.RequiresDecryption)
        DownloadUrlResolver.DecryptDownloadUrl(video);
    //Download video
    VideoDownloader downloader = new VideoDownloader(video, Path.Combine(Application.StartupPath + "\\", video.Title + video.VideoExtension));
    downloader.DownloadProgressChanged += Downloader_DownloadProgressChanged;
    //Create a new thread to download file
    Thread thread = new Thread(() => { downloader.Execute(); }) { IsBackground = true };
    thread.Start();
}

Add the DonwloadProcessChanged event allows you to update the ProgressBar and Label controls.

private void Downloader_DownloadProgressChanged(object sender, ProgressEventArgs e)
{
    //Update progressbar
    Invoke(new MethodInvoker(delegate ()
    {
        progressBar.Value = (int)e.ProgressPercentage;
        lblPercentage.Text = $"{string.Format("{0:0.##}", e.ProgressPercentage)}%";//C# 6.0
        progressBar.Update();
    }));
}

If you want to download audio from youtube you can modify your code as shown below.

var audioDownloader = new AudioDownloader(video, Path.Combine("C:/Downloads", video.Title + video.AudioExtension));

You can use the progress bar control to display the percentage of downloads completed. As you know, ProgressBar control is typically used when an application performs tasks such as copying files, downloading files or printing documents.

By using the ProgressBar in your application, you alert the user that the application is performing a lengthy task and that the application is still responding.