-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathconvert.js
216 lines (182 loc) · 5.76 KB
/
convert.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
'use strict';
var Promise = require('bluebird');
var fs = Promise.promisifyAll(require('fs'));
var yaml = require('js-yaml');
var colors = require('colors');
var childProcess = require('child_process');
var mkdirp = require('mkdirp');
var mkdirpAsync = Promise.promisify(mkdirp);
var path = require('path');
var moment = require('moment');
var Queue = require('promise-queue');
var filewalker = require('filewalker');
var JSONStream = require('JSONStream');
var config = yaml.safeLoad(fs.readFileSync(path.join(__dirname, 'convert.yml'), 'utf8'));
var srcDirectory = path.resolve(__dirname, config.srcDirectory || 'complete');
var dstDirectory = path.resolve(__dirname, config.dstDirectory || 'converted');
var dirScanInterval = config.dirScanInterval || 300;
var maxConcur = config.maxConcur || 1;
Queue.configure(Promise.Promise);
var queue = new Queue(maxConcur, Infinity);
function getCurrentDateTime() {
return moment().format('MM/DD/YYYY - HH:mm:ss');
}
function printMsg(msg) {
console.log(colors.gray(`[${getCurrentDateTime()}]`), msg);
}
function printErrorMsg(msg) {
console.log(colors.gray(`[${getCurrentDateTime()}]`), colors.red('[ERROR]'), msg);
}
function getFiles() {
let files = [];
return new Promise((resolve, reject) => {
filewalker(srcDirectory, { maxPending: 1, matchRegExp: /(\.ts|\.flv)$/ })
.on('file', (p, stats) => {
// select only "not hidden" files and not empty files (>10KBytes)
if (!p.match(/(^\.|\/\.)/) && stats.size > 10240) {
// push path relative to srcDirectory
files.push(p);
}
})
.on('done', () => {
resolve(files);
})
.walk();
});
}
function getAudioCodec(srcFile) {
return new Promise((resolve, reject) => {
let audioCodec = '';
let spawnArguments = [
'-v', 'error',
'-select_streams', 'a:0',
'-show_streams',
'-print_format', 'json',
srcFile
];
let ffprobeProcess = childProcess.spawn('ffprobe', spawnArguments);
ffprobeProcess.stdout.pipe(JSONStream.parse('streams.0')).on('data', data => {
audioCodec = data.codec_name;
});
ffprobeProcess.on('close', code => {
if (code !== 0) {
reject(`Failed to get audio codec from ${srcFile}`);
} else {
resolve(audioCodec);
}
});
}).timeout(5000); // 5 seconds
}
function getSpawnArguments(srcFile, dstFile) {
return getAudioCodec(srcFile)
.then(audioCodec => (audioCodec === 'aac')
? [ // aac
'-i', srcFile,
'-y',
'-hide_banner',
'-loglevel', 'panic',
'-movflags', '+faststart',
'-c:v', 'copy',
'-c:a', 'copy',
'-bsf:a', 'aac_adtstoasc',
'-copyts',
'-start_at_zero',
dstFile
]
: [ // speex or something else
'-i', srcFile,
'-y',
'-hide_banner',
'-loglevel', 'panic',
'-movflags', '+faststart',
'-c:v', 'copy',
'-c:a', 'aac',
'-b:a', '64k',
dstFile
]
);
}
function convertFile(srcFile) {
let startTs = moment();
let src = path.join(srcDirectory, srcFile);
let dstPath = path.resolve(path.dirname(path.join(dstDirectory, srcFile)));
let dstFile = path.basename(srcFile, path.extname(srcFile)) + '.mp4';
let tempDst = path.join(dstPath, `~${dstFile}`);
let dst = path.join(dstPath, dstFile);
return mkdirpAsync(dstPath)
.then(() => getSpawnArguments(src, tempDst))
.then(spawnArguments => new Promise((resolve, reject) => {
printMsg(`Starting ${colors.green(srcFile)}...`);
// printMsg('ffmpeg ' + spawnArguments.join(' '));
let ffmpegProcess = childProcess.spawn('ffmpeg', spawnArguments);
ffmpegProcess.stdout.on('data', data => {
printMsg(data.toString());
});
ffmpegProcess.stderr.on('data', data => {
printMsg(data.toString());
});
ffmpegProcess.on('close', code => {
if (code !== 0) {
reject(`Failed to convert ${srcFile}`);
} else {
let mtime;
fs.statAsync(src)
.then(stats => {
// remember "modification time" of original file
mtime = Math.ceil(stats.mtime.getTime() / 1000);
})
.then(() => config.deleteAfter ? fs.unlinkAsync(src) : fs.renameAsync(src, `${src}.bak`))
.then(() => fs.renameAsync(tempDst, dst))
.then(() => fs.utimesAsync(dst, mtime, mtime))
.then(() => {
let duration = moment.duration(moment().diff(startTs)).asSeconds().toString();
printMsg(`Finished ${colors.green(srcFile)} after ${colors.magenta(duration)} s`);
resolve();
})
.catch(err => {
reject(err.toString());
});
}
});
}));
}
function mainLoop() {
let startTs = moment().unix();
Promise
.try(() => getFiles())
.then(files => new Promise((resolve, reject) => {
printMsg(files.length + ' file(s) to convert');
if (files.length === 0) {
resolve();
} else {
files.forEach(file => {
queue
.add(() => convertFile(file))
.then(() => {
if ((queue.getPendingLength() + queue.getQueueLength()) === 0) {
resolve();
}
})
.catch(err => {
printErrorMsg(err);
});
});
}
}))
.catch(err => {
if (err) {
printErrorMsg(err);
}
})
.finally(() => {
let seconds = startTs - moment().unix() + dirScanInterval;
if (seconds < 5) {
seconds = 5;
}
printMsg(`Done, will scan the folder in ${seconds} seconds`);
setTimeout(mainLoop, seconds * 1000);
});
}
mkdirp.sync(srcDirectory);
mkdirp.sync(dstDirectory);
mainLoop();