Update index.js
Browse files
index.js
CHANGED
|
@@ -17,78 +17,7 @@ let fetch;
|
|
| 17 |
(async () => {
|
| 18 |
fetch = (await import('node-fetch')).default;
|
| 19 |
})();
|
| 20 |
-
const ffmpeg = require('fluent-ffmpeg');
|
| 21 |
-
const { spawn } = require('child_process');
|
| 22 |
|
| 23 |
-
function convertWebmToMp4(webmBuffer) {
|
| 24 |
-
return new Promise((resolve, reject) => {
|
| 25 |
-
const ffmpegProcess = spawn('ffmpeg', [
|
| 26 |
-
'-i', '-', // Input dari stdin
|
| 27 |
-
'-f', 'mp4', // Output format
|
| 28 |
-
'pipe:1' // Output ke stdout
|
| 29 |
-
]);
|
| 30 |
-
|
| 31 |
-
let chunks = []; // Define chunks array to accumulate data
|
| 32 |
-
|
| 33 |
-
ffmpegProcess.on('error', (err) => {
|
| 34 |
-
reject(err);
|
| 35 |
-
});
|
| 36 |
-
|
| 37 |
-
ffmpegProcess.stdout.on('data', (chunk) => { // Listen for data events from stdout
|
| 38 |
-
chunks.push(chunk); // Accumulate data chunks
|
| 39 |
-
});
|
| 40 |
-
|
| 41 |
-
ffmpegProcess.stdin.on('error', (err) => { // Handle errors on stdin
|
| 42 |
-
reject(err);
|
| 43 |
-
});
|
| 44 |
-
|
| 45 |
-
ffmpegProcess.on('close', (code) => {
|
| 46 |
-
if (code !== 0) {
|
| 47 |
-
reject(new Error(`ffmpeg process exited with code ${code}`));
|
| 48 |
-
} else {
|
| 49 |
-
resolve(Buffer.concat(chunks)); // Concatenate all chunks into a single buffer
|
| 50 |
-
}
|
| 51 |
-
});
|
| 52 |
-
|
| 53 |
-
ffmpegProcess.stdin.write(webmBuffer, (err) => { // Write data to stdin
|
| 54 |
-
if (err) {
|
| 55 |
-
reject(err); // If there's an error during writing, reject the promise
|
| 56 |
-
} else {
|
| 57 |
-
ffmpegProcess.stdin.end(); // End the stdin stream after writing
|
| 58 |
-
}
|
| 59 |
-
});
|
| 60 |
-
});
|
| 61 |
-
}
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
// Menggunakan fungsi konversi sebelum mengonversi ke MP3
|
| 66 |
-
async function convertVideoToMp3(videoBuffer) {
|
| 67 |
-
try {
|
| 68 |
-
const mp4Buffer = await convertWebmToMp4(videoBuffer);
|
| 69 |
-
const mp3Buffer = await new Promise((resolve, reject) => {
|
| 70 |
-
let mp3Buffer = Buffer.alloc(0);
|
| 71 |
-
|
| 72 |
-
ffmpeg()
|
| 73 |
-
.input(mp4Buffer)
|
| 74 |
-
.inputFormat('mp4')
|
| 75 |
-
.outputFormat('mp3')
|
| 76 |
-
.on('error', (err) => {
|
| 77 |
-
reject(err);
|
| 78 |
-
})
|
| 79 |
-
.on('data', (chunk) => {
|
| 80 |
-
mp3Buffer = Buffer.concat([mp3Buffer, chunk]);
|
| 81 |
-
})
|
| 82 |
-
.on('end', () => {
|
| 83 |
-
resolve(mp3Buffer);
|
| 84 |
-
})
|
| 85 |
-
.run();
|
| 86 |
-
});
|
| 87 |
-
return mp3Buffer;
|
| 88 |
-
} catch (err) {
|
| 89 |
-
throw err;
|
| 90 |
-
}
|
| 91 |
-
}
|
| 92 |
|
| 93 |
// Regex untuk mengidentifikasi URL YouTube dan Mega
|
| 94 |
const ytIdRegex = /(?:http(?:s|):\/\/|)(?:(?:www\.|)?youtube(?:\-nocookie|)\.com\/(?:shorts\/)?(?:watch\?.*(?:|\&)v=|embed\/|v\/)?|youtu\.be\/)([-_0-9A-Za-z]{11})/;
|
|
@@ -349,12 +278,9 @@ async function ytmp3(url, bitrate = 'lowestaudio') {
|
|
| 349 |
try {
|
| 350 |
const ID = ytdl.getVideoID(url),
|
| 351 |
data = await ytdl.getInfo('https://www.youtube.com/watch?v=' + ID)
|
| 352 |
-
let audioStream = await ytdl(ID, { filter: 'audioonly', quality: 'lowestaudio', });
|
| 353 |
-
let buffer = await streamToBuffer(audioStream)
|
| 354 |
-
let buffmp4 = await convertWebmToMp4(buffer)
|
| 355 |
-
let konvert = await convertVideoToMp3(buffmp4)
|
| 356 |
-
let upload = await uploadBuffer(konvert)
|
| 357 |
let format = ytdl.chooseFormat(data.formats, { filter: 'audioonly', quality: bitrate });
|
|
|
|
|
|
|
| 358 |
|
| 359 |
if (format) {
|
| 360 |
return {
|
|
|
|
| 17 |
(async () => {
|
| 18 |
fetch = (await import('node-fetch')).default;
|
| 19 |
})();
|
|
|
|
|
|
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
// Regex untuk mengidentifikasi URL YouTube dan Mega
|
| 23 |
const ytIdRegex = /(?:http(?:s|):\/\/|)(?:(?:www\.|)?youtube(?:\-nocookie|)\.com\/(?:shorts\/)?(?:watch\?.*(?:|\&)v=|embed\/|v\/)?|youtu\.be\/)([-_0-9A-Za-z]{11})/;
|
|
|
|
| 278 |
try {
|
| 279 |
const ID = ytdl.getVideoID(url),
|
| 280 |
data = await ytdl.getInfo('https://www.youtube.com/watch?v=' + ID)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 281 |
let format = ytdl.chooseFormat(data.formats, { filter: 'audioonly', quality: bitrate });
|
| 282 |
+
let buffer = await (await fetch(format.url)).arrayBuffer();
|
| 283 |
+
let upload = await uploadBuffer(buffer)
|
| 284 |
|
| 285 |
if (format) {
|
| 286 |
return {
|