[C#] Get YouTube Thumbnail

DataMine
by DataMine · 7 posts
9 years ago in .Net (C#, VB, etc)
Posted 9 years ago · Author
This is a modified version of my YouTube thumbnail downloader here: viewtopic.php?f=109&t=9911 It's purpose is to display the video thumbnail on the form. I use this to quickly check lots of video thumbnails for different reasons.

As with the thumbnail downloader, most of the code is for sanitizing the input and is also thread safe and ready for multithreading.

It can accept 3 types of input:

-A basic YouTube url:
https://www.youtube.com/watch?v=VIDEO ID

-A video thumbnail link:
https://i.ytimg.com/vi/VIDEO ID/0.jpg

- A plain video id

Usage:
Code
getThumbnail(INPUT HERE);


Method:
Code
private void getThumbnail(object oYTurl)
{
   string sYTurl = oYTurl.ToString(), vidID = null;

   //if the input isn't empty
   if(!string.IsNullOrEmpty(sYTurl))
   {
      //if the input matches a basic YT link
      if(Regex.IsMatch(sYTurl, "(http|https)://www\\.youtube\\.com/watch\\?v=.*"))
      {
         //remove everything but the video id
         vidID = Regex.Replace(sYTurl, "(http|https)://www\\.youtube\\.com/watch\\?v=", "");
      }
      //if the input is a thumbnail url
      else if (Regex.IsMatch(sYTurl, "(http|https)://i.*\\.ytimg.com/vi/.*/0.jpg"))
      {
         //scrub the url leaving behind the video id
         vidID = Regex.Replace(sYTurl, "(http|https)://i.*\\.ytimg.com/vi/", "").Replace("/0.jpg","");
      }
      else
      {
         //if the input isn't a basic YT link and not a thumbnail link
         //then check if it is roughly the same size as a video id
         if (sYTurl.Length > 5 && sYTurl.Length < 12)
         {
            using (WebClient wc = new WebClient())
            {
               try
               {
                  //pass the input to the video api
                  wc.DownloadString("https://gdata.youtube.com/feeds/api/videos/" + sYTurl);

                  //if no errors, the input is a valid video id
                  vidID = sYTurl;
               }
               catch (WebException we)
               {
                  //The input returns a 404 and therefore is not a valid id
                  HttpWebResponse errorResponse = we.Response as HttpWebResponse;
                  if (errorResponse.StatusCode == HttpStatusCode.NotFound || errorResponse.StatusCode == HttpStatusCode.RequestTimeout)
                  {
                     return;
                  }
               }
            }
         }
      }

      //if the vidID is not empty
      if(!string.IsNullOrEmpty(vidID))
      {
         createPB("https://i.ytimg.com/vi/" + vidID.ToString() + "/maxresdefault.jpg");
                        createPB("https://i.ytimg.com/vi/" + vidID.ToString() + "/0.jpg");
      }   
   }
}


This is the createPB method I use to display the resulting thumbnail. It's quick and dirty but does the job
Code
public void createPB(object url)
        {
            PictureBox pb = new PictureBox();
            pb.Size = new Size(120, 90);
            pb.Tag = url.ToString();
            pb.SizeMode = PictureBoxSizeMode.Zoom;
            pb.DoubleClick += loadimg;

            try
            {
                pb.Load(url.ToString());
            }
            catch (WebException we)
            {
                HttpWebResponse errorResponse = we.Response as HttpWebResponse;
                if (errorResponse.StatusCode == HttpStatusCode.NotFound || errorResponse.StatusCode == HttpStatusCode.RequestTimeout)
                {
                    return;
                }
            }

            if (this.InvokeRequired)
            {
                this.BeginInvoke((MethodInvoker)delegate()
                {
                    flowLayoutPanel1.Controls.Add(pb);
                });
            }
            else
            {
                flowLayoutPanel1.Controls.Add(pb);
            }           
        }


This is the loadimg method that allows you to open the thumbnail in your browser by double clicking the picturebox
Code
 public void loadimg(object sender, EventArgs e)
        {
            PictureBox pb = (PictureBox)sender;

            Process.Start(pb.Tag.ToString());
        }
Posted 6 years ago
Does this method still work? I seen this post is a bit old.
Posted 6 years ago · Author
smykey wrote:
Does this method still work? I seen this post is a bit old.


Yeah, it does. I'm using it in a newish program I made for myself to collect thumbnails.
Posted 6 years ago
Does anyone know where to find a good youtube to mp3 converter.. It seems the ones that are out that I find lately have been giving errors, and copywrite errors. I used to be able to download mp3 from a youtube to mp3 converter no problem. I can still do so it has just became harder to find youtube links that will actually convert. Does anyone have a good custom one? If so could you share it or share what you use?
Posted 6 years ago · Author
@smykey


I use Jdownloader2. It can get you the video, audio, thumbnail and subtitles if you want them to almost any video.

http://jdownloader.org/jdownloader2
Posted 6 years ago
@DateMine

I am not looking for a video converter. I need a youtube to mp3 converter. Something that will take the youtube links then convert them into a mp3 format. I found a good working one for now but they usually only last about a week before copywriters get them and then they are unable to convert, and it takes up to 25 minutes to find a working link that will convert. I was wondering if anyone could make something custom, or knows anything that will work at all times?
Posted 6 years ago · Author
@smykey


JDownloader 2 does that. When you queue up a link it will give you a list of files to pick from: the video file, the audio (mp3/m4a etc..), the thumbnail and the subtitles. In your case you would just ignore everything but the mp3.

Image

If the format you want isn't there, you can right click on the item in the queue and select "add additional variants" and it will give you list of even more stuff available to download from the link. Typically the video or audio in different formats.

Image

You can also change how the YouTube plugin works so it only displays the audio when you queue up a link, saving you time.

Image


And the best part about JDownloader2 is not only can you queue up entire playlists or hundreds of videos at a time but it also works with most sites, not just YouTube. They also have an active community site where you can request features and bug fixes.

https://board.jdownloader.org/?langid=1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Sign in

Already have an account? Sign in here

SIGN IN NOW

Create an account

Sign up for a new account in our community. It's easy!

REGISTER A NEW ACCOUNT
Select a forum Protection     Help & Support     Introductions     Mafia News     IMVU News General Discussion     IMVU Lounge        IMVU Series / Roleplaying        Social Games     Mafia Market     Mafia Tools        Premium IMVU Tools        Off Topic Tools     Off Topic     Contests Creator Corner     Graphics Design        Photoshop        GIMP     Basic Creator Help     Catalog And Product Showcase     3D Meshing        3Ds Max        Sketchup        Blender Gangsters with Connections     White Hat Activities        Google Hacking        Trackers Programming Corner     Coding        Python        .Net (C#, VB, etc)        Flash        JAVA        Autoit        Batch        HTML & CSS        Javascript        PHP        Other        IMVU Homepage Codes           General           About me Panel           Messages Panel           Special Someone Panel           Visitors Panel           New Products Panel           Rankings Panel           Wishlist Panel           My Badges Panel           Outfits Panel           Url Panel           Groups Panel           Slideshow Panel           My Room Panel           Sandbox panel           Layouts     Help & Requests Free Credits     Approved Methods     Submit Methods Free Money     Approved Methods     Submit Methods Adult Corner     Get Mafia AP Here     AP Lounge        AP Social Games        Casual Dating Tips     IMVU Slave Market & Escorts