ilhamdev commited on
Commit
78bfdd6
·
verified ·
1 Parent(s): a83d66e

Update index.js

Browse files
Files changed (1) hide show
  1. index.js +50 -7
index.js CHANGED
@@ -13,6 +13,8 @@ const cloudscraper = require('cloudscraper')
13
  const acrcloud = require("acrcloud");
14
  const { File } = require('megajs')
15
  const { BingChat } = require("bing-chat-cjs-rnz");
 
 
16
  let fetch;
17
 
18
  (async () => {
@@ -274,9 +276,9 @@ async function ytmp4(url, quality = 'highestvideo') {
274
  try {
275
  const ID = ytdl.getVideoID(url),
276
  data = await ytdl.getInfo('https://www.youtube.com/watch?v=' + ID)
277
- /*const videoStream = await ytdl(ID, { filter: 'videoandaudio', quality: 'highestvideo' });
278
  let buffer = await streamToBuffer(videoStream)
279
- let upload = await uploadBuffer(buffer)*/
280
  let format = ytdl.chooseFormat(data.formats, { filter: 'videoandaudio', quality: quality });
281
 
282
  if (format) {
@@ -291,7 +293,7 @@ async function ytmp4(url, quality = 'highestvideo') {
291
  quality: format.qualityLabel,
292
  thumb: data.videoDetails.thumbnails[0].url,
293
  dl_url: format.url,
294
- //url_v2: upload,
295
  }
296
  } else {
297
  throw new Error('No suitable format found');
@@ -305,9 +307,50 @@ async function ytmp3(url, bitrate = 'lowestaudio') {
305
  try {
306
  const ID = ytdl.getVideoID(url),
307
  data = await ytdl.getInfo('https://www.youtube.com/watch?v=' + ID)
308
- /*const audioStream = await ytdl(ID, { filter: 'audioonly', quality: bitrate });
309
  let buffer = await streamToBuffer(audioStream)
310
- let upload = await uploadBuffer(buffer)*/
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
311
  let format = ytdl.chooseFormat(data.formats, { filter: 'audioonly', quality: bitrate });
312
 
313
  if (format) {
@@ -321,8 +364,8 @@ async function ytmp3(url, bitrate = 'lowestaudio') {
321
  size: format.contentLength ? formatSize(format.contentLength) : 0,
322
  quality: format.audioQuality,
323
  thumb: data.videoDetails.thumbnails[0].url,
324
- dl_url: format.url,
325
- //url_v2: upload
326
  }
327
  } else {
328
  throw new Error('No suitable format found');
 
13
  const acrcloud = require("acrcloud");
14
  const { File } = require('megajs')
15
  const { BingChat } = require("bing-chat-cjs-rnz");
16
+ const path = require("path")
17
+ const fs = require("fs")
18
  let fetch;
19
 
20
  (async () => {
 
276
  try {
277
  const ID = ytdl.getVideoID(url),
278
  data = await ytdl.getInfo('https://www.youtube.com/watch?v=' + ID)
279
+ const videoStream = await ytdl(ID, { filter: 'videoandaudio', quality: 'highestvideo' });
280
  let buffer = await streamToBuffer(videoStream)
281
+ let upload = await uploadBuffer(buffer)
282
  let format = ytdl.chooseFormat(data.formats, { filter: 'videoandaudio', quality: quality });
283
 
284
  if (format) {
 
293
  quality: format.qualityLabel,
294
  thumb: data.videoDetails.thumbnails[0].url,
295
  dl_url: format.url,
296
+ url_v2: upload,
297
  }
298
  } else {
299
  throw new Error('No suitable format found');
 
307
  try {
308
  const ID = ytdl.getVideoID(url),
309
  data = await ytdl.getInfo('https://www.youtube.com/watch?v=' + ID)
310
+ const audioStream = await ytdl(ID, { filter: 'audioonly', quality: bitrate });
311
  let buffer = await streamToBuffer(audioStream)
312
+ async function convertToAudio(buffer, ext) {
313
+ try {
314
+ const tmp = path.join(os.tmpdir(), `${+new Date()}.${ext}`);
315
+ const out = `${tmp}.mp3`;
316
+ await fs.promises.writeFile(tmp, buffer);
317
+
318
+ const ffmpegProcess = cp.spawn('ffmpeg', [
319
+ '-y',
320
+ '-i', tmp,
321
+ '-vn',
322
+ '-ac', '2',
323
+ '-b:a', '128k',
324
+ '-ar', '44100',
325
+ '-f', 'mp3',
326
+ out
327
+ ]);
328
+
329
+ await new Promise((resolve, reject) => {
330
+ ffmpegProcess.on('error', (err) => {
331
+ console.error(err);
332
+ reject(err);
333
+ });
334
+ ffmpegProcess.on('close', (code) => {
335
+ if (code !== 0) {
336
+ reject(`ffmpeg process closed with code: ${code}`);
337
+ } else {
338
+ resolve();
339
+ }
340
+ });
341
+ });
342
+
343
+ const result = await fs.promises.readFile(out);
344
+ await fs.promises.unlink(tmp);
345
+ await fs.promises.unlink(out);
346
+ return result;
347
+ } catch (e) {
348
+ console.error(e);
349
+ throw e;
350
+ }
351
+ }
352
+ let konver = await convertToAudio(buffer, "mp4")
353
+ let upload = await uploadBuffer(konver)
354
  let format = ytdl.chooseFormat(data.formats, { filter: 'audioonly', quality: bitrate });
355
 
356
  if (format) {
 
364
  size: format.contentLength ? formatSize(format.contentLength) : 0,
365
  quality: format.audioQuality,
366
  thumb: data.videoDetails.thumbnails[0].url,
367
+ dl_url: format.url,
368
+ url_v2: upload
369
  }
370
  } else {
371
  throw new Error('No suitable format found');