Spaces:
Paused
Paused
Update index.js
Browse files
index.js
CHANGED
|
@@ -91,6 +91,35 @@ async function getHDThumbnailUrl(videoId) {
|
|
| 91 |
}
|
| 92 |
}
|
| 93 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
// Fungsi untuk mendapatkan ID video dari URL
|
| 95 |
async function GetId(data) {
|
| 96 |
const regex = /(?:https?:\/\/)?(?:www\.)?(?:youtu(?:be\.com\/(?:watch\?(?:v=|vi=)|v\/|vi\/)|\.be\/|be\.com\/embed\/|be\.com\/shorts\/)|youtube\.com\/\?(?:v=|vi=))([\w-]{11})/;
|
|
@@ -158,12 +187,12 @@ function generateRandomName(length) {
|
|
| 158 |
// Fungsi untuk mendapatkan URL audio MP3 dari video YouTube
|
| 159 |
async function getAudioMP3Url(videoUrl) {
|
| 160 |
try {
|
| 161 |
-
const info = await ytdl.getInfo(videoUrl);
|
| 162 |
-
const audioFormat = ytdl.chooseFormat(info.formats, { filter: 'audioonly', quality: 'highestaudio' });
|
| 163 |
const requestData = {"url":videoUrl,"vQuality":"360","filenamePattern":"pretty","isAudioOnly":"true"}
|
| 164 |
const config = { headers: { 'Accept': 'application/json', 'Content-Type': 'application/json', 'User-Agent': 'Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Mobile Safari/537.36', 'Referer': 'https://cobalt.tools/' } };
|
| 165 |
const { data: video } = await axios.post('https://api.cobalt.tools/api/json', requestData, config);
|
| 166 |
const path_audio = path.join(tempDir, generateRandomName(10) + '.mp3');
|
|
|
|
|
|
|
| 167 |
let uploadResult;
|
| 168 |
let convert;
|
| 169 |
|
|
@@ -177,9 +206,7 @@ async function getAudioMP3Url(videoUrl) {
|
|
| 177 |
.on('end', async () => {
|
| 178 |
try {
|
| 179 |
const buffer = fs.readFileSync(path_audio);
|
| 180 |
-
|
| 181 |
-
const hd_thumbnail = await getHDThumbnailUrl(id_video);
|
| 182 |
-
convert = await addAudioTags(buffer, info.videoDetails.title, info.videoDetails.ownerChannelName, 2024, hd_thumbnail);
|
| 183 |
const buffer2 = fs.readFileSync(convert.path);
|
| 184 |
|
| 185 |
fs.unlinkSync(path_audio);
|
|
@@ -198,12 +225,13 @@ async function getAudioMP3Url(videoUrl) {
|
|
| 198 |
|
| 199 |
return {
|
| 200 |
status: 200,
|
| 201 |
-
title:
|
| 202 |
result: {
|
| 203 |
url_path: `https://${process.env.SPACE_HOST}/temp/${path.basename(convert.path)}`,
|
| 204 |
curl_path: `https://${process.env.SPACE_HOST}/temp/${path.basename(convert.path)}?download=1&filename=${info.videoDetails.title}`,
|
| 205 |
path: convert.path
|
| 206 |
-
}
|
|
|
|
| 207 |
};
|
| 208 |
} catch (error) {
|
| 209 |
console.error('Error:', error);
|
|
|
|
| 91 |
}
|
| 92 |
}
|
| 93 |
|
| 94 |
+
const getVideoDetailsWithApi = async (videoId) => {
|
| 95 |
+
const url = `https://www.googleapis.com/youtube/v3/videos?part=snippet&id=${videoId}&key=AIzaSyBPkpdJEGtAHebbaP3_CcA1_urfMFfeLLg`;
|
| 96 |
+
|
| 97 |
+
try {
|
| 98 |
+
const response = await axios.get(url);
|
| 99 |
+
const video = response.data.items[0];
|
| 100 |
+
|
| 101 |
+
const title = video.snippet.title;
|
| 102 |
+
const description = video.snippet.description;
|
| 103 |
+
const thumbnail = video.snippet.thumbnails.high.url;
|
| 104 |
+
const channelTitle = video.snippet.channelTitle;
|
| 105 |
+
const publishedAt = video.snippet.publishedAt;
|
| 106 |
+
const tags = video.snippet.tags;
|
| 107 |
+
|
| 108 |
+
const videoDetails = {
|
| 109 |
+
title: title,
|
| 110 |
+
description: description,
|
| 111 |
+
thumbnail: thumbnail,
|
| 112 |
+
channelTitle: channelTitle,
|
| 113 |
+
publishedAt: publishedAt,
|
| 114 |
+
tags: tags,
|
| 115 |
+
};
|
| 116 |
+
|
| 117 |
+
return videoDetails
|
| 118 |
+
} catch (error) {
|
| 119 |
+
console.error('An error occurred:', error);
|
| 120 |
+
}
|
| 121 |
+
};
|
| 122 |
+
|
| 123 |
// Fungsi untuk mendapatkan ID video dari URL
|
| 124 |
async function GetId(data) {
|
| 125 |
const regex = /(?:https?:\/\/)?(?:www\.)?(?:youtu(?:be\.com\/(?:watch\?(?:v=|vi=)|v\/|vi\/)|\.be\/|be\.com\/embed\/|be\.com\/shorts\/)|youtube\.com\/\?(?:v=|vi=))([\w-]{11})/;
|
|
|
|
| 187 |
// Fungsi untuk mendapatkan URL audio MP3 dari video YouTube
|
| 188 |
async function getAudioMP3Url(videoUrl) {
|
| 189 |
try {
|
|
|
|
|
|
|
| 190 |
const requestData = {"url":videoUrl,"vQuality":"360","filenamePattern":"pretty","isAudioOnly":"true"}
|
| 191 |
const config = { headers: { 'Accept': 'application/json', 'Content-Type': 'application/json', 'User-Agent': 'Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Mobile Safari/537.36', 'Referer': 'https://cobalt.tools/' } };
|
| 192 |
const { data: video } = await axios.post('https://api.cobalt.tools/api/json', requestData, config);
|
| 193 |
const path_audio = path.join(tempDir, generateRandomName(10) + '.mp3');
|
| 194 |
+
const id_video = await GetId(videoUrl);
|
| 195 |
+
const infoVids = await getVideoDetailsWithApi(id_video);
|
| 196 |
let uploadResult;
|
| 197 |
let convert;
|
| 198 |
|
|
|
|
| 206 |
.on('end', async () => {
|
| 207 |
try {
|
| 208 |
const buffer = fs.readFileSync(path_audio);
|
| 209 |
+
convert = await addAudioTags(buffer, infoVids.title, infoVids.channelTitle, 2024, infoVids.thumbnail);
|
|
|
|
|
|
|
| 210 |
const buffer2 = fs.readFileSync(convert.path);
|
| 211 |
|
| 212 |
fs.unlinkSync(path_audio);
|
|
|
|
| 225 |
|
| 226 |
return {
|
| 227 |
status: 200,
|
| 228 |
+
title: infoVids.title,
|
| 229 |
result: {
|
| 230 |
url_path: `https://${process.env.SPACE_HOST}/temp/${path.basename(convert.path)}`,
|
| 231 |
curl_path: `https://${process.env.SPACE_HOST}/temp/${path.basename(convert.path)}?download=1&filename=${info.videoDetails.title}`,
|
| 232 |
path: convert.path
|
| 233 |
+
},
|
| 234 |
+
infoVids
|
| 235 |
};
|
| 236 |
} catch (error) {
|
| 237 |
console.error('Error:', error);
|