Update index.js
Browse files
index.js
CHANGED
|
@@ -3,6 +3,7 @@ const morgan = require('morgan');
|
|
| 3 |
const express = require('express');
|
| 4 |
const ytdl = require('ytdl-core');
|
| 5 |
const { Writable, pipeline } = require('stream');
|
|
|
|
| 6 |
const util = require('util');
|
| 7 |
const axios = require('axios');
|
| 8 |
|
|
@@ -227,7 +228,85 @@ const youtubedl = async (url) => {
|
|
| 227 |
audio
|
| 228 |
};
|
| 229 |
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 230 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 231 |
const app = express()
|
| 232 |
.set('json spaces', 4)
|
| 233 |
.use(morgan('dev'))
|
|
@@ -276,19 +355,27 @@ const app = express()
|
|
| 276 |
res.json({ message: e });
|
| 277 |
}
|
| 278 |
})
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 294 |
.listen(7860, () => console.log('App running on port 7860'));
|
|
|
|
| 3 |
const express = require('express');
|
| 4 |
const ytdl = require('ytdl-core');
|
| 5 |
const { Writable, pipeline } = require('stream');
|
| 6 |
+
const ffmpeg = require("fluent-ffmpeg")
|
| 7 |
const util = require('util');
|
| 8 |
const axios = require('axios');
|
| 9 |
|
|
|
|
| 228 |
audio
|
| 229 |
};
|
| 230 |
};
|
| 231 |
+
async function streamToBuffer(stream) {
|
| 232 |
+
const chunks = [];
|
| 233 |
+
const captureChunks = new Writable({
|
| 234 |
+
write(chunk, encoding, callback) {
|
| 235 |
+
chunks.push(chunk);
|
| 236 |
+
callback();
|
| 237 |
+
}
|
| 238 |
+
});
|
| 239 |
+
|
| 240 |
+
await util.promisify(pipeline)(stream, captureChunks);
|
| 241 |
+
|
| 242 |
+
return Buffer.concat(chunks);
|
| 243 |
+
}
|
| 244 |
+
async function fileDitch(media){
|
| 245 |
+
return new Promise(async (resolve, reject) => {
|
| 246 |
+
let {fileTypeFromBuffer} = await (await import('file-type'))
|
| 247 |
+
let mime = await fileTypeFromBuffer(media)
|
| 248 |
+
let form = new FormData()
|
| 249 |
+
|
| 250 |
+
form.append("files[]", media, `file-${new Date().getTime()}.${mime.ext}`)
|
| 251 |
|
| 252 |
+
axios.post("https://up1.fileditch.com/temp/upload.php", form, {
|
| 253 |
+
headers: {
|
| 254 |
+
"User-Agent": generateRandomUserAgent(),
|
| 255 |
+
"X-Forwarded-For": generateRandomIP(),
|
| 256 |
+
...form.getHeaders()
|
| 257 |
+
}
|
| 258 |
+
}).then(({ data }) => resolve(data?.files[0]?.url)).catch(reject)
|
| 259 |
+
})
|
| 260 |
+
//https://up1.fileditch.com/upload.php
|
| 261 |
+
}
|
| 262 |
+
async function convertMp4ToAudio(inputBuffer) {
|
| 263 |
+
return new Promise((resolve, reject) => {
|
| 264 |
+
const inputStream = new Readable();
|
| 265 |
+
inputStream.push(inputBuffer);
|
| 266 |
+
inputStream.push(null);
|
| 267 |
+
|
| 268 |
+
const outputBuffer = [];
|
| 269 |
+
const outputStream = new Writable({
|
| 270 |
+
write(chunk, encoding, callback) {
|
| 271 |
+
outputBuffer.push(chunk);
|
| 272 |
+
callback();
|
| 273 |
+
}
|
| 274 |
+
});
|
| 275 |
+
|
| 276 |
+
ffmpeg(inputStream)
|
| 277 |
+
.toFormat('mp3')
|
| 278 |
+
.on('end', () => {
|
| 279 |
+
console.log('Conversion finished!');
|
| 280 |
+
resolve(Buffer.concat(outputBuffer));
|
| 281 |
+
})
|
| 282 |
+
.on('error', (err) => {
|
| 283 |
+
console.error('Error during conversion:', err);
|
| 284 |
+
reject(err);
|
| 285 |
+
})
|
| 286 |
+
.pipe(outputStream);
|
| 287 |
+
});
|
| 288 |
+
}
|
| 289 |
+
async function ytAPI(url) {
|
| 290 |
+
try {
|
| 291 |
+
const ID = ytdl.getVideoID(url)
|
| 292 |
+
//let videoStream = await ytdl(ID, { filter: 'audioandvideo', quality: 'highestvideo' });
|
| 293 |
+
let data = await ytdl.getInfo('https://www.youtube.com/watch?v=' + ID)
|
| 294 |
+
let format = ytdl.chooseFormat(data.formats, { filter: 'videoandaudio', quality: 'highestvideo' });
|
| 295 |
+
let audioStream = await ytdl(ID, {filter: "audioandvideo", quality:"lowestvideo"})
|
| 296 |
+
//let buffermp4 = await streamToBuffer(videoStream)
|
| 297 |
+
let buffermp3 = await streamToBuffer(audioStream)
|
| 298 |
+
buffermp3 = await convertMp4ToAudio(buffermp3)
|
| 299 |
+
//buffermp4 = await fileDitch(buffermp4)
|
| 300 |
+
buffermp3 = await fileDitch(buffermp3)
|
| 301 |
+
return {
|
| 302 |
+
mp4_url: format.url,
|
| 303 |
+
mp3_url: buffermp3,
|
| 304 |
+
}
|
| 305 |
+
} catch (err) {
|
| 306 |
+
console.error('Error occurred:', err);
|
| 307 |
+
return null;
|
| 308 |
+
}
|
| 309 |
+
}
|
| 310 |
const app = express()
|
| 311 |
.set('json spaces', 4)
|
| 312 |
.use(morgan('dev'))
|
|
|
|
| 355 |
res.json({ message: e });
|
| 356 |
}
|
| 357 |
})
|
| 358 |
+
.get('/ytdl', async (req, res) => {
|
| 359 |
+
try {
|
| 360 |
+
let { url } = req.query;
|
| 361 |
+
//if (!ytIdRegex.test(url)) return res.json({ message: 'Invalid URL' });
|
| 362 |
+
if (!ytdl.validateURL(url)) return res.json({ message: 'Invalid URL' });
|
| 363 |
+
let data = await ytAPI(url) || {};
|
| 364 |
+
let videoID = ytdl.getVideoID(url);
|
| 365 |
+
let dataInfo = await yts({ videoId: videoID }) || {};
|
| 366 |
+
//let otherAPI = await (await axios.get("https://line.1010diy.com/web/free-mp3-finder/detail?url="+url))?.data?.data ? await (await axios.get("https://line.1010diy.com/web/free-mp3-finder/detail?url="+url))?.data?.data : null
|
| 367 |
+
|
| 368 |
+
let response = {
|
| 369 |
+
...dataInfo,
|
| 370 |
+
mp4: data.mp4_url,
|
| 371 |
+
mp3: data.mp3_url,
|
| 372 |
+
//other: otherAPI || null
|
| 373 |
+
};
|
| 374 |
+
|
| 375 |
+
return res.json(response);
|
| 376 |
+
} catch (e) {
|
| 377 |
+
console.log(e);
|
| 378 |
+
return res.status(500).json({ message: e.message });
|
| 379 |
+
}
|
| 380 |
+
})
|
| 381 |
.listen(7860, () => console.log('App running on port 7860'));
|