Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Created by SharpDevelop.
- * User: Admin
- * Date: 5/10/2021
- * Time: 8:52 PM
- *
- * This program is an example of how to download all youtube thumbnails.
- * For working program, please visit my website dosaidsoft.com and look under the software category.
- */
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Windows.Forms;
- using System.Web;
- using System.Net;
- using System.Text;
- using System.IO;
- namespace ThumbExtractor
- {
- /// <summary>
- /// Description of MainForm.
- /// </summary>
- public partial class MainForm : Form
- {
- public const string STR_FIND_TITLE = "\"title\":\"";
- public WebClient happy;
- public MainForm()
- {
- InitializeComponent();
- happy = new WebClient();
- }
- void BtnSaveClick(object sender, EventArgs e)
- {
- int count = txtThumb.Lines.Length;
- int cur = 0;
- if (txtPath.Text == "")
- {
- MessageBox.Show("You need to browse for a directory or type one in first!", "Select Folder Please", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
- return;
- }
- if (!Directory.Exists(txtPath.Text))
- {
- MessageBox.Show("The directory you put in the box does not exist. Please make it first.", "Folder does not exist", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
- return;
- }
- if (!txtPath.Text.EndsWith("\\"))
- txtPath.Text += "\\";
- btnSave.Enabled = false;
- prog.Maximum = count;
- prog.Value = 0;
- foreach (string vid in txtThumb.Lines)
- {
- GetThumb(vid);
- cur++;
- btnSave.Text = cur.ToString() + " / " + count.ToString();
- prog.Value = cur;
- Refresh();
- }
- btnSave.Text = "Save Thumbs";
- btnSave.Enabled = true;
- }
- public void GetThumb(string url)
- {
- //another way for later - provides more data: https://www.youtube.com/get_video_info?video_id=
- //but it's much slower
- string api = "https://noembed.com/embed?url=" + url;
- string ytCode;
- if (url.Contains("youtu.be"))
- ytCode = url.Substring(url.LastIndexOf('/') + 1);
- else
- ytCode = GetArgs(url, "v", '?');
- ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
- string result = happy.DownloadString(api);
- int loc = result.IndexOf(STR_FIND_TITLE);
- loc += STR_FIND_TITLE.Length;
- string theTitle = result.Substring(loc, result.IndexOf('"', loc) - loc);
- string toLoad = "https://i.ytimg.com/vi/" + ytCode + "/hqdefault.jpg";
- string fixedFile = txtPath.Text + ScrubFileName(theTitle) + ".jpg";
- happy.DownloadFile(toLoad, fixedFile);
- picPrev.ImageLocation = fixedFile;
- if (File.Exists(picPrev.ImageLocation))
- picPrev.Load();
- }
- public string GetArgs(string args, string key, char query)
- {
- var iqs = args.IndexOf(query);
- return iqs == -1
- ? string.Empty
- : HttpUtility.ParseQueryString(iqs < args.Length - 1
- ? args.Substring(iqs + 1) : string.Empty)[key];
- }
- public string ScrubFileName(string value)
- {
- StringBuilder sb = new StringBuilder(value);
- foreach (char item in Path.GetInvalidFileNameChars())
- {
- sb.Replace(item.ToString(), "");
- }
- return sb.ToString();
- }
- void BtnHelpClick(object sender, EventArgs e)
- {
- 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\". " +
- "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 " +
- "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 " +
- "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 " +
- "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 " +
- "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);
- }
- void BtnBrowseClick(object sender, EventArgs e)
- {
- if (fBrowser.ShowDialog() == DialogResult.OK)
- {
- if (!fBrowser.SelectedPath.EndsWith("\\"))
- txtPath.Text = fBrowser.SelectedPath + "\\";
- else
- txtPath.Text = fBrowser.SelectedPath;
- }
- }
- void ChkDataCheckedChanged(object sender, EventArgs e)
- {
- 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 " +
- "much slower unfortunately.", "Not available", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement