-
Notifications
You must be signed in to change notification settings - Fork 18
/
index.js
146 lines (123 loc) · 3.36 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
'use strict'
const execa = require('execa')
const ffmpeg = require('fluent-ffmpeg')
const path = require('path')
const probe = require('ffmpeg-probe')
const rmfr = require('rmfr')
const tempy = require('tempy')
const noop = () => { }
module.exports = async (opts) => {
const {
log = noop,
// general output options
quality = 2,
width,
height,
input,
output,
numFrames,
numFramesPercent = 0.05,
// image strip options
padding = 0,
margin = 0,
cols,
rows = 1,
color,
// gif options
gifski = {
fps: 10,
quality: 80,
fast: false
}
} = opts
const info = await probe(input)
const numFramesTotal = parseInt(info.streams[0].nb_frames)
const ext = path.extname(output).slice(1).toLowerCase()
const isGIF = (ext === 'gif')
let numFramesToCapture = numFrames || numFramesPercent * numFramesTotal
if (!isGIF && rows > 0 && cols > 0) {
numFramesToCapture = rows * cols
}
numFramesToCapture = Math.max(1, Math.min(numFramesTotal, numFramesToCapture)) | 0
const nthFrame = (numFramesTotal / numFramesToCapture) | 0
const tempDir = isGIF && tempy.directory()
const tempOutput = isGIF
? path.join(tempDir, 'frame-%d.png')
: output
const result = {
output,
numFrames: numFramesToCapture
}
await new Promise((resolve, reject) => {
let scale = null
let tile = null
if (width && height) {
result.width = width | 0
result.height = height | 0
scale = `scale=${width}:${height}`
} else if (width) {
result.width = width | 0
result.height = (info.height * width / info.width) | 0
scale = `scale=${width}:-1`
} else if (height) {
result.height = height | 0
result.width = (info.width * height / info.height) | 0
scale = `scale=-1:${height}`
} else {
result.width = info.width
result.height = info.height
}
if (!isGIF) {
const numRows = Math.max(1, rows | 0)
const numCols = Math.max(1, Math.ceil(cols || numFramesToCapture / numRows))
tile = [
`tile=${numCols}x${numRows}`,
padding && `padding=${padding}`,
margin && `margin=${margin}`,
color && `color=${color}`
].filter(Boolean).join(':')
result.rows = numRows
result.cols = numCols
result.padding = padding
result.margin = margin
}
const filter = [
`select=not(mod(n\\,${nthFrame}))`,
scale,
tile
].filter(Boolean).join(',')
ffmpeg(input)
.outputOptions(isGIF ? [
'-vsync', 'vfr'
] : [
'-vframes', 1
])
.outputOptions([
'-q:v', quality,
'-vf', filter
])
.output(tempOutput)
.on('start', (cmd) => log && log({ cmd }))
.on('end', () => resolve())
.on('error', (err) => reject(err))
.run()
})
if (isGIF) {
const framePattern = tempOutput.replace('%d', '*')
const escapePath = arg => arg.replace(/(\s+)/g, '\\$1')
const params = [
'-o', escapePath(output),
'--fps', gifski.fps,
gifski.fast && '--fast',
'--quality', gifski.quality,
'--quiet',
escapePath(framePattern)
].filter(Boolean)
const executable = process.env.GIFSKI_PATH || 'gifski'
const cmd = [ executable ].concat(params).join(' ')
if (log) log(cmd)
await execa.shell(cmd)
await rmfr(tempDir)
}
return result
}