UNPKG

5.02 kBMarkdownView Raw
1# simple-thumbnail
2[![All Contributors](https://img.shields.io/badge/all_contributors-3-orange.svg?style=flat-square)](#contributors)
3[![npm version](https://badge.fury.io/js/simple-thumbnail.svg)](https://badge.fury.io/js/simple-thumbnail)
4[![Build Status](https://travis-ci.org/ScottyFillups/simple-thumbnail.svg?branch=master)](https://travis-ci.org/ScottyFillups/simple-thumbnail)
5[![Coverage Status](https://coveralls.io/repos/github/ScottyFillups/simple-thumbnail/badge.svg?branch=master)](https://coveralls.io/github/ScottyFillups/simple-thumbnail?branch=master)
6[![install size](https://packagephobia.now.sh/badge?p=simple-thumbnail)](https://packagephobia.now.sh/result?p=simple-thumbnail)
7
8A minimal library that produces thumbnails from images and videos using FFmpeg.
9
10## Installation
11
12```bash
13$ npm install simple-thumbnail --save
14```
15
16## Usage
17
18```js
19const genThumbnail = require('simple-thumbnail')
20
21// promise
22genThumbnail('path/to/image.png', 'output/file/path.png', '250x?')
23 .then(() => console.log('done!'))
24 .catch(err => console.error(err))
25
26// async/await
27async function run () {
28 try {
29 await genThumbnail('http://www.example.com/foo.webm', 'output/file/path.png', '250x?')
30 console.log('Done!')
31 } catch (err) {
32 console.error(err)
33 }
34}
35
36run()
37
38// genThumbnail also supports piping to write streams, so you can do this with Express!
39app.get('/some/endpoint', (req, res) => {
40 genThumbnail('path/to/video.webm', res, '150x100')
41 .then(() => console.log('done!'))
42 .catch(err => console.error(err))
43})
44```
45
46## Getting FFmpeg
47
48For those who don't have FFmpeg installed, there's an NPM package that installs it for you: https://www.npmjs.com/package/ffmpeg-static
49
50```js
51const ffmpeg = require('ffmpeg-static')
52const genThumbnail = require('simple-thumbnail')
53
54async function download () {
55 await genThumbnail('https://www.w3schools.com/Html/mov_bbb.webm', 'bunny.webm', '150x?', {
56 path: ffmpeg.path
57 })
58
59 console.log('Done!')
60}
61
62download()
63```
64
65## API
66
67#### genThumbnail(input, output, size, [config])
68
69Returns of a `Promise` which resolves on thumbnail creation.
70
71#### input
72
73Type: `String | stream.Readable`
74
75The URL, file path, or read-stream of an image or video.
76
77#### output
78
79Type: `String | stream.Writable | Null`
80
81The file path of the generated thumbnail, a write-stream, or null. If null, `genThumbnail` will resolve to a read-stream that you can pipe somewhere. If you're specifying a file path, make sure the directories exist.
82
83#### size
84
85Type: `String`
86
87The dimensions of the generated thumbnail. The `size` argument may have one of the following formats:
88
89* `150x100`: set a fixed output size.
90* `150x?`: set a fixed width and compute the height automatically.
91* `?x100`: set a fixed height and compute the width automatically.
92* `50%`: rescale both width and height to given percentage.
93
94#### config
95
96Type: `Object`
97
98A configuration object, see details below.
99
100#### config.path
101
102Type: `String`
103
104The path of the `ffmpeg` binary. If omitted, the path will be set to the `FFMPEG_PATH` environment variable. If the environment variable is not set, `ffmpeg` will be invoked directly (ie. `ffmpeg [...]`).
105
106#### config.seek
107
108Type: `String`
109
110Seeks the video to the provided time. The time must be in the following form: `hh:mm:ss[.ms]`, eg. `00:00:42.23`. If omitted, the video time will be set to `00:00:00` (ie. the first frame).
111
112## Contributors
113
114<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
115<!-- prettier-ignore -->
116| [<img src="https://avatars2.githubusercontent.com/u/18666879?v=4" width="100px;"/><br /><sub><b>Philip Scott</b></sub>](http://scottyfillups.io)<br />[💻](https://github.com/ScottyFillups/simple-thumbnail/commits?author=ScottyFillups "Code") [⚠️](https://github.com/ScottyFillups/simple-thumbnail/commits?author=ScottyFillups "Tests") [📖](https://github.com/ScottyFillups/simple-thumbnail/commits?author=ScottyFillups "Documentation") | [<img src="https://avatars1.githubusercontent.com/u/2668906?v=4" width="100px;"/><br /><sub><b>cmd430</b></sub>](https://github.com/cmd430)<br />[💻](https://github.com/ScottyFillups/simple-thumbnail/commits?author=cmd430 "Code") [🤔](#ideas-cmd430 "Ideas, Planning, & Feedback") | [<img src="https://avatars0.githubusercontent.com/u/682269?v=4" width="100px;"/><br /><sub><b>Andre-John Mas</b></sub>](http://terra-azure.org)<br />[💻](https://github.com/ScottyFillups/simple-thumbnail/commits?author=ajmas "Code") [🐛](https://github.com/ScottyFillups/simple-thumbnail/issues?q=author%3Aajmas "Bug reports") |
117| :---: | :---: | :---: |
118<!-- ALL-CONTRIBUTORS-LIST:END -->
119<!-- ALL-CONTRIBUTORS-LIST: START - Do not remove or modify this section -->
120<!-- ALL-CONTRIBUTORS-LIST:END -->
121
122This project follows the [all-contributors](https://github.com/kentcdodds/all-contributors) specification.
123
124Contributions of any kind are welcome!
125
126## Contributing
127
128See [CONTRIBUTING.md](https://github.com/ScottyFillups/simple-thumbnail/blob/master/CONTRIBUTING.md).