/** * Facebook Video & Reels Native Downloader * 100% Native Facebook Direct Stream Scraper (Zero-Dependency) * * Target: https://www.facebook.com * Author: Vamz Spectre * License: MIT * * Cara Penggunaan via CLI: * node facebook-native-downloader.js "https://www.facebook.com/watch/?v=10153231379946729" * * Download Script: * curl -sL "https://code.vamxapi.my.id/snippet/raw/facebook-native-downloader" -o facebook-native-downloader.js */ /** * Decode escape sequence URL Facebook (\u0026 -> &, \/ -> /, dll) * @param {string} raw - String URL ter-escape * @returns {string} URL bersih */ function decodeFbUrl(raw) { if (!raw) return ""; try { return JSON.parse(`"${raw}"`); } catch { return raw .replace(/\\u0025/g, "%") .replace(/\\u0026/g, "&") .replace(/\\u002F/gi, "/") .replace(/\\\//g, "/") .replace(/&/g, "&"); } } /** * Bersihkan karakter HTML Entities pada teks atau judul * @param {string} str - String dengan entitas HTML * @returns {string} String bersih */ function decodeHtmlEntities(str) { if (!str) return ""; return str .replace(/&#x([0-9a-fA-F]+);/g, (_, hex) => String.fromCharCode(parseInt(hex, 16))) .replace(/&#(\d+);/g, (_, dec) => String.fromCharCode(parseInt(dec, 10))) .replace(/&/g, "&") .replace(/"/g, '"') .replace(/</g, "<") .replace(/>/g, ">") .replace(/ /g, " "); } /** * Normalisasi URL Facebook (Reels/Watch/Post) * @param {string} url - URL Facebook mentah * @returns {string} URL ter-normalisasi */ function normalizeFacebookUrl(url) { let u = url.trim(); const reelMatch = u.match(/(?:facebook\.com|fb\.watch)\/reel\/(\d+)/i); if (reelMatch && reelMatch[1]) { return `https://www.facebook.com/watch/?v=${reelMatch[1]}`; } return u; } /** * Ekstrak metadata dan direct stream URL video Facebook (HD & SD) * @param {string} fbUrl - URL video atau reels Facebook * @returns {Promise} Metadata dan download link */ async function downloadFacebook(fbUrl) { if (!fbUrl || typeof fbUrl !== "string") { return { status: false, creator: "Vamz Spectre", message: "Parameter URL Facebook wajib diisi." }; } const cleanUrl = normalizeFacebookUrl(fbUrl); if (!/facebook\.com|fb\.watch/i.test(cleanUrl)) { return { status: false, creator: "Vamz Spectre", message: "URL tidak valid. Harus menyertakan domain facebook.com atau fb.watch." }; } const headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36", "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8", "Accept-Language": "id-ID,id;q=0.9,en-US;q=0.8,en;q=0.7", "Sec-Ch-Ua": '"Not(A:Brand";v="99", "Google Chrome";v="133", "Chromium";v="133"', "Sec-Ch-Ua-Mobile": "?0", "Sec-Ch-Ua-Platform": '"Windows"', "Sec-Fetch-Dest": "document", "Sec-Fetch-Mode": "navigate", "Sec-Fetch-Site": "none", "Sec-Fetch-User": "?1", "Upgrade-Insecure-Requests": "1" }; try { const res = await fetch(cleanUrl, { headers, redirect: "follow" }); if (!res.ok) { return { status: false, creator: "Vamz Spectre", message: `Gagal memuat halaman Facebook (HTTP status ${res.status})` }; } const html = await res.text(); // 1. Ekstrak Video Stream HD let hdUrl = ""; const hdMatch = html.match(/"browser_native_hd_url":"([^"]+)"/) || html.match(/"playable_url_quality_hd":"([^"]+)"/) || html.match(/"hd_src":"([^"]+)"/) || html.match(/"hd_src_no_ratelimit":"([^"]+)"/); if (hdMatch && hdMatch[1]) { hdUrl = decodeFbUrl(hdMatch[1]); } // 2. Ekstrak Video Stream SD let sdUrl = ""; const sdMatch = html.match(/"browser_native_sd_url":"([^"]+)"/) || html.match(/"playable_url":"([^"]+)"/) || html.match(/"sd_src":"([^"]+)"/) || html.match(/"sd_src_no_ratelimit":"([^"]+)"/); if (sdMatch && sdMatch[1]) { sdUrl = decodeFbUrl(sdMatch[1]); } // 3. Fallback OpenGraph jika stream native belum ditemukan if (!hdUrl && !sdUrl) { const ogVideo = html.match(/([^<]+)<\/title>/i); if (titleMatch && titleMatch[1]) { title = decodeHtmlEntities(decodeFbUrl(titleMatch[1].replace(/ \| Facebook$/i, "").trim())); } // 5. Ekstrak Thumbnail let thumbnail = ""; const thumbMatch = html.match(/"); process.exit(1); } downloadFacebook(targetUrl) .then((res) => { console.log(JSON.stringify(res, null, 2)); process.exit(res.status ? 0 : 1); }) .catch((err) => { console.error(JSON.stringify({ status: false, creator: "Vamz Spectre", error: err.message }, null, 2)); process.exit(1); }); } module.exports = { downloadFacebook };