NEWS

[C#] Hướng dẫn download video Youtube trong lap trinh csharp

[C#] Hướng dẫn download video Youtube trong lap trinh csharp
Đăng bởi: Thảo Meo - Lượt xem: 16311 18:43:05, 21/03/2017C#   In bài viết

Bài viết này, mình hướng dẫn các bạn cách download video Youtube về máy tính của mình sử dụng ngôn ngữ lập trình csharp.

Trong tutorial này, mình sử dụng thư viện Youtube Extractor để download video về. 

Các bạn, có thể sử dụng công cụ quản lý thư viện Nuget để tải về. Nếu bạn nào còn chưa biết sử dụng Nuget có thể tìm hiểu qua những bài mình đã giới thiệu.

Ứng dụng download video bằng lập trình Csharp này, thư viện Youtube Extractor có hỗ trợ cho chúng ta chọn chất lượng video để tải về, các bạn có thể chọn chất lượng: 320, 480, hoặc 720HD.

Giao diện demo ứng dụng:

download video youtube lap trinh csharp

Trong bài viết này: 

Mình sử dụng các component sau.

1. Textbox (txtLink)

2. Combobox (cobResolution)

3. Progressbar 

4. Và nút button để download

Source code bằng C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using YoutubeExtractor;

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

        private void Form1_Load(object sender, EventArgs e)
        {
            cobResolution.SelectedIndex = 0;
        }
        string link;
        private void btnDownload_Click(object sender, EventArgs e)
        {
            progressBar1.Minimum = 0;
            progressBar1.Maximum = 100;
            IEnumerable videos = DownloadUrlResolver.GetDownloadUrls(txtLink.Text);
            VideoInfo video = videos.First(p => p.VideoType == VideoType.Mp4 && p.Resolution == Convert.ToInt32(cobResolution.Text));
            if (video.RequiresDecryption)
               DownloadUrlResolver.DecryptDownloadUrl(video);
            //Link video 
             link = Application.StartupPath + "" + video.Title + video.VideoExtension;
            link = link.Replace("|", "_").Replace(" ", "_");
            VideoDownloader downloader = new VideoDownloader(video, link);
            downloader.DownloadProgressChanged += DownloadProgressChanged;
            Thread thread = new Thread(() => { downloader.Execute(); }) { IsBackground = true };
            thread.Start();
        }
        private void DownloadProgressChanged(object sender, ProgressEventArgs e)
        {
            Invoke(new MethodInvoker(delegate ()
            {
                progressBar1.Value = (int)e.ProgressPercentage;
                lblPercent.Text = $"{string.Format("{0:0,##}", e.ProgressPercentage)}%";
                progressBar1.Update();
                if(e.ProgressPercentage == 100)
                {
                    Process.Start(link);
                }
            }));
        }
    }
}

Video demo ứng dụng

CHÚC CÁC BẠN THÀNH CÔNG!

DOWNLOAD SOURCE

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[C#] Hướng dẫn download video Youtube trong lap trinh csharp
Đăng bởi: Thảo Meo - Lượt xem: 16311 18:43:05, 21/03/2017C#   In bài viết

CÁC BÀI CÙNG CHỦ ĐỀ

Đọc tiếp
.