-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.coffee
42 lines (30 loc) · 993 Bytes
/
index.coffee
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
{exec} = require 'child_process'
escape = require 'shell-escape'
path = require 'path'
temp = require('temp').track()
togif = (input, output, options, callback) ->
rate = options.rate or 10
ffmpeg = options.ffmpeg or 'ffmpeg'
input = escape([input])
output = escape([output])
temp.mkdir {}, (err, tempDir) ->
return callback(err) if err
palettePath = path.join(tempDir, '/palette.png')
filters = "fps=#{rate}"
if options.width
filters += ",scale=#{options.width}:-1:flags=lanczos"
cmd = [ffmpeg]
cmd.push('-i', input)
cmd.push('-vf', '"' + filters + ',palettegen"')
cmd.push('-y', palettePath)
cmd = cmd.join(' ')
exec cmd, (err) ->
return callback(err) if err
cmd = [ffmpeg]
cmd.push('-i', input)
cmd.push('-i', palettePath)
cmd.push('-lavfi', '"' + filters + ' [x]; [x][1:v] paletteuse"')
cmd.push('-y', output)
cmd = cmd.join(' ')
exec(cmd, callback)
module.exports = togif