/
capabilities.js
207 lines (178 loc) · 6.48 KB
/
capabilities.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
var exec = require('child_process').exec,
Registry = require('./registry');
var avCodecRegexp = /^ ([D ])([E ])([VAS])([S ])([D ])([T ]) ([^ ]+) +(.*)$/;
var ffCodecRegexp = /^ ([D\.])([E\.])([VAS])([I\.])([L\.])([S\.]) ([^ ]+) +(.*)$/;
var ffEncodersRegexp = /\(encoders:([^\)]+)\)/;
var ffDecodersRegexp = /\(decoders:([^\)]+)\)/;
var formatRegexp = /^ ([D ])([E ]) ([^ ]+) +(.*)$/;
function copy(src, dest) {
Object.keys(src).forEach(function(k) {
dest[k] = src[k];
});
}
exports = module.exports = function capabilities(command) {
command.prototype.getAvailableCodecs = function(callback) {
var codecs = Registry.instance.get('capabilityCodecs');
if (!codecs) {
var command = [this.ffmpegPath, '-codecs'];
exec(command.join(' '), function(err, stdout, stderr) {
if (err) {
return callback(err);
}
var lines = stdout.split('\n');
var data = {};
lines.forEach(function(line) {
var match = line.match(avCodecRegexp);
if (match) {
data[match[7]] = {
type: { 'V': 'video', 'A': 'audio', 'S': 'subtitle' }[match[3]],
description: match[8],
canDecode: match[1] === 'D',
canEncode: match[2] === 'E',
drawHorizBand: match[4] === 'S',
directRendering: match[5] === 'D',
weirdFrameTruncation: match[6] === 'T'
};
}
match = line.match(ffCodecRegexp);
if (match) {
var codecData = data[match[7]] = {
type: { 'V': 'video', 'A': 'audio', 'S': 'subtitle' }[match[3]],
description: match[8],
canDecode: match[1] === 'D',
canEncode: match[2] === 'E',
intraFrameOnly: match[4] === 'I',
isLossy: match[5] === 'L',
isLossless: match[6] === 'S'
};
var encoders = codecData.description.match(ffEncodersRegexp);
encoders = encoders ? encoders[1].trim().split(' ') : [];
var decoders = codecData.description.match(ffDecodersRegexp);
decoders = decoders ? decoders[1].trim().split(' ') : [];
if (encoders.length || decoders.length) {
var coderData = {};
copy(codecData, coderData);
delete coderData.canEncode;
delete coderData.canDecode;
encoders.forEach(function(name) {
data[name] = {};
copy(coderData, data[name]);
data[name].canEncode = true;
});
decoders.forEach(function(name) {
if (name in data) {
data[name].canDecode = true;
} else {
data[name] = {};
copy(coderData, data[name]);
data[name].canDecode = true;
}
});
}
}
});
Registry.instance.set('capabilityCodecs', data);
callback(null, data);
});
} else {
callback(null, codecs);
}
};
command.prototype.getAvailableFormats = function(callback) {
var formats = Registry.instance.get('capabilityFormats');
if (!formats) {
var command = [this.ffmpegPath, '-formats'];
exec(command.join(' '), function(err, stdout, stderr) {
if (err) {
return callback(err);
}
var lines = stdout.split('\n');
var data = {};
lines.forEach(function(line) {
var match = line.match(formatRegexp);
if (match) {
data[match[3]] = {
description: match[4],
canDemux: match[1] === 'D',
canMux: match[2] === 'E'
};
}
});
Registry.instance.set('capabilityFormats', data);
callback(null, data);
});
} else {
callback(null, formats);
}
};
command.prototype._checkFormatCapabilities = function(callback) {
var options = this.options;
if (options.format || options.fromFormat) {
this.getAvailableFormats(function(err, formats) {
if (err) {
callback(new Error('cannot get available formats: ' + err.message));
} else {
if (options.format) {
if (!(options.format in formats)) {
return callback(new Error('format ' + options.format + ' not available'));
}
if (!formats[options.format].canMux) {
return callback(new Error('cannot mux output using format ' + options.format));
}
}
if (options.fromFormat) {
if (!(options.fromFormat in formats)) {
return callback(new Error('format ' + options.fromFormat + ' not available'));
}
if (!formats[options.fromFormat].canDemux) {
return callback(new Error('cannot demux input using format ' + options.fromFormat));
}
}
callback();
}
});
} else {
callback();
}
};
command.prototype._checkCodecCapabilities = function(callback) {
var options = this.options;
if (options.audio.codec || options.video.codec) {
this.getAvailableCodecs(function(err, codecs) {
if (err) {
callback(new Error('cannot get available codecs: ' + err.message));
} else {
if (options.audio.codec) {
if (!(options.audio.codec in codecs) || codecs[options.audio.codec].type !== 'audio') {
return callback(new Error('audio codec ' + options.audio.codec + ' not available'));
}
if (!codecs[options.audio.codec].canEncode) {
return callback(new Error('codec ' + options.audio.codec + ' cannot encode audio'));
}
}
if (options.video.codec) {
if (!(options.video.codec in codecs) || codecs[options.video.codec].type !== 'video') {
return callback(new Error('video codec ' + options.video.codec + ' not available'));
}
if (!codecs[options.video.codec].canEncode) {
return callback(new Error('codec ' + options.video.codec + ' cannot encode video'));
}
}
callback();
}
});
} else {
callback();
}
};
command.prototype._checkCapabilities = function(callback) {
var self = this;
this._checkFormatCapabilities(function(err) {
if (err) {
callback(err);
} else {
self._checkCodecCapabilities(callback);
}
});
};
};