forked from pusspounder/CapMyFreeCamsChaturbateNodeJS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert.js
167 lines (139 loc) · 3.72 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
'use strict';
var Promise = require('bluebird');
var fs = Promise.promisifyAll(require('fs'));
var mv = require('mv');
var S = require('string');
var yaml = require('js-yaml');
var colors = require('colors');
var childProcess = require('child_process');
var mkdirp = require('mkdirp');
var path = require('path');
var moment = require('moment');
var _ = require('underscore');
function getCurrentDateTime() {
return moment().format('YYYY-MM-DDTHHmmss'); // The only true way of writing out dates and times, ISO 8601
};
function printMsg(msg) {
console.log(colors.blue('[' + getCurrentDateTime() + ']'), msg);
}
function printErrorMsg(msg) {
console.log(colors.blue('[' + getCurrentDateTime() + ']'), colors.red('[ERROR]'), msg);
}
function getTimestamp() {
return Math.floor(new Date().getTime() / 1000);
}
var startTs;
var config = yaml.safeLoad(fs.readFileSync('convert.yml', 'utf8'));
config.srcDirectory = path.resolve(config.srcDirectory);
config.dstDirectory = path.resolve(config.dstDirectory);
mkdirp.sync(config.srcDirectory);
mkdirp.sync(config.dstDirectory);
function getFiles() {
return fs
.readdirAsync(config.srcDirectory)
.then(function(files) {
return _.filter(files, function(file) {
return S(file).endsWith('.ts') || S(file).endsWith('.flv');
});
});
}
function convertFile(srcFile) {
var dstFile;
var srcFile;
var spawnArguments;
if (S(srcFile).endsWith('.ts')) {
dstFile = S(srcFile).chompRight('ts').s + 'mp4';
spawnArguments = [
'-i',
config.srcDirectory + '/' + srcFile,
'-y',
'-hide_banner',
'-loglevel',
'panic',
'-c:v',
'copy',
'-c:a',
'copy',
'-bsf:a',
'aac_adtstoasc',
'-copyts',
config.srcDirectory + '/' + dstFile
];
}
if (S(srcFile).endsWith('.flv')) {
dstFile = S(srcFile).chompRight('flv').s + 'mp4';
spawnArguments = [
'-i',
config.srcDirectory + '/' + srcFile,
'-y',
'-hide_banner',
'-loglevel',
'panic',
'-movflags',
'+faststart',
'-c:v',
'copy',
'-strict',
'-2',
'-q:a',
'100',
config.srcDirectory + '/' + dstFile
];
}
if (!dstFile) {
printErrorMsg('Failed to convert ' + srcFile);
return ;
}
printMsg('Converting ' + srcFile + ' to ' + dstFile);
var convertProcess = childProcess.spawnSync('ffmpeg', spawnArguments);
if (convertProcess.status != 0) {
printErrorMsg('Failed to convert ' + srcFile);
if (convertProcess.error) {
printErrorMsg(convertProcess.error.toString());
}
return;
}
if (config.deleteAfter) {
fs.unlink(config.srcDirectory + '/' + srcFile, function(err) {
// do nothing, shit happens
});
} else {
mv(config.srcDirectory + '/' + srcFile, config.dstDirectory + '/' + srcFile, function(err) {
if (err) {
printErrorMsg(err.toString());
}
});
}
mv(config.srcDirectory + '/' + dstFile, config.dstDirectory + '/' + dstFile, function(err) {
if (err) {
printErrorMsg(err.toString());
}
});
}
function mainLoop() {
startTs = getTimestamp();
Promise
.try(function() {
return getFiles();
})
.then(function(files) {
if (files.length > 0) {
printMsg(files.length + ' file(s) to convert');
_.each(files, convertFile);
} else {
printMsg('No files found');
}
})
.catch(function(err) {
printErrorMsg(err);
})
.finally(function() {
var seconds = startTs - getTimestamp() + config.dirScanInterval;
if (seconds < 5) {
seconds = 5;
}
printMsg('Done, will scan the folder in ' + seconds + ' second(s).');
setTimeout(mainLoop, seconds * 1000);
});
}
mainLoop();