Spaces:
Paused
Paused
Update index.js
Browse files
index.js
CHANGED
|
@@ -339,42 +339,36 @@ async function cobalt(config) {
|
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
// Fungsi untuk mendapatkan URL audio MP3 dari video YouTube
|
| 346 |
async function getAudioMP3Url(videoUrl) {
|
| 347 |
try {
|
| 348 |
-
/*------------------
|
| 349 |
-
const requestData = {"url":videoUrl,"vQuality":"360","filenamePattern":"pretty","isAudioOnly":"true"}
|
| 350 |
-
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/' } };
|
| 351 |
-
*/
|
| 352 |
-
let obj = { url: videoUrl }
|
| 353 |
-
const video = await fetchCobaltOnly(obj.url, { isAudioOnly: 'audio', vQuality: 'max' })
|
| 354 |
-
|
| 355 |
-
const path_audio = path.join(tempDir, generateRandomName(10) + '.mp3');
|
| 356 |
const id_video = await GetId(videoUrl);
|
| 357 |
const infoVids = await getVideoDetailsWithApi(id_video);
|
| 358 |
-
let uploadResult;
|
| 359 |
-
let convert;
|
| 360 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 361 |
await new Promise((resolve, reject) => {
|
| 362 |
-
ffmpeg()
|
| 363 |
-
.
|
| 364 |
-
.inputOptions('-err_detect ignore_err')
|
| 365 |
-
.outputOptions('-strict experimental')
|
| 366 |
-
.input(video.url)
|
| 367 |
-
.outputOptions('-f mp3')
|
| 368 |
-
.outputOptions('-acodec libmp3lame')
|
| 369 |
-
.outputOptions('-ab 128k')
|
| 370 |
-
.outputOptions('-ar 44100')
|
| 371 |
.on('end', async () => {
|
| 372 |
try {
|
|
|
|
| 373 |
const buffer = fs.readFileSync(path_audio);
|
| 374 |
-
|
| 375 |
-
const buffer2 = fs.readFileSync(convert.path);
|
| 376 |
|
| 377 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 378 |
|
| 379 |
resolve();
|
| 380 |
} catch (error) {
|
|
@@ -385,16 +379,17 @@ async function getAudioMP3Url(videoUrl) {
|
|
| 385 |
console.error('FFmpeg conversion error:', err);
|
| 386 |
reject(err);
|
| 387 |
})
|
| 388 |
-
.save(
|
| 389 |
});
|
| 390 |
|
|
|
|
| 391 |
return {
|
| 392 |
status: 200,
|
| 393 |
title: infoVids.title,
|
| 394 |
result: {
|
| 395 |
-
url_path: `https://${process.env.SPACE_HOST}/temp/${path.basename(
|
| 396 |
-
curl_path: `https://${process.env.SPACE_HOST}/temp/${path.basename(
|
| 397 |
-
path:
|
| 398 |
},
|
| 399 |
infoVids
|
| 400 |
};
|
|
@@ -404,7 +399,9 @@ async function getAudioMP3Url(videoUrl) {
|
|
| 404 |
}
|
| 405 |
}
|
| 406 |
|
| 407 |
-
|
|
|
|
|
|
|
| 408 |
app.get('/ytmp3', async (req, res) => {
|
| 409 |
try {
|
| 410 |
const { url } = req.query;
|
|
|
|
| 339 |
|
| 340 |
|
| 341 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 342 |
async function getAudioMP3Url(videoUrl) {
|
| 343 |
try {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 344 |
const id_video = await GetId(videoUrl);
|
| 345 |
const infoVids = await getVideoDetailsWithApi(id_video);
|
|
|
|
|
|
|
| 346 |
|
| 347 |
+
// Unduh file audio terlebih dahulu
|
| 348 |
+
let obj = { url: videoUrl }
|
| 349 |
+
const video = await fetchCobaltOnly(obj.url, { isAudioOnly: 'audio', vQuality: 'max', filenamePattern:"pretty"})
|
| 350 |
+
const path_audio = path.join(tempDir, generateRandomName(10) + '.mp3');
|
| 351 |
+
const path_audio_edit = path_audio.replace('.mp3', '_edit.mp3');
|
| 352 |
+
|
| 353 |
+
// Download file audio
|
| 354 |
+
const response = await axios.get(video.url, { responseType: 'arraybuffer' });
|
| 355 |
+
fs.writeFileSync(path_audio, response.data);
|
| 356 |
+
|
| 357 |
+
// Tambahkan metadata ke file yang diunduh
|
| 358 |
await new Promise((resolve, reject) => {
|
| 359 |
+
ffmpeg(path_audio)
|
| 360 |
+
.outputOptions('-f mp3', '-acodec libmp3lame', '-ab 128k', '-ar 44100')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 361 |
.on('end', async () => {
|
| 362 |
try {
|
| 363 |
+
// Tambahkan metadata
|
| 364 |
const buffer = fs.readFileSync(path_audio);
|
| 365 |
+
const edited = await addAudioTags(buffer, infoVids.title, infoVids.channelTitle, 2024, infoVids.thumbnail);
|
|
|
|
| 366 |
|
| 367 |
+
// Ganti file lama dengan file yang sudah diedit
|
| 368 |
+
const buffer2 = fs.readFileSync(edited.path);
|
| 369 |
+
fs.writeFileSync(path_audio_edit, buffer2);
|
| 370 |
+
fs.unlinkSync(path_audio); // Hapus file lama
|
| 371 |
+
fs.renameSync(path_audio_edit, path_audio); // Rename menjadi file asli
|
| 372 |
|
| 373 |
resolve();
|
| 374 |
} catch (error) {
|
|
|
|
| 379 |
console.error('FFmpeg conversion error:', err);
|
| 380 |
reject(err);
|
| 381 |
})
|
| 382 |
+
.save(path_audio_edit); // Simpan sementara dengan nama _edit.mp3
|
| 383 |
});
|
| 384 |
|
| 385 |
+
// Kembalikan hasil akhir
|
| 386 |
return {
|
| 387 |
status: 200,
|
| 388 |
title: infoVids.title,
|
| 389 |
result: {
|
| 390 |
+
url_path: `https://${process.env.SPACE_HOST}/temp/${path.basename(path_audio)}`,
|
| 391 |
+
curl_path: `https://${process.env.SPACE_HOST}/temp/${path.basename(path_audio)}?download=1&filename=${infoVids.title}`,
|
| 392 |
+
path: path_audio
|
| 393 |
},
|
| 394 |
infoVids
|
| 395 |
};
|
|
|
|
| 399 |
}
|
| 400 |
}
|
| 401 |
|
| 402 |
+
|
| 403 |
+
|
| 404 |
+
|
| 405 |
app.get('/ytmp3', async (req, res) => {
|
| 406 |
try {
|
| 407 |
const { url } = req.query;
|