Skip to content

Instantly share code, notes, and snippets.

@panzi
Last active May 7, 2022 10:59
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save panzi/da8ce741931f3f943f3213a189f88099 to your computer and use it in GitHub Desktop.
Save panzi/da8ce741931f3f943f3213a189f88099 to your computer and use it in GitHub Desktop.
Make a screenshot of a video.

Create a new bookmark and use this as the link:

javascript:(function()%7Bvar%20t%3Ddocument.querySelectorAll(%22video%22)%2Ce%3D%5B%5D%3Bfor(var%20a%20of%20t)a.paused%7C%7Ca.ended%7C%7Ce.push(a)%3Bvar%20n%3D0%3D%3D%3De.length%3Ft%3Ae%2Co%3Dnull%2Cr%3D0%3Bfor(var%20a%20of%20n)%7Bvar%20d%3Da.videoWidth*a.videoHeight%3Bd%3Er%26%26(o%3Da%2Cr%3Dd)%7Do%3Ffunction(t)%7Bvar%20e%3Ddocument.createElement(%22canvas%22)%3Be.width%3Dt.videoWidth%2Ce.height%3Dt.videoHeight%2Ccontext%3De.getContext(%222d%22)%3Bvar%20a%3Dnew%20Date%3Bcontext.drawImage(t%2C0%2C0%2Ct.videoWidth%2Ct.videoHeight)%3Bvar%20n%3De.toDataURL(%22image%2Fpng%22)%2Co%3Ddocument.createElement(%22a%22)%3Bo.href%3Dn%2Co.download%3D%22Screenshot_%22%2Ba.getFullYear()%2B%22_%22%2BString(a.getMonth()%2B1).padStart(2%2C%220%22)%2B%22_%22%2BString(a.getDate()).padStart(2%2C%220%22)%2B%22_%22%2BString(a.getHours()).padStart(2%2C%220%22)%2B%22-%22%2BString(a.getMinutes()).padStart(2%2C%220%22)%2B%22-%22%2BString(a.getSeconds()).padStart(2%2C%220%22)%2B%22.png%22%2Co.click()%7D(o)%3Aalert(%22No%20video%20found!%22)%7D)()

When you click that link a screenshot of the the biggest playing video is made (if none is playing then of the biggest loaded video). If now video is found you might need to view only the (i)frame of the video.

javascript:(function() {
function screenshot(video) {
var canvas = document.createElement('canvas');
canvas.width = video.videoWidth;
canvas.height = video.videoHeight;
context = canvas.getContext('2d');
var now = new Date();
context.drawImage(video, 0, 0, video.videoWidth, video.videoHeight);
var url = canvas.toDataURL('image/png');
var link = document.createElement('a');
link.href = url;
link.download = 'Screenshot_'+
now.getFullYear()+'_'+String(now.getMonth()+1).padStart(2, '0')+'_'+String(now.getDate()).padStart(2, '0')+'_'+
String(now.getHours()).padStart(2, '0')+'-'+String(now.getMinutes()).padStart(2, '0')+'-'+String(now.getSeconds()).padStart(2, '0')+
'.png';
link.click();
}
var videos = document.querySelectorAll('video');
var playing = [];
for (var video of videos) {
if (!video.paused && !video.ended) {
playing.push(video);
}
}
var selected = playing.length === 0 ? videos : playing;
var biggestVideo = null;
var maxArea = 0;
for (var video of selected) {
var area = video.videoWidth * video.videoHeight;
if (area > maxArea) {
biggestVideo = video;
maxArea = area;
}
}
if (biggestVideo) {
screenshot(biggestVideo);
} else {
alert('No video found!');
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment