Skip to content

Instantly share code, notes, and snippets.

@tonY1883
Created September 14, 2017 02:18
Show Gist options
  • 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");
}
}
@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