- [C#] Hướng dẫn lưu tất cả hình ảnh từ File Excel vào thư mục window
- [DATABASE] Hướng dẫn import và export hình ảnh image từ Sqlserver
- [DATABASE] Hướng dẫn sử dụng Hàm ASCII trong sqlserver
- [C#] Hướng dẫn fix lỗi Visual Studio 2022 not Support Target Net Framework 4.5.2
- [C#] Giới thiệu thư viện Sunny UI thiết kế giao diện trên Winform
- [DATABASE] Hướng dẫn thêm và cập nhật Extended Property Column trong Table Sqlserver
- [DEVEXPRESS] Hướng dẫn sử dụng Vertical Gridview để hiển thị thông tin sản phẩm
- [C#] Hướng dẫn sử dụng Json Schema để Validate chuỗi string có phải json
- [C#] Hướng dẫn sử dụng công cụ Clean Code trên Visual Studio
- [C#] Hướng dẫn Drag and Drop File vào RichTextBox
- [C#] Hướng dẫn tạo hiệu ứng Karaoke Text Effect Winform
- [C#] Sử dụng thư viện ZedGraph vẽ biểu đồ Line, Bar, Pie trên Winform
- [DATABASE] Hướng dẫn sort sắp xếp địa chỉ IP trên sqlserver sử dụng hàm PARSENAME
- [C#] Theo dõi sử kiện process Start hay Stop trên Winform
- [ASP.NET] Chia sẻ source code chụp hình sử dụng camera trên website
- [C#] Chạy ứng dụng trên Virtual Desktop (màn hình ảo) Winform
- [C#] Mã hóa và giải mã Data Protection API trên winform
- [C#] Hướng dẫn tạo Gradient Background trên Winform
- [DATABASE] Hướng dẫn convert Epoch to DateTime trong sqlserver
- [DATABASE] Hướng dẫn sử dụng STRING_AGG và CONCAT_WS trong sqlserver 2017
[C#] Hướng dẫn download video Youtube trong lap trinh csharp
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:
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!