Advertisement
Guest User

Download many YouTube thumbnails in C#

a guest
May 11th, 2021
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.91 KB | None | 0 0
  1. /*
  2.  * Created by SharpDevelop.
  3.  * User: Admin
  4.  * Date: 5/10/2021
  5.  * Time: 8:52 PM
  6.  *
  7.  * This program is an example of how to download all youtube thumbnails.
  8.  * For working program, please visit my website dosaidsoft.com and look under the software category.
  9.  */
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Drawing;
  13. using System.Windows.Forms;
  14. using System.Web;
  15. using System.Net;
  16. using System.Text;
  17. using System.IO;
  18.  
  19. namespace ThumbExtractor
  20. {
  21.     /// <summary>
  22.     /// Description of MainForm.
  23.     /// </summary>
  24.     public partial class MainForm : Form
  25.     {
  26.         public const string STR_FIND_TITLE = "\"title\":\"";
  27.         public WebClient happy;
  28.        
  29.         public MainForm()
  30.         {
  31.             InitializeComponent();
  32.             happy = new WebClient();
  33.         }
  34.         void BtnSaveClick(object sender, EventArgs e)
  35.         {
  36.             int count = txtThumb.Lines.Length;
  37.             int cur = 0;
  38.            
  39.             if (txtPath.Text == "")
  40.             {
  41.                 MessageBox.Show("You need to browse for a directory or type one in first!", "Select Folder Please", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  42.                 return;
  43.             }
  44.             if (!Directory.Exists(txtPath.Text))
  45.             {
  46.                 MessageBox.Show("The directory you put in the box does not exist. Please make it first.", "Folder does not exist", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  47.                 return;
  48.             }
  49.             if (!txtPath.Text.EndsWith("\\"))
  50.                 txtPath.Text += "\\";
  51.            
  52.             btnSave.Enabled = false;
  53.             prog.Maximum = count;
  54.             prog.Value = 0;
  55.             foreach (string vid in txtThumb.Lines)
  56.             {
  57.                 GetThumb(vid);
  58.                 cur++;
  59.                 btnSave.Text = cur.ToString() + " / " + count.ToString();
  60.                 prog.Value = cur;
  61.                 Refresh();
  62.             }
  63.             btnSave.Text = "Save Thumbs";
  64.             btnSave.Enabled = true;
  65.         }
  66.        
  67.         public void GetThumb(string url)
  68.         {
  69.             //another way for later - provides more data: https://www.youtube.com/get_video_info?video_id=
  70.             //but it's much slower
  71.            
  72.             string api = "https://noembed.com/embed?url=" + url;
  73.             string ytCode;
  74.             if (url.Contains("youtu.be"))
  75.                 ytCode = url.Substring(url.LastIndexOf('/') + 1);
  76.             else
  77.                 ytCode = GetArgs(url, "v", '?');
  78.             ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
  79.             string result = happy.DownloadString(api);
  80.             int loc = result.IndexOf(STR_FIND_TITLE);
  81.             loc += STR_FIND_TITLE.Length;
  82.             string theTitle = result.Substring(loc, result.IndexOf('"', loc) - loc);
  83.             string toLoad = "https://i.ytimg.com/vi/" + ytCode + "/hqdefault.jpg";
  84.             string fixedFile = txtPath.Text + ScrubFileName(theTitle) + ".jpg";
  85.             happy.DownloadFile(toLoad, fixedFile);
  86.             picPrev.ImageLocation = fixedFile;
  87.             if (File.Exists(picPrev.ImageLocation))
  88.                 picPrev.Load();
  89.         }
  90.  
  91.         public string GetArgs(string args, string key, char query)
  92.         {
  93.             var iqs = args.IndexOf(query);
  94.             return iqs == -1
  95.                 ? string.Empty
  96.                 : HttpUtility.ParseQueryString(iqs < args.Length - 1
  97.                     ? args.Substring(iqs + 1) : string.Empty)[key];
  98.         }
  99.        
  100.         public string ScrubFileName(string value)
  101.         {
  102.            StringBuilder sb = new StringBuilder(value);
  103.            foreach (char item in Path.GetInvalidFileNameChars())
  104.            {
  105.               sb.Replace(item.ToString(), "");
  106.            }
  107.            return sb.ToString();
  108.         }
  109.         void BtnHelpClick(object sender, EventArgs e)
  110.         {
  111.             MessageBox.Show("You paste a list of youtube video links in the box. For getting large batches of links, use the firefox add-on called \"copy selected links\". " +
  112.                             "You then select all the videos (if more than about 50, then do it in segments) and copy them. If there are duplicates or erronious ones, then " +
  113.                             "remove them. You can use the tool at https://dedupelist.com to remove the duplicates, but they shouldn't cause an issue. The images will be stored " +
  114.                             "in the directory you select with the browse button and will be named with the title of the video. Save data will save all the videos info like " +
  115.                             "title, date, url, etc. to a file called YouTube.txt located in the same directory. This is intended to be used in conjunction with a good downloader " +
  116.                             "like mediahuman which will get the youtube videos or audio files to go along with this.\n\n(C) 2021 DosaidSoft. V" + Application.ProductVersion, "Help and About", MessageBoxButtons.OK, MessageBoxIcon.Information);
  117.         }
  118.         void BtnBrowseClick(object sender, EventArgs e)
  119.         {
  120.             if (fBrowser.ShowDialog() == DialogResult.OK)
  121.             {
  122.                 if (!fBrowser.SelectedPath.EndsWith("\\"))
  123.                     txtPath.Text = fBrowser.SelectedPath + "\\";
  124.                 else
  125.                     txtPath.Text = fBrowser.SelectedPath;
  126.             }
  127.         }
  128.         void ChkDataCheckedChanged(object sender, EventArgs e)
  129.         {
  130.             MessageBox.Show("Sorry, this feature is not available in this version. If you want it, please contact me on my website dosaidsoft.com. It will make the download process " +
  131.                             "much slower unfortunately.", "Not available", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  132.         }
  133.     }
  134. }
  135.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement