social-dl 0.9.1 → 0.9.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -31,10 +31,12 @@ npx social-dl https://youtu.be/BJs3DBrMTSc
31
31
  npx social-dl https://youtu.be/BJs3DBrMTSc --mp3 -o audio.m4a
32
32
  npx social-dl https://facebook.com/share/r/xxx/ --json
33
33
  npx social-dl https://www.tiktok.com/@user/video/123 --no-watermark
34
+ npx social-dl https://www.instagram.com/reel/CODE/ --cookies ~/cookies.txt -o instagram.mp4
35
+ npx social-dl https://www.tiktok.com/@user/video/123 --cookies-from-browser chrome -o tiktok.mp4
34
36
  npx social-dl doctor
35
37
  ```
36
38
 
37
- Use `--quality 720p`, `--json`, `--no-watermark`, and `-o output.mp4` as needed. YouTube may reject datacenter IPs; `doctor` reports this as `BOT_DETECTED`.
39
+ Use `--quality 720p`, `--json`, `--no-watermark`, and `-o output.mp4` as needed. For login-required Instagram or TikTok posts, pass an exported cookie file with `--cookies path/to/cookies.txt` or use `--cookies-from-browser chrome`, `firefox`, `edge`, or `brave`. YouTube may reject datacenter IPs; `doctor` reports this as `BOT_DETECTED`.
38
40
 
39
41
  ## Library
40
42
 
@@ -81,8 +83,8 @@ await youtubeMp3Save('https://youtu.be/BJs3DBrMTSc', './audio.m4a');
81
83
  ```js
82
84
  import { instagramDl, instagramSave } from 'social-dl/instagram';
83
85
 
84
- const instagram = await instagramDl('https://www.instagram.com/reel/CODE/');
85
- await instagramSave('https://www.instagram.com/reel/CODE/', './instagram.mp4');
86
+ const instagram = await instagramDl('https://www.instagram.com/reel/CODE/', { cookiesPath: './cookies.txt' });
87
+ await instagramSave('https://www.instagram.com/reel/CODE/', './instagram.mp4', 'best', { cookiesPath: './cookies.txt' });
86
88
  ```
87
89
 
88
90
  #### TikTok
@@ -90,8 +92,8 @@ await instagramSave('https://www.instagram.com/reel/CODE/', './instagram.mp4');
90
92
  ```js
91
93
  import { tiktokDl, tiktokSave } from 'social-dl/tiktok';
92
94
 
93
- const tiktok = await tiktokDl('https://www.tiktok.com/@user/video/1234567890123456789');
94
- await tiktokSave('https://www.tiktok.com/@user/video/1234567890123456789', './tiktok.mp4', 'NOWM');
95
+ const tiktok = await tiktokDl('https://www.tiktok.com/@user/video/1234567890123456789', { cookiesFromBrowser: 'chrome' });
96
+ await tiktokSave('https://www.tiktok.com/@user/video/1234567890123456789', './tiktok.mp4', 'NOWM', { cookiesFromBrowser: 'chrome' });
95
97
  ```
96
98
 
97
99
  #### Twitter/X
@@ -145,8 +147,8 @@ Readable.fromWeb(stream).pipe(res);
145
147
 
146
148
  ## API reference
147
149
 
148
- - `universalDl(url, { audioOnly, quality })` — return normalized direct-CDN formats.
149
- - `universalSave(url, destination, { audioOnly, quality, noWatermark })` — save a selected format.
150
+ - `universalDl(url, { audioOnly, quality, cookiesPath, cookiesFromBrowser })` — return normalized direct-CDN formats.
151
+ - `universalSave(url, destination, { audioOnly, quality, noWatermark, cookiesPath, cookiesFromBrowser })` — save a selected format.
150
152
  - `universalStream(url, options)` — return a Web `ReadableStream` for piping.
151
153
  - `doctor({ log })` — test provider reachability, Node, and ffmpeg.
152
154
  - Every platform exports `xxxDl()` and `xxxSave()`.
package/bin/cli.js CHANGED
@@ -4,8 +4,11 @@ import { pipeline } from 'node:stream/promises';
4
4
  import { universalDl, universalSave } from '../src/index.js';
5
5
  import { doctor } from '../src/doctor.js';
6
6
  import { randomUserAgent } from '../src/utils/fetcher.js';
7
+ import { createRequire } from 'node:module';
8
+ import { mkdir } from 'node:fs/promises';
7
9
 
8
- const VERSION = '0.9.0';
10
+ const require = createRequire(import.meta.url);
11
+ const VERSION = require('../package.json').version;
9
12
  const HELP = `social-dl ${VERSION}\n\nUsage:\n social-dl <url> [-o file] [--quality 720p]\n social-dl <url> --mp3 -o audio.m4a\n social-dl <url> --json\n social-dl <url> --no-watermark\n social-dl doctor [--json]\n\nOptions: --quality <label> --mp3 --json --no-watermark -o, --output <file> --help`;
10
13
  const color = (code, text) => process.stdout.isTTY ? `\x1b[${code}m${text}\x1b[0m` : text;
11
14
 
@@ -18,6 +21,8 @@ function parse(argv) {
18
21
  else if (arg === '--json') opts.json = true;
19
22
  else if (arg === '--no-watermark') opts.noWatermark = true;
20
23
  else if (arg === '--quality') opts.quality = argv[++i];
24
+ else if (arg === '--cookies') opts.cookiesPath = argv[++i];
25
+ else if (arg === '--cookies-from-browser') opts.cookiesFromBrowser = argv[++i];
21
26
  else if (arg === '-o' || arg === '--output') opts.output = argv[++i];
22
27
  else opts._.push(arg);
23
28
  }
@@ -43,14 +48,17 @@ async function streamSave(info, output, quality, noWatermark) {
43
48
  }
44
49
 
45
50
  async function main() {
46
- const opts = parse(process.argv.slice(2));
51
+ const argv = process.argv.slice(2);
52
+ if (argv.includes('--version') || argv.includes('-v')) return console.log(VERSION);
53
+ await mkdir('cache/cookies', { recursive: true });
54
+ const opts = parse(argv);
47
55
  if (opts.help || opts._[0] === undefined) return console.log(HELP);
48
56
  if (opts._[0] === 'doctor') return opts.json ? console.log(JSON.stringify(await doctor({ log: false }), null, 2)) : await doctor();
49
57
  const url = opts._[0];
50
- const info = await universalDl(url, { audioOnly: opts.audioOnly });
58
+ const info = await universalDl(url, opts);
51
59
  if (opts.json) return console.log(JSON.stringify(info, null, 2));
52
60
  const output = opts.output || `${info.id}.${opts.audioOnly ? 'm4a' : (info.formats[0]?.type || 'mp4')}`;
53
- await streamSave(info, output, opts.quality, opts.noWatermark);
61
+ await universalSave(url, output, opts);
54
62
  console.log(`${color(32, 'Saved')} ${output}`);
55
63
  }
56
64
 
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "social-dl",
3
- "version": "0.9.1",
3
+ "version": "0.9.5",
4
4
  "license": "SEE LICENSE IN LICENSE",
5
5
  "description": "Direct CDN social downloader - YouTube, TikTok, Facebook, Instagram, Twitter, Pinterest, CapCut - No API, No keys, Never dies",
6
6
  "main": "src/index.js",
7
7
  "type": "module",
8
8
  "bin": {
9
- "social-dl": "./bin/cli.js"
9
+ "social-dl": "bin/cli.js"
10
10
  },
11
11
  "exports": {
12
12
  ".": "./src/index.js",
@@ -24,6 +24,9 @@
24
24
  "test": "node tests/test.js",
25
25
  "dev": "node src/index.js"
26
26
  },
27
+ "optionalDependencies": {
28
+ "yt-dlp-exec": "^1.0.2"
29
+ },
27
30
  "keywords": [
28
31
  "youtube downloader",
29
32
  "tiktok downloader",
@@ -54,10 +57,8 @@
54
57
  "node": ">=18.0.0"
55
58
  },
56
59
  "files": [
57
- "src/",
58
60
  "bin/",
59
- "index.d.ts",
60
- "README.md",
61
- "LICENSE"
61
+ "src/",
62
+ "README.md"
62
63
  ]
63
64
  }
package/src/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { UnsupportedError } from './errors.js';
2
2
  import { facebookDl, facebookSave } from './platforms/facebook.js';
3
- import { youtubeDl, youtubeSave } from './platforms/youtube.js';
3
+ import { IOS_MEDIA_HEADERS, YOUTUBE_MEDIA_HEADERS, youtubeDl, youtubeSave } from './platforms/youtube.js';
4
4
  import { youtubeMp3Dl, youtubeMp3Save } from './platforms/ytmp3.js';
5
5
  import { instagramDl, instagramSave } from './platforms/instagram.js';
6
6
  import { tiktokDl, tiktokSave } from './platforms/tiktok.js';
@@ -25,10 +25,10 @@ export function detectPlatform(value) {
25
25
  /** Download metadata using the correct platform extractor. */
26
26
  export async function universalDl(url, opts = {}) {
27
27
  switch (detectPlatform(url)) {
28
- case 'facebook': return facebookDl(url);
29
- case 'youtube': return opts.audioOnly ? youtubeMp3Dl(url) : youtubeDl(url);
30
- case 'instagram': return instagramDl(url);
31
- case 'tiktok': return tiktokDl(url);
28
+ case 'facebook': return facebookDl(url, opts);
29
+ case 'youtube': return opts.audioOnly ? youtubeMp3Dl(url, opts) : youtubeDl(url, opts);
30
+ case 'instagram': return instagramDl(url, opts);
31
+ case 'tiktok': return tiktokDl(url, opts);
32
32
  case 'twitter': return twitterDl(url);
33
33
  case 'pinterest': return pinterestDl(url);
34
34
  case 'capcut': return capcutDl(url);
@@ -39,8 +39,14 @@ export async function universalDl(url, opts = {}) {
39
39
  /** Save a selected format from the correct platform. */
40
40
  export async function universalSave(url, dest = './video.mp4', opts = {}) {
41
41
  const quality = opts.quality || 'best';
42
+ const platform = detectPlatform(url);
43
+ if (platform === 'youtube') return opts.audioOnly ? youtubeMp3Save(url, dest, quality, opts) : youtubeSave(url, dest, quality, opts);
44
+ if (platform === 'facebook') return facebookSave(url, dest, quality, opts);
45
+ if (platform === 'instagram') return instagramSave(url, dest, quality, opts);
46
+ if (platform === 'tiktok') return tiktokSave(url, dest, quality, opts);
42
47
  const result = await universalDl(url, opts);
43
- return saveResult(result, dest, quality, {}, opts.noWatermark === true);
48
+ const headers = detectPlatform(url) === 'youtube' ? { ...YOUTUBE_MEDIA_HEADERS, fallbackHeaders: IOS_MEDIA_HEADERS } : {};
49
+ return saveResult(result, dest, quality, headers, opts.noWatermark === true);
44
50
  }
45
51
 
46
52
  export * from './errors.js';
@@ -1,11 +1,27 @@
1
- import { fetchWithRetry, downloadTo } from '../utils/fetcher.js';
1
+ import { mkdir, rename, stat, unlink } from 'node:fs/promises';
2
+ import { join } from 'node:path';
3
+ import { fetchWithRetry } from '../utils/fetcher.js';
2
4
  import { sortFormats, saveResult } from '../utils/media.js';
3
- import { NotFoundError, PrivateError } from '../errors.js';
5
+ import { cookieArgs, runYtDlp } from '../utils/ytdlp.js';
6
+ import { NotFoundError, PrivateError, UnsupportedError } from '../errors.js';
4
7
  import { CREATOR } from '../constants.js';
5
8
 
6
- const DESKTOP = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/120.0 Safari/537.36';
9
+ export const UA = 'Mozilla/5.0 (Linux; Android 15) AppleWebKit/537.36 Chrome/131 Mobile Safari/537.36';
10
+ export const TMP_DIR = 'cache/temp';
11
+ const FB_HEADERS = { 'User-Agent': UA, Accept: '*/*', 'Accept-Language': 'en-US,en;q=0.9', Referer: 'https://www.facebook.com/', Origin: 'https://www.facebook.com/' };
7
12
  const unescape = value => { try { return JSON.parse(`"${value}"`); } catch { return value.replace(/\\u0025/g, '%').replace(/\\u0026/g, '&').replace(/\\\//g, '/'); } };
8
13
 
14
+ export function isFacebookUrl(value) {
15
+ try {
16
+ const parsed = new URL(value);
17
+ return /facebook\.com\/share\/r\//i.test(parsed.href)
18
+ || /fb\.watch/i.test(parsed.hostname)
19
+ || /facebook\.com\/reel\//i.test(parsed.href)
20
+ || /web\.facebook\.com\/reel\//i.test(parsed.href)
21
+ || /facebook\.com\/watch/i.test(parsed.href);
22
+ } catch { return false; }
23
+ }
24
+
9
25
  function extract(html) {
10
26
  const find = name => { const match = html.match(new RegExp(`"${name}":"([^"]+)"`)); return match && unescape(match[1]); };
