Skip to content

Commit

Permalink
feat: add webm support
Browse files Browse the repository at this point in the history
  • Loading branch information
Ploc committed Nov 12, 2018
1 parent c2fd364 commit fae3d80
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,14 @@ This method supports all the same options as `.image()`.
```

Transcodes the video in `source` to a web-friendly format and lower bitrate, and writes it in `target`.
Currently doesn't support any options, simply pass an empty hash (`{}`).

#### format

The default export format is mp4. You can specify an export format by adding a `format` option whose value can be `mp4` or `webm`.

```js
opts = { format: webm }
```

The `.video()` call returns an [EventEmitter](https://nodejs.org/api/events.html)
to follow the progress of the conversion, since it can take a long time.
Expand Down
9 changes: 7 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,13 @@ exports.image = function (source, target, options, callback) {
exports.video = function (source, target, options, callback) {
// create target folder if needed
mkdirp.sync(path.dirname(target))
// always output to mp4 which is well read on the web
const args = ['-i', source, '-r', '25', '-vsync', '2', '-movflags', '+faststart', '-y', target, '-f', 'mp4', '-vcodec', 'libx264', '-ab', '96k']
const args = ['-i', source, '-r', '25', '-vsync', '2', '-movflags', '+faststart', '-y', target, '-ab', '96k']
// output to mp4 or webm which are well read on the web
if (options.format === 'webm') {
args.push('-f', 'webm', '-vcodec', 'libvpx-vp9')
} else {
args.push('-f', 'mp4', '-vcodec', 'libx264')
}
// AVCHD/MTS videos need a full-frame export to avoid interlacing artefacts
if (path.extname(source).toLowerCase() === '.mts') {
args.push('-vf', 'yadif=1', '-qscale:v', '4')
Expand Down
Binary file added test-data/expected/videos/short.webm.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions test/video.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ tape('can downsample a video for a smaller filesize', (test) => {
})
})

tape('can convert a video to webm', (test) => {
diff.video(test, {
input: 'videos/short.mp4',
expect: 'videos/short.webm',
options: {
format: 'webm'
}
})
})

tape('can report progress when processing videos', (t) => {
const report = []
const input = 'test-data/input/videos/big_buck_bunny.mp4'
Expand Down

0 comments on commit fae3d80

Please sign in to comment.