ilhamdev commited on
Commit
06c148c
·
verified ·
1 Parent(s): 2a81e9d

Update index.js

Browse files
Files changed (1) hide show
  1. index.js +49 -7
index.js CHANGED
@@ -1,7 +1,12 @@
1
  const yts = require('yt-search');
2
  const morgan = require('morgan');
3
  const express = require('express');
4
- const ytdl = require('ytdl-core')
 
 
 
 
 
5
 
6
  // Regex untuk mengidentifikasi URL YouTube dan Mega
7
  const ytIdRegex = /(?:http(?:s|):\/\/|)(?:(?:www\.|)?youtube(?:\-nocookie|)\.com\/(?:shorts\/)?(?:watch\?.*(?:|\&)v=|embed\/|v\/)?|youtu\.be\/)([-_0-9A-Za-z]{11})/;
@@ -17,6 +22,35 @@ const post = async (url, form, headers = {}) => {
17
  };
18
 
19
  //YTDL-CORE
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  function formatViews(viewCount) {
21
  if (viewCount >= 1000000000) {
22
  return (viewCount / 1000000000).toFixed(1) + 'B';
@@ -70,8 +104,11 @@ function formatSize(bytes, si = false, dp = 2) {
70
  async function ytmp4(url, quality = 'highestvideo') {
71
  try {
72
  const ID = ytdl.getVideoID(url),
73
- data = await ytdl.getInfo('https://www.youtube.com/watch?v=' + ID),
74
- format = ytdl.chooseFormat(data.formats, { quality: quality });
 
 
 
75
 
76
  if (format) {
77
  return {
@@ -85,7 +122,8 @@ async function ytmp4(url, quality = 'highestvideo') {
85
  quality: format.qualityLabel,
86
  thumb: data.videoDetails.thumbnails[0].url,
87
  dl_url: format.url,
88
- }
 
89
  } else {
90
  throw new Error('No suitable format found');
91
  }
@@ -97,8 +135,11 @@ async function ytmp4(url, quality = 'highestvideo') {
97
  async function ytmp3(url, bitrate = 'lowestaudio') {
98
  try {
99
  const ID = ytdl.getVideoID(url),
100
- data = await ytdl.getInfo('https://www.youtube.com/watch?v=' + ID),
101
- format = ytdl.chooseFormat(data.formats, { filter: 'audioonly', quality: bitrate });
 
 
 
102
 
103
  if (format) {
104
  return {
@@ -111,7 +152,8 @@ async function ytmp3(url, bitrate = 'lowestaudio') {
111
  size: format.contentLength ? formatSize(format.contentLength) : 0,
112
  quality: format.audioQuality,
113
  thumb: data.videoDetails.thumbnails[0].url,
114
- dl_url: format.url,
 
115
  }
116
  } else {
117
  throw new Error('No suitable format found');
 
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 } = require('stream');
6
+ const util = require('util');
7
+ const FormData = require('form-data');
8
+ const axios = require('axios');
9
+ const { fileTypeFromBuffer } = require('file-type')
10
 
11
  // Regex untuk mengidentifikasi URL YouTube dan Mega
12
  const ytIdRegex = /(?:http(?:s|):\/\/|)(?:(?:www\.|)?youtube(?:\-nocookie|)\.com\/(?:shorts\/)?(?:watch\?.*(?:|\&)v=|embed\/|v\/)?|youtu\.be\/)([-_0-9A-Za-z]{11})/;
 
22
  };
23
 
24
  //YTDL-CORE
25
+ async function uploadBuffer(buffer) {
26
+ //file: (binary)
27
+ //expirationTime: 1
28
+ let mime = await fileTypeFromBuffer(buffer)
29
+ let formData = new FormData()
30
+ formData.append('file', buffer, `${Date.now()}.${mime.ext}`)
31
+ formData.append('expirationTime', 1)
32
+ let { data } = await axios.post(`https://cdn.itzpire.site/upload`, formData, {
33
+ headers: {
34
+ ...formData.getHeaders(),
35
+ 'accept': 'application/json',
36
+ 'Content-Type': 'multipart/form-data'
37
+ }
38
+ })
39
+ return data?.fileUrl
40
+ }
41
+ async function streamToBuffer(stream) {
42
+ const chunks = [];
43
+ const captureChunks = new Writable({
44
+ write(chunk, encoding, callback) {
45
+ chunks.push(chunk);
46
+ callback();
47
+ }
48
+ });
49
+
50
+ await util.promisify(pipeline)(stream, captureChunks);
51
+
52
+ return Buffer.concat(chunks);
53
+ }
54
  function formatViews(viewCount) {
55
  if (viewCount >= 1000000000) {
56
  return (viewCount / 1000000000).toFixed(1) + 'B';
 
104
  async function ytmp4(url, quality = 'highestvideo') {
105
  try {
106
  const ID = ytdl.getVideoID(url),
107
+ data = await ytdl.getInfo('https://www.youtube.com/watch?v=' + ID)
108
+ const videoStream = await ytdl(ID, { quality: 'highestvideo' });
109
+ let buffer = await streamToBuffer(videoStream)
110
+ let upload = await uploadBuffer(buffer)
111
+ let format = ytdl.chooseFormat(data.formats, { quality: quality });
112
 
113
  if (format) {
114
  return {
 
122
  quality: format.qualityLabel,
123
  thumb: data.videoDetails.thumbnails[0].url,
124
  dl_url: format.url,
125
+ url_v2: upload,
126
+ }
127
  } else {
128
  throw new Error('No suitable format found');
129
  }
 
135
  async function ytmp3(url, bitrate = 'lowestaudio') {
136
  try {
137
  const ID = ytdl.getVideoID(url),
138
+ data = await ytdl.getInfo('https://www.youtube.com/watch?v=' + ID)
139
+ let audioStream = await ytdl(ID, { filter: 'audioonly', quality: 'lowestaudio', });
140
+ let buffer = await streamToBuffer(audioStream)
141
+ let upload = await uploadBuffer(buffer)
142
+ let format = ytdl.chooseFormat(data.formats, { filter: 'audioonly', quality: bitrate });
143
 
144
  if (format) {
145
  return {
 
152
  size: format.contentLength ? formatSize(format.contentLength) : 0,
153
  quality: format.audioQuality,
154
  thumb: data.videoDetails.thumbnails[0].url,
155
+ dl_url: format.url,
156
+ url_v2: upload
157
  }
158
  } else {
159
  throw new Error('No suitable format found');