-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyoutube.ts
59 lines (47 loc) · 1.46 KB
/
youtube.ts
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
import { YtdlCore, toPipeableStream } from "@ybd-project/ytdl-core";
import fs from "fs";
import path from "path";
import chalk from "chalk";
async function downloadYoutubeAudio(url: string): Promise<void> {
try {
if (!url) {
throw new Error("Please provide a YouTube URL");
}
console.log(chalk.cyan("🔍 Fetching video information..."));
const ytdl = new YtdlCore({});
let videoTitle = "";
ytdl.getBasicInfo(url).then((info) => {
videoTitle = info.videoDetails.title.replace(/[^\w\s]/gi, "_");
});
const stream = await ytdl.download(url, {
// TODO: add flag for audio only in future
filter: "audioonly",
});
const outputFilePath = path.join(
process.cwd(),
"audio",
`${videoTitle}.mp3`
);
toPipeableStream(stream).pipe(fs.createWriteStream(outputFilePath));
console.log(chalk.green(`✅ Download finished: ${outputFilePath}`));
} catch (error) {
if (error instanceof Error) {
console.error(chalk.red("Error:", error.message));
}
process.exit(1);
}
}
// Get command line arguments
const args = process.argv.slice(2);
const urlArg = args.find((arg) => arg.startsWith("--url="));
if (!urlArg) {
console.error(
chalk.red("Please provide a YouTube URL using --url=<youtube_url>")
);
process.exit(1);
}
const url = urlArg.split("--url=")[1];
downloadYoutubeAudio(url).catch((error) => {
console.error(chalk.red("Fatal error:", error));
process.exit(1);
});