Skip to content

Instantly share code, notes, and snippets.

@tonY1883
Created September 14, 2017 02:18
Show Gist options
  • Star 47 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tonY1883/a3b85925081688de569b779b4657439b to your computer and use it in GitHub Desktop.
Save tonY1883/a3b85925081688de569b779b4657439b to your computer and use it in GitHub Desktop.
A small trick to check if youtube video exist with its id.
function validVideoId(id) {
var img = new Image();
img.src = "http://img.youtube.com/vi/" + id + "/mqdefault.jpg";
img.onload = function () {
checkThumbnail(this.width);
}
}
function checkThumbnail(width) {
//HACK a mq thumbnail has width of 320.
//if the video does not exist(therefore thumbnail don't exist), a default thumbnail of 120 width is returned.
if (width === 120) {
alert("Error: Invalid video id");
}
}
@technoknol
Copy link

Was looking for this only. Thanks man !

@dkiselew
Copy link

dkiselew commented Dec 7, 2018

OMG, you are genius!

@eltonmorais
Copy link

I'm using the same URL, but making a GET request. On response 200, EXIST. On anything else, don't exist.

@ashsepra
Copy link

nice one

@QiQiBiBi
Copy link

QiQiBiBi commented Mar 5, 2020

Good try!

@MrMaavin
Copy link

MrMaavin commented May 12, 2020

I'm using the same URL, but making a GET request. On response 200, EXIST. On anything else, don't exist.

@eltonmorais Can you maybe share your Code?

@ciskosv
Copy link

ciskosv commented Feb 5, 2021

Hope you don't mind, I used your logic to port it to python, my script works with a given string with all capital letters and randomly switch some letters to lowercase to find out which is the right combination to get a working ID. I used it for a riddle and your script helped to solve it. Thanks!

https://github.com/ciskosv/FindCorrectYoutubeID

@biomiker
Copy link

This still seems to work very nicely, I'm using it with HEAD instead of GET so there is no image response body at all, just a 200 for exists and 404 for not exists.

@biomiker
Copy link

I just want to add the GET and HEAD notwithstanding, for in-browser use your original code is key because of cross-origin restrictions. Thanks again!

@tientq64
Copy link

tientq64 commented Apr 3, 2021

I found another one, it returns a smaller image:
"http://img.youtube.com/vi/" + id + "/default.jpg"

@Esn024
Copy link

Esn024 commented May 17, 2021

This will work EXCEPT for a very small number of Youtube IDs that do exist, but nevertheless lack a preview image and have that 120px-wide one instead. Typically this is because the account owner had deleted them then undeleted them, and sometimes the preview images don't come back. An example is this one: "iUjUnrpsp6I".
I just remove them from the results manually like so, but maybe there's a better way:

function validVideoId(id) {
var img = new Image();

	if (id != 'iUjUnrpsp6I') {
		img.src = "http://img.youtube.com/vi/" + id + "/mqdefault.jpg";
		img.onload = function () {
			checkThumbnail(this.width);
		}
	}
}

@gavinsykes
Copy link

This has saved my sanity, thank you for this! My codebase is in PHP so I took the liberty of taking the principle and rewriting it. I may as well paste here what I did for if anyone needs it!:

function getYouTubeThumbnailDimensionsByID(string $id): Array {
    $image_curl = curl_init();
    curl_setopt($image_curl, CURLOPT_URL, "http://img.youtube.com/vi/$id/mqdefault.jpg");
    curl_setopt($image_curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($image_curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
    $image_result = curl_exec($image_curl);

    $image = imagecreatefromstring($image_result);

    return [
        "x" => imagesx($image),
        "y" => imagesy($image)
    ];
}

Then of course just check if x == 120.

@benant
Copy link

benant commented Mar 21, 2022

There are exceptions, but it's still the best way to simply check in Javascript. I tried to compare sha1 hash values with difficulty.
Thank you.

@RemLawrence
Copy link

Thank you for the trick, saved my day

@mark34
Copy link

mark34 commented Sep 14, 2022

you beauty! I've found nothing else that works.

@The3ven
Copy link

The3ven commented Jan 1, 2024

thanks man it works

@The3ven
Copy link

The3ven commented Jan 1, 2024

my approach is this btw :
im using venilla js.

const is_valid_ytd_video = async (id) => {
let response = await axios.request("https://www.youtube.com/embed/" + id)
return response.data.toLowerCase().includes("Video unavailable".toLowerCase());
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment