-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
78 lines (64 loc) · 2.15 KB
/
index.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
const ffmpeg = require("fluent-ffmpeg");
const proc = new ffmpeg();
const urlRegex = require("url-regex");
const fetch = require("node-fetch");
var oneDownload = false;
if (process.argv.length <= 2) {
console.log('Missing URL.');
console.log('Usage: node index.js <reddit-url> [output-folder]');
console.log('(the default output folder is ./');
console.log('Example: node index.js https://www.reddit.com/r/ItHadToBeBrazil/comments/chjfh1/i_wonder_how_much_that_upgrade_costed/ ~/Videos');
process.exit(0);
}
const url = process.argv[2];
var outputFolder = './';
if (process.argv.length >= 4) outputFolder = process.argv[3];
if (!outputFolder.endsWith('/')) outputFolder += '/';
const _res = [1080, 720, 480, 360, 240, 140, 120, '2_4_M', '1_2_M'];
console.log(`Output folder > ${outputFolder}`);
console.log(`URL > ${url}`);
fetch(url)
.then(res => {
return res.text();
})
.then(body => {
const urls = body.match(urlRegex());
const mediaUrls = urls.filter(url => url.includes("v.redd.it"));
let mediaUrl = mediaUrls[0].split("https://v.redd.it/")[1];
mediaId = mediaUrl.split("/")[0];
testUrls(mediaId);
});
function testUrls(mediaId) {
console.log('Hold on, Fetching the Best Quality');
_res.forEach(res => {
fetch(`https://v.redd.it/${mediaId}/DASH_${res}`)
.then(response => {
if (response.status === 200 && !oneDownload) {
oneDownload = true;
console.log('Downloading With : ' + res + ' Please Wait ...');
scrape(mediaId, res);
}
});
});
}
function scrape(mediaId, res) {
proc.addInput(`https://v.redd.it/${mediaId}/DASH_${res}`)
.output(`${outputFolder}${mediaId}-${res}.mp4`)
.on("error", err => {
console.log("Error: " + err);
})
.on("end", () => {
console.log("Done");
});
fetch(`https://v.redd.it/${mediaId}/DASH_audio.mp4`)
.then(resp => {
if (resp.status === 200) {
console.log('Founded audio track...')
proc.addInput(`https://v.redd.it/${mediaId}/audio`);
} else {
console.log('No audio track...');
}
console.log('Downloading and converting...');
proc.run();
});
}