Last active
July 29, 2022 06:53
-
-
Save phaseOne/039feb9757477cd1ed3c5b90a7251fd6 to your computer and use it in GitHub Desktop.
Download the currently displayed frame of a YouTube video as a PNG
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') ); | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.