-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
236 lines (206 loc) · 6.72 KB
/
Copy pathindex.js
File metadata and controls
236 lines (206 loc) · 6.72 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#!/usr/bin/env node
const { spawn } = require('child_process');
const prompts = require('prompts');
const path = require('path');
const fs = require('fs');
/**
* Downloads YouTube video(s) with specified format using yt-dlp
* @param {string} url - YouTube video or playlist URL
* @param {string} outputPath - Destination folder for files
* @param {string} format - Download format: 'video', 'audio', or 'both'
* @returns {Promise<void>}
*/
async function downloadAndConvert(url, outputPath, format) {
return new Promise((resolve, reject) => {
// Ensure output directory exists
if (!fs.existsSync(outputPath)) {
fs.mkdirSync(outputPath, { recursive: true });
}
let args = [];
// Configure arguments based on format choice
switch (format) {
case 'audio':
args = [
'-x', // Extract audio
'--audio-format', 'mp3', // Convert to MP3
'--audio-quality', '0', // Best quality
'-o', path.join(outputPath, '%(playlist_index)03d-%(title)s.%(ext)s'),
'--no-playlist',
url
];
break;
case 'video':
args = [
'-f', 'best[ext=mp4]', // Best MP4 video
'-o', path.join(outputPath, '%(playlist_index)03d-%(title)s.%(ext)s'),
'--no-playlist',
url
];
break;
case 'both':
args = [
'-f', 'best[ext=mp4]', // Download video
'--write-auto-subs', // Optional: download subtitles
'-o', path.join(outputPath, '%(playlist_index)03d-%(title)s.%(ext)s'),
'--no-playlist',
url
];
break;
}
// If URL contains 'playlist', enable playlist mode
if (url.includes('playlist') || url.includes('&list=')) {
const playlistIndex = args.indexOf('--no-playlist');
if (playlistIndex !== -1) {
args.splice(playlistIndex, 1);
args.push('--yes-playlist');
}
}
console.log(`\nStarting ${format} download...\n`);
const ytdlp = spawn('yt-dlp', args);
ytdlp.stdout.on('data', (data) => {
process.stdout.write(data.toString());
});
ytdlp.stderr.on('data', (data) => {
process.stderr.write(data.toString());
});
ytdlp.on('close', async (code) => {
if (code === 0) {
if (format === 'both') {
// Download audio separately when 'both' is selected
console.log('\n📹 Video download completed! Now downloading audio...\n');
try {
await downloadAudio(url, outputPath);
console.log('\n✓ Both video and audio downloads completed successfully!\n');
resolve();
} catch (audioError) {
reject(audioError);
}
} else {
console.log(`\n✓ ${format.charAt(0).toUpperCase() + format.slice(1)} download completed successfully!\n`);
resolve();
}
} else {
reject(new Error(`yt-dlp exited with code ${code}`));
}
});
ytdlp.on('error', (err) => {
if (err.code === 'ENOENT') {
reject(new Error('yt-dlp is not installed or not in PATH. Please install it first.'));
} else {
reject(err);
}
});
});
}
/**
* Downloads audio separately (used when 'both' format is selected)
* @param {string} url - YouTube video or playlist URL
* @param {string} outputPath - Destination folder for audio files
* @returns {Promise<void>}
*/
function downloadAudio(url, outputPath) {
return new Promise((resolve, reject) => {
const audioPath = path.join(outputPath, 'audio');
// Ensure audio directory exists
if (!fs.existsSync(audioPath)) {
fs.mkdirSync(audioPath, { recursive: true });
}
const args = [
'-x', // Extract audio
'--audio-format', 'mp3', // Convert to MP3
'--audio-quality', '0', // Best quality
'-o', path.join(audioPath, '%(playlist_index)03d-%(title)s.%(ext)s'),
'--no-playlist',
url
];
// If URL contains 'playlist', enable playlist mode
if (url.includes('playlist') || url.includes('&list=')) {
const playlistIndex = args.indexOf('--no-playlist');
if (playlistIndex !== -1) {
args.splice(playlistIndex, 1);
args.push('--yes-playlist');
}
}
const ytdlp = spawn('yt-dlp', args);
ytdlp.stdout.on('data', (data) => {
process.stdout.write(data.toString());
});
ytdlp.stderr.on('data', (data) => {
process.stderr.write(data.toString());
});
ytdlp.on('close', (code) => {
if (code === 0) {
resolve();
} else {
reject(new Error(`Audio download failed with code ${code}`));
}
});
ytdlp.on('error', (err) => {
reject(err);
});
});
}
/**
* Validates YouTube URL format
* @param {string} url - URL to validate
* @returns {boolean}
*/
function isValidYouTubeUrl(url) {
const youtubeRegex = /^(https?:\/\/)?(www\.)?(youtube\.com|youtu\.be)\/.+/;
return youtubeRegex.test(url);
}
/**
* Main function to run the CLI application
*/
async function main() {
console.log('==============================');
console.log(' ~~ YOUTUBE DOWNLOADER ~~ ');
console.log('==============================');
try {
const questions = [
{
type: 'text',
name: 'url',
message: 'Enter YouTube video or playlist URL:',
validate: (value) => {
if (!value) return 'URL is required';
// if (!isValidYouTubeUrl(value)) return 'Please enter a valid YouTube URL';
return true;
}
},
{
type: 'select',
name: 'format',
message: 'What would you like to download?',
choices: [
{ title: '🎵 Audio only (MP3)', value: 'audio' },
{ title: '🎬 Video only (MP4)', value: 'video' },
{ title: '🎵🎬 Both audio and video', value: 'both' }
],
initial: 0
},
{
type: 'text',
name: 'outputPath',
message: 'Enter destination folder:',
initial: './downloads',
validate: (value) => {
if (!value) return 'Destination folder is required';
return true;
}
}
];
const response = await prompts(questions);
// Check if user cancelled (Ctrl+C)
if (!response.url || !response.format || !response.outputPath) {
console.log('\nOperation cancelled.');
process.exit(0);
}
await downloadAndConvert(response.url, response.outputPath, response.format);
} catch (error) {
console.error('\n✗ Error:', error.message);
process.exit(1);
}
}
// Run the application
main();