Skip to content

Instantly share code, notes, and snippets.

@phaseOne
Last active July 29, 2022 06:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phaseOne/039feb9757477cd1ed3c5b90a7251fd6 to your computer and use it in GitHub Desktop.
Save phaseOne/039feb9757477cd1ed3c5b90a7251fd6 to your computer and use it in GitHub Desktop.
Download the currently displayed frame of a YouTube video as a PNG
const saveBlob = (blob, fileName) => {
const a = document.createElement("a");
document.body.appendChild(a);
a.style = "display: none";
const url = window.URL.createObjectURL(blob);
a.href = url;
a.download = fileName;
a.click();
window.URL.revokeObjectURL(url);
};
const platformConfigs = {
"vimeo.com": { "selector": "div.player_container > div.player > div.vp-video-wrapper > div.vp-video > div > video" },
"youtube.com": { "selector": "#movie_player > div.html5-video-container > video" }
};
const supportedPlatforms = Object.keys(platformConfigs);
const platform = supportedPlatforms.find( platform => window.location.hostname.includes(platform) );
if (platform === undefined) { throw new Error("unable to identify video platform") }
else { console.debug("Platform:", platform); }
(function () {
const videoEl = document.querySelector(platformConfigs[platform].selector);
const canvas = new OffscreenCanvas(videoEl.videoWidth, videoEl.videoHeight);
canvas.getContext('2d', { alpha: false }).drawImage(videoEl, 0, 0, canvas.width, canvas.height);
canvas.convertToBlob({ options: { type: 'image/png' } }).then( blob => saveBlob(blob, 'frame.png') );
}());
@phaseOne
Copy link
Author

phaseOne commented Jun 6, 2022

Simply paste this into the DevTools console, and a file named frame.png will download. Note: the resolution of the still frame is dependent upon the current video resolution. If the playback resolution is set to Auto, then the image may not be the highest resolution available due to buffering. If you want the highest resolution, manually set the resolution to the highest, and then run the script.

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