Update index.js
Browse files
index.js
CHANGED
|
@@ -210,7 +210,7 @@ let mime = await fileTypeFromBuffer(buffer)
|
|
| 210 |
return fileUrl?.url_response
|
| 211 |
}
|
| 212 |
} catch (error) {
|
| 213 |
-
return
|
| 214 |
}
|
| 215 |
}
|
| 216 |
async function streamToBuffer(stream) {
|
|
@@ -279,11 +279,12 @@ function formatSize(bytes, si = false, dp = 2) {
|
|
| 279 |
async function ytmp4(url, quality = 'highestvideo') {
|
| 280 |
try {
|
| 281 |
const ID = ytdl.getVideoID(url),
|
| 282 |
-
data = await ytdl.getInfo('https://www.youtube.com/watch?v=' + ID)
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
|
|
|
| 287 |
|
| 288 |
if (format) {
|
| 289 |
return {
|
|
@@ -297,65 +298,69 @@ async function ytmp4(url, quality = 'highestvideo') {
|
|
| 297 |
quality: format.qualityLabel,
|
| 298 |
thumb: data.videoDetails.thumbnails[0].url,
|
| 299 |
dl_url: format.url,
|
| 300 |
-
|
| 301 |
-
|
| 302 |
} else {
|
| 303 |
throw new Error('No suitable format found');
|
| 304 |
}
|
| 305 |
} catch (error) {
|
| 306 |
console.error('Error occurred:', error);
|
| 307 |
-
return
|
| 308 |
}
|
| 309 |
}
|
|
|
|
| 310 |
async function ytmp3(url, bitrate = 'lowestaudio') {
|
| 311 |
try {
|
| 312 |
const ID = ytdl.getVideoID(url),
|
| 313 |
-
data = await ytdl.getInfo('https://www.youtube.com/watch?v=' + ID)
|
| 314 |
const audioStream = await ytdl(ID, { filter: 'audioonly', quality: bitrate });
|
| 315 |
-
let buffer = await streamToBuffer(audioStream)
|
|
|
|
| 316 |
async function convertToAudio(buffer, ext) {
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
});
|
| 338 |
-
ffmpegProcess.on('close', (code) => {
|
| 339 |
-
if (code !== 0) {
|
| 340 |
-
reject(`ffmpeg process closed with code: ${code}`);
|
| 341 |
-
} else {
|
| 342 |
-
resolve();
|
| 343 |
-
}
|
| 344 |
-
});
|
| 345 |
});
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 355 |
}
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
|
|
|
|
|
|
|
|
|
| 359 |
|
| 360 |
if (format) {
|
| 361 |
return {
|
|
@@ -369,14 +374,14 @@ async function ytmp3(url, bitrate = 'lowestaudio') {
|
|
| 369 |
quality: format.audioQuality,
|
| 370 |
thumb: data.videoDetails.thumbnails[0].url,
|
| 371 |
dl_url: format.url,
|
| 372 |
-
|
| 373 |
-
}
|
| 374 |
} else {
|
| 375 |
throw new Error('No suitable format found');
|
| 376 |
}
|
| 377 |
} catch (error) {
|
| 378 |
console.error('Error occurred:', error);
|
| 379 |
-
return
|
| 380 |
}
|
| 381 |
}
|
| 382 |
|
|
|
|
| 210 |
return fileUrl?.url_response
|
| 211 |
}
|
| 212 |
} catch (error) {
|
| 213 |
+
return null
|
| 214 |
}
|
| 215 |
}
|
| 216 |
async function streamToBuffer(stream) {
|
|
|
|
| 279 |
async function ytmp4(url, quality = 'highestvideo') {
|
| 280 |
try {
|
| 281 |
const ID = ytdl.getVideoID(url),
|
| 282 |
+
data = await ytdl.getInfo('https://www.youtube.com/watch?v=' + ID);
|
| 283 |
+
const videoStream = await ytdl(ID, { filter: 'videoandaudio', quality: 'highestvideo' });
|
| 284 |
+
let buffer = await streamToBuffer(videoStream);
|
| 285 |
+
let upload = await uploadBuffer(buffer);
|
| 286 |
+
let urel = upload != null ? { url_v2: upload } : null;
|
| 287 |
+
let format = ytdl.chooseFormat(data.formats, { filter: 'videoandaudio', quality: quality });
|
| 288 |
|
| 289 |
if (format) {
|
| 290 |
return {
|
|
|
|
| 298 |
quality: format.qualityLabel,
|
| 299 |
thumb: data.videoDetails.thumbnails[0].url,
|
| 300 |
dl_url: format.url,
|
| 301 |
+
...urel,
|
| 302 |
+
};
|
| 303 |
} else {
|
| 304 |
throw new Error('No suitable format found');
|
| 305 |
}
|
| 306 |
} catch (error) {
|
| 307 |
console.error('Error occurred:', error);
|
| 308 |
+
return error;
|
| 309 |
}
|
| 310 |
}
|
| 311 |
+
|
| 312 |
async function ytmp3(url, bitrate = 'lowestaudio') {
|
| 313 |
try {
|
| 314 |
const ID = ytdl.getVideoID(url),
|
| 315 |
+
data = await ytdl.getInfo('https://www.youtube.com/watch?v=' + ID);
|
| 316 |
const audioStream = await ytdl(ID, { filter: 'audioonly', quality: bitrate });
|
| 317 |
+
let buffer = await streamToBuffer(audioStream);
|
| 318 |
+
|
| 319 |
async function convertToAudio(buffer, ext) {
|
| 320 |
+
try {
|
| 321 |
+
const tmp = path.join(os.tmpdir(), `${+new Date()}.${ext}`);
|
| 322 |
+
const out = `${tmp}.mp3`;
|
| 323 |
+
await fs.promises.writeFile(tmp, buffer);
|
| 324 |
+
|
| 325 |
+
const ffmpegProcess = cp.spawn('ffmpeg', [
|
| 326 |
+
'-y',
|
| 327 |
+
'-i', tmp,
|
| 328 |
+
'-vn',
|
| 329 |
+
'-ac', '2',
|
| 330 |
+
'-b:a', '128k',
|
| 331 |
+
'-ar', '44100',
|
| 332 |
+
'-f', 'mp3',
|
| 333 |
+
out
|
| 334 |
+
]);
|
| 335 |
+
|
| 336 |
+
await new Promise((resolve, reject) => {
|
| 337 |
+
ffmpegProcess.on('error', (err) => {
|
| 338 |
+
console.error(err);
|
| 339 |
+
reject(err);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 340 |
});
|
| 341 |
+
ffmpegProcess.on('close', (code) => {
|
| 342 |
+
if (code !== 0) {
|
| 343 |
+
reject(`ffmpeg process closed with code: ${code}`);
|
| 344 |
+
} else {
|
| 345 |
+
resolve();
|
| 346 |
+
}
|
| 347 |
+
});
|
| 348 |
+
});
|
| 349 |
+
|
| 350 |
+
const result = await fs.promises.readFile(out);
|
| 351 |
+
await fs.promises.unlink(tmp);
|
| 352 |
+
await fs.promises.unlink(out);
|
| 353 |
+
return result;
|
| 354 |
+
} catch (e) {
|
| 355 |
+
console.error(e);
|
| 356 |
+
throw e;
|
| 357 |
}
|
| 358 |
+
}
|
| 359 |
+
|
| 360 |
+
let konver = await convertToAudio(buffer, "mp4");
|
| 361 |
+
let upload = await uploadBuffer(konver);
|
| 362 |
+
let urel = upload != null ? { url_v2: upload } : null;
|
| 363 |
+
let format = ytdl.chooseFormat(data.formats, { filter: 'audioonly', quality: bitrate });
|
| 364 |
|
| 365 |
if (format) {
|
| 366 |
return {
|
|
|
|
| 374 |
quality: format.audioQuality,
|
| 375 |
thumb: data.videoDetails.thumbnails[0].url,
|
| 376 |
dl_url: format.url,
|
| 377 |
+
...urel,
|
| 378 |
+
};
|
| 379 |
} else {
|
| 380 |
throw new Error('No suitable format found');
|
| 381 |
}
|
| 382 |
} catch (error) {
|
| 383 |
console.error('Error occurred:', error);
|
| 384 |
+
return error;
|
| 385 |
}
|
| 386 |
}
|
| 387 |
|