How to Download Youtube Video in C#

By FoxLearn 11/23/2024 4:19:51 AM   26.02K
To download a video from YouTube in a C# Windows Forms Application using the YoutubeExtractor library, you can follow these steps.

YouTube is one of the most popular platforms for video content, but sometimes, users might want to download a video for offline viewing. In this article, we’ll walk through how to build a YouTube downloader using C# that includes a progress bar for showing the download status.

What is YoutubeExtractor?

YoutubeExtractor is a .NET library written in C# that enables downloading YouTube videos and extracting their audio tracks with ease.

It 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

How to Download YouTube Video in C#?

Open Visual Studio, then create a new Windows Froms Application project, then right-click on your project, and select the Manage NuGet Packages from your Visual Studio.

Next, Add the YoutubeExtractor library to your project by opening your Nuget Packages Manager, then search for 'youtube extractor' and install it to your project.

C# Downloader youtube video

Drag and drop the 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.

youtube downloader c#

To download a youtube video you need to select the video resolution from the combobox, then click download button.

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;
}

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

// youtube download c# video
private void btnDownload_Click(object sender, EventArgs e)
{
    progressBar.Minimum = 0; // Set the minimum value for progress
    progressBar.Maximum = 100; // Set the maximum value for progress
    // c# youtube downloader, get url youtube video
    IEnumerable<VideoInfo> videos = DownloadUrlResolver.GetDownloadUrls(txtUrl.Text);
    // Select the desired video format (MP4) and resolution
    VideoInfo video = videos.First(p => p.VideoType == VideoType.Mp4 && p.Resolution == Convert.ToInt32(cboResolution.Text));
    // Check if decryption is needed and decrypt the URL if required
    if (video.RequiresDecryption)
        DownloadUrlResolver.DecryptDownloadUrl(video);
    // Create a VideoDownloader instance to download the video, c# youtube video downloader
    VideoDownloader downloader = new VideoDownloader(video, Path.Combine(Application.StartupPath + "\\", video.Title + video.VideoExtension));
    // Attach an event handler to track download progress
    downloader.DownloadProgressChanged += Downloader_DownloadProgressChanged;
    // c# create a new thread to download file
    Thread thread = new Thread(() => { downloader.Execute(); }) { IsBackground = true };
    thread.Start();
}

The following code demonstrates how to implement a YouTube downloader with a progress bar in C#.

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

private void Downloader_DownloadProgressChanged(object sender, ProgressEventArgs e)
{
    // Update ProgressBar and Label on the UI thread
    Invoke(new MethodInvoker(delegate ()
    {
        // Update ProgressBar value
        progressBar.Value = (int)e.ProgressPercentage;
        // Update Label to display percentage
        lblPercentage.Text = $"{string.Format("{0:0.##}", e.ProgressPercentage)}%";//C# 6.0
        progressBar.Update();
    }));
}

The Invoke method is used to update the UI elements from the background thread safely. Since ProgressChanged is fired on a different thread, we use Invoke to marshal the update to the UI thread.

The ProgressBar value is updated with the download percentage (e.ProgressPercentage). The label displays the exact percentage in a readable format ({0:0.##}) for better clarity.

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.

This is simple way to create a YouTube downloader in C# with a progress bar to track the download status. By using multithreading, the UI remains responsive while the video is being downloaded. The progress is updated in real-time, allowing users to see how much of the video has been downloaded.