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");
}
}
@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