UNPKG

5.53 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 fs = require('fs')
20const genThumbnail = require('simple-thumbnail')
21
22// promise
23genThumbnail('path/to/image.png', 'output/file/path.png', '250x?')
24 .then(() => console.log('done!'))
25 .catch(err => console.error(err))
26
27// async/await
28async function run () {
29 try {
30 await genThumbnail('http://www.example.com/foo.webm', 'output/file/path.png', '250x?')
31 console.log('Done!')
32 } catch (err) {
33 console.error(err)
34 }
35}
36
37run()
38
39// genThumbnail also supports piping to write streams, so you can do this with Express!
40app.get('/some/endpoint', (req, res) => {
41 genThumbnail('path/to/video.webm', res, '150x100')
42 .then(() => console.log('done!'))
43 .catch(err => console.error(err))
44})
45
46// duplex streams
47fs.createReadStream('path/to/image')
48 .pipe(genThumbnail(null, null, '250x?'))
49 .pipe(fs.createWriteStream('output/file/path.jpg'))
50```
51
52## Getting FFmpeg
53
54For those who don't have FFmpeg installed, there's an NPM package that installs it for you: https://www.npmjs.com/package/ffmpeg-static
55
56```js
57const ffmpeg = require('ffmpeg-static')
58const genThumbnail = require('simple-thumbnail')
59
60async function download () {
61 await genThumbnail('https://www.w3schools.com/Html/mov_bbb.webm', 'bunny.webm', '150x?', {
62 path: ffmpeg.path
63 })
64
65 console.log('Done!')
66}
67
68download()
69```
70
71## API
72
73#### genThumbnail(input, output, size, [config])
74
75Returns of a `Promise` which resolves on thumbnail creation, or a `stream.Duplex` (see below).
76
77#### input
78
79Type: `String | stream.Readable | Null`
80
81The URL, file path, or read-stream of an image or video. If both the input and output are null, then `genThumbnail` will return a `stream.Duplex`.
82
83#### output
84
85Type: `String | stream.Writable | Null`
86
87The 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.
88
89#### size
90
91Type: `String`
92
93The dimensions of the generated thumbnail. The `size` argument may have one of the following formats:
94
95* `150x100`: set a fixed output size.
96* `150x?`: set a fixed width and compute the height automatically.
97* `?x100`: set a fixed height and compute the width automatically.
98* `50%`: rescale both width and height to given percentage.
99
100#### config
101
102Type: `Object`
103
104A configuration object, see details below.
105
106#### config.path
107
108Type: `String`
109
110The 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 [...]`).
111
112#### config.seek
113
114Type: `String`
115
116Seeks 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).
117
118#### config.args
119
120Type: `Array<String>`
121
122FFmpeg arguments that override the synthetic arguments created by `simple-thumbnail`; you'll likely need to pass include your own `-i` flag, `-y` flag, etc.
123
124## Contributors
125
126<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
127<!-- prettier-ignore -->
128| [<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") |
129| :---: | :---: | :---: |
130<!-- ALL-CONTRIBUTORS-LIST:END -->
131<!-- ALL-CONTRIBUTORS-LIST: START - Do not remove or modify this section -->
132<!-- ALL-CONTRIBUTORS-LIST:END -->
133
134This project follows the [all-contributors](https://github.com/kentcdodds/all-contributors) specification.
135
136Contributions of any kind are welcome!
137
138## Contributing
139
140See [CONTRIBUTING.md](https://github.com/ScottyFillups/simple-thumbnail/blob/master/CONTRIBUTING.md).