ilhamdev commited on
Commit
2a5bb76
·
verified ·
1 Parent(s): ef03ae0

Update index.js

Browse files
Files changed (1) hide show
  1. index.js +41 -87
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 ytdl = require('ytdl-core');
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 ID = ytdl.getVideoID(url),
284
- data = await ytdl.getInfo('https://www.youtube.com/watch?v=' + ID);
285
- const videoStream = await ytdl(ID, { filter: format => format.hasVideo && format.hasAudio, quality: quality });
 
 
 
286
  let buffer = await streamToBuffer(videoStream);
287
  let upload = await uploadBuffer(buffer);
288
  let urel = upload != null ? { url_v2: upload } : null;
289
- let format = ytdl.chooseFormat(data.formats, { filter: format => format.hasVideo && format.hasAudio, quality: quality });
290
-
291
- if (format) {
292
- return {
293
- title: data.videoDetails.title,
294
- description: data.videoDetails.description,
295
- channel: data.videoDetails.ownerChannelName,
296
- views: formatViews(data.videoDetails.viewCount),
297
- publish: data.videoDetails.publishDate,
298
- duration: formatDuration(data.videoDetails.lengthSeconds),
299
- size: format.contentLength ? formatSize(format.contentLength) : 0,
300
- quality: format.qualityLabel,
301
- thumb: data.videoDetails.thumbnails[0].url,
302
- dl_url: format.url,
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 ID = ytdl.getVideoID(url),
317
- data = await ytdl.getInfo('https://www.youtube.com/watch?v=' + ID);
318
- const audioStream = await ytdl(ID, { filter: 'audioonly', quality: bitrate });
319
- let buffer = await streamToBuffer(audioStream);
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
- let format = ytdl.chooseFormat(data.formats, { filter: 'audioonly', quality: bitrate });
366
-
367
- if (format) {
368
- return {
369
- title: data.videoDetails.title,
370
- description: data.videoDetails.description,
371
- channel: data.videoDetails.ownerChannelName,
372
- views: formatViews(data.videoDetails.viewCount),
373
- publish: data.videoDetails.publishDate,
374
- duration: formatDuration(data.videoDetails.lengthSeconds),
375
- size: format.contentLength ? formatSize(format.contentLength) : 0,
376
- quality: format.audioQuality,
377
- thumb: data.videoDetails.thumbnails[0].url,
378
- dl_url: format.url,
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;