-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrunchy.js
74 lines (64 loc) · 2.36 KB
/
crunchy.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
const request = require("request");
const { spawn } = require('child_process');
const fs = require('fs');
const cliProgress = require('cli-progress');
main();
function main() {
if(process.argv.length < 3) {
console.log('Usage: crunchy [url]');
} else {
download(process.argv[2]);
}
}
function download(url) {
let duration = -1;
const options = {
url: url,
headers: {}
};
const outname = url.replace(/https?:\/\/.+\.[a-z]+(\/[a-z-]{0,5})?\//, "").replace(/[^a-zA-Z0-9]/g, "_")+'.mkv';
request.get(options, function (error, response, body) {
if (error) {
return console.log(error);
}
const start = "vilos.config.media = ";
const end = "vilos.config.analytics = ";
if(body.search(start)===-1 || body.search(end)===-1) {
console.log('error');
return
}
const test = body.substring(body.search(start) + start.length, body.search(end) - 7);
let langs = {};
const meta = JSON.parse(test);
duration = Math.floor(meta.metadata.duration);
const streams = meta.streams;
for (let i in streams) {
langs[streams[i].hardsub_lang] = streams[i].url
}
if (fs.existsSync(outname)) {
fs.unlinkSync(outname);
}
getStream(langs["enUS"])
});
function getStream(url) {
function pad(num) {
var s = "0" + num;
return s.substr(s.length-2);
}
let ffmpeg = spawn("ffmpeg", ['-hide_banner', '-v', 'quiet', '-stats', '-i', url, '-c', 'copy', outname]);
const durationstr = pad(Math.floor(duration/60/60)) + ':' + pad(Math.floor(duration/60)) + ':' + pad(duration%60);
const bar1 = new cliProgress.Bar({}, cliProgress.Presets.shades_classic);
console.log("Downloading to: ", outname);
bar1.start(duration, 0);
ffmpeg.stderr.on('data', (data) => {
let timestr = data.toString().substr(data.toString().search(/[0-9]{2}:[0-9]{2}:[0-9]{2}/),8);
let timearr = timestr.split(":");
time = 1000*(parseInt(timearr[0])*60*60 + parseInt(timearr[1])*60 + parseInt(timearr[2]));
bar1.update(time);
});
ffmpeg.on('close', (code) => {
bar1.stop();
console.log(`ffmpeg exited with code ${code}`);
});
}
}