Update index.js
Browse files
index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
const yts = require('yt-search');
|
| 2 |
const morgan = require('morgan');
|
| 3 |
const express = require('express');
|
| 4 |
-
const
|
| 5 |
const { Writable, pipeline } = require('stream');
|
| 6 |
const util = require('util');
|
| 7 |
const axios = require('axios');
|
|
@@ -280,31 +280,29 @@ function formatSize(bytes, si = false, dp = 2) {
|
|
| 280 |
|
| 281 |
async function ytmp4(url, quality = 'highestvideo') {
|
| 282 |
try {
|
| 283 |
-
const
|
| 284 |
-
|
| 285 |
-
|
|
|
|
|
|
|
|
|
|
| 286 |
let buffer = await streamToBuffer(videoStream);
|
| 287 |
let upload = await uploadBuffer(buffer);
|
| 288 |
let urel = upload != null ? { url_v2: upload } : null;
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
...urel,
|
| 304 |
-
};
|
| 305 |
-
} else {
|
| 306 |
-
throw new Error('No suitable format found');
|
| 307 |
-
}
|
| 308 |
} catch (error) {
|
| 309 |
console.error('Error occurred:', error);
|
| 310 |
return error;
|
|
@@ -313,74 +311,30 @@ async function ytmp4(url, quality = 'highestvideo') {
|
|
| 313 |
|
| 314 |
async function ytmp3(url, bitrate = 'lowestaudio') {
|
| 315 |
try {
|
| 316 |
-
const
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
async function convertToAudio(buffer, ext) {
|
| 322 |
-
try {
|
| 323 |
-
const tmp = path.join(os.tmpdir(), `${+new Date()}.${ext}`);
|
| 324 |
-
const out = `${tmp}.mp3`;
|
| 325 |
-
await fs.promises.writeFile(tmp, buffer);
|
| 326 |
-
|
| 327 |
-
const ffmpegProcess = cp.spawn('ffmpeg', [
|
| 328 |
-
'-y',
|
| 329 |
-
'-i', tmp,
|
| 330 |
-
'-vn',
|
| 331 |
-
'-ac', '2',
|
| 332 |
-
'-b:a', '128k',
|
| 333 |
-
'-ar', '44100',
|
| 334 |
-
'-f', 'mp3',
|
| 335 |
-
out
|
| 336 |
-
]);
|
| 337 |
-
|
| 338 |
-
await new Promise((resolve, reject) => {
|
| 339 |
-
ffmpegProcess.on('error', (err) => {
|
| 340 |
-
console.error(err);
|
| 341 |
-
reject(err);
|
| 342 |
-
});
|
| 343 |
-
ffmpegProcess.on('close', (code) => {
|
| 344 |
-
if (code !== 0) {
|
| 345 |
-
reject(`ffmpeg process closed with code: ${code}`);
|
| 346 |
-
} else {
|
| 347 |
-
resolve();
|
| 348 |
-
}
|
| 349 |
-
});
|
| 350 |
-
});
|
| 351 |
-
|
| 352 |
-
const result = await fs.promises.readFile(out);
|
| 353 |
-
await fs.promises.unlink(tmp);
|
| 354 |
-
await fs.promises.unlink(out);
|
| 355 |
-
return result;
|
| 356 |
-
} catch (e) {
|
| 357 |
-
console.error(e);
|
| 358 |
-
throw e;
|
| 359 |
-
}
|
| 360 |
-
}
|
| 361 |
|
|
|
|
| 362 |
let konver = await convertToAudio(buffer, "mp4");
|
| 363 |
let upload = await uploadBuffer(konver);
|
| 364 |
let urel = upload != null ? { url_v2: upload } : null;
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
...urel,
|
| 380 |
-
};
|
| 381 |
-
} else {
|
| 382 |
-
throw new Error('No suitable format found');
|
| 383 |
-
}
|
| 384 |
} catch (error) {
|
| 385 |
console.error('Error occurred:', error);
|
| 386 |
return error;
|
|
|
|
| 1 |
const yts = require('yt-search');
|
| 2 |
const morgan = require('morgan');
|
| 3 |
const express = require('express');
|
| 4 |
+
const youtubedl = require('youtubedl-core');
|
| 5 |
const { Writable, pipeline } = require('stream');
|
| 6 |
const util = require('util');
|
| 7 |
const axios = require('axios');
|
|
|
|
| 280 |
|
| 281 |
async function ytmp4(url, quality = 'highestvideo') {
|
| 282 |
try {
|
| 283 |
+
const info = await youtubedl.getInfo(url);
|
| 284 |
+
const videoStream = youtubedl(url, {
|
| 285 |
+
quality: 'highestvideo',
|
| 286 |
+
filter: (format) => format.container === 'mp4' && format.qualityLabel === quality
|
| 287 |
+
});
|
| 288 |
+
|
| 289 |
let buffer = await streamToBuffer(videoStream);
|
| 290 |
let upload = await uploadBuffer(buffer);
|
| 291 |
let urel = upload != null ? { url_v2: upload } : null;
|
| 292 |
+
|
| 293 |
+
return {
|
| 294 |
+
title: info.title,
|
| 295 |
+
description: info.description,
|
| 296 |
+
channel: info.channel,
|
| 297 |
+
views: formatViews(info.view_count),
|
| 298 |
+
publish: info.upload_date,
|
| 299 |
+
duration: formatDuration(info.duration),
|
| 300 |
+
size: info.formats[0].filesize,
|
| 301 |
+
quality: info.formats[0].quality,
|
| 302 |
+
thumb: info.thumbnail,
|
| 303 |
+
dl_url: info.url,
|
| 304 |
+
...urel,
|
| 305 |
+
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 306 |
} catch (error) {
|
| 307 |
console.error('Error occurred:', error);
|
| 308 |
return error;
|
|
|
|
| 311 |
|
| 312 |
async function ytmp3(url, bitrate = 'lowestaudio') {
|
| 313 |
try {
|
| 314 |
+
const info = await youtubedl.getInfo(url);
|
| 315 |
+
const audioStream = youtubedl(url, {
|
| 316 |
+
quality: 'highestaudio',
|
| 317 |
+
filter: 'audioonly'
|
| 318 |
+
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 319 |
|
| 320 |
+
let buffer = await streamToBuffer(audioStream);
|
| 321 |
let konver = await convertToAudio(buffer, "mp4");
|
| 322 |
let upload = await uploadBuffer(konver);
|
| 323 |
let urel = upload != null ? { url_v2: upload } : null;
|
| 324 |
+
|
| 325 |
+
return {
|
| 326 |
+
title: info.title,
|
| 327 |
+
description: info.description,
|
| 328 |
+
channel: info.channel,
|
| 329 |
+
views: formatViews(info.view_count),
|
| 330 |
+
publish: info.upload_date,
|
| 331 |
+
duration: formatDuration(info.duration),
|
| 332 |
+
size: info.formats[0].filesize,
|
| 333 |
+
quality: info.formats[0].quality,
|
| 334 |
+
thumb: info.thumbnail,
|
| 335 |
+
dl_url: info.url,
|
| 336 |
+
...urel,
|
| 337 |
+
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 338 |
} catch (error) {
|
| 339 |
console.error('Error occurred:', error);
|
| 340 |
return error;
|