-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDownTube.mjs
139 lines (122 loc) · 4.88 KB
/
DownTube.mjs
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/env node
import readline from 'readline';
import { exec } from 'child_process';
// Default configuration
let folderLocation = '/tmp'; // Default to /tmp if no path is provided.
let disableViaYtDlp = false; // Set to true to remove " (via yt-dlp)" from filenames.
let setToTrueToMoveChannelNameToEnd = false; // Set to true to move channel name to the end of the filename.
// Utility functions
const isSingleOrPlaylistVideo = (url) => url.includes("/watch?v=") || url.includes("/v/");
const isPlaylist = (url) => url.includes("&list=");
const isFullChannel = (url) => url.includes("/channel/") || url.includes("/@");
const ytDlpCommand = (url, mode, quality = '', downloadPath) => {
let command = "yt-dlp ";
let isSPV = isSingleOrPlaylistVideo(url);
let isPL = isPlaylist(url);
let isFullCH = isFullChannel(url);
let outputFolder = downloadPath || folderLocation;
if (isPL) {
outputFolder += '/%(playlist)s';
} else if (isFullCH) {
outputFolder += '/%(uploader)s';
}
// Define output template
let outputTemplate = `${outputFolder}/%(title)s (via yt-dlp).%(ext)s`;
if (!isPL && !isFullCH) {
outputTemplate = `${outputFolder}/%(uploader)s - %(title)s (via yt-dlp).%(ext)s`;
}
if (isPL || isFullCH) {
command += isPL ? "--yes-playlist " : "";
command += isFullCH ? "--download-archive channel_archive.txt " : ""; // Using an archive file to avoid re-downloads
}
switch (mode) {
case 'audio':
command += `--extract-audio --audio-format m4a --audio-quality 0 -o "${outputTemplate}" -f "bestaudio[ext=m4a]/bestaudio/bestvideo+bestaudio" "${url}"`;
break;
case 'video':
let videoQuality = 'bestvideo+bestaudio';
if (quality) {
videoQuality = `bestvideo[height<=${quality}]+bestaudio/best`;
}
command += `-f "${videoQuality}" --merge-output-format mkv -o "${outputTemplate}" "${url}"`;
break;
case 'comments':
case 'chat':
command += `--write-${mode} -o "${outputTemplate}" "${url}"`;
break;
}
// Remove channel-specific parts if not a full channel download
if (!isFullCH) {
command = command.replace(/--download-archive channel_archive\.txt /g, "");
}
if (disableViaYtDlp === true) {
command = command.replace(" (via yt-dlp)", "");
}
if (setToTrueToMoveChannelNameToEnd === true) {
command = command.replace("%(uploader)s - ", "");
command = command.replace(".%(ext)s", " - %(uploader)s.%(ext)s");
}
// Execute the command directly in the terminal
console.log("Executing the following yt-dlp command:\n" + command);
exec(command, (error, stdout, stderr) => {
if (error) {
console.error(`Error executing command: ${error.message}`);
return;
}
if (stderr) {
console.error(`stderr: ${stderr}`);
}
console.log(`stdout:\n${stdout}`);
console.log("\x1b[32mDownload completed successfully!\x1b[0m"); // Green success message
// Pause for 3 seconds before exiting
setTimeout(() => {
console.log("Exiting in 3 seconds...");
}, 3000);
});
};
// CLI Interface
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.question("Enter the folder location for downloads (leave empty for default: /tmp):\n> ", (inputFolder) => {
const downloadPath = inputFolder || '/tmp';
rl.question("Enter the YouTube URL:\n> ", (url) => {
console.log("Select download mode:");
console.log("1: Audio (m4a)");
console.log("2: Video (Best)");
console.log("3: Video (4K)");
console.log("4: Video (1080p)");
console.log("5: Video (720p)");
console.log("6: Comments");
console.log("7: Chat");
rl.question("> ", (mode) => {
switch (mode) {
case '1':
ytDlpCommand(url, 'audio', '', downloadPath);
break;
case '2':
ytDlpCommand(url, 'video', '', downloadPath);
break;
case '3':
ytDlpCommand(url, 'video', '2160', downloadPath);
break;
case '4':
ytDlpCommand(url, 'video', '1080', downloadPath);
break;
case '5':
ytDlpCommand(url, 'video', '720', downloadPath);
break;
case '6':
ytDlpCommand(url, 'comments', '', downloadPath);
break;
case '7':
ytDlpCommand(url, 'chat', '', downloadPath);
break;
default:
console.log("Invalid choice.");
}
rl.close();
});
});
});