Windows Forms: Download Youtube Video in C#

How to make a Youtube downloader using YoutubeExtractor in C#. YoutubeExtractor is a library for .NET, written in C#, that allows to download videos from YouTube and/or extract their audio track

Step 1Click New Project, then select Visual C# on the left, then Windows and then select Windows Forms Application. Name your project "DownloadYoutube" and then click OK

download youtube in c#Step 2: Right click on your project select Manage NuGet Packages -> Search youtube extractor -> Install

youtube extractor in c#Step 3: Design youtube download form as below

youtube extractor in c#

Step 4: Add code to handle download file

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

VIDEO TUTORIALS