-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathytmp3.js
executable file
·59 lines (53 loc) · 2.03 KB
/
ytmp3.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
const readline = require("readline");
const fs = require("fs").promises;
const path = require("path")
const ytdl = require("ytdl-core");
const ffmpeg = require("fluent-ffmpeg");
const { Command } = require("commander");
const convertIntoValidFilename = (name) => {
return (name.replace(/[\/|\\:*?"<>]/g, " "));
}
const downloadYtVideoToMp3 = (url, i) => {
ytdl.getBasicInfo(url)
.then(info => {
let stream = ytdl(url, { quality: "highestaudio" });
let start = Date.now();
const title = info.videoDetails.title;
ffmpeg(stream)
.audioBitrate(128)
.save(path.join(process.cwd(), convertIntoValidFilename(`${title}.mp3`)))
.on("progress", p => {
console.log(`Downloading ${title}`);
})
.on("end", () => {
console.log(`\n${i}. ${title} Finished downloading in ${Date.now() - start}\n`);
});
})
.catch(reason => {
console.error(reason);
})
}
const program = new Command();
program
.name("ytmp3")
.description("Command line application for downloading youtube videos into mp3 file")
.version("Beta 0.1")
.option("-f, --file <string>", "Source filename default to playlist.txt in current directory if any", "playlist.txt")
.option("-o, --output <string>", "Output directory default to current directory", process.cwd())
.action(async (options) => {
const file = options.file;
const output = options.output;
let data;
try {
data = await fs.readFile(path.join(process.cwd(), file), "utf-8");
} catch(error) {
console.log(`ERROR: ${error}`);
}
data = data.split("\n");
data.forEach(async (url, i) => await downloadYtVideoToMp3(url, i));
})
program.command("download")
.description("Download from a single url")
.argument("<string>", "The URL")
.action(url => downloadYtVideoToMp3(url, 1));
program.parse(process.argv);