John Work John Work - 4 years ago 121
C# Question

How do I download multiple videos from YouTube with Multiline TextBox?

How do I download multiple videos from YouTube with Multiline TextBox?

Here is my code so far:

var split = txtURL.Text.Split(',');
for (int i = 0; i < split.Length; i++)
{
IEnumerable<VideoInfo> videolar = DownloadUrlResolver.GetDownloadUrls(split[i]);
VideoInfo video = videolar.First(p => p.VideoType == VideoType.Mp4 && p.Resolution == Convert.ToInt32(comboBox1.Text));
if (video.RequiresDecryption)
{
DownloadUrlResolver.DecryptDownloadUrl(video);
}
VideoDownloader downloader = new VideoDownloader(video, Path.Combine(Application.StartupPath + "\\ " + video.Title + GetSafeFileName(video.Title, '_') + video.VideoExtension));
downloader.DownloadProgressChanged += Downloader_DownloadProgressChanged;
Thread thread = new Thread(() => { downloader.Execute(); }) { IsBackground = true };
thread.Start();
}

Answer Source

People who want to start this, Must add this nuget package:

Install-Package YoutubeExtractor

Here is the page: YoutubeExtractor

Finally I download the URL Extractor and create the hole project.

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    private void Form1_Load(object sender, EventArgs e)
    {
    }
    BackgroundWorker bw = new BackgroundWorker();
    int currentLine = 0;
    ProgressBar progressBar1 = new ProgressBar();
    string[] lines;
    string resolution;

    private void btnIndir_Click(object sender, EventArgs e)
    {
        resolution = comboBox1.Text;  //360, 480, 720, 1080

        lines = txtURL.Text.Split(System.Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
        bw.DoWork += new DoWorkEventHandler(bw_DoWork);
        bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(DownloadNextLine);
        bw.RunWorkerAsync();
    }

    public void bw_DoWork(object sender, DoWorkEventArgs e)
    {
        progressBar1.Value = 0;
        var split = lines[currentLine].Split(new char[]{','}, StringSplitOptions.RemoveEmptyEntries);
        for (int i = 0; i < split.Length; i++)
        {
            IEnumerable<VideoInfo> videolar = DownloadUrlResolver.GetDownloadUrls(split[i]);
            VideoInfo video = videolar.First(p => p.VideoType == VideoType.Mp4 && p.Resolution == Convert.ToInt32(resolution));
            if (video.RequiresDecryption)
            {
                DownloadUrlResolver.DecryptDownloadUrl(video);
            }

            VideoDownloader downloader = new VideoDownloader(video, System.IO.Path.Combine(Application.StartupPath + "\\ " + GetSafeFileName(video.Title) + '_' + video.VideoExtension));
            downloader.DownloadProgressChanged += downloader_DownloadProgressChanged;
            downloader.Execute();
        }
    }

    private string GetSafeFileName(string title)
    {
        title.Replace("/", "").Replace(",", "").Replace("\\", "").Replace("+", "");
        return title;
    }

    void downloader_DownloadProgressChanged(object sender, ProgressEventArgs e)
    {
        SetPbValue((int)e.ProgressPercentage);
    }

    void SetPbValue(int percent)
    {
        if (progressBar2.InvokeRequired)
            progressBar2.Invoke(new Action<int>(SetPbValue), percent);
        else
            progressBar2.Value = percent;
    }

    public void DownloadNextLine(object sender, RunWorkerCompletedEventArgs e)
    {
        currentLine++;
        if (currentLine == lines.Length)
            return;
        bw.RunWorkerAsync();
    }

}

It tested and worked perfectly. And now i am happy to have this useful app. But if you error persists, I am definitely sure that one of the lines has an invalid video page url. And the comma ',' at the end is for the error explanation, not end of url. Because the items in someText.Split(',') will never have any comma.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download