/** * ======================================================== * Name: YouTube Direct Media Downloader & Stream Scraper * * Cara Download: * curl -sL "https://code.vamxapi.my.id/snippet/raw/downbyt" -o downbyt.js * * Cara Eksekusi: * node downbyt.js "https://www.youtube.com/watch?v=dQw4w9WgXcQ" * * Author: Vamz Spectre * Source: https://vamxapi.my.id * ======================================================== */ const { execFile } = require("child_process"); const util = require("util"); const execFileAsync = util.promisify(execFile); /** * Format bytes ke ukuran yang mudah dibaca (KB, MB, GB) */ function formatSize(bytes) { if (!bytes || bytes <= 0) return null; const k = 1024; const sizes = ["Bytes", "KB", "MB", "GB"]; const i = Math.floor(Math.log(bytes) / Math.log(k)); return `${(bytes / Math.pow(k, i)).toFixed(2)} ${sizes[i]}`; } /** * Normalisasi dan ekstraksi ID / URL YouTube */ function extractYouTubeId(urlOrId) { if (!urlOrId) return null; const str = urlOrId.trim(); if (/^[a-zA-Z0-9_-]{11}$/.test(str)) return str; const match = str.match( /(?:youtu\.be\/|youtube\.com\/(?:embed\/|v\/|watch\?v=|watch\?.+&v=|shorts\/))([a-zA-Z0-9_-]{11})/i ); return match ? match[1] : null; } /** * Scraper & Extractor YouTube Langsung dari youtube.com * @param {string} inputUrl - URL Video, Shorts, atau Video ID YouTube * @returns {Promise} */ async function downbyt(inputUrl) { if (!inputUrl) throw new Error("URL atau ID YouTube wajib disertakan!"); const videoId = extractYouTubeId(inputUrl); const targetUrl = videoId ? `https://www.youtube.com/watch?v=${videoId}` : inputUrl.trim(); try { const { stdout } = await execFileAsync( "yt-dlp", [ "--dump-json", "--no-warnings", "--no-check-certificates", "--no-playlist", targetUrl ], { maxBuffer: 50 * 1024 * 1024 } ); const data = JSON.parse(stdout); const formats = data.formats || []; // Filter & Organisasi Kualitas Video (144p - 4K) const videoMap = new Map(); for (const f of formats) { if (f.height && f.url && (f.ext === "mp4" || f.ext === "webm")) { const key = `${f.height}p`; const current = videoMap.get(key); // Prioritaskan mp4 atau bitrate tertinggi if (!current || (f.ext === "mp4" && current.ext !== "mp4") || (f.tbr > (current.tbr || 0))) { const rawSize = f.filesize || f.filesize_approx || null; videoMap.set(key, { quality: key, resolution: f.resolution || `${f.width}x${f.height}`, extension: f.ext, fps: f.fps || null, size: rawSize, size_formatted: formatSize(rawSize), vcodec: f.vcodec, tbr: f.tbr || null, download_url: f.url }); } } } // Urutkan kualitas video dari terendah ke tertinggi const videos = Array.from(videoMap.values()) .sort((a, b) => parseInt(a.quality) - parseInt(b.quality)) .map(({ tbr, ...item }) => item); // Filter Audio (m4a, webm, opus) const audioList = []; for (const f of formats) { if (f.vcodec === "none" && f.acodec !== "none" && f.url) { const rawSize = f.filesize || f.filesize_approx || null; audioList.push({ format_id: f.format_id, extension: f.ext, bitrate: f.abr ? `${Math.round(f.abr)} kbps` : null, size: rawSize, size_formatted: formatSize(rawSize), acodec: f.acodec, download_url: f.url }); } } // Best direct streams const bestVideo = videos[videos.length - 1] || null; const bestAudio = audioList[audioList.length - 1] || null; return { success: true, id: data.id, title: data.title, uploader: data.uploader || data.channel, channel_url: data.channel_url || data.uploader_url || null, duration: data.duration || null, duration_formatted: data.duration_string || null, view_count: data.view_count || null, like_count: data.like_count || null, thumbnail: data.thumbnail, best: { video: bestVideo, audio: bestAudio }, videos, audios: audioList }; } catch (err) { throw new Error(`Gagal memproses YouTube: ${err.message}`); } } /** * CLI Runner: * Langsung eksekusi jika dijalankan via terminal: node downbyt.js "" */ const argUrl = process.argv[2]; if (argUrl) { downbyt(argUrl) .then(result => { console.log(JSON.stringify(result, null, 2)); }) .catch(err => { console.error(JSON.stringify({ success: false, error: err.message }, null, 2)); process.exit(1); }); } else if (process.argv[1] && process.argv.length <= 2) { console.log("Usage: node downbyt.js \"\""); } if (typeof module !== "undefined" && module.exports) { module.exports = { extractYouTubeId, downbyt, default: downbyt }; }