Update index.js
Browse files
index.js
CHANGED
|
@@ -18,28 +18,57 @@ let fetch;
|
|
| 18 |
fetch = (await import('node-fetch')).default;
|
| 19 |
})();
|
| 20 |
const ffmpeg = require('fluent-ffmpeg');
|
|
|
|
| 21 |
|
| 22 |
-
function
|
| 23 |
return new Promise((resolve, reject) => {
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
});
|
| 41 |
}
|
| 42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
// Regex untuk mengidentifikasi URL YouTube dan Mega
|
| 44 |
const ytIdRegex = /(?:http(?:s|):\/\/|)(?:(?:www\.|)?youtube(?:\-nocookie|)\.com\/(?:shorts\/)?(?:watch\?.*(?:|\&)v=|embed\/|v\/)?|youtu\.be\/)([-_0-9A-Za-z]{11})/;
|
| 45 |
|
|
@@ -301,7 +330,8 @@ async function ytmp3(url, bitrate = 'lowestaudio') {
|
|
| 301 |
data = await ytdl.getInfo('https://www.youtube.com/watch?v=' + ID)
|
| 302 |
let audioStream = await ytdl(ID, { filter: 'audioonly', quality: 'lowestaudio', });
|
| 303 |
let buffer = await streamToBuffer(audioStream)
|
| 304 |
-
let
|
|
|
|
| 305 |
let upload = await uploadBuffer(konvert)
|
| 306 |
let format = ytdl.chooseFormat(data.formats, { filter: 'audioonly', quality: bitrate });
|
| 307 |
|
|
|
|
| 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 |
+
ffmpegProcess.on('error', (err) => {
|
| 32 |
+
reject(err);
|
| 33 |
+
});
|
| 34 |
+
|
| 35 |
+
ffmpegProcess.on('close', () => {
|
| 36 |
+
resolve(Buffer.concat(chunks));
|
| 37 |
+
});
|
| 38 |
+
|
| 39 |
+
ffmpegProcess.stdin.write(webmBuffer);
|
| 40 |
+
ffmpegProcess.stdin.end();
|
| 41 |
});
|
| 42 |
}
|
| 43 |
|
| 44 |
+
// Menggunakan fungsi konversi sebelum mengonversi ke MP3
|
| 45 |
+
async function convertVideoToMp3(videoBuffer) {
|
| 46 |
+
try {
|
| 47 |
+
const mp4Buffer = await convertWebmToMp4(videoBuffer);
|
| 48 |
+
const mp3Buffer = await new Promise((resolve, reject) => {
|
| 49 |
+
let mp3Buffer = Buffer.alloc(0);
|
| 50 |
+
|
| 51 |
+
ffmpeg()
|
| 52 |
+
.input(mp4Buffer)
|
| 53 |
+
.inputFormat('mp4')
|
| 54 |
+
.outputFormat('mp3')
|
| 55 |
+
.on('error', (err) => {
|
| 56 |
+
reject(err);
|
| 57 |
+
})
|
| 58 |
+
.on('data', (chunk) => {
|
| 59 |
+
mp3Buffer = Buffer.concat([mp3Buffer, chunk]);
|
| 60 |
+
})
|
| 61 |
+
.on('end', () => {
|
| 62 |
+
resolve(mp3Buffer);
|
| 63 |
+
})
|
| 64 |
+
.run();
|
| 65 |
+
});
|
| 66 |
+
return mp3Buffer;
|
| 67 |
+
} catch (err) {
|
| 68 |
+
throw err;
|
| 69 |
+
}
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
// Regex untuk mengidentifikasi URL YouTube dan Mega
|
| 73 |
const ytIdRegex = /(?:http(?:s|):\/\/|)(?:(?:www\.|)?youtube(?:\-nocookie|)\.com\/(?:shorts\/)?(?:watch\?.*(?:|\&)v=|embed\/|v\/)?|youtu\.be\/)([-_0-9A-Za-z]{11})/;
|
| 74 |
|
|
|
|
| 330 |
data = await ytdl.getInfo('https://www.youtube.com/watch?v=' + ID)
|
| 331 |
let audioStream = await ytdl(ID, { filter: 'audioonly', quality: 'lowestaudio', });
|
| 332 |
let buffer = await streamToBuffer(audioStream)
|
| 333 |
+
let buffmp4 = await convertWebmToMp4(buffer)
|
| 334 |
+
let konvert = await convertVideoToMp3(buffmp4)
|
| 335 |
let upload = await uploadBuffer(konvert)
|
| 336 |
let format = ytdl.chooseFormat(data.formats, { filter: 'audioonly', quality: bitrate });
|
| 337 |
|