-
Notifications
You must be signed in to change notification settings - Fork 11
/
avconv.js
231 lines (189 loc) · 7.11 KB
/
avconv.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
var spawn = require('child_process').spawn,
util = require('util'),
AvStream = require('./lib/avstream');
function toMilliSeconds(time) {
var d = time.split(/[:.]/),
ms = 0;
if (d.length === 4) {
ms += parseInt(d[0], 10) * 3600 * 1000;
ms += parseInt(d[1], 10) * 60 * 1000;
ms += parseInt(d[2], 10) * 1000;
ms += parseInt(d[3], 10) * 10;
} else {
ms += parseInt(d[0], 10) * 1000;
ms += parseInt(d[1], 10);
}
return ms;
}
function findDuration(data) {
var result = /duration: (\d+:\d+:\d+.\d+)/i.exec(data),
duration;
if (result && result[1]) {
duration = toMilliSeconds(result[1]);
}
return duration;
}
function findTime(data) {
var time;
if (data.substring(0, 5) === 'frame') {
var result = /time=(\d+.\d+)/i.exec(data);
if (result && result[1]) {
time = toMilliSeconds(result[1]);
}
}
return time;
}
var
VIDEO_META = {CODEC: 0, FORMAT: 1, RESOLUTION: 2, BITRATE: 3, FPS: 4},
AUDIO_META = {CODEC: 0, SAMPLERATE: 1, SPATIALIZATION: 2, SAMPLEFORMAT: 3, BITRATE: 4}
;
function parseMetaData(output) {
var
meta = {input:{},output:{}},
streamIndex,
streamData,
metaType,
metaData,
tmp;
function getInteger(v) {return parseInt(v,10)}
function getChannelCount(v){
if (v == "mono") return 1;
if (v == "stereo") return 2;
if (v && v.indexOf('.') != -1) {
return v.split('.').map(getInteger)
.reduce(function(a,b){return a+b});
}
return v;
}
// process lines
output.split("\n").forEach(function (dataLine) {
// get metadata type
if (/^Input/i.test(dataLine))
metaType = "input";
else if (/^Output/i.test(dataLine))
metaType = "output";
else if (/^Stream mapping/i.test(dataLine))
metaType = null;
if (!metaType) return;
metaData = meta[metaType];
// is io meta data
if (/^\s*Duration/.test(dataLine)) {
dataLine
.split(',')
.map(function(d){return d.split(/:\s/)})
.forEach(function(kv){metaData[kv[0].toLowerCase().trim()]=kv[1]});
if (metaData.duration)
metaData.duration = toMilliSeconds(metaData.duration);
if (metaData.bitrate)
metaData.bitrate = getInteger(metaData.bitrate);
if (metaData.start)
metaData.start = parseFloat(metaData.start);
} else if (/^\s*Stream #/.test(dataLine)) { // is stream meta data
// resolve stream indices
tmp = dataLine.match(/#(\d+)[.:](\d+)/);
if (!tmp) return;
streamIndex = tmp.slice(1).map(getInteger);
// get or create stream structure
if (!metaData.stream) metaData.stream = [];
streamData = metaData.stream[streamIndex[0]] || (metaData.stream[streamIndex[0]] = []);
streamData = streamData[streamIndex[1]] || (streamData[streamIndex[1]] = {});
// get stream type
tmp = dataLine.match(/video|audio/i);
if (!tmp) return;
streamData.type = tmp[0].toLowerCase();
// prepare stream data
tmp = dataLine.replace(/.*?(Video|Audio):/i, '')
.replace(/\([^)]*\)/g, '')
.split(", ").map(function(v){
return v.trim().replace(/ \S.*$/, '');
});
// parse stream data
if (streamData.type == "video") {
streamData.codec = tmp[VIDEO_META.CODEC];
streamData.format = tmp[VIDEO_META.FORMAT];
streamData.resolution = tmp[VIDEO_META.RESOLUTION].split("x").map(getInteger);
streamData.bitrate = getInteger(tmp[VIDEO_META.BITRATE+(metaType=="output"?1:0)]);
if (metaType == "input")
streamData.fps = parseFloat(tmp[VIDEO_META.FPS]);
} else if (streamData.type == "audio") {
streamData.codec = tmp[AUDIO_META.CODEC];
streamData.samplerate = getInteger(tmp[AUDIO_META.SAMPLERATE]);
streamData.channels = getChannelCount(tmp[AUDIO_META.SPATIALIZATION]);
streamData.sampleformat = tmp[AUDIO_META.SAMPLEFORMAT];
streamData.bitrate = getInteger(tmp[AUDIO_META.BITRATE]);
}
}
});
return meta;
}
module.exports = function avconv(params, command) {
var stream = new AvStream(),
command = command || 'avconv',
// todo: use a queue to deal with the spawn EMFILE exception
// see http://www.runtime-era.com/2012/10/quick-and-dirty-nodejs-exec-limit-queue.html
// currently I have added a dirty workaround on the server by increasing
// the file max descriptor with 'sudo sysctl -w fs.file-max=100000'
avconv = spawn(command, params);
// General avconv output is always written into stderr
if (avconv.stderr) {
avconv.stderr.setEncoding('utf8');
var output = '',
duration,
time,
progress;
avconv.stderr.on('data', function(data) {
time = null;
// Keep the output so that we can parse stuff anytime,
// i.E. duration or meta data
output += data;
if (!duration) {
duration = findDuration(output);
} else {
time = findTime(data);
}
if (duration && time) {
progress = time / duration;
if (progress > 1) {
progress = 1; // Fix floating point error
}
// Tell the world that progress is made
stream.emit('progress', progress);
}
// Emit conversion information as messages
stream.emit('message', data);
});
}
// When avconv outputs anything to stdout, it's probably converted data
if (avconv.stdout) {
avconv.stdout.on('data', function(data) {
stream.push(data)
});
}
// Pipe the stream to avconv standard input
if (avconv.stdin) {
// Reduce overhead when receiving a pipe
stream.on('pipe', function(source) {
// Unpipe the source (input) stream from AvStream
source.unpipe(stream);
// And pipe it to avconv's stdin instead
source.pipe(avconv.stdin);
});
// When data is written to AvStream, send it to avconv's stdin
stream.on('inputData', function(data) {
avconv.stdin.write(data);
});
}
avconv.on('error', function(data) {
stream.emit('error', data);
});
// New stdio api introduced the exit event not waiting for open pipes
var eventType = avconv.stdio ? 'close' : 'exit';
avconv.on(eventType, function(exitCode, signal) {
stream.end();
stream.emit('exit', exitCode, signal, parseMetaData(output));
});
stream.kill = function() {
avconv.kill();
};
return stream;
};