11
27
  const hd = find('browser_native_hd_url') || find('playable_url_quality_hd');
@@ -16,27 +32,69 @@ function extract(html) {
16
32
  ]);
17
33
  }
18
34
 
19
- /** Download metadata for a Facebook video or reel. */
20
- export async function facebookDl(url) {
21
- const resolved = await fetchWithRetry(url, { headers: { 'User-Agent': DESKTOP } });
35
+ function idOf(url) { return url.match(/reel\/(\d+)|videos\/(\d+)|[?&]v=(\d+)|\/(\d+)\/?/)?.slice(1).find(Boolean) || url; }
36
+ function fallbackError(error) { return error instanceof PrivateError || error instanceof NotFoundError || /PRIVATE|NOT_FOUND|empty|404|403|login|checkpoint/i.test(error?.message || ''); }
37
+ function outputFile(base) { return join(TMP_DIR, `${base}.mp4`); }
38
+ async function validFile(file) { try { return (await stat(file)).isFile() && (await stat(file)).size > 4096; } catch { return false; } }
39
+ async function cleanup(file) { await unlink(file).catch(() => {}); }
40
+
41
+ async function ytdlpInfo(url, options = {}) {
42
+ const { stdout } = await runYtDlp(['--dump-json', '--no-download', '--no-playlist', '--skip-download', '--no-warnings', '--remote-components', 'ejs:github', '--js-runtimes', 'node', '--user-agent', UA, ...cookieArgs(options), url]);
43
+ const line = stdout.trim().split(/\r?\n/).find(Boolean);
44
+ if (!line) throw new Error('yt-dlp returned no Facebook video information.');
45
+ return JSON.parse(line);
46
+ }
47
+ function normalizeFallback(data, url) {
48
+ const direct = data.url || data.requested_formats?.find(item => item.url)?.url;
49
+ return { creator: CREATOR, platform: 'facebook', id: data.id || idOf(url), title: data.title, author: data.uploader || data.channel, originalUrl: url, formats: direct ? [{ tag: 'BEST', quality: `${data.height || 720}p`, type: 'mp4', url: direct, hasAudio: true }] : [], fallback: true };
50
+ }
51
+ async function facebookFallbackInfo(url, options) { return normalizeFallback(await ytdlpInfo(url, options), url); }
52
+
53
+ async function facebookFallbackDownload(url, dest, options = {}) {
54
+ await mkdir(TMP_DIR, { recursive: true });
55
+ const base = `fb-${Date.now()}-${Math.random().toString(36).slice(2)}`;
56
+ const temp = outputFile(base);
57
+ try {
58
+ await runYtDlp(['--no-playlist', '--remote-components', 'ejs:github', '--js-runtimes', 'node', '--no-warnings', '--user-agent', UA, '-f', 'bv*[ext=mp4][vcodec^=avc1][height<=1080]+ba[ext=m4a]/b[ext=mp4]/best', '--merge-output-format', 'mp4', '-o', temp, ...cookieArgs(options), url]);
59
+ if (!await validFile(temp)) throw new Error('Facebook yt-dlp output was empty or too small.');
60
+ await rename(temp, dest);
61
+ return dest;
62
+ } catch (error) {
63
+ if (/ENOENT|not found|spawn/i.test(error.message)) throw new Error('Facebook blocked. Run npm i yt-dlp-exec or pip install yt-dlp');
64
+ throw error;
65
+ } finally { await cleanup(temp); }
66
+ }
67
+
68
+ async function facebookTier1(url) {
69
+ const resolved = await fetchWithRetry(url, { headers: FB_HEADERS });
22
70
  const originalUrl = resolved.url || url;
23
- const id = originalUrl.match(/reel\/(\d+)|videos\/(\d+)|[?&]v=(\d+)|\/(\d+)\/?$/)?.slice(1).find(Boolean) || originalUrl;
24
- let html = await (await fetchWithRetry(originalUrl, { headers: { 'User-Agent': DESKTOP } })).text();
71
+ const id = idOf(originalUrl);
72
+ let html = await (await fetchWithRetry(originalUrl, { headers: FB_HEADERS })).text();
25
73
  let formats = extract(html);
26
74
  if (html.length < 5000 || formats.length === 0) {
27
75
  await new Promise(resolve => setTimeout(resolve, 1000));
28
76
  const basicUrl = originalUrl.replace(/www\.facebook\.com/i, 'mbasic.facebook.com');
29
- html = await (await fetchWithRetry(basicUrl, { headers: { 'User-Agent': DESKTOP } })).text();
77
+ html = await (await fetchWithRetry(basicUrl, { headers: FB_HEADERS })).text();
30
78
  formats = extract(html);
31
79
  }
32
- if (!formats.length) {
33
- if (/login|checkpoint|not found/i.test(html)) throw new PrivateError();
34
- throw new NotFoundError('PRIVATE_OR_NOT_FOUND');
35
- }
80
+ if (!formats.length) { if (/login|checkpoint|not found/i.test(html)) throw new PrivateError(); throw new NotFoundError('PRIVATE_OR_NOT_FOUND'); }
36
81
  return { creator: CREATOR, platform: 'facebook', id, originalUrl, formats };
37
82
  }
38
83
 
84
+ /** Download metadata for a Facebook video or reel. */
85
+ export async function facebookDl(url, options = {}) {
86
+ if (!isFacebookUrl(url)) throw new UnsupportedError('Invalid Facebook URL.');
87
+ try { return await facebookTier1(url); }
88
+ catch (error) {
89
+ if (!fallbackError(error)) throw error;
90
+ try { return await facebookFallbackInfo(url, options); }
91
+ catch (fallback) { if (/ENOENT|not found|spawn/i.test(fallback.message)) throw new Error('Facebook blocked. Run npm i yt-dlp-exec or pip install yt-dlp'); throw fallback; }
92
+ }
93
+ }
94
+
39
95
  /** Save a Facebook video to disk. */
40
- export async function facebookSave(url, dest = './video.mp4', quality = 'best') {
41
- return saveResult(await facebookDl(url), dest, quality, { 'User-Agent': DESKTOP, Referer: 'https://www.facebook.com/' });
96
+ export async function facebookSave(url, dest = './video.mp4', quality = 'best', options = {}) {
97
+ const result = await facebookDl(url, options);
98
+ if (result.fallback) return facebookFallbackDownload(url, dest, options);
99
+ return saveResult(result, dest, quality, FB_HEADERS);
42
100
  }
@@ -1,25 +1,81 @@
1
- import { fetchWithRetry } from '../utils/fetcher.js';
1
+ import { mkdir, readFile, rename, stat, unlink } from 'node:fs/promises';
2
+ import { join } from 'node:path';
3
+ import { fetchWithRetry, downloadTo } from '../utils/fetcher.js';
2
4
  import { saveResult, sortFormats } from '../utils/media.js';
5
+ import { cookieArgs, runYtDlp } from '../utils/ytdlp.js';
3
6
  import { unescapeJson } from '../utils/parser.js';
4
7
  import { NotFoundError, PrivateError, UnsupportedError } from '../errors.js';
5
8
  import { CREATOR } from '../constants.js';
6
9
 
7
- const UAS = ['Mozilla/5.0 (iPhone; CPU iPhone OS 16_6 like Mac OS X) AppleWebKit/605.1.15', 'Mozilla/5.0 (Linux; Android 14; Pixel 8) AppleWebKit/537.36 Chrome/120 Mobile Safari/537.36'];
8
- const headers = () => ({ 'User-Agent': UAS[Math.floor(Math.random() * UAS.length)], 'X-IG-App-ID': '936619743392459', Accept: 'application/json' });
9
- const codeOf = url => new URL(url).pathname.match(/\/(?:p|reel|tv)\/([A-Za-z0-9_-]+)/)?.[1] || new URL(url).pathname.match(/share\/([A-Za-z0-9_-]+)/)?.[1];
10
+ export const UA = 'Mozilla/5.0 (Linux; Android 15) AppleWebKit/537.36 Chrome/131 Mobile Safari/537.36';
11
+ export const TMP_DIR = 'cache/temp';
12
+ const IG_HEADERS = { 'User-Agent': UA, Accept: '*/*', 'Accept-Language': 'en-US,en;q=0.9', Referer: 'https://www.instagram.com/', Origin: 'https://www.instagram.com/' };
13
+ const codeOf = url => new URL(url).pathname.match(/\/(?:p|reel|tv)\/([A-Za-z0-9_-]+)/)?.[1];
14
+ async function headersFor(options = {}) { return options.cookiesPath ? { ...IG_HEADERS, Cookie: await readFile(options.cookiesPath, 'utf8') } : IG_HEADERS; }
10
15
 
16
+ export function isInstagramUrl(value) {
17
+ try { return /instagram\.com\/(?:reel|p|tv)\//i.test(new URL(value).href); } catch { return false; }
18
+ }
19
+ export function cleanInstagramUrl(value) { const parsed = new URL(value); return `${parsed.origin}${parsed.pathname.endsWith('/') ? parsed.pathname : `${parsed.pathname}/`}`; }
11
20
  function fromHtml(html) {
12
21
  const find = key => { const m = html.match(new RegExp(`"${key}":"([^"]+)"`)); return m && unescapeJson(m[1]); };
13
22
  const urls = [...html.matchAll(/"url":"(https:[^\"]+)"/g)].map(m => unescapeJson(m[1])).filter(url => /(?:scontent|cdninstagram)/i.test(url) && /\.mp4(?:[?&]|$)/i.test(url));
14
23
  return sortFormats([find('video_url') || urls[0], ...urls].filter(Boolean).map((url, i) => ({ tag: i ? 'SD' : 'HD', quality: i ? '480p' : '720p', type: 'mp4', url })));
15
24
  }
25
+ function fallbackError(error) { return error instanceof PrivateError || error instanceof NotFoundError || /PRIVATE|NOT_FOUND|LOGIN|404|403|empty|accounts\/login/i.test(error?.message || ''); }
26
+ function outputFile(base) { return join(TMP_DIR, `${base}.mp4`); }
27
+ async function validFile(file) { try { return (await stat(file)).isFile() && (await stat(file)).size > 4096; } catch { return false; } }
28
+ async function cleanup(file) { await unlink(file).catch(() => {}); }
29
+ async function ytdlpInfo(url, options = {}) {
30
+ const { stdout } = await runYtDlp(['--dump-json', '--no-download', '--no-playlist', '--skip-download', '--no-warnings', '--remote-components', 'ejs:github', '--js-runtimes', 'node', '--user-agent', UA, '--add-header', 'Referer:https://www.instagram.com/', '-f', 'best', ...cookieArgs(options), url]);
31
+ const line = stdout.trim().split(/\r?\n/).find(Boolean);
32
+ if (!line) throw new Error('yt-dlp returned no Instagram video information.');
33
+ return JSON.parse(line);
34
+ }
35
+ function normalizeFallback(data, url, id) {
36
+ const direct = data.url || data.requested_formats?.find(item => item.url)?.url;
37
+ return { creator: CREATOR, platform: 'instagram', id: data.id || id, username: data.uploader_id, author: data.uploader, caption: data.description, originalUrl: url, formats: direct ? [{ tag: 'BEST', quality: `${data.height || 720}p`, type: 'mp4', url: direct, hasAudio: true }] : [], fallback: true };
38
+ }
39
+ async function ddinstagramFallback(url, id, options = {}) {
40
+ const cookie = options.cookiesPath ? await readFile(options.cookiesPath, 'utf8') : '';
41
+ const headers = cookie ? { ...IG_HEADERS, Cookie: cookie } : IG_HEADERS;
42
+ const parse = html => {
43
+ const match = html.match(/"video_url":"(https:[^"]+)"/) || html.match(/video_versions.*?"url":"(https:[^"]+)"/s);
44
+ if (!match) return null;
45
+ try { return decodeURIComponent(unescapeJson(match[1])); } catch { return unescapeJson(match[1]); }
46
+ };
47
+ for (const endpoint of [`https://www.ddinstagram.com/reel/${id}/`, ...(cookie ? [`https://www.instagram.com/reel/${id}/`] : [])]) {
48
+ try {
49
+ const response = await fetchWithRetry(endpoint, { headers: endpoint.includes('instagram.com/reel') ? { ...headers, 'X-IG-App-ID': '936619743392459' } : headers });
50
+ const video = parse(await response.text());
51
+ if (video) return { creator: CREATOR, platform: 'instagram', id, originalUrl: url, formats: [{ tag: 'DDINSTAGRAM', quality: 'best', type: 'mp4', url: video, hasAudio: true }], fallback: true, fallbackMethod: 'ddinstagram' };
52
+ } catch { /* try the next endpoint */ }
53
+ }
54
+ throw new NotFoundError('DDINSTAGRAM_EMPTY_MEDIA');
55
+ }
56
+ async function instagramFallbackInfo(url, id, options = {}) {
57
+ try { return normalizeFallback(await ytdlpInfo(url, options), url, id); }
58
+ catch { return ddinstagramFallback(url, id, options); }
59
+ }
60
+ async function instagramFallbackDownload(url, dest, options = {}) {
61
+ await mkdir(TMP_DIR, { recursive: true });
62
+ const base = `ig-${Date.now()}-${Math.random().toString(36).slice(2)}`;
63
+ const temp = outputFile(base);
64
+ try {
65
+ await runYtDlp(['--no-playlist', '--remote-components', 'ejs:github', '--js-runtimes', 'node', '--no-warnings', '--user-agent', UA, '--add-header', 'Referer:https://www.instagram.com/', '-f', 'best', '--merge-output-format', 'mp4', '-o', temp, ...cookieArgs(options), url]);
66
+ if (!await validFile(temp)) throw new Error('Instagram yt-dlp output was empty or too small.');
67
+ await rename(temp, dest);
68
+ return dest;
69
+ } catch (error) {
70
+ if (/ENOENT|not found|spawn/i.test(error.message)) throw new Error('Instagram blocked. Run npm i yt-dlp-exec or pip install yt-dlp');
71
+ throw error;
72
+ } finally { await cleanup(temp); }
73
+ }
16
74
 
17
- /** Download metadata and direct formats for an Instagram post or reel. */
18
- export async function instagramDl(url) {
19
- const id = codeOf(url);
20
- if (!id) throw new UnsupportedError('Invalid Instagram URL.');
21
- const base = `https://www.instagram.com/p/${id}/`;
22
- let response = await fetchWithRetry(`${base}?__a=1&__d=dis`, { headers: headers() });
75
+ async function instagramTier1(url, id, options = {}) {
76
+ const requestHeaders = await headersFor(options);
77
+ const base = `https://www.instagram.com/${url.match(/\/(reel|p|tv)\//i)?.[1] || 'p'}/${id}/`;
78
+ let response = await fetchWithRetry(`${base}?__a=1&__d=dis`, { headers: requestHeaders });
23
79
  let formats = [];
24
80
  let item;
25
81
  if (response.ok) {
@@ -34,7 +90,7 @@ export async function instagramDl(url) {
34
90
  } catch { /* use embed fallback */ }
35
91
  }
36
92
  if (!formats.length) {
37
- response = await fetchWithRetry(`https://www.instagram.com/reel/${id}/embed/`, { headers: headers() });
93
+ response = await fetchWithRetry(`${base}embed/`, { headers: requestHeaders });
38
94
  const html = await response.text();
39
95
  if (response.url.includes('/accounts/login')) throw new PrivateError();
40
96
  formats = fromHtml(html);
@@ -44,5 +100,23 @@ export async function instagramDl(url) {
44
100
  return { creator: CREATOR, platform: 'instagram', id, username: item?.user?.username, author: item?.user?.username, caption: item?.caption?.text, originalUrl: url, formats };
45
101
  }
46
102
 
103
+ /** Download metadata and direct formats for an Instagram post or reel. */
104
+ export async function instagramDl(url, options = {}) {
105
+ if (!isInstagramUrl(url)) throw new UnsupportedError('Invalid Instagram URL.');
106
+ const cleanUrl = cleanInstagramUrl(url);
107
+ const id = codeOf(cleanUrl);
108
+ try { return await instagramTier1(cleanUrl, id, options); }
109
+ catch (error) {
110
+ if (!fallbackError(error)) throw error;
111
+ try { return await instagramFallbackInfo(cleanUrl, id, options); }
112
+ catch (fallback) { if (/ENOENT|not found|spawn/i.test(fallback.message)) throw new Error('Instagram blocked. Run npm i yt-dlp-exec or pip install yt-dlp'); throw fallback; }
113
+ }
114
+ }
115
+
47
116
  /** Save an Instagram video or image. */
48
- export async function instagramSave(url, dest = './ig.mp4', quality = 'best') { return saveResult(await instagramDl(url), dest, quality, { Referer: 'https://www.instagram.com/' }); }
117
+ export async function instagramSave(url, dest = './ig.mp4', quality = 'best', options = {}) {
118
+ const result = await instagramDl(url, options);
119
+ if (result.fallback && result.fallbackMethod === 'ddinstagram') return downloadTo(result.formats[0].url, dest, { headers: await headersFor(options) });
120
+ if (result.fallback) return instagramFallbackDownload(cleanInstagramUrl(url), dest, options);
121
+ return saveResult(result, dest, quality, await headersFor(options));
122
+ }
@@ -1,33 +1,148 @@
1
+ import { execFile } from 'node:child_process';
2
+ import { promisify } from 'node:util';
3
+ import { mkdir, readFile, unlink, writeFile } from 'node:fs/promises';
4
+ import { join } from 'node:path';
1
5
  import { fetchWithRetry } from '../utils/fetcher.js';
2
6
  import { resolveShortUrl } from '../utils/resolver.js';
3
7
  import { saveResult, sortFormats } from '../utils/media.js';
4
8
  import { extractJson, getPath, unescapeJson } from '../utils/parser.js';
5
9
  import { NotFoundError, PrivateError, UnsupportedError } from '../errors.js';
6
10
  import { CREATOR } from '../constants.js';
11
+ import { ytDlpDownloadUrl, ytDlpInfoUrl } from '../utils/ytdlp.js';
7
12
 
8
- const UA = 'Mozilla/5.0 (iPhone; CPU iPhone OS 16_6 like Mac OS X) AppleWebKit/605.1.15 Version/16.6 Mobile/15E148 Safari/604.1';
13
+ const execFileAsync = promisify(execFile);
14
+ const UA = 'Mozilla/5.0 (Linux; Android 15) AppleWebKit/537.36 Chrome/131 Mobile Safari/537.36';
15
+ export const TIKWM_API = 'https://www.tikwm.com/api/';
9
16
  const clean = value => value && unescapeJson(value);
10
17
 
18
+ export function isTikTokUrl(value) {
19
+ try { return new URL(value).hostname.toLowerCase().includes('tiktok.com'); } catch { return false; }
20
+ }
21
+
22
+ export function validMediaBuffer(buffer) { return Buffer.isBuffer(buffer) && buffer.length > 4096; }
23
+
24
+ async function tikwmApi(url) {
25
+ const response = await fetchWithRetry(TIKWM_API, {
26
+ method: 'POST',
27
+ headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', 'User-Agent': UA, Referer: 'https://www.tikwm.com/' },
28
+ body: new URLSearchParams({ url, hd: '1' }).toString()
29
+ });
30
+ if (!response.ok) throw new Error(`TIKWM_HTTP_${response.status}`);
31
+ const json = await response.json();
32
+ if (json?.code !== 0 || !json.data) throw new NotFoundError('TIKTOK_PRIVATE_OR_NOT_FOUND');
33
+ return json.data;
34
+ }
35
+
36
+ async function getMedia(url) {
37
+ const response = await fetchWithRetry(url, { headers: { 'User-Agent': UA, Referer: 'https://www.tikwm.com/' } });
38
+ if (!response.ok) throw new Error(`TIKTOK_MEDIA_HTTP_${response.status}`);
39
+ const buffer = Buffer.from(await response.arrayBuffer());
40
+ if (!validMediaBuffer(buffer)) throw new Error('TIKTOK_MEDIA_TOO_SMALL');
41
+ return buffer;
42
+ }
43
+
44
+ function normalizeTikwm(data, originalUrl, id) {
45
+ const author = data.author || {};
46
+ const video = data.hdplay || data.play;
47
+ const formats = sortFormats([video && { tag: 'NOWM', quality: '720p', type: 'mp4', isNoWatermark: true, url: video }]);
48
+ return {
49
+ creator: CREATOR,
50
+ platform: 'tiktok',
51
+ id: String(data.id || id),
52
+ username: author.unique_id ? `@${author.unique_id}` : undefined,
53
+ author: author.nickname,
54
+ title: data.title,
55
+ views: data.play_count,
56
+ likes: data.digg_count,
57
+ comments: data.comment_count,
58
+ shares: data.share_count,
59
+ downloads: data.download_count,
60
+ originalUrl: data.share_url || originalUrl,
61
+ formats,
62
+ music: data.music,
63
+ fallback: true
64
+ };
65
+ }
66
+ function normalizeYtDlp(data, originalUrl, id) {
67
+ const direct = data.url || data.requested_formats?.find(item => item.url)?.url;
68
+ return { creator: CREATOR, platform: 'tiktok', id: data.id || id, author: data.uploader, title: data.title, originalUrl, formats: direct ? [{ tag: 'BEST', quality: `${data.height || 720}p`, type: 'mp4', url: direct, hasAudio: true }] : [], fallback: true, fallbackMethod: 'yt-dlp' };
69
+ }
70
+
71
+ async function tiktokFallback(url, id) {
72
+ const data = await tikwmApi(url);
73
+ return normalizeTikwm(data, url, id);
74
+ }
75
+
11
76
  /** Download metadata and direct CDN formats for a TikTok video. */
12
- export async function tiktokDl(url) {
13
- const resolved = await resolveShortUrl(url);
14
- const id = resolved.match(/\/video\/(\d+)/)?.[1] || resolved.match(/\/v\/(\d+)/)?.[1];
77
+ export async function tiktokDl(url, options = {}) {
78
+ if (!isTikTokUrl(url)) throw new UnsupportedError('Invalid TikTok URL.');
79
+ let resolved = url;
80
+ try { resolved = await resolveShortUrl(url); } catch { /* preserve the original URL for fallback providers */ }
81
+ const id = resolved.match(/\/video\/(\d+)/)?.[1] || resolved.match(/\/v\/(\d+)/)?.[1] || new URL(resolved).pathname.split('/').filter(Boolean).pop() || url;
15
82
  if (!id) throw new UnsupportedError('Invalid TikTok URL.');
16
83
  const pageUrl = resolved.match(/\/video\//) ? resolved : `https://www.tiktok.com/@a/video/${id}`;
17
- const response = await fetchWithRetry(pageUrl, { headers: { 'User-Agent': UA, Accept: 'text/html' } });
18
- const html = await response.text();
19
- const json = extractJson(html, '__UNIVERSAL_DATA_FOR_REHYDRATION__');
20
- const video = getPath(json, '__DEFAULT_SCOPE__.webapp.video-detail.videoData.itemInfo.itemStruct.video');
21
- const item = getPath(json, '__DEFAULT_SCOPE__.webapp.video-detail.videoData.itemInfo.itemStruct');
22
- const play = clean(video?.playAddr) || clean(html.match(/"playAddr":"([^"]+)"/)?.[1]);
23
- const download = clean(video?.downloadAddr) || clean(html.match(/"downloadAddr":"([^"]+)"/)?.[1]);
24
- const formats = sortFormats([
25
- download && { tag: 'NOWM', quality: '720p', type: 'mp4', isNoWatermark: true, url: download },
26
- play && { tag: 'WM', quality: '720p', type: 'mp4', isNoWatermark: false, url: play }
27
- ]);
28
- if (!formats.length) { if (/user-posts|login/i.test(html)) throw new PrivateError('PRIVATE_OR_NOT_FOUND'); throw new NotFoundError('PRIVATE_OR_NOT_FOUND'); }
29
- return { creator: CREATOR, platform: 'tiktok', id, username: item?.author?.uniqueId ? `@${item.author.uniqueId}` : undefined, author: item?.author?.nickname, title: item?.desc, originalUrl: resolved, formats };
84
+ try {
85
+ const response = await fetchWithRetry(pageUrl, { headers: { 'User-Agent': UA, Accept: 'text/html' } });
86
+ const html = await response.text();
87
+ const json = extractJson(html, '__UNIVERSAL_DATA_FOR_REHYDRATION__');
88
+ const video = getPath(json, '__DEFAULT_SCOPE__.webapp.video-detail.videoData.itemInfo.itemStruct.video');
89
+ const item = getPath(json, '__DEFAULT_SCOPE__.webapp.video-detail.videoData.itemInfo.itemStruct');
90
+ const play = clean(video?.playAddr) || clean(html.match(/"playAddr":"([^"]+)"/)?.[1]);
91
+ const download = clean(video?.downloadAddr) || clean(html.match(/"downloadAddr":"([^"]+)"/)?.[1]);
92
+ const formats = sortFormats([
93
+ download && { tag: 'NOWM', quality: '720p', type: 'mp4', isNoWatermark: true, url: download },
94
+ play && { tag: 'WM', quality: '720p', type: 'mp4', isNoWatermark: false, url: play }
95
+ ]);
96
+ if (!formats.length) throw new PrivateError('PRIVATE_OR_NOT_FOUND');
97
+ return { creator: CREATOR, platform: 'tiktok', id, username: item?.author?.uniqueId ? `@${item.author.uniqueId}` : undefined, author: item?.author?.nickname, title: item?.desc, originalUrl: resolved, formats };
98
+ } catch (error) {
99
+ try { return normalizeYtDlp(await ytDlpInfoUrl(url, options), url, id); }
100
+ catch { return tiktokFallback(url, id); }
101
+ }
102
+ }
103
+
104
+ /** Download TikTok MP4 or MP3 bytes using Tier 1 first, then TikWM. */
105
+ export async function tiktokDownload(url, format = 'mp4') {
106
+ const normalizedFormat = String(format).toLowerCase();
107
+ if (!['mp3', 'mp4'].includes(normalizedFormat)) throw new UnsupportedError('Invalid TikTok format.');
108
+ const result = await tiktokDl(url);
109
+ if (!result.fallback) {
110
+ const wanted = result.formats[0];
111
+ if (!wanted) throw new NotFoundError('PRIVATE_OR_NOT_FOUND');
112
+ if (normalizedFormat === 'mp4') return { buffer: await getMedia(wanted.url), mimetype: 'video/mp4', extension: 'mp4' };
113
+ }
114
+ const data = await tikwmApi(url);
115
+ if (normalizedFormat === 'mp4') return { buffer: await getMedia(data.hdplay || data.play), mimetype: 'video/mp4', extension: 'mp4' };
116
+ if (data.music) return { buffer: await getMedia(data.music), mimetype: 'audio/mpeg', extension: 'mp3' };
117
+ const video = await getMedia(data.hdplay || data.play);
118
+ const dir = join(process.cwd(), 'cache', 'temp');
119
+ const base = join(dir, `tt-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`);
120
+ const input = `${base}.mp4`;
121
+ const output = `${base}.mp3`;
122
+ await mkdir(dir, { recursive: true });
123
+ try {
124
+ await writeFile(input, video);
125
+ await execFileAsync('ffmpeg', ['-y', '-i', input, '-vn', '-c:a', 'libmp3lame', '-q:a', '2', output], { maxBuffer: 32 * 1024 * 1024 });
126
+ const buffer = await readFile(output);
127
+ if (!validMediaBuffer(buffer)) throw new Error('TIKTOK_AUDIO_TOO_SMALL');
128
+ return { buffer, mimetype: 'audio/mpeg', extension: 'mp3' };
129
+ } finally {
130
+ await Promise.allSettled([unlink(input), unlink(output)]);
131
+ }
30
132
  }
31
133
 
32
134
  /** Save a TikTok video to disk. */
33
- export async function tiktokSave(url, dest = './tt.mp4', quality = 'best') { return saveResult(await tiktokDl(url), dest, quality, { Referer: 'https://www.tiktok.com/' }); }
135
+ export async function tiktokSave(url, dest = './tt.mp4', quality = 'best', options = {}) {
136
+ const result = await tiktokDl(url, options);
137
+ if (result.fallbackMethod === 'yt-dlp') {
138
+ const output = await ytDlpDownloadUrl(url, 'mp4', options);
139
+ await writeFile(dest, output.buffer);
140
+ return dest;
141
+ }
142
+ if (result.fallback) {
143
+ const output = await tiktokDownload(url, dest.toLowerCase().endsWith('.mp3') ? 'mp3' : 'mp4');
144
+ await writeFile(dest, output.buffer);
145
+ return dest;
146
+ }
147
+ return saveResult(result, dest, quality, { Referer: 'https://www.tiktok.com/' });
148
+ }
@@ -5,12 +5,23 @@ import { NotFoundError, UnsupportedError } from '../errors.js';
5
5
  import { CREATOR } from '../constants.js';
6
6
 
7
7
  const idOf = url => new URL(url).pathname.match(/status\/(\d+)/)?.[1];
8
+ async function fxtwitter(id, originalUrl) {
9
+ const response = await fetchWithRetry(`https://api.fxtwitter.com/status/${id}`, { headers: { 'User-Agent': 'Mozilla/5.0', Accept: 'application/json' } });
10
+ if (!response.ok) throw new Error(`FXTWITTER_HTTP_${response.status}`);
11
+ const json = await response.json();
12
+ const tweet = json.tweet || json;
13
+ const formats = sortFormats((tweet.media?.all || []).flatMap(item => item.videos || []).map(video => ({ tag: `${video.bitrate || 0}kbps`, quality: `${video.height || 720}p`, type: 'mp4', url: video.url, bitrate: video.bitrate })));
14
+ if (!formats.length) throw new NotFoundError('NO_X_VIDEO');
15
+ return { creator: CREATOR, platform: 'twitter', id, author: tweet.author?.screen_name ? `@${tweet.author.screen_name}` : undefined, title: tweet.text, originalUrl, formats, fallback: true };
16
+ }
8
17
 
9
18
  /** Download metadata and direct MP4 variants for an X/Twitter post. */
10
19
  export async function twitterDl(url) {
11
20
  const id = idOf(url);
12
21
  if (!id) throw new UnsupportedError('Invalid Twitter URL.');
13
- const response = await fetchWithRetry(`https://cdn.syndication.twimg.com/tweet-result?id=${id}&lang=en&features=tfw_timeline_list%3D%7B%7D`, { headers: { 'User-Agent': 'Mozilla/5.0' } });
22
+ let response;
23
+ try { response = await fetchWithRetry(`https://cdn.syndication.twimg.com/tweet-result?id=${id}&lang=en&features=tfw_timeline_list%3D%7B%7D`, { headers: { 'User-Agent': 'Mozilla/5.0' } }); }
24
+ catch { return fxtwitter(id, url); }
14
25
  const json = response.ok ? await response.json() : {};
15
26
  const media = json.mediaDetails?.flatMap(item => item.video_info?.variants || []) || [];
16
27
  let formats = sortFormats(media.filter(v => v.content_type === 'video/mp4' && v.url).map(v => ({ tag: `${Math.round((v.bitrate || 0) / 1000) || 0}kbps`, quality: `${Math.round((v.bitrate || 0) / 1000) || 0}kbps`, type: 'mp4', url: v.url, bitrate: v.bitrate })));
@@ -19,7 +30,9 @@ export async function twitterDl(url) {
19
30
  const urls = [...html.matchAll(/https:[^" ]*video\.twimg\.com[^" ]+\.mp4[^" ]*/g)].map(m => unescapeJson(m[0]));
20
31
  formats = urls.map((item, index) => ({ tag: index ? '480p' : '720p', quality: index ? '480p' : '720p', type: 'mp4', url: item }));
21
32
  }
22
- if (!formats.length) throw new NotFoundError();
33
+ if (!formats.length) {
34
+ try { return await fxtwitter(id, url); } catch { throw new NotFoundError(); }
35
+ }
23
36
  return { creator: CREATOR, platform: 'twitter', id, author: json.user?.screen_name ? `@${json.user.screen_name}` : undefined, title: json.text, originalUrl: url, formats };
24
37
  }
25
38
 
@@ -1,22 +1,49 @@
1
1
  import { fetchWithRetry, getVisitorData } from '../utils/fetcher.js';
2
2
  import { saveResult, sortFormats, qualityOf } from '../utils/media.js';
3
- import { NotFoundError, UnsupportedError } from '../errors.js';
3
+ import { BotDetectedError, NotFoundError, UnsupportedError } from '../errors.js';
4
4
  import { CREATOR } from '../constants.js';
5
+ import { fallbackInstallMessage, ytDlpDownload, ytDlpInfo } from '../utils/ytdlp.js';
5
6
 
6
7
  const KEY = 'AIzaSyA8eiZmM1FaDVjRy-df2KTyQ_vz_yYM39w';
8
+ export const UA = 'Mozilla/5.0 (Linux; Android 15) AppleWebKit/537.36 Chrome/131 Mobile Safari/537.36';
9
+ export const INNERTUBE_CONTEXT = { client: { clientName: 'WEB', clientVersion: '2.20260813.01.00', hl: 'es', gl: 'DO' } };
10
+ export const INNERTUBE_URL = 'https://www.youtube.com/youtubei/v1/search?prettyPrint=false';
7
11
  const ENDPOINT = `https://www.youtube.com/youtubei/v1/player?key=${KEY}`;
8
12
  const CLIENTS = [
9
- { name: 'ANDROID', version: '20.10.38', ua: 'com.google.android.youtube/20.10.38 (Linux; U; Android 14) gzip' },
10
- { name: 'IOS', version: '19.45.4', deviceModel: 'iPhone15,2', ua: 'com.google.ios.youtube/19.45.4 (iPhone15,2; U; CPU iOS 17_5_1 like Mac OS X)' },
11
- { name: 'ANDROID_MUSIC', version: '6.51', ua: 'com.google.android.apps.youtube.music/6.51 (Linux; U; Android 14) gzip' }
13
+ { name: 'WEB', version: '2.20260813.01.00', ua: UA },
14
+ { name: 'IOS', version: '19.09.3', deviceModel: 'iPhone16,2', ua: 'com.google.ios.youtube/19.09.3 (iPhone16,2; U; iOS 17.2; en_US)' }
12
15
  ];
16
+ export const YOUTUBE_MEDIA_HEADERS = {
17
+ 'User-Agent': UA,
18
+ Accept: '*/*',
19
+ 'Accept-Language': 'en-US,en;q=0.9',
20
+ Referer: 'https://www.youtube.com/',
21
+ Origin: 'https://www.youtube.com'
22
+ };
23
+ export const IOS_MEDIA_HEADERS = {
24
+ ...YOUTUBE_MEDIA_HEADERS,
25
+ 'User-Agent': 'com.google.ios.youtube/19.09.3 (iPhone16,2; U17D201)'
26
+ };
27
+ function fallbackAllowed(error) { return error?.code === 'BOT_DETECTED' || /YOUTUBE_HTTP_4\d\d|403|automated|unusual traffic|NO_PLAYABLE|UNAVAILABLE/i.test(error?.message || ''); }
28
+ function fromYtDlp(data, url, id) {
29
+ const streams = data.requested_formats || [];
30
+ const video = streams.find(item => item.vcodec && item.vcodec !== 'none' && item.url) || (data.vcodec && data.url ? data : null);
31
+ const audio = streams.find(item => item.acodec && item.acodec !== 'none' && item.url) || (data.acodec && data.url ? data : null);
32
+ const direct = video?.url || data.url;
33
+ const format = direct ? [{ tag: data.height ? `${data.height}p` : 'best', quality: data.height ? `${data.height}p` : 'best', type: 'mp4', hasAudio: Boolean(video?.acodec && video.acodec !== 'none') || Boolean(data.acodec && data.acodec !== 'none'), url: direct, height: data.height, bitrate: data.tbr }] : [];
34
+ return { creator: CREATOR, platform: 'youtube', id, client: 'YT-DLP', title: data.title, author: data.uploader || data.channel, thumbnail: data.thumbnail, originalUrl: url, formats: format, ...(video?.url && audio?.url && video.url !== audio.url ? { needsMux: true, videoUrl: video.url, audioUrl: audio.url } : {}), fallback: true };
35
+ }
36
+ async function ytDlpFallback(url, id, cause, options = {}) {
37
+ try { return fromYtDlp(await ytDlpInfo(id, options), url, id); }
38
+ catch (error) { throw new Error(`${fallbackInstallMessage()} (${cause?.message || error.message})`); }
39
+ }
13
40
 
14
41
  /** Extract an 11-character YouTube video id. */
15
42
  export function videoId(url) { return new URL(url).toString().match(/(?:youtu\.be\/|[?&]v=|shorts\/)([A-Za-z0-9_-]{11})/)?.[1]; }
16
43
 
17
44
  /** Send one direct Innertube player request for a specific client profile. */
18
45
  export async function innertubeRequest(id, client, visitorData) {
19
- const context = { client: { clientName: client.name, clientVersion: client.version, ...(client.deviceModel ? { deviceModel: client.deviceModel } : {}), hl: 'en', gl: 'US' }, ...(visitorData ? { visitorData } : {}) };
46
+ const context = { ...INNERTUBE_CONTEXT, client: { ...INNERTUBE_CONTEXT.client, clientName: client.name, clientVersion: client.version, ...(client.deviceModel ? { deviceModel: client.deviceModel } : {}) }, ...(visitorData ? { visitorData } : {}) };
20
47
  const response = await fetchWithRetry(ENDPOINT, { method: 'POST', headers: { 'Content-Type': 'application/json', 'User-Agent': client.ua, ...(visitorData ? { 'X-Goog-Visitor-Id': visitorData } : {}), Origin: 'https://www.youtube.com', Referer: 'https://www.youtube.com/' }, body: JSON.stringify({ context, videoId: id, contentCheckOk: true, racyCheckOk: true }) });
21
48
  if (!response.ok) throw new Error(`YOUTUBE_HTTP_${response.status}`);
22
49
  return response.json();
@@ -30,33 +57,53 @@ export async function player(id) {
30
57
  for (const client of CLIENTS) {
31
58
  try {
32
59
  data = await innertubeRequest(id, client, visitorData);
33
- if (data?.streamingData?.formats || data?.streamingData?.adaptiveFormats) return data;
60
+ if (data?.streamingData?.formats || data?.streamingData?.adaptiveFormats) {
61
+ data.__socialDlClient = client.name;
62
+ return data;
63
+ }
34
64
  if (data?.playabilityStatus?.status === 'ERROR' && JSON.stringify(data).includes('403')) continue;
35
65
  lastError = new Error(data?.playabilityStatus?.reason || 'UNAVAILABLE');
36
66
  } catch (error) {
37
67
  lastError = error;
38
68
  }
39
69
  }
40
- if (!data?.streamingData) throw lastError || new Error(data?.playabilityStatus?.reason || 'UNAVAILABLE');
70
+ if (!data?.streamingData) {
71
+ if (lastError?.code === 'BOT_DETECTED' || /403|automated|unusual traffic/i.test(lastError?.message || '')) throw new BotDetectedError();
72
+ throw lastError || new Error(data?.playabilityStatus?.reason || 'UNAVAILABLE');
73
+ }
41
74
  return data;
42
75
  }
43
76
 
44
77
  /** Download metadata and direct formats for a YouTube video. */
45
- export async function youtubeDl(url) {
78
+ export async function youtubeDl(url, options = {}) {
46
79
  const id = videoId(url);
47
80
  if (!id) throw new UnsupportedError('Invalid YouTube URL.');
48
- const data = await player(id);
49
- if (!data.streamingData) throw new NotFoundError('PRIVATE_OR_AGE_RESTRICTED');
81
+ let data;
82
+ try { data = await player(id); }
83
+ catch (error) {
84
+ if (!fallbackAllowed(error)) throw error;
85
+ return ytDlpFallback(url, id, error, options);
86
+ }
87
+ if (!data.streamingData) return ytDlpFallback(url, id, new NotFoundError('PRIVATE_OR_AGE_RESTRICTED'), options);
50
88
  const all = [...(data.streamingData.formats || []), ...(data.streamingData.adaptiveFormats || [])];
51
- const formats = sortFormats(all.filter(item => item.url && /^video\/mp4/.test(item.mimeType || '')).map(item => ({ tag: qualityOf(item, 'video'), quality: qualityOf(item, 'video'), type: 'mp4', hasAudio: !item.mimeType?.includes('video-only'), url: item.url, bitrate: item.bitrate, height: item.height })));
89
+ const formats = sortFormats(all.filter(item => item.url && /^video\/mp4/.test(item.mimeType || '')).map(item => ({ tag: qualityOf(item, 'video'), quality: qualityOf(item, 'video'), type: 'mp4', hasAudio: !item.mimeType?.includes('video-only'), url: item.url, bitrate: item.bitrate, height: item.height }))).sort((a, b) => Number(b.hasAudio) - Number(a.hasAudio));
52
90
  if (!formats.length) throw new NotFoundError('NO_PLAYABLE_MP4');
53
91
  const best = formats[0];
54
92
  const audio = all.find(item => item.url && String(item.itag) === '140' && item.mimeType?.startsWith('audio/')) || all.find(item => item.url && item.mimeType?.startsWith('audio/'));
55
93
  const mux = best.height >= 1080 && best.hasAudio === false && audio;
56
- return { creator: CREATOR, platform: 'youtube', id, title: data.videoDetails?.title, thumbnail: `https://i.ytimg.com/vi/${id}/maxresdefault.jpg`, originalUrl: url, formats, ...(mux ? { needsMux: true, videoUrl: best.url, audioUrl: audio.url } : {}) };
94
+ return { creator: CREATOR, platform: 'youtube', id, client: data.__socialDlClient, title: data.videoDetails?.title, thumbnail: `https://i.ytimg.com/vi/${id}/maxresdefault.jpg`, originalUrl: url, formats, ...(mux ? { needsMux: true, videoUrl: best.url, audioUrl: audio.url } : {}) };
57
95
  }
58
96
 
59
97
  /** Save a YouTube video to disk. */
60
- export async function youtubeSave(url, dest = './yt.mp4', quality = 'best') { return saveResult(await youtubeDl(url), dest, quality); }
98
+ export async function youtubeSave(url, dest = './yt.mp4', quality = 'best', options = {}) {
99
+ const result = await youtubeDl(url, options);
100
+ if (result.fallback) {
101
+ const { writeFile } = await import('node:fs/promises');
102
+ const output = await ytDlpDownload(result.id, 'mp4', options);
103
+ await writeFile(dest, output.buffer);
104
+ return dest;
105
+ }
106
+ return saveResult(result, dest, quality, { ...YOUTUBE_MEDIA_HEADERS, fallbackHeaders: IOS_MEDIA_HEADERS });
107
+ }
61
108
 
62
109
  export { CLIENTS };
@@ -1,13 +1,24 @@
1
- import { player, videoId } from './youtube.js';
1
+ import { IOS_MEDIA_HEADERS, player, videoId, YOUTUBE_MEDIA_HEADERS } from './youtube.js';
2
2
  import { saveResult, sortFormats } from '../utils/media.js';
3
3
  import { NotFoundError, UnsupportedError } from '../errors.js';
4
4
  import { CREATOR } from '../constants.js';
5
+ import { fallbackInstallMessage, ytDlpDownload, ytDlpInfo } from '../utils/ytdlp.js';
5
6
 
6
7
  /** Download audio-only metadata from YouTube (m4a/opus, not transcoded MP3). */
7
- export async function youtubeMp3Dl(url) {
8
+ export async function youtubeMp3Dl(url, options = {}) {
8
9
  const id = videoId(url);
9
10
  if (!id) throw new UnsupportedError('Invalid YouTube URL.');
10
- const data = await player(id);
11
+ let data;
12
+ try { data = await player(id); }
13
+ catch (error) {
14
+ if (!error?.code && !/403|automated|unusual traffic|UNAVAILABLE/i.test(error?.message || '')) throw error;
15
+ try {
16
+ const fallback = await ytDlpInfo(id);
17
+ return { creator: CREATOR, platform: 'youtube', id, client: 'YT-DLP', title: fallback.title, author: fallback.uploader || fallback.channel, thumbnail: fallback.thumbnail, isAudioOnly: true, originalUrl: url, formats: [], fallback: true };
18
+ } catch (fallbackError) {
19
+ throw new Error(`${fallbackInstallMessage()} (${fallbackError.message})`);
20
+ }
21
+ }
11
22
  const source = data.streamingData?.adaptiveFormats || [];
12
23
  const formats = sortFormats(source.filter(item => item.url && item.mimeType?.startsWith('audio/')).map(item => {
13
24
  const type = item.mimeType.includes('webm') ? 'opus' : 'm4a';
@@ -15,8 +26,25 @@ export async function youtubeMp3Dl(url) {
15
26
  return { tag: type === 'opus' ? `OPUS_${kbps}` : `MP3_${kbps}`, quality: `${kbps}kbps`, type, hasAudio: true, isAudioOnly: true, url: item.url, bitrate: item.bitrate };
16
27
  }));
17
28
  if (!formats.length) throw new NotFoundError('PRIVATE_OR_AGE_RESTRICTED');
18
- return { creator: CREATOR, platform: 'youtube', id, title: data.videoDetails?.title, thumbnail: `https://i.ytimg.com/vi/${id}/maxresdefault.jpg`, isAudioOnly: true, originalUrl: url, formats };
29
+ if (!formats.length) {
30
+ try {
31
+ const fallback = await ytDlpInfo(id);
32
+ return { creator: CREATOR, platform: 'youtube', id, client: 'YT-DLP', title: fallback.title, author: fallback.uploader || fallback.channel, thumbnail: fallback.thumbnail, isAudioOnly: true, originalUrl: url, formats: [], fallback: true };
33
+ } catch (fallbackError) {
34
+ throw new Error(`${fallbackInstallMessage()} (${fallbackError.message})`);
35
+ }
36
+ }
37
+ return { creator: CREATOR, platform: 'youtube', id, client: data.__socialDlClient, title: data.videoDetails?.title, thumbnail: `https://i.ytimg.com/vi/${id}/maxresdefault.jpg`, isAudioOnly: true, originalUrl: url, formats };
19
38
  }
20
39
 
21
40
  /** Save the best YouTube audio stream to disk. */
22
- export async function youtubeMp3Save(url, dest = './audio.m4a', quality = 'best') { return saveResult(await youtubeMp3Dl(url), dest, quality); }
41
+ export async function youtubeMp3Save(url, dest = './audio.m4a', quality = 'best', options = {}) {
42
+ const result = await youtubeMp3Dl(url, options);
43
+ if (result.fallback) {
44
+ const { writeFile } = await import('node:fs/promises');
45
+ const output = await ytDlpDownload(result.id, 'mp3', options);
46
+ await writeFile(dest, output.buffer);
47
+ return dest;
48
+ }
49
+ return saveResult(result, dest, quality, { ...YOUTUBE_MEDIA_HEADERS, fallbackHeaders: IOS_MEDIA_HEADERS });
50
+ }
@@ -0,0 +1,2 @@
1
+ export const UA = 'Mozilla/5.0 (Linux; Android 15) AppleWebKit/537.36 Chrome/131 Mobile Safari/537.36';
2
+ export const TMP = 'cache/temp';
@@ -1,9 +1,7 @@
1
1
  import { BotDetectedError } from '../errors.js';
2
2
 
3
3
  const USER_AGENTS = [
4
- 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/120.0 Safari/537.36',
5
- 'Mozilla/5.0 (iPhone; CPU iPhone OS 16_6 like Mac OS X) AppleWebKit/605.1.15 Version/16.6 Mobile/15E148 Safari/604.1',
6
- 'Mozilla/5.0 (Linux; Android 14; Pixel 8) AppleWebKit/537.36 Chrome/120.0 Mobile Safari/537.36'
4
+ 'Mozilla/5.0 (Linux; Android 15) AppleWebKit/537.36 Chrome/131 Mobile Safari/537.36'
7
5
  ];
8
6
 
9
7
  /** Pick a browser-like user agent. */
@@ -16,7 +14,7 @@ export async function fetchWithRetry(input, options = {}, retries = 1) {
16
14
  let lastError;
17
15
  for (let attempt = 0; attempt <= retries; attempt += 1) {
18
16
  try {
19
- const headers = { 'User-Agent': randomUserAgent(), ...(options.headers || {}) };
17
+ const headers = { 'User-Agent': randomUserAgent(), Accept: '*/*', 'Accept-Language': 'en-US,en;q=0.9', ...(options.headers || {}) };
20
18
  const response = await fetch(input, { ...options, headers, redirect: options.redirect || 'follow' });
21
19
  if (response.status === 403) {
22
20
  const body = await response.clone().text();
@@ -56,8 +54,21 @@ export async function fetchText(input, options = {}) {
56
54
 
57
55
  /** Fetch a direct media URL and write it to disk. */
58
56
  export async function downloadTo(url, dest, options = {}) {
59
- const response = await fetchWithRetry(url, options);
60
- if (!response.ok) throw new Error(`DOWNLOAD_HTTP_${response.status}`);
57
+ const { fallbackHeaders, ...requestOptions } = options;
58
+ const attempts = [requestOptions.headers, fallbackHeaders].filter(Boolean);
59
+ let response;
60
+ let lastError;
61
+ for (let index = 0; index < Math.max(1, attempts.length); index += 1) {
62
+ try {
63
+ response = await fetchWithRetry(url, { ...requestOptions, headers: attempts[index] }, 0);
64
+ if (response.ok || response.status !== 403 || index === attempts.length - 1) break;
65
+ } catch (error) {
66
+ lastError = error;
67
+ if (index === attempts.length - 1) throw error;
68
+ }
69
+ }
70
+ if (lastError && !response) throw lastError;
71
+ if (!response?.ok) throw new Error(`DOWNLOAD_HTTP_${response?.status || 'FAILED'}`);
61
72
  const { writeFile } = await import('node:fs/promises');
62
73
  const data = Buffer.from(await response.arrayBuffer());
63
74
  await writeFile(dest, data);
@@ -4,7 +4,7 @@ import { fetchWithRetry } from './fetcher.js';
4
4
  export async function resolveShortUrl(url) {
5
5
  const input = new URL(url);
6
6
  const host = input.hostname.toLowerCase();
7
- const shortHosts = ['vm.tiktok.com', 'pin.it', 't.co', 'fb.watch'];
7
+ const shortHosts = ['vm.tiktok.com', 'vt.tiktok.com', 'm.tiktok.com', 'pin.it', 't.co', 'fb.watch'];
8
8
  const isCapcutShort = host.endsWith('capcut.com') && input.pathname.startsWith('/t/');
9
9
  if (!shortHosts.includes(host) && !isCapcutShort) return url;
10
10
  const response = await fetchWithRetry(url, { method: 'GET', redirect: 'follow' });
@@ -0,0 +1,103 @@
1
+ import { execFile } from 'node:child_process';
2
+ import { mkdir, readFile, readdir, stat, unlink } from 'node:fs/promises';
3
+ import { join } from 'node:path';
4
+ import { promisify } from 'node:util';
5
+ import { UA } from './constants.js';
6
+ const execFileAsync = promisify(execFile);
7
+ const BASE_ARGS = [
8
+ '--no-playlist', '--no-warnings', '--newline', '--no-part', '--no-continue',
9
+ '--retries', '3', '--fragment-retries', '3', '--file-access-retries', '3',
10
+ '--extractor-retries', '3', '--remote-components', 'ejs:github', '--js-runtimes', 'node',
11
+ '--user-agent', UA
12
+ ];
13
+
14
+ /** Run a yt-dlp-compatible executable and return stdout/stderr. */
15
+ export async function runYtDlp(args) {
16
+ const commands = [];
17
+ try {
18
+ const mod = await import('yt-dlp-exec');
19
+ const binary = mod.default?.default || mod.default || mod;
20
+ if (typeof binary === 'function') {
21
+ const result = await binary(args, { maxBuffer: 32 * 1024 * 1024, env: { ...process.env } });
22
+ return { stdout: String(result?.stdout || result || ''), stderr: String(result?.stderr || '') };
23
+ }
24
+ } catch {
25
+ // Optional dependency is intentionally allowed to be absent.
26
+ }
27
+ commands.push('yt-dlp', 'yt-dlp-exec');
28
+ let lastError;
29
+ for (const command of commands) {
30
+ try {
31
+ const result = await execFileAsync(command, args, { maxBuffer: 32 * 1024 * 1024, env: { ...process.env } });
32
+ return { stdout: String(result.stdout || ''), stderr: String(result.stderr || '') };
33
+ } catch (error) {
34
+ lastError = error;
35
+ if (!/ENOENT|not found|spawn/i.test(error?.code || error?.message || '')) throw error;
36
+ }
37
+ }
38
+ throw new Error(String(lastError?.stderr || lastError?.stdout || lastError?.message || 'yt-dlp is not installed').trim());
39
+ }
40
+
41
+ export function ytDlpUrl(id) { return `https://www.youtube.com/watch?v=${id}`; }
42
+ export function cookieArgs(options = {}) {
43
+ const args = [];
44
+ if (options.cookiesPath) args.push('--cookies', options.cookiesPath);
45
+ if (options.cookiesFromBrowser) args.push('--cookies-from-browser', options.cookiesFromBrowser);
46
+ return args;
47
+ }
48
+
49
+ /** Get yt-dlp JSON metadata for a video. */
50
+ export async function ytDlpInfo(id, options = {}) {
51
+ return ytDlpInfoUrl(ytDlpUrl(id), options);
52
+ }
53
+ export async function ytDlpInfoUrl(url, options = {}) {
54
+ const { stdout } = await runYtDlp(['--dump-json', '--no-download', '--no-playlist', '--skip-download', '--no-warnings', '--remote-components', 'ejs:github', '--js-runtimes', 'node', '--user-agent', UA, ...cookieArgs(options), url]);
55
+ const line = stdout.trim().split(/\r?\n/).find(Boolean);
56
+ if (!line) throw new Error('yt-dlp returned no video information.');
57
+ return JSON.parse(line);
58
+ }
59
+
60
+ function valid(file) {
61
+ return stat(file).then(info => info.isFile() && info.size > 4096).catch(() => false);
62
+ }
63
+ async function locate(base, dir) {
64
+ for (const name of await readdir(dir).catch(() => [])) {
65
+ if (name.startsWith(base) && await valid(join(dir, name))) return join(dir, name);
66
+ }
67
+ return null;
68
+ }
69
+ async function cleanup(base, dir) {
70
+ await Promise.all((await readdir(dir).catch(() => [])).filter(name => name.startsWith(base)).map(name => unlink(join(dir, name)).catch(() => {})));
71
+ }
72
+
73
+ /** Download a YouTube video or audio fallback into memory, then clean temp files. */
74
+ export async function ytDlpDownload(id, format, options = {}) {
75
+ return ytDlpDownloadUrl(ytDlpUrl(id), format, options);
76
+ }
77
+ export async function ytDlpDownloadUrl(url, format, options = {}) {
78
+ if (!['mp3', 'mp4'].includes(format)) throw new Error('Invalid yt-dlp format.');
79
+ const dir = join(process.cwd(), 'cache', 'temp');
80
+ await mkdir(dir, { recursive: true });
81
+ const base = `yt-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`;
82
+ const output = join(dir, `${base}.%(ext)s`);
83
+ const args = format === 'mp3'
84
+ ? [...BASE_ARGS, ...cookieArgs(options), '-f', 'bestaudio[ext=m4a]/bestaudio', '-x', '--audio-format', 'mp3', '--audio-quality', '0', '-o', output, url]
85
+ : [...BASE_ARGS, ...cookieArgs(options), '-f', 'bv*[ext=mp4][vcodec^=avc1][height<=720]+ba[ext=m4a][acodec^=mp4a]/b[ext=mp4][height<=720]', '--merge-output-format', 'mp4', '--remux-video', 'mp4', '-o', output, url];
86
+ let file;
87
+ try {
88
+ await runYtDlp(args);
89
+ file = await locate(base, dir);
90
+ if (!file) throw new Error(`yt-dlp did not produce a valid ${format.toUpperCase()} file.`);
91
+ const buffer = await readFile(file);
92
+ return format === 'mp3' ? { buffer, mimetype: 'audio/mpeg', extension: 'mp3' } : { buffer, mimetype: 'video/mp4', extension: 'mp4' };
93
+ } finally {
94
+ if (file) await unlink(file).catch(() => {});
95
+ await cleanup(base, dir);
96
+ }
97
+ }
98
+
99
+ export function fallbackInstallMessage() {
100
+ return 'YouTube blocked datacenter IP. Install fallback: npm i yt-dlp-exec or pip install yt-dlp --remote-components ejs:github';
101
+ }
102
+
103
+ export { BASE_ARGS, UA as YTDLP_USER_AGENT };
package/index.d.ts DELETED
@@ -1,31 +0,0 @@
1
- export interface Format { tag: string; quality: string; type: string; url: string; hasAudio?: boolean; isAudioOnly?: boolean; isNoWatermark?: boolean; }
2
- export interface DlResult { creator?: string; platform: string; id: string; title?: string; author?: string; thumbnail?: string; originalUrl: string; isAudioOnly?: boolean; formats: Format[]; needsMux?: boolean; videoUrl?: string; audioUrl?: string; }
3
- export interface UniversalOptions { audioOnly?: boolean; quality?: string; noWatermark?: boolean; }
4
- export interface StreamResult { stream: ReadableStream<Uint8Array>; filename: string; info: DlResult; contentType: string; contentLength?: string | null; }
5
- export function detectPlatform(url: string): string;
6
- export function universalDl(url: string, opts?: UniversalOptions): Promise<DlResult>;
7
- export function universalSave(url: string, dest?: string, opts?: UniversalOptions): Promise<string>;
8
- export function universalStream(url: string, opts?: UniversalOptions): Promise<StreamResult>;
9
- export function doctor(options?: { log?: boolean }): Promise<any>;
10
- export function facebookDl(url: string): Promise<DlResult>;
11
- export function facebookSave(url: string, dest?: string, quality?: string): Promise<string>;
12
- export function youtubeDl(url: string): Promise<DlResult>;
13
- export function youtubeSave(url: string, dest?: string, quality?: string): Promise<string>;
14
- export function youtubeMp3Dl(url: string): Promise<DlResult>;
15
- export function youtubeMp3Save(url: string, dest?: string, quality?: string): Promise<string>;
16
- export function instagramDl(url: string): Promise<DlResult>;
17
- export function instagramSave(url: string, dest?: string, quality?: string): Promise<string>;
18
- export function tiktokDl(url: string): Promise<DlResult>;
19
- export function tiktokSave(url: string, dest?: string, quality?: string): Promise<string>;
20
- export function twitterDl(url: string): Promise<DlResult>;
21
- export function twitterSave(url: string, dest?: string, quality?: string): Promise<string>;
22
- export function pinterestDl(url: string): Promise<DlResult>;
23
- export function pinterestSave(url: string, dest?: string, quality?: string): Promise<string>;
24
- export function capcutDl(url: string): Promise<DlResult>;
25
- export function capcutSave(url: string, dest?: string, quality?: string): Promise<string>;
26
- export class SocialDlError extends Error { code: string; }
27
- export class BotDetectedError extends SocialDlError {}
28
- export class PrivateError extends SocialDlError {}
29
- export class NotFoundError extends SocialDlError {}
30
- export class UnsupportedError extends SocialDlError {}
31
- export const CREATOR: string;