-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
106 lines (93 loc) · 2.53 KB
/
app.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
/*var ffmpeg = require('ffmpeg');*/
var _ = require('underscore');
var q = require('q');
var FS = require('q-io/fs');
var path = require('path');
var sys = require('sys');
var exec = require('child_process').exec;
var ffmpeg = require('fluent-ffmpeg');
var puts = function (error, stdout, stderr) {
'use strict';
sys.puts(stderr);
};
var sourceDir = process.argv[2];
FS.exists(sourceDir + '/ogv/')
.then(function (exists) {
'use strict';
if (!exists) {
return FS.makeDirectory(sourceDir + '/ogv/').then(FS.exists(sourceDir + '/wmv/'));
}
else {
return FS.exists(sourceDir + '/wmv/');
}
})
.then(function (exists) {
'use strict';
if (!exists) {
return FS.makeDirectory(sourceDir + '/wmv/').then(FS.exists(FS.list(sourceDir + '/mov/')));
}
else {
return FS.list(sourceDir + '/mov/');
}
})
.then(FS.list(sourceDir + '/mov/'))
.then(function (list) {
'use strict';
var count = 0;
var metaConvert = function (fullList, dataList) {
if (dataList.length === 0) {
return;
}
var format = dataList.shift();
convertWrap(_.clone(list), format)
.then(function () {
metaConvert(fullList, dataList);
});
};
var convertWrap = function (list, data) {
var deferred = q.defer();
convert(list, data, deferred);
return deferred.promise;
};
var convert = function (list, data, deferred) {
var filePath = list.shift();
var filename = path.basename(filePath, '.mov');
console.log('Converting ' + filename + ' to format ' + data.format);
var proc = new ffmpeg({
source: sourceDir + '/mov/' + filePath,
timeout: 0
})
.withVideoCodec(data.vcodec)
.withAudioCodec(data.acodec)
.withAudioQuality(5)
.addOption('-qscale:v', 7)
.keepPixelAspect(true)
.onProgress(function (progress) {
process.stdout.write('Frames Rendered: ' + progress.frames + ' Percent Done: ' + progress.percent + '\r');
})
.saveToFile(sourceDir + '/' + data.format + '/' + filename + '.' + data.format, function (stdout, stderr) {
console.log('');
console.log('file has been converted succesfully\r');
if (list.length === 0) {
deferred.resolve();
}
else {
convert(list, data, deferred);
}
});
};
var movFile = function (fileName) {
return path.extname(fileName) === '.mov';
};
list = _.filter(list, movFile);
metaConvert(list, [{
format: 'ogv',
vcodec: 'libtheora',
acodec: 'libvorbis'
}, {
format: 'wmv',
vcodec: 'msmpeg4',
acodec: 'wmav2'
}]);
})
.done();