Update index.js
Browse files15 unmodified lines161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127109 unmodified lines23723823924024124224324424524624724824925025125225325425515 unmodified lines return response;};
//YTDL-COREfunction formatViews(viewCount) { if (viewCount >= 1000000000) { return (viewCount / 1000000000).toFixed(1) + 'B'; } else if (viewCount >= 1000000) { return (viewCount / 1000000).toFixed(1) + 'M'; } else { return viewCount >= 1000 ? (viewCount / 1000).toFixed(1) + 'K' : viewCount.toString(); }}
function formatDuration(durationInSeconds) { const hours = Math.floor(durationInSeconds / 3600); const minutes = Math.floor((durationInSeconds % 3600) / 60); const seconds = durationInSeconds % 60;
return hours > 0 ? hours + ':' + minutes.toString().padStart(2, '0') + ':' + seconds.toString().padStart(2, '0') : minutes + ':' + seconds.toString().padStart(2, '0');}
function formatSize(bytes, si = false, dp = 2) { const thresh = si ? 1000 : 1024;
if (Math.abs(bytes) < thresh) { return `${bytes} B`; }
const units = si ? ["kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"] : ["KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"]; let u = -1; const r = 10 ** dp;
do { bytes /= thresh; ++u; } while ( Math.round(Math.abs(bytes) * r) / r >= thresh && u < units.length - 1 );
return `${bytes.toFixed(dp)} ${units[u]}`;}
async function ytmp4(url, quality = 'highestvideo') { try { const ID = ytdl.getVideoID(url), data = await ytdl.getInfo('https://www.youtube.com/watch?v=' + ID), format = ytdl.chooseFormat(data.formats, { quality: quality });
if (format) { return { title: data.videoDetails.title, description: data.videoDetails.description, channel: data.videoDetails.ownerChannelName, views: formatViews(data.videoDetails.viewCount), publish: data.videoDetails.publishDate, duration: formatDuration(data.videoDetails.lengthSeconds), size: format.contentLength ? formatSize(format.contentLength) : 0, quality: format.qualityLabel, thumb: data.videoDetails.thumbnails[0].url, dl_url: format.url, } } else { throw new Error('No suitable format found'); } } catch (error) { console.error('Error occurred:', error); return null; }}async function ytmp3(url, bitrate = 'lowestaudio') { try { const ID = ytdl.getVideoID(url), data = await ytdl.getInfo('https://www.youtube.com/watch?v=' + ID), format = ytdl.chooseFormat(data.formats, { filter: 'audioonly', quality: bitrate });
if (format) { return { title: data.videoDetails.title, description: data.videoDetails.description, channel: data.videoDetails.ownerChannelName, views: formatViews(data.videoDetails.viewCount), publish: data.videoDetails.publishDate, duration: formatDuration(data.videoDetails.lengthSeconds), size: format.contentLength ? formatSize(format.contentLength) : 0, quality: format.audioQuality, thumb: data.videoDetails.thumbnails[0].url, dl_url: format.url, } } else { throw new Error('No suitable format found'); } } catch (error) { console.error('Error occurred:', error); return null; }}
// Fungsi untuk mengkonversi video dari YouTubeconst convert = async (url, v_id, ftype, fquality, fname, token, timeExpire) => { let params = {109 unmodified lines res.json({ message: e }); } }) .get('/dl', async (req, res) => { try { let { url } = req.query if (!ytIdRegex.test(url)) return res.json({ message: 'Invalid URL' }); let video = await ytmp4(url) let audio = await ytmp3(url) return res.json({ video: video, audio: audio }) } catch (e) { console.log(e); res.json({ message: e }); } }) .listen(7860, () => console.log('App running on port 7860'));index.js+121