forked from vvo/gifify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
187 lines (145 loc) · 4.43 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
var duration = require('moment').duration;
var spawn = require('child_process').spawn;
require('moment-duration-format');
var debug = require('debug')('gifify');
module.exports = gifify;
function gifify(streamOrFile, opts) {
if (typeof streamOrFile === 'string') {
opts.inputFilePath = streamOrFile;
}
if (opts.fps === undefined) {
opts.fps = 10;
}
if (opts.speed === undefined) {
opts.speed = 1;
}
if (opts.colors === undefined) {
opts.colors = 80;
}
if (opts.compress === undefined) {
opts.compress = 40;
}
if (opts.from !== undefined && typeof opts.from === 'number' ||
typeof opts.from === 'string' && opts.from.indexOf(':') === -1) {
opts.from = parseFloat(opts.from) * 1000;
}
if (opts.to !== undefined && typeof opts.to === 'number' ||
typeof opts.to === 'string' && opts.to.indexOf(':') === -1) {
opts.to = parseFloat(opts.to) * 1000;
}
var ffmpegArgs = computeFFmpegArgs(opts);
var convertArgs = computeConvertArgs(opts);
var gifsicleArgs = computeGifsicleArgs(opts);
var ffmpeg = spawn('ffmpeg', ffmpegArgs);
var convert = spawn('convert', convertArgs);
var gifsicle = spawn('gifsicle', gifsicleArgs);
[ffmpeg, convert, gifsicle].forEach(function handleErrors(child) {
child.on('error', gifsicle.emit.bind(gifsicle, 'error'));
child.stderr.on('data', function gotSomeErrors(buf) {
// emit errors on the resolved stream
gifsicle.stdout.emit('error', buf.toString());
});
});
// https://github.com/joyent/node/issues/8652
ffmpeg.stdin.on('error', function ignoreStdinError(){});
// ffmpeg.stdout.on('error', function() {})
// convert.stdin.on('error', function(){});
// convert.stdout.on('error', function() {});
// gifsicle.stdin.on('error', function() {});
// gifsicle.stdout.on('error', function() {})
if (!opts.inputFilePath) {
streamOrFile.pipe(ffmpeg.stdin);
}
ffmpeg.stdout.pipe(convert.stdin);
convert.stdout.pipe(gifsicle.stdin);
return gifsicle.stdout;
}
function computeFFmpegArgs(opts) {
var FFmpegTimeFormat = 'hh:mm:ss.SSS';
// FFmpeg options
// https://www.ffmpeg.org/ffmpeg.html#Options
var args = [
'-loglevel', 'panic'
];
// fast seek to opts.from - 500ms
// see http://superuser.com/a/704118/35651
if (opts.from !== undefined) {
args.push('-ss', duration(opts.from).format(FFmpegTimeFormat, {trim: false}));
}
if (opts.inputFilePath) {
args.push('-i', opts.inputFilePath);
} else {
// stdin as input
// https://www.ffmpeg.org/ffmpeg-protocols.html#pipe
args.push('-i', 'pipe:0');
}
if (opts.to !== undefined) {
args.push('-to', duration(opts.to).format(FFmpegTimeFormat, {trim: false}));
}
// framerate
args.push('-r', opts.fps);
if (opts.resize || opts.subtitles || opts.reverse) {
// filters
args.push('-vf');
var filters = [];
// resize filter
if (opts.resize) {
filters.push('scale=' + opts.resize);
}
if (opts.subtitles !== undefined) {
filters.push('subtitles=' + opts.subtitles);
}
if (opts.reverse !== undefined) {
filters.push('reverse');
}
args.push(filters.join(','));
}
// encoding filter and codec
args.push('-f', 'image2pipe', '-vcodec', 'ppm');
// force video sync so that even if nothing moves in the video, we get a constant frame rate
// seems buggy, not what I want, still, some videos are failing to encode
// args.push('-vsync', '1');
// write on stdout
args.push('pipe:1');
debug('ffmpeg args: %j', args);
return args;
}
function computeConvertArgs(opts) {
// Convert options
// http://www.imagemagick.rg/script/convert.php#options
var args = [
'-',
'+dither',
'-layers', 'Optimize'
];
if (opts.text) {
args.push(
'-gravity', 'South',
'-fill', 'white',
'-stroke', 'black',
'-strokewidth', '1',
'-pointsize', '40',
'-annotate', '+20+20', opts.text
);
}
args.push('gif:-');
debug('convert args: %j', args);
return args;
}
function computeGifsicleArgs(opts) {
// Gifsicle options
// http://www.lcdf.org/gifsicle/man.html
// --lossy is not yet into master, https://github.com/kohler/gifsicle/pull/16
var args = [
'-O3',
'--lossy=' + opts.compress * 2,
'--colors=' + opts.colors,
'--delay', Math.round(100 / opts.fps / opts.speed),
'--no-warnings'
];
if (opts.loop === false) {
args.push('--no-loopcount');
}
debug('gifsicle args: %j', args);
return args;
}