How to Download Youtube Video in C#

By FoxLearn 7/18/2024 7:41:50 AM   25.14K
To download a video from YouTube in a C# Windows Forms Application using the YoutubeExtractor library, you can follow these steps.

Creating a YouTube video downloader in C# involves several steps, Here's a basic outline of how to use third-party libraries for downloading.

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 for 'youtube extractor' and install it to your project.

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

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

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.

c# youtube video downloader

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 downloader c#
private void btnDownload_Click(object sender, EventArgs e)
{
    progressBar.Minimum = 0;
    progressBar.Maximum = 100;
    // c# get url youtube 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);
    // c# youtube video downloader
    VideoDownloader downloader = new VideoDownloader(video, Path.Combine(Application.StartupPath + "\\", video.Title + video.VideoExtension));
    downloader.DownloadProgressChanged += Downloader_DownloadProgressChanged;
    // c# 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)
{
    // c# 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.