Files
video-frames/README.md

174 lines
5.6 KiB
Markdown
Raw Normal View History

2022-07-08 09:15:27 +05:30
[circleci-image]: https://circleci.com/gh/n3r4zzurr0/video-frames.svg?style=shield
[circleci-url]: https://circleci.com/gh/n3r4zzurr0/video-frames
2022-04-30 12:39:12 +05:30
[npm-image]: https://img.shields.io/npm/v/video-frames.svg
[npm-url]: https://npmjs.org/package/video-frames
[size-image]: https://img.shields.io/bundlephobia/minzip/video-frames@latest
[size-url]: https://bundlephobia.com/result?p=video-frames@latest
2022-07-08 09:15:27 +05:30
[vulnerabilities-image]: https://snyk.io/test/npm/video-frames/badge.svg
[vulnerabilities-url]: https://snyk.io/test/npm/video-frames
2022-04-30 20:36:37 +05:30
[standard-image]: https://img.shields.io/badge/code_style-standard-brightgreen.svg
[standard-url]: https://standardjs.com
2022-07-08 09:15:27 +05:30
[license-image]: https://img.shields.io/github/license/n3r4zzurr0/video-frames.svg
[license-url]: https://github.com/n3r4zzurr0/video-frames/blob/main/LICENSE
2022-04-30 12:39:12 +05:30
2022-05-05 11:05:24 +05:30
# video-frames
2022-07-08 09:15:27 +05:30
[![circleci][circleci-image]][circleci-url] [![npm][npm-image]][npm-url] [![size][size-image]][size-url] [![known vulnerabilities][vulnerabilities-image]][vulnerabilities-url] [![javascript style guide][standard-image]][standard-url] [![license][license-image]][license-url]
2022-04-30 12:39:12 +05:30
2022-05-05 11:05:24 +05:30
Client-side video frames extraction as base64 encoded images.
2022-04-30 12:39:12 +05:30
2022-09-09 09:46:03 +05:30
**[Demo](https://n3r4zzurr0.in/video-frames/) / [CodePen](https://codepen.io/n3r4zzurr0/pen/abqOXpQ?editors=1010)**
2022-05-05 11:41:13 +05:30
2022-07-08 09:15:27 +05:30
<hr>
2022-05-05 12:36:40 +05:30
:warning: Doesn't work in Safari on iOS
2022-04-30 20:36:37 +05:30
2022-04-30 20:39:01 +05:30
From [Apple Developer Documentation](https://developer.apple.com/documentation/webkitjs/canvasrenderingcontext2d/1630282-drawimage),
2022-04-30 20:36:37 +05:30
> The image object can be an `img` element, a `canvas` element, or a `video` element. **Use of the `video` element is not supported in Safari on iOS**, however.
2022-07-08 09:21:35 +05:30
<hr><br>
2022-07-08 09:15:27 +05:30
2022-05-09 11:11:43 +05:30
## Installation
**npm**
2022-04-30 12:39:12 +05:30
```
npm install video-frames
```
2022-05-09 11:11:43 +05:30
**CDN**
```html
<script src="https://cdn.jsdelivr.net/npm/video-frames@1/dist/videoframes.umd.min.js"></script>
```
or
```html
<script src="https://unpkg.com/video-frames@1"></script>
```
2022-04-30 12:39:12 +05:30
## Usage
```js
const videoFrames = require('video-frames');
videoFrames({
// Big Buck Bunny (1080p 60fps)
url: 'http://distribution.bbb3d.renderfarming.net/video/mp4/bbb_sunflower_1080p_60fps_normal.mp4',
// Extract 10 evenly spaced (time-wise) frames
count: 10
}).then((frames) => {
// frames is an array of objects
// [
// {
// offset: (timestamp in seconds)
// image: (base64 encoded image)
// },
// ...
// ]
});
```
## API
2022-05-02 22:43:33 +05:30
### `videoFrames(options)`
2022-04-30 12:39:12 +05:30
Returns a `Promise` for when all frames have been extracted. There are a few properties that can be set in `options`.
2022-05-02 22:43:33 +05:30
#### `options`
2022-04-30 12:39:12 +05:30
2022-05-02 22:43:33 +05:30
* #### `url` (required)
2022-04-30 12:39:12 +05:30
2022-05-02 22:43:33 +05:30
Default value: *empty*
2022-04-30 12:39:12 +05:30
2022-05-05 11:05:24 +05:30
The URL (self, remote [an [`Access-Control-Allow-Origin`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Origin) header must be present in case of a remote URL], or blob) of the source video from which the frames are to be extracted. Since the [`video`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/video) element is used in the extraction process, the allowed formats are the ones that are playable in it. You can search for the supported formats on [caniuse.com/?search=video%20format](https://caniuse.com/?search=video%20format)
2022-04-30 12:39:12 +05:30
2022-05-02 22:43:33 +05:30
* #### `width`
2022-04-30 12:39:12 +05:30
2022-05-02 22:43:33 +05:30
Default value: `128`
2022-04-30 12:39:12 +05:30
2022-05-02 22:43:33 +05:30
Width of the extracted frames in pixels.
2022-04-30 12:39:12 +05:30
If no value for `width` is set, but a value for `height` is set, then the `width` will be calculated using the video dimensions.
2022-05-02 22:43:33 +05:30
* #### `height`
2022-04-30 12:39:12 +05:30
2022-05-02 22:43:33 +05:30
Default value: `auto`
2022-04-30 12:39:12 +05:30
2022-05-02 22:43:33 +05:30
Height of the extracted frames in pixels.
2022-04-30 12:39:12 +05:30
If not set, `height` is calculated automatically from the value of `width` using the video dimensions.
2022-05-02 22:43:33 +05:30
* #### `format`
2022-04-30 12:39:12 +05:30
2022-05-02 22:43:33 +05:30
Default value: `image/png`
2022-04-30 12:39:12 +05:30
2022-05-02 22:43:33 +05:30
MIME type of the extracted frames.
2022-04-30 12:39:12 +05:30
Since the [`canvas`](https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API) element is used for drawing the frames and `toDataURL(format)` is used for reading them as base64 encoded images, the allowed MIME types are the ones that are supported by `toDataURL`.
2022-05-02 22:43:33 +05:30
From [MDN](https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/toDataURL#parameters),
2022-04-30 12:39:12 +05:30
2022-05-02 22:43:33 +05:30
> **`toDataURL(type)`**
>
> ...
>
> **`type`**
>
> A string indicating the image format. The default type is `image/png`; this image format will be also used if the specified type is not supported.
2022-04-30 12:39:12 +05:30
2022-05-02 22:43:33 +05:30
So, if a type is not supported, it will fall back to `image/png`.
2022-04-30 12:39:12 +05:30
2022-05-02 22:43:33 +05:30
* #### `startTime`
2022-04-30 12:39:12 +05:30
2022-05-02 22:43:33 +05:30
Default value: `0`
2022-04-30 12:39:12 +05:30
2022-05-02 22:43:33 +05:30
Start timestamp (in seconds) of the range from where the frames are to be extracted.
2022-04-30 12:39:12 +05:30
It will be ignored if a valid value for `offsets` is set.
2022-05-02 22:43:33 +05:30
* #### `endTime`
2022-04-30 12:39:12 +05:30
2022-05-02 22:43:33 +05:30
Default value: *Video Duration*
2022-04-30 12:39:12 +05:30
2022-05-02 22:43:33 +05:30
End timestamp (in seconds) of the range from where the frames are to be extracted.
2022-04-30 12:39:12 +05:30
It will be ignored if a valid value for `offsets` is set.
2022-05-02 22:43:33 +05:30
* #### `count`
2022-04-30 12:39:12 +05:30
2022-05-02 22:43:33 +05:30
Default value: `1`
2022-04-30 12:39:12 +05:30
2022-05-02 22:43:33 +05:30
Number of frames to be extracted from the range set by `startTime` and `endTime`.
2022-04-30 12:39:12 +05:30
The frames are extracted from evenly spaced timestamps across the range.
It will be ignored if a valid value for `offsets` is set.
2022-05-02 22:43:33 +05:30
* #### `offsets`
2022-04-30 12:39:12 +05:30
2022-05-02 22:43:33 +05:30
Default value: `[]`
2022-04-30 12:39:12 +05:30
2022-05-02 22:43:33 +05:30
Array of timestamps (in seconds) to extract frames at.
2022-04-30 12:39:12 +05:30
If a valid value for `offsets` is set, `startTime`, `endTime`, and `count` are **ignored**.
2022-05-02 22:43:33 +05:30
* #### `onLoad`
2022-04-30 12:39:12 +05:30
2022-05-02 22:43:33 +05:30
Default value: `false`
2022-04-30 12:39:12 +05:30
2022-05-02 22:43:33 +05:30
Function to be called when the source video has loaded and the extraction process has started.
2022-04-30 12:39:12 +05:30
2022-05-02 22:43:33 +05:30
```js
onLoad: () => { console.log('video loaded') }
```
2022-04-30 12:39:12 +05:30
2022-05-02 22:43:33 +05:30
* #### `onProgress`
2022-04-30 12:39:12 +05:30
2022-05-02 22:43:33 +05:30
Default value: `false`
2022-04-30 12:39:12 +05:30
2022-05-02 22:43:33 +05:30
Function to be called on every successful frame extraction.
```js
onProgress: (framesExtracted) => { console.log(`${framesExtracted} frames extracted`) }
```
```js
onProgress: (framesExtracted, totalFrames) => { console.log(`${framesExtracted} of ${totalFrames} frames extracted`) }
```
2022-04-30 12:39:12 +05:30
## License
2022-07-08 09:15:27 +05:30
MIT © [Utkarsh Verma](https://github.com/n3r4zzurr0)