forked from pusspounder/CapMyFreeCamsChaturbateNodeJS
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathconvert.js
163 lines (130 loc) · 4.59 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
// MyFreeCams File Converter v.1.0.6
'use strict';
let Promise = require('bluebird');
let fs = Promise.promisifyAll(require('fs'));
let yaml = require('js-yaml');
let colors = require('colors');
let childProcess = require('child_process');
let mkdirp = require('mkdirp');
let path = require('path');
let moment = require('moment');
let Queue = require('promise-queue');
let filewalker = require('filewalker');
function getCurrentTime() {
return moment().format(`HH:mm:ss`);
}
function printMsg(msg) {
console.log(colors.gray(`[` + getCurrentTime() + `]`), msg);
}
function printErrorMsg(msg) {
console.log(colors.gray(`[` + getCurrentTime() + `]`), colors.red(`[ERROR]`), msg);
}
function getTimestamp() {
return Math.floor(new Date().getTime() / 1000);
}
let config = yaml.safeLoad(fs.readFileSync(path.join(__dirname, 'convert.yml'), 'utf8'));
let srcDirectory = path.resolve(__dirname, config.srcDirectory || 'complete');
let dstDirectory = path.resolve(__dirname, config.dstDirectory || 'converted');
let convertProgram = config.convertProgram || 'ffmpeg284';
let dirScanInterval = config.dirScanInterval || 300;
let maxConcur = config.maxConcur || 1;
Queue.configure(Promise.Promise);
let queue = new Queue(maxConcur, Infinity);
function getFiles() {
let files = [];
return new Promise((resolve, reject) => {
filewalker(srcDirectory, { maxPending: 1, matchRegExp: /(\.ts|\.flv|\.mp4)$/ })
.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 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)) + '.flv';
let dst = path.join(dstDirectory, `~${dstFile}`);
let tempDst = path.join(dstPath, dstFile);
mkdirp.sync(dstPath);
let stats = fs.statSync(src);
printMsg(`Starting ${colors.gray(srcFile)} @ size ${colors.yellow((stats.size/1048576).toFixed(2))} MB`);
let convertProcess;
if (convertProgram == 'ffmpeg') {
convertProcess = childProcess.spawnSync(convertProgram,['-i',src,'-y','-hide_banner','-loglevel','panic','-c:v','copy','-c:a','aac','-b:a','128k','-copyts','-start_at_zero',dst])};
if (convertProgram == 'ffmpeg284') {
convertProcess = childProcess.spawnSync(convertProgram,['-i',src,'-y','-hide_banner','-loglevel','panic','-c:v','copy','-c:a','libvo_aacenc','-b:a','128k','-copyts','-start_at_zero',dst])};
let duration = moment.duration(moment().diff(startTs)).asSeconds().toString();
printMsg(`Finished ${colors.green(dstFile)} after ${colors.cyan(duration)} sec.`);
if (convertProcess.status != 0) {
printErrorMsg(`Failed to convert ${colors.red(srcFile)}`);
if (convertProcess.error) {
printErrorMsg(convertProcess.error.toString());
}
return;
}
if (config.deleteAfter) {
fs.unlink(src, function(err) {
// do nothing, shit happens
});
} else {
fs.renameAsync(src, `${src}.bak`, function(err) {
if (err) {
printErrorMsg(err.toString());
}
});
}
fs.renameAsync(dst, tempDst, function(err) {
if (err) {
printErrorMsg(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))
.catch(err => {
printErrorMsg(err);
})
.finally(() => {
if ((queue.getPendingLength() + queue.getQueueLength()) === 0) {
resolve();
}
});
});
}
}))
.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();