项目作者: feross

项目描述 :
Capture video screenshot from a `
高级语言: JavaScript
项目地址: git://github.com/feross/capture-frame.git
创建时间: 2016-09-15T22:26:20Z
项目社区:https://github.com/feross/capture-frame

开源协议:MIT License

下载


capture-frame travis npm downloads javascript style guide

Capture video screenshot from a <video> tag (at the current time)

Sauce Test Status

Works in the browser with browserify! This module is used by WebTorrent Desktop.

install

  1. npm install capture-frame

usage

simple example

  1. const captureFrame = require('capture-frame')
  2. const frame = captureFrame('.video') // Buffer that contains .png file data
  3. // show the captured video frame in the DOM
  4. const image = document.createElement('img')
  5. image.width = frame.width
  6. image.height = frame.height
  7. image.src = window.URL.createObjectURL(new window.Blob([frame.image]))
  8. document.body.appendChild(image)

complete example

  1. const captureFrame = require('capture-frame')
  2. const video = document.createElement('video')
  3. video.addEventListener('canplay', onCanPlay)
  4. video.volume = 0
  5. video.autoplay = true
  6. video.muted = true // most browsers block autoplay unless muted
  7. video.setAttribute('crossOrigin', 'anonymous') // optional, when cross-domain
  8. video.src = 'http://example.com/test.mp4'
  9. function onCanPlay () {
  10. video.removeEventListener('canplay', onCanPlay)
  11. video.addEventListener('seeked', onSeeked)
  12. video.currentTime = 2 // seek 2 seconds into the video
  13. }
  14. function onSeeked () {
  15. video.removeEventListener('seeked', onSeeked)
  16. const frame = captureFrame(video)
  17. // unload video element, to prevent memory leaks
  18. video.pause()
  19. video.src = ''
  20. video.load()
  21. // show the captured image in the DOM
  22. const image = document.createElement('img')
  23. image.width = frame.width
  24. image.height = frame.height
  25. image.src = window.URL.createObjectURL(new window.Blob([frame.image]))
  26. document.body.appendChild(image)
  27. }

api

frame = captureFrame(video, [format])

Capture a video frame the the video tag specified by video. This can be a
reference to a video element in the page, or a string CSS selector. This must
refer to a video element.

Optionally, specify a format for the image to be captured in. Must be one of
“png”, “jpeg”, or “webp”.

The returned object, frame, has three properties:

frame.image

The captured image, as a Buffer.

frame.width

The captured image’s width, as a number.

frame.height

The captured image’s height, as a number.

license

MIT. Copyright (c) Feross Aboukhadijeh.