ilhamdev commited on
Commit
c317478
·
verified ·
1 Parent(s): f94538c

Update index.js

Browse files
Files changed (1) hide show
  1. index.js +121 -0
index.js CHANGED
@@ -16,6 +16,112 @@ const post = async (url, form, headers = {}) => {
16
  return response;
17
  };
18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  // Fungsi untuk mengkonversi video dari YouTube
20
  const convert = async (url, v_id, ftype, fquality, fname, token, timeExpire) => {
21
  let params = {
@@ -131,4 +237,19 @@ const app = express()
131
  res.json({ message: e });
132
  }
133
  })
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
134
  .listen(7860, () => console.log('App running on port 7860'));
 
16
  return response;
17
  };
18
 
19
+ //YTDL-CORE
20
+ function formatViews(viewCount) {
21
+ if (viewCount >= 1000000000) {
22
+ return (viewCount / 1000000000).toFixed(1) + 'B';
23
+ } else if (viewCount >= 1000000) {
24
+ return (viewCount / 1000000).toFixed(1) + 'M';
25
+ } else {
26
+ return viewCount >= 1000
27
+ ? (viewCount / 1000).toFixed(1) + 'K'
28
+ : viewCount.toString();
29
+ }
30
+ }
31
+
32
+ function formatDuration(durationInSeconds) {
33
+ const hours = Math.floor(durationInSeconds / 3600);
34
+ const minutes = Math.floor((durationInSeconds % 3600) / 60);
35
+ const seconds = durationInSeconds % 60;
36
+
37
+ return hours > 0
38
+ ? hours +
39
+ ':' +
40
+ minutes.toString().padStart(2, '0') +
41
+ ':' +
42
+ seconds.toString().padStart(2, '0')
43
+ : minutes + ':' + seconds.toString().padStart(2, '0');
44
+ }
45
+
46
+ function formatSize(bytes, si = false, dp = 2) {
47
+ const thresh = si ? 1000 : 1024;
48
+
49
+ if (Math.abs(bytes) < thresh) {
50
+ return `${bytes} B`;
51
+ }
52
+
53
+ const units = si
54
+ ? ["kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"]
55
+ : ["KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"];
56
+ let u = -1;
57
+ const r = 10 ** dp;
58
+
59
+ do {
60
+ bytes /= thresh;
61
+ ++u;
62
+ } while (
63
+ Math.round(Math.abs(bytes) * r) / r >= thresh &&
64
+ u < units.length - 1
65
+ );
66
+
67
+ return `${bytes.toFixed(dp)} ${units[u]}`;
68
+ }
69
+
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 {
78
+ title: data.videoDetails.title,
79
+ description: data.videoDetails.description,
80
+ channel: data.videoDetails.ownerChannelName,
81
+ views: formatViews(data.videoDetails.viewCount),
82
+ publish: data.videoDetails.publishDate,
83
+ duration: formatDuration(data.videoDetails.lengthSeconds),
84
+ size: format.contentLength ? formatSize(format.contentLength) : 0,
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
+ }
92
+ } catch (error) {
93
+ console.error('Error occurred:', error);
94
+ return null;
95
+ }
96
+ }
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 {
105
+ title: data.videoDetails.title,
106
+ description: data.videoDetails.description,
107
+ channel: data.videoDetails.ownerChannelName,
108
+ views: formatViews(data.videoDetails.viewCount),
109
+ publish: data.videoDetails.publishDate,
110
+ duration: formatDuration(data.videoDetails.lengthSeconds),
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');
118
+ }
119
+ } catch (error) {
120
+ console.error('Error occurred:', error);
121
+ return null;
122
+ }
123
+ }
124
+
125
  // Fungsi untuk mengkonversi video dari YouTube
126
  const convert = async (url, v_id, ftype, fquality, fname, token, timeExpire) => {
127
  let params = {
 
237
  res.json({ message: e });
238
  }
239
  })
240
+ .get('/dl', async (req, res) => {
241
+ try {
242
+ let { url } = req.query
243
+ if (!ytIdRegex.test(url)) return res.json({ message: 'Invalid URL' });
244
+ let video = await ytmp4(url)
245
+ let audio = await ytmp3(url)
246
+ return res.json({
247
+ video: video,
248
+ audio: audio
249
+ })
250
+ } catch (e) {
251
+ console.log(e);
252
+ res.json({ message: e });
253
+ }
254
+ })
255
  .listen(7860, () => console.log('App running on port 7860'));