Advertisement
Guest User

Untitled

a guest
Jul 24th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.44 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading;
  9. using System.IO;
  10. using System.Text.RegularExpressions;
  11. using ZennoLab.CommandCenter;
  12. using ZennoLab.InterfacesLibrary;
  13. using ZennoLab.InterfacesLibrary.ProjectModel;
  14. using ZennoLab.InterfacesLibrary.ProjectModel.Collections;
  15. using ZennoLab.InterfacesLibrary.ProjectModel.Enums;
  16. using ZennoLab.Macros;
  17. using Global.ZennoExtensions;
  18. using ZennoLab.Emulation;
  19. using YoutubeExplode;
  20. using YoutubeExplode.Models;
  21. using Tyrrrz.Extensions;
  22. using System.Threading.Tasks;
  23.  
  24.  
  25. namespace YoutubeDownloader
  26. {
  27.     public static class YouTube
  28.     {
  29.        
  30.         private static string NormalizeId(string input)
  31.         {
  32.             string id = "";
  33.             if (!YoutubeClient.TryParseVideoId(input, out id))
  34.             return id;
  35.         }
  36.  
  37.         /// <summary>
  38.         /// Turns file size in bytes into human-readable string
  39.         /// </summary>
  40.         private static string NormalizeFileSize(long fileSize)
  41.         {
  42.             string[] units = { "B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };
  43.             double size = fileSize;
  44.             int unit = 0;
  45.  
  46.             while (size >= 1024)
  47.             {
  48.                 size /= 1024;
  49.                 ++unit;
  50.             }
  51.  
  52.             return "{size:0.#} {units[unit]}";
  53.         }
  54.        
  55. private static async Task MainAsync()
  56.         {
  57.             // Client
  58.             var client = new YoutubeClient();
  59.  
  60.             // Get the video ID
  61.             string id = "https://www.youtube.com/watch?v=rW2ay4RVVnI";
  62.             id = NormalizeId(id);
  63.  
  64.             // Get the video info
  65.             var videoInfo = await client.GetVideoInfoAsync(id);
  66.  
  67.  
  68.             // Get the most preferable stream
  69.             var streamInfo = videoInfo.MixedStreams
  70.                 .OrderBy(s => s.VideoQuality)
  71.                 .Last();
  72.             string normalizedFileSize = NormalizeFileSize(streamInfo.ContentLength);
  73.  
  74.             // Compose file name, based on metadata
  75.             string fileExtension = streamInfo.Container.GetFileExtension();
  76.             string fileName = "{videoInfo.Title}.{fileExtension}";
  77.  
  78.             // Remove illegal characters from file name
  79.             fileName = fileName.Except(Path.GetInvalidFileNameChars());
  80.  
  81.  
  82.             await client.DownloadMediaStreamAsync(streamInfo, fileName);
  83.         }
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement