-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
66 lines (60 loc) · 1.56 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
const spawnSync = require('child_process').spawnSync;
const exec = require('child_process').exec;
function parseLine(line, callback) {
var typeRE = new RegExp('^(\\w+)$');
var matches = typeRE.exec(line);
if(matches) {
callback(null, matches[1], null);
return;
}
var keyValueRE = new RegExp('^(\.+)\\s+:\\s(.+)$');
matches = keyValueRE.exec(line);
if(!matches) {
console.log(line);
callback("Error: wrong mediainfo output", null, null);
} else
callback(null, matches[1], matches[2]);
};
function parseOutput(stdout, callback) {
var lines = stdout.toString().split("\n");
var output = {};
output.format = {};
output.tracks = [];
var isTrack = false;
for(var i = 0; i < lines.length; i++) {
if(lines[i].length == 0)
continue;
var line = lines[i];
parseLine(line, (err, key, value) => {
if(err) {
callback(err, output);
return;
}
else if(value) {
if(!isTrack)
output.format[key.trim()] = value;
else
output.tracks[output.tracks.length-1][key.trim()] = value;
}
else {
if(key != "General") {
isTrack = true;
output.tracks.push({ type: key });
}
}
});
i++;
}
callback(null, output);
}
module.exports = {
mediainfo: function(file, callback) {
exec('mediainfo ' + file, (error, stdout, stderr) => {
parseOutput(stdout, callback);
});
},
mediainfoSync: function(file, callback) {
var output = spawnSync('mediainfo', [file]);
parseOutput(output.stdout, callback);
}
};