Git Product home page Git Product logo

capture-video-frame's Introduction

capture-video-frame

A micro-library for taking screenshots from a running HTML5 video.

Installation

$ npm install --save capture-video-frame

or

$ yarn add capture-video-frame

Example

HTML:

<video id="my-video" autoplay>
  <source src="movie.mp4" type="video/mp4" />
</video>

<img id="my-screenshot" />

JavaScript:

import captureVideoFrame from "capture-video-frame";

// ...

// Call captureVideoFrame() when you want to record a screenshot
const frame = captureVideoFrame("my-video-id", "png");

// Show the image
const img = document.getElementById("my-screenshot");
img.setAttribute("src", frame.dataUri);

// Upload the image
const formData = new FormData();
formData.append("file", frame.blob, `my-screenshot.${frame.format}`);

const response = await fetch("/api/upload", {
  method: "POST",
  body: formData,
});

Browser support

Tested on current Chrome and Firefox.

API

captureVideoFrame(source, format, quality)

Parameters

source (element or string, mandatory) Source video. If string, id of the element.

format (string, optional) Output image format. Can be either png or jpeg. png is the default.

quality (number, optional) A Number between 0 and 1 indicating image quality if the requested type is image/jpeg or image/webp. The default value is 0.92.

Return value

  • Object with blob, dataUri, and format properties if the capture succeeded
  • false if the capture failed.

Image blob can be easily uploaded to the server using XHR2 FormData API.

Image data URI can be easily set as <img> src attribute to show the captured image.

License

MIT

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.