ytdl Downloader mp3/mp4
Satu file, tanpa dependency (pakai `fetch` bawaan Node.js 18+). Mengikuti alur resmi convertytmp3.org: scrape homepage → ambel `apiKey` + path JS → scrape JS → endpoint `auth/init` → `convert` → poll `progress` → `downloadURL`.
JSJavaScriptPublicScraperTanpa lisensi1 file · 199 baris · 8.1 KBDiperbarui 22 Okt 202623 views
https://codeku.itsuki-nakano.my.id/repo/alwayslenzz/ytdl-downloader-mp3-mp4curl -s https://codeku.itsuki-nakano.my.id/raw/alwayslenzz/ytdl-downloader-mp3-mp4FILES
ytdl.js · javascript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/usr/bin/env node
/**
* ytmp3.js — downloader MP3/MP4 dari convertytmp3.org (1 file, tanpa dependency)
*
* by Nakano code
* WhatsApp Channel: https://whatsapp.com/channel/0029VbDytkmBqbrF0rFKr12w
*
* Pakai : node ytmp3.js <link-youtube> [mp3|mp4] [-o file]
* Contoh : node ytmp3.js https://youtu.be/dQw4w9WgXcQ mp3 -o lagu.mp3
* node ytmp3.js "https://youtube.com/watch?v=xxx" mp4 -o video.mp4
* Output : JSON { title, id, format, download, file? }
*
* Flow mengikuti persis JS resmi situsnya:
* scrape homepage -> apiKey + path JS -> scrape JS -> endpoint (atob)
* auth -> init (Bearer) -> convert (ikut redirect) -> poll progress -> download URL
* Fallback: endpoint workers.dev internal kalau auth/init gagal jaringan.
*/
const SITE = 'https://convertytmp3.org';
const UA =
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36';
const SLEEP = (ms) => new Promise((r) => setTimeout(r, ms));
// origin convertytmp3.org ikut dikirim di tiap request (dipakai situsnya sendiri)
function hdr(extra = {}) {
return {
'User-Agent': UA,
Accept: 'application/json, text/plain, */*',
'Accept-Language': 'id-ID,id;q=0.9,en;q=0.8',
Origin: SITE,
Referer: SITE + '/',
...extra,
};
}
async function get(url, { headers = hdr(), json = true, timeout = 60000 } = {}) {
const res = await fetch(url, { headers, redirect: 'follow', signal: AbortSignal.timeout(timeout) });
const body = json ? await res.json().catch(() => null) : await res.text();
return { status: res.status, body, headers: res.headers };
}
// --- 1. scrape homepage: apiKey + path bundle JS ---
async function scrapeHome() {
const { status, body } = await get(SITE, { json: false });
if (status !== 200 || typeof body !== 'string') throw new Error(`homepage HTTP ${status}`);
const key = body.match(/var\s+apiKey\s*=\s*'([^']+)'/)?.[1];
const jsPath = body.match(/<script[^>]+src="(\/js\/[^"]+\.js)"/)?.[1];
if (!key) throw new Error('apiKey tidak ditemukan di homepage (struktur situs berubah?)');
if (!jsPath) throw new Error('path JS bundle tidak ditemukan di homepage');
return { apiKey: key, jsPath };
}
// --- 2. scrape bundle JS: endpoint utama (atob) + fallback worker ---
async function scrapeEndpoints(jsPath) {
const { status, body } = await get(SITE + jsPath, { json: false });
if (status !== 200 || typeof body !== 'string') throw new Error(`JS bundle HTTP ${status}`);
const atobs = [...body.matchAll(/atob\("([A-Za-z0-9+/=]+)"\)/g)].map((m) => m[1]);
const decoded = atobs.map((s) => {
try {
return Buffer.from(s, 'base64').toString('utf8');
} catch {
return '';
}
});
const endpoint = decoded.find((s) => /^[a-z0-9.-]+\.[a-z]{2,}$/i.test(s));
const fallback = decoded.find((s) => s.startsWith('https://') && s.includes('workers.dev'));
if (!endpoint) throw new Error('endpoint tidak ditemukan di JS bundle');
return { endpoint, fallback: fallback || null };
}
// --- 3. auth -> init ---
async function authInit(apiKey, endpoint) {
const ts = Date.now();
const auth = await get(`https://epsilon.${endpoint}/api/v1/auth?api_key=${apiKey}&_=${ts}`);
if (auth.status === 403) throw new Error('auth HTTP 403 — IP/host kamu diblokir server situsnya');
const a = auth.body;
if (!a || a.error > 0 || !a.key) throw new Error(`auth gagal: ${JSON.stringify(a)}`);
const init = await get(`https://epsilon.${endpoint}/api/v1/init?_=${Date.now()}`, {
headers: hdr({ Authorization: `Bearer ${a.key}` }),
});
const i = init.body;
if (!i || i.error > 0 || !i.convertURL) throw new Error(`init gagal: ${JSON.stringify(i)}`);
return i.convertURL;
}
// --- 4. convert (ikut redirect) ---
async function convert(convertURL, id, format) {
let url = `${convertURL}&v=${id}&f=${format}&_=${Date.now()}`;
for (let n = 0; n < 5; n++) {
const { status, body } = await get(url);
if (status === 429) throw new Error('HTTP 429 — rate limited, coba lagi nanti');
if (!body || body.error > 0) throw new Error(`convert gagal: ${JSON.stringify(body)}`);
if (body.redirect && body.redirectURL) {
url = `${body.redirectURL}&v=${id}&f=${format}&_=${Date.now()}`;
continue;
}
return body;
}
throw new Error('convert terlalu banyak redirect');
}
// --- 5. poll progress sampai selesai ---
async function pollProgress(progressURL, downloadURL, id, format) {
for (let n = 0; n < 100; n++) {
const { status, body } = await get(`${progressURL}&_=${Date.now()}`);
if (status === 429) throw new Error('HTTP 429 — rate limited, coba lagi nanti');
if (!body || body.error > 0) throw new Error(`progress gagal: ${JSON.stringify(body)}`);
if (body.progress >= 3 || !body.progress) return body.title || '';
await SLEEP(3000);
}
throw new Error('progress timeout (5 menit)');
}
// --- 6. fallback internal (workers.dev) kalau auth/init mati ---
async function viaFallback(fallback, apiKey, id, format) {
const url = `${fallback}&k=${apiKey}&v=${id}&f=${format}&_=${Date.now()}`;
const { status, body } = await get(url);
if (status !== 200 || !body) throw new Error(`fallback HTTP ${status}: ${JSON.stringify(body)}`);
if (body.error > 0) throw new Error(`fallback error ${body.error}`);
return body; // { downloadURL, progressURL?, title? }
}
function videoId(url) {
const m = /(?:youtu\.be\/|youtube\.com\/(?:embed\/|live\/|shorts\/)|[?&]v=)([A-Za-z0-9_-]{11})/.exec(url);
if (m) return m[1];
if (/^[A-Za-z0-9_-]{11}$/.test(url)) return url;
throw new Error('link YouTube tidak dikenali');
}
async function ytmp3(link, format = 'mp3', opts = {}) {
if (!/^(mp3|mp4)$/.test(format)) throw new Error('format harus mp3 atau mp4');
const id = videoId(link);
const home = await scrapeHome();
const { endpoint, fallback } = await scrapeEndpoints(home.jsPath);
let result;
try {
const convertURL = await authInit(home.apiKey, endpoint);
result = await convert(convertURL, id, format);
if (result.progressURL && !opts.fast) {
const title = await pollProgress(result.progressURL, result.downloadURL, id, format);
result.title = result.title || title;
}
} catch (e) {
if (!fallback) throw e;
// jalur resmi gagal -> pakai endpoint internal situsnya
result = await viaFallback(fallback, home.apiKey, id, format);
if (result.progressURL && !opts.fast) {
const title = await pollProgress(result.progressURL, result.downloadURL, id, format);
result.title = result.title || title;
}
}
if (!result.downloadURL) throw new Error('downloadURL tidak ada di respons');
// persis klik tombol Download di situsnya: tambah &v &f &r
const download = `${result.downloadURL}&v=${id}&f=${format}&r=new.convertytmp3.org`;
return { title: result.title || '', id, format, download };
}
// --- CLI ---
async function downloadFile(url, file) {
const res = await fetch(url, { headers: hdr(), redirect: 'follow', signal: AbortSignal.timeout(600000) });
if (!res.ok) throw new Error(`unduh gagal: HTTP ${res.status}`);
const { createWriteStream } = require('node:fs');
const { pipeline } = require('node:stream/promises');
await pipeline(res.body, createWriteStream(file));
return file;
}
async function main() {
const args = process.argv.slice(2);
const link = args.find((a) => !a.startsWith('-') && /^(mp3|mp4)$/i.test(a) === false && !args[args.indexOf(a) - 1]?.startsWith('-o'));
const format = (args.find((a) => /^(mp3|mp4)$/i.test(a)) || 'mp3').toLowerCase();
const oIdx = args.findIndex((a) => a === '-o' || a === '--out');
const outFile = oIdx > -1 ? args[oIdx + 1] : null;
if (!link) {
console.error('Pakai: node ytmp3.js <link-youtube> [mp3|mp4] [-o file]');
process.exit(1);
}
console.error('[*] by alfidev — https://www.whatsapp.com/channel/0029VbCOLKRKrWQtIO1vzN0E');
const info = await ytmp3(link, format);
if (outFile) {
console.error(`[*] mengunduh ke ${outFile} ...`);
info.file = await downloadFile(info.download, outFile);
console.error(`[+] selesai: ${info.file}`);
}
console.log(JSON.stringify(info, null, 2));
}
if (require.main === module) {
main().catch((e) => {
console.error('[!] ' + e.message);
process.exit(1);
});
}
module.exports = { ytmp3, videoId };PLAYGROUND
Siap
Klik Jalankan untuk melihat output.
Kode berjalan di kotak terisolasi di browser kamu. Tidak punya akses ke cookie, akun, atau server NAKANO CODE, dan tidak bisa membuka koneksi internet. Batas waktu 4 detik. Kode bot yang memanggil library WhatsApp (seperti Baileys) tidak bisa jalan di sini.