-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
78 lines (62 loc) · 1.84 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 ytdl = require('ytdl-core');
const fs = require('fs');
const os = require('os');
const { createInterface } = require('readline');
const readline = createInterface({
input: process.stdin,
output: process.stdout
});
const run = async () => {
try {
const url = await urlPrompt();
const mp3Name = await getMp3Name();
const mp3DownloadPath = `${ os.homedir() }/Downloads/${ mp3Name }.mp3`;
const res = await download(url, mp3DownloadPath);
console.log(res);
} catch (error) {
console.log(error)
}
}
run();
function download(url, output = 'audio.mp3', defFilter = { filter: 'audioonly' }) {
const stream = ytdl(url, defFilter);
stream.pipe(fs.createWriteStream(output, { flags: 'w' }));
stream.on('progress', (chunkLength, downloaded, total) => {
const floatDownloaded = downloaded / 1_000_000;
const floatTotal = total / 1_000_000;
const percentage = (downloaded / total) * 100;
console.log(`${floatDownloaded.toFixed(2)} MB of ${floatTotal.toFixed(2)} MB ${percentage.toFixed(2)}%`);
});
return new Promise((resolve, reject) => {
stream.on('finish', () => {
resolve('done')
});
stream.on('error', (error) => {
console.log(error);
reject('error');
});
});
}
async function prompt(question) {
const readLineAsync = msg => {
return new Promise(resolve => {
readline.question(msg, userRes => resolve(userRes));
});
}
const userRes = await readLineAsync(question);
return userRes;
}
async function urlPrompt() {
const mp3Url = await prompt('Paste the URL press Control + C to exit:');
if(!mp3Url || !mp3Url.startsWith('https://www.youtube.com/watch?v=')) {
return urlPrompt();
}
return mp3Url;
}
async function getMp3Name() {
const mp3Name = await prompt('Type the mp3 file name or press Control + C to exit:');
if(!mp3Name) {
return getMp3Name();
}
return mp3Name;
}