[C#] Download YouTube Thumbnail

DataMine
by DataMine · 4 posts
10 years ago in .Net (C#, VB, etc)
Posted 10 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