video-snapshot

Capture video snapshot

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         video-snapshot
// @namespace    https://ciffelia.com/
// @version      1.0.1
// @description  Capture video snapshot
// @author       Ciffelia <[email protected]> (https://ciffelia.com/)
// @license      MIT
// @homepage     https://github.com/ciffelia/video-snapshot#readme
// @supportURL   https://github.com/ciffelia/video-snapshot/issues
// @include      *
// @grant        GM_registerMenuCommand
// @run-at       document-idle
// ==/UserScript==

(function () {
  'use strict';

  const createCanvasFromVideo = videoElm => {
    const canvasElm = document.createElement('canvas');
    canvasElm.width = videoElm.videoWidth;
    canvasElm.height = videoElm.videoHeight;

    const ctx = canvasElm.getContext('2d');
    ctx.drawImage(videoElm, 0, 0, videoElm.videoWidth, videoElm.videoHeight);

    return canvasElm
  };

  const downloadBlob = (blob, filename) => {
    const a = document.createElement('a');
    a.style.display = 'none';
    a.href = URL.createObjectURL(blob);
    a.download = filename;

    document.body.appendChild(a);
    a.click();
    document.body.removeChild(a);
  };

  const capture = () => {
    const video = document.querySelector('video');

    const canvas = createCanvasFromVideo(video);

    canvas.toBlob(blob => {
      downloadBlob(blob, 'snapshot.png');
    });
  };

  GM_registerMenuCommand('Capture video snapshot', capture, 'c');

}());