[C#] Download YouTube Thumbnail

DataMine
by DataMine · 4 posts
9 years ago in .Net (C#, VB, etc)
Posted 9 years ago · Author
This is a basic function I wrote to download a YouTube video thumbnail image. Most of the code is for sanitizing the input to make sure that it doesn't crash. This method is thread safe and already set up for multi-threading so you can download multiple thumbnails at a time.

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=csharp file=Untitled.txt]dlThumbnail(INPUT HERE);[/code]

Method:
[code=csharp file=Untitled.txt]private void dlThumbnail(object oYTurl)
{
string sYTurl = oYTurl.ToString(), thumbnailUrl = 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
sYTurl = Regex.Replace(sYTurl, "(http|https)://www\\.youtube\\.com/watch\\?v=", "");

//create the thumbnail url
thumbnailUrl = string.Format("{0}{1}{2}", "https://i.ytimg.com/vi/", sYTurl, "/0.jpg");
}
//if the input already is a thumbnail url
else if (Regex.IsMatch(sYTurl, "(http|https)://i.*\\.ytimg.com/vi/.*/0.jpg"))
{
//pass it to the thumbnail variable
thumbnailUrl = sYTurl;
}
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
//create the thumbnail url
thumbnailUrl = string.Format("{0}{1}{2}", "https://i.ytimg.com/vi/", sYTurl, "/0.jpg");
}
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 thumbnail url is not empty
if(!string.IsNullOrEmpty(thumbnailUrl))
{
//check the local drive for an existing file
if(!File.Exists(saveDir + Regex.Replace(thumbnailUrl,"(http|https)://i.*\\.ytimg.com/vi/","").Replace("/0.jpg","")))
{
//if the file doesn't exist, download it
using(WebClient _wc = new WebClient())
{
_wc.DownloadFile(thumbnailUrl, saveDir + Regex.Replace(thumbnailUrl, "(http|https)://i.*\\.ytimg.com/vi/", "").Replace("/0.jpg", ".jpg"));
}
}
}
}
}[/code]
Posted 9 years ago
There's an automated program that does that...
Posted 9 years ago · Author
QLT wrote:
There's an automated program that does that...

That does what? Grab a YouTube video thumbnail? Well, that's cool..but I prefer to do things myself and this code can be combined with other code to do other tasks.

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