Windows Forms: Download Youtube Video in C#

How to make a Youtube downloader using YoutubeExtractor in C#. The YoutubeExtractor is a library for .NET, written in C#, 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 Manage NuGet Packages. Next, search youtube extractor and install it.

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 a TextBox, Combobox, Progressbar, label and Button from your visual studio toolbox into your Windows Forms Application, then design a simple UI that allows you to enter a youtube url into the textbox, select a video resolution from the combobox, then click download button to download the youtube video

youtube extractor in c#

You can add your code to handle the download button click event as shown below

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Windows.Forms;
using YoutubeExtractor;

namespace DownloadYoutube
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

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

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

        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.