Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.IO;
- using System.Text.RegularExpressions;
- using ZennoLab.CommandCenter;
- using ZennoLab.InterfacesLibrary;
- using ZennoLab.InterfacesLibrary.ProjectModel;
- using ZennoLab.InterfacesLibrary.ProjectModel.Collections;
- using ZennoLab.InterfacesLibrary.ProjectModel.Enums;
- using ZennoLab.Macros;
- using Global.ZennoExtensions;
- using ZennoLab.Emulation;
- using YoutubeExplode;
- using YoutubeExplode.Models;
- using Tyrrrz.Extensions;
- using System.Threading.Tasks;
- namespace YoutubeDownloader
- {
- public static class YouTube
- {
- private static string NormalizeId(string input)
- {
- string id = "";
- if (!YoutubeClient.TryParseVideoId(input, out id))
- return id;
- }
- /// <summary>
- /// Turns file size in bytes into human-readable string
- /// </summary>
- private static string NormalizeFileSize(long fileSize)
- {
- string[] units = { "B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };
- double size = fileSize;
- int unit = 0;
- while (size >= 1024)
- {
- size /= 1024;
- ++unit;
- }
- return "{size:0.#} {units[unit]}";
- }
- private static async Task MainAsync()
- {
- // Client
- var client = new YoutubeClient();
- // Get the video ID
- string id = "https://www.youtube.com/watch?v=rW2ay4RVVnI";
- id = NormalizeId(id);
- // Get the video info
- var videoInfo = await client.GetVideoInfoAsync(id);
- // Get the most preferable stream
- var streamInfo = videoInfo.MixedStreams
- .OrderBy(s => s.VideoQuality)
- .Last();
- string normalizedFileSize = NormalizeFileSize(streamInfo.ContentLength);
- // Compose file name, based on metadata
- string fileExtension = streamInfo.Container.GetFileExtension();
- string fileName = "{videoInfo.Title}.{fileExtension}";
- // Remove illegal characters from file name
- fileName = fileName.Except(Path.GetInvalidFileNameChars());
- await client.DownloadMediaStreamAsync(streamInfo, fileName);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement