@pieda/video-dl 1.7.0 → 1.9.0

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.
@@ -1,8 +1,8 @@
1
1
  type TUseInstagram = {
2
+ ytdlpPath: string;
2
3
  videoId: string;
3
4
  dest: string;
4
5
  debug?: boolean;
5
- headless?: boolean;
6
6
  };
7
- export declare const useInstagram: ({ videoId, dest, debug, headless, }: TUseInstagram) => Promise<[Error | null, boolean | null]>;
7
+ export declare const useInstagram: ({ ytdlpPath, videoId, dest, debug, }: TUseInstagram) => Promise<[Error | null, boolean | null]>;
8
8
  export {};
@@ -1,8 +1,9 @@
1
1
  type TUseVimeo = {
2
+ ytdlpPath: string;
2
3
  videoId: string;
3
4
  dest: string;
4
5
  downloadBtnSelector?: string;
5
6
  debug?: boolean;
6
7
  };
7
- export declare const useVimeo: ({ videoId, dest, debug, }: TUseVimeo) => Promise<[Error | null, boolean | null]>;
8
+ export declare const useVimeo: ({ ytdlpPath, videoId, dest, debug, }: TUseVimeo) => Promise<[Error | null, boolean | null]>;
8
9
  export {};
@@ -1,7 +1,8 @@
1
1
  type TUseYoutube = {
2
+ ytdlpPath: string;
2
3
  videoId: string;
3
4
  dest: string;
4
5
  debug?: boolean;
5
6
  };
6
- export declare const useYoutube: ({ videoId, dest, debug, }: TUseYoutube) => Promise<[Error | null, boolean | null]>;
7
+ export declare const useYoutube: ({ ytdlpPath, videoId, dest, debug, }: TUseYoutube) => Promise<[Error | null, boolean | null]>;
7
8
  export {};
package/dist/video-dl.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const l=o=>new Promise((t,u)=>{import(o).then(i=>t(i.default)).catch(u)}),m=async(o,t,u)=>{const i=await l("fs"),e=await l("axios");return console.log(`Downloading ${o} to ${t}`),new Promise((n,w)=>{e.get(o,{responseType:"stream",headers:u}).then(s=>{const p=s.data,a=i.createWriteStream(t);p.pipe(a),a.on("finish",function(){a.close(()=>{n()})})}).catch(w)})},f=(o=!1)=>({log:(...t)=>{o&&console.log(...t)},error:(...t)=>{o&&console.error(...t)}}),h=async({videoId:o,dest:t,debug:u=!1})=>{f(u).log(`Downloading video: ${o} to: ${t}`);const e=await l("fs"),n=await l("path"),w=await l("youtubei.js");e.existsSync(n.dirname(t))||e.mkdirSync(n.dirname(t),{recursive:!0},777);const s=await w.create({cache:!1,generate_session_locally:!0});try{const p=await s.download(o,{type:"video+audio",quality:"best",format:"mp4",client:"WEB"}),a=e.createWriteStream(t);for await(const g of b(p))a.write(g);return[null,!0]}catch(p){return console.log("error",p),[p,null]}};async function*b(o){const t=o.getReader();try{for(;;){const{done:u,value:i}=await t.read();if(u)return;yield i}}finally{t.releaseLock()}}const y=async({videoId:o,dest:t,debug:u=!1,headless:i=!0})=>{try{const e=f(u);e.log(`Downloading video: ${o} to: ${t}`);const n=`https://m.facebook.com/watch/?v=${o}`,w=await l("fs"),s=await l("path"),p=await l("puppeteer");w.existsSync(s.dirname(t))||(w.mkdirSync(s.dirname(t),{recursive:!0},777),e.log(`建立 ${s.dirname(t)}`));const a={};i||(a.headless=!1);const g=await p.launch(a);e.log("啟動 puppeteer");const r=await g.newPage();await r.setUserAgent("'Mozilla/5.0 (iPhone; CPU iPhone OS 16_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.6 Mobile/15E148 Safari/604.1'"),e.log("開新分頁"),e.log(`前往 ${n}`),await Promise.race([r.goto(n,{waitUntil:"networkidle0"}),r.waitForSelector("video")]),e.log("找到 video 元素");const c=await r.$eval("video",d=>d.getAttribute("src"));return e.log(`取得 video url: ${c}`),await g.close(),e.log("關閉 puppeteer"),await m(c,t),e.log(`下載完成 ${c}`),[null,!0]}catch(e){return[e,null]}},v=async({videoId:o,dest:t,debug:u=!1})=>{const i=f(u);i.log(`Downloading video: ${o} to: ${t}`);const e=await l("fs"),n=await l("path"),w=await l("axios"),s=async()=>{const a=await w.get("https://vimeo.com/_next/viewer",{responseType:"json",headers:{"X-Requested-With":"XMLHttpRequest"}}),{jwt:g}=a.data;return g||""},p=async(a="",g="")=>{const r=await w.get(`https://api.vimeo.com/videos/${a}?&fields=download`,{responseType:"json",headers:{"X-Requested-With":"XMLHttpRequest",Authorization:`jwt ${g}`}}),{download:c=[]}=r.data,d=c[0]||null;return(d==null?void 0:d.link)||""};return e.existsSync(n.dirname(t))||(e.mkdirSync(n.dirname(t),{recursive:!0},777),i.log(`建立 ${n.dirname(t)}`)),new Promise(async(a,g)=>{try{const r=await s();i.log(`取得 jwt ${r}`);const c=await p(o,r);i.log(`取得 videoUrl ${c}`),await m(c,t),i.log(`下載完成 ${t}`),a([null,!0])}catch(r){a([r,null])}})},$=async({videoId:o,dest:t,debug:u=!1,headless:i=!0})=>{try{const e=f(u);e.log(`Downloading video: ${o} to: ${t}`);const n=`https://www.instagram.com/reel/${o}/`,w=await l("fs"),s=await l("path"),p=await l("puppeteer");w.existsSync(s.dirname(t))||(w.mkdirSync(s.dirname(t),{recursive:!0},777),e.log(`建立 ${s.dirname(t)}`));const a={};i||(a.headless=!1);const g=await p.launch(a);e.log("啟動 puppeteer");const r=await g.newPage();await r.setUserAgent("'Mozilla/5.0 (iPhone; CPU iPhone OS 16_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.6 Mobile/15E148 Safari/604.1'"),e.log("開新分頁"),e.log(`前往 ${n}`),await Promise.race([r.goto(n,{waitUntil:"networkidle0"}),r.waitForSelector("video")]),e.log("找到 video 元素");const c=await r.$eval("video",d=>d.getAttribute("src"));return e.log(`取得 video url: ${c}`),await g.close(),e.log("關閉 puppeteer"),await m(c,t),e.log(`下載完成 ${c}`),[null,!0]}catch(e){return[e,null]}},k=async({videoLink:o,dest:t,debug:u=!1,headless:i=!0})=>{try{const e=f(u);e.log(`Downloading video: ${o} to: ${t}`);const n=o,w=await l("fs"),s=await l("path"),p=await l("puppeteer");w.existsSync(s.dirname(t))||(w.mkdirSync(s.dirname(t),{recursive:!0},777),e.log(`建立 ${s.dirname(t)}`));const a={};i||(a.headless=!1);const g=await p.launch(a);e.log("啟動 puppeteer");const r=await g.newPage();e.log("開新分頁"),e.log(`前往 ${n}`),await Promise.race([r.goto(n,{waitUntil:"networkidle0"}),r.waitForSelector("video")]),e.log("找到 video 元素");const c=await r.$eval('video source[src^="https://www.tiktok.com/aweme/v1/play/"]',d=>d.getAttribute("src"));return e.log(`取得 video url: ${c}`),await g.close(),e.log("關閉 puppeteer"),await m(c,t,{"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",Referer:"https://www.tiktok.com/"}),e.log(`下載完成 ${c}`),[null,!0]}catch(e){return[e,null]}},S=({type:o})=>{switch(o){case"youtube":case"yt-short":return h;case"facebook":case"fb-reel":return y;case"vimeo":return v;case"instagram":case"ig":case"ig-reel":return $;case"tiktok":case"tiktok-reel":return k;default:throw new Error(`Invalid video type: ${o}`)}};exports.useDownloader=S;exports.useFacebook=y;exports.useInstagram=$;exports.useTiktok=k;exports.useVimeo=v;exports.useYoutube=h;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const l=i=>new Promise((r,t)=>{import(i).then(g=>r(g.default)).catch(t)}),f=async(i,r,t)=>{const g=await l("fs"),e=await l("axios");return console.log(`Downloading ${i} to ${r}`),new Promise((w,s)=>{e.get(i,{responseType:"stream",headers:t}).then(u=>{const p=u.data,a=g.createWriteStream(r);p.pipe(a),a.on("finish",function(){a.close(()=>{w()})})}).catch(s)})},d=(i=!1)=>({log:(...r)=>{i&&console.log(...r)},error:(...r)=>{i&&console.error(...r)}}),y=async({ytdlpPath:i,videoId:r,dest:t,debug:g=!1})=>{const e=d(g);e.log(`Downloading video: ${r} to: ${t}`);const w=await l("fs"),s=await l("path"),{exec:u}=await l("child_process"),p=(a="")=>new Promise((c,o)=>{const n=`${i} -f b -g https://www.youtube.com/watch?v=${a}`;e.log(`[yt-dlp] 執行命令: ${n}`),u(n,(m,h,$)=>{if(e.log(`[yt-dlp] stdout: ${h}`),!h.includes("https://")){c("");return}c(h)})});return w.existsSync(s.dirname(t))||(w.mkdirSync(s.dirname(t),{recursive:!0},777),e.log(`建立 ${s.dirname(t)}`)),new Promise(async(a,c)=>{try{const o=await p(r);if(!o)throw new Error("無法取得 videoUrl");e.log(`取得 videoUrl ${o}`),await f(o,t).catch(n=>{throw e.error(`下載失敗: ${n.message}`),n}),e.log(`下載完成 ${t}`),a([null,!0])}catch(o){e.error(`下載失敗: ${o.message}`),a([o,null])}})},k=async({videoId:i,dest:r,debug:t=!1,headless:g=!0})=>{try{const e=d(t);e.log(`Downloading video: ${i} to: ${r}`);const w=`https://m.facebook.com/watch/?v=${i}`,s=await l("fs"),u=await l("path"),p=await l("puppeteer");s.existsSync(u.dirname(r))||(s.mkdirSync(u.dirname(r),{recursive:!0},777),e.log(`建立 ${u.dirname(r)}`));const a={};g||(a.headless=!1);const c=await p.launch(a);e.log("啟動 puppeteer");const o=await c.newPage();await o.setUserAgent("'Mozilla/5.0 (iPhone; CPU iPhone OS 16_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.6 Mobile/15E148 Safari/604.1'"),e.log("開新分頁"),e.log(`前往 ${w}`),await Promise.race([o.goto(w,{waitUntil:"networkidle0"}),o.waitForSelector("video")]),e.log("找到 video 元素");const n=await o.$eval("video",m=>m.getAttribute("src"));return e.log(`取得 video url: ${n}`),await c.close(),e.log("關閉 puppeteer"),await f(n,r),e.log(`下載完成 ${n}`),[null,!0]}catch(e){return[e,null]}},v=async({ytdlpPath:i,videoId:r,dest:t,debug:g=!1})=>{const e=d(g);e.log(`Downloading video: ${r} to: ${t}`);const w=await l("fs"),s=await l("path"),{exec:u}=await l("child_process"),p=(a="")=>new Promise((c,o)=>{const n=`${i} -f b -g https://vimeo.com/${a}`;console.log(`執行命令: ${n}`),u(n,(m,h,$)=>{if(e.log(`yt-dlp stdout: ${h}`),!h.includes("https://")){c("");return}c(h)})});return w.existsSync(s.dirname(t))||(w.mkdirSync(s.dirname(t),{recursive:!0},777),e.log(`建立 ${s.dirname(t)}`)),new Promise(async(a,c)=>{try{const o=await p(r);if(!o)throw new Error("無法取得 videoUrl");e.log(`取得 videoUrl ${o}`),await f(o,t).catch(n=>{throw e.error(`下載失敗: ${n.message}`),n}),e.log(`下載完成 ${t}`),a([null,!0])}catch(o){e.error(`下載失敗: ${o.message}`),a([o,null])}})},b=async({ytdlpPath:i,videoId:r,dest:t,debug:g=!1})=>{const e=d(g);e.log(`Downloading video: ${r} to: ${t}`);const w=await l("fs"),s=await l("path"),{exec:u}=await l("child_process"),p=(a="")=>new Promise((c,o)=>{const n=`https://www.instagram.com/reel/${a}/`,m=`${i} -f b -g ${n}`;console.log(`執行命令: ${m}`),u(m,(h,$,P)=>{if(e.log(`yt-dlp stdout: ${$}`),!$.includes("https://")){c("");return}c($)})});return w.existsSync(s.dirname(t))||(w.mkdirSync(s.dirname(t),{recursive:!0},777),e.log(`建立 ${s.dirname(t)}`)),new Promise(async(a,c)=>{try{const o=await p(r);if(!o)throw new Error("無法取得 videoUrl");e.log(`取得 videoUrl ${o}`),await f(o,t).catch(n=>{throw e.error(`下載失敗: ${n.message}`),n}),e.log(`下載完成 ${t}`),a([null,!0])}catch(o){e.error(`下載失敗: ${o.message}`),a([o,null])}})},S=async({videoLink:i,dest:r,debug:t=!1,headless:g=!0})=>{try{const e=d(t);e.log(`Downloading video: ${i} to: ${r}`);const w=i,s=await l("fs"),u=await l("path"),p=await l("puppeteer");s.existsSync(u.dirname(r))||(s.mkdirSync(u.dirname(r),{recursive:!0},777),e.log(`建立 ${u.dirname(r)}`));const a={};g||(a.headless=!1);const c=await p.launch(a);e.log("啟動 puppeteer");const o=await c.newPage();e.log("開新分頁"),e.log(`前往 ${w}`),await Promise.race([o.goto(w,{waitUntil:"networkidle0"}),o.waitForSelector("video")]),e.log("找到 video 元素");const n=await o.$eval('video source[src^="https://www.tiktok.com/aweme/v1/play/"]',m=>m.getAttribute("src"));return e.log(`取得 video url: ${n}`),await c.close(),e.log("關閉 puppeteer"),await f(n,r,{"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",Referer:"https://www.tiktok.com/"}),e.log(`下載完成 ${n}`),[null,!0]}catch(e){return[e,null]}},U=({type:i})=>{switch(i){case"youtube":case"yt-short":return y;case"facebook":case"fb-reel":return k;case"vimeo":return v;case"instagram":case"ig":case"ig-reel":return b;case"tiktok":case"tiktok-reel":return S;default:throw new Error(`Invalid video type: ${i}`)}};exports.useDownloader=U;exports.useFacebook=k;exports.useInstagram=b;exports.useTiktok=S;exports.useVimeo=v;exports.useYoutube=y;
package/dist/video-dl.js CHANGED
@@ -1,219 +1,206 @@
1
- const l = (o) => new Promise((t, u) => {
2
- import(o).then((i) => t(i.default)).catch(u);
3
- }), m = async (o, t, u) => {
4
- const i = await l("fs"), e = await l("axios");
5
- return console.log(`Downloading ${o} to ${t}`), new Promise((n, w) => {
6
- e.get(o, {
1
+ const l = (i) => new Promise((r, t) => {
2
+ import(i).then((u) => r(u.default)).catch(t);
3
+ }), f = async (i, r, t) => {
4
+ const u = await l("fs"), e = await l("axios");
5
+ return console.log(`Downloading ${i} to ${r}`), new Promise((w, c) => {
6
+ e.get(i, {
7
7
  responseType: "stream",
8
- headers: u
9
- }).then((s) => {
10
- const p = s.data, a = i.createWriteStream(t);
8
+ headers: t
9
+ }).then((g) => {
10
+ const p = g.data, a = u.createWriteStream(r);
11
11
  p.pipe(a), a.on("finish", function() {
12
12
  a.close(() => {
13
- n();
13
+ w();
14
14
  });
15
15
  });
16
- }).catch(w);
16
+ }).catch(c);
17
17
  });
18
- }, f = (o = !1) => ({
19
- log: (...t) => {
20
- o && console.log(...t);
18
+ }, d = (i = !1) => ({
19
+ log: (...r) => {
20
+ i && console.log(...r);
21
21
  },
22
- error: (...t) => {
23
- o && console.error(...t);
22
+ error: (...r) => {
23
+ i && console.error(...r);
24
24
  }
25
- }), h = async ({
26
- videoId: o,
25
+ }), y = async ({
26
+ ytdlpPath: i,
27
+ videoId: r,
27
28
  dest: t,
28
29
  debug: u = !1
29
30
  }) => {
30
- f(u).log(`Downloading video: ${o} to: ${t}`);
31
- const e = await l("fs"), n = await l("path"), w = await l("youtubei.js");
32
- e.existsSync(n.dirname(t)) || e.mkdirSync(n.dirname(t), { recursive: !0 }, 777);
33
- const s = await w.create({
34
- cache: !1,
35
- generate_session_locally: !0
36
- });
37
- try {
38
- const p = await s.download(o, {
39
- type: "video+audio",
40
- quality: "best",
41
- format: "mp4",
42
- client: "WEB"
43
- }), a = e.createWriteStream(t);
44
- for await (const g of y(p))
45
- a.write(g);
46
- return [null, !0];
47
- } catch (p) {
48
- return console.log("error", p), [p, null];
49
- }
50
- };
51
- async function* y(o) {
52
- const t = o.getReader();
53
- try {
54
- for (; ; ) {
55
- const { done: u, value: i } = await t.read();
56
- if (u)
31
+ const e = d(u);
32
+ e.log(`Downloading video: ${r} to: ${t}`);
33
+ const w = await l("fs"), c = await l("path"), { exec: g } = await l("child_process"), p = (a = "") => new Promise((s, o) => {
34
+ const n = `${i} -f b -g https://www.youtube.com/watch?v=${a}`;
35
+ e.log(`[yt-dlp] 執行命令: ${n}`), g(n, (m, h, $) => {
36
+ if (e.log(`[yt-dlp] stdout: ${h}`), !h.includes("https://")) {
37
+ s("");
57
38
  return;
58
- yield i;
39
+ }
40
+ s(h);
41
+ });
42
+ });
43
+ return w.existsSync(c.dirname(t)) || (w.mkdirSync(c.dirname(t), { recursive: !0 }, 777), e.log(`建立 ${c.dirname(t)}`)), new Promise(async (a, s) => {
44
+ try {
45
+ const o = await p(r);
46
+ if (!o)
47
+ throw new Error("無法取得 videoUrl");
48
+ e.log(`取得 videoUrl ${o}`), await f(o, t).catch((n) => {
49
+ throw e.error(`下載失敗: ${n.message}`), n;
50
+ }), e.log(`下載完成 ${t}`), a([null, !0]);
51
+ } catch (o) {
52
+ e.error(`下載失敗: ${o.message}`), a([o, null]);
59
53
  }
60
- } finally {
61
- t.releaseLock();
62
- }
63
- }
64
- const $ = async ({
65
- videoId: o,
66
- dest: t,
67
- debug: u = !1,
68
- headless: i = !0
54
+ });
55
+ }, k = async ({
56
+ videoId: i,
57
+ dest: r,
58
+ debug: t = !1,
59
+ headless: u = !0
69
60
  }) => {
70
61
  try {
71
- const e = f(u);
72
- e.log(`Downloading video: ${o} to: ${t}`);
73
- const n = `https://m.facebook.com/watch/?v=${o}`, w = await l("fs"), s = await l("path"), p = await l("puppeteer");
74
- w.existsSync(s.dirname(t)) || (w.mkdirSync(s.dirname(t), { recursive: !0 }, 777), e.log(`建立 ${s.dirname(t)}`));
62
+ const e = d(t);
63
+ e.log(`Downloading video: ${i} to: ${r}`);
64
+ const w = `https://m.facebook.com/watch/?v=${i}`, c = await l("fs"), g = await l("path"), p = await l("puppeteer");
65
+ c.existsSync(g.dirname(r)) || (c.mkdirSync(g.dirname(r), { recursive: !0 }, 777), e.log(`建立 ${g.dirname(r)}`));
75
66
  const a = {};
76
- i || (a.headless = !1);
77
- const g = await p.launch(a);
67
+ u || (a.headless = !1);
68
+ const s = await p.launch(a);
78
69
  e.log("啟動 puppeteer");
79
- const r = await g.newPage();
80
- await r.setUserAgent(
70
+ const o = await s.newPage();
71
+ await o.setUserAgent(
81
72
  "'Mozilla/5.0 (iPhone; CPU iPhone OS 16_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.6 Mobile/15E148 Safari/604.1'"
82
- ), e.log("開新分頁"), e.log(`前往 ${n}`), await Promise.race([
83
- r.goto(n, {
73
+ ), e.log("開新分頁"), e.log(`前往 ${w}`), await Promise.race([
74
+ o.goto(w, {
84
75
  waitUntil: "networkidle0"
85
76
  }),
86
- r.waitForSelector("video")
77
+ o.waitForSelector("video")
87
78
  ]), e.log("找到 video 元素");
88
- const c = await r.$eval("video", (d) => d.getAttribute("src"));
89
- return e.log(`取得 video url: ${c}`), await g.close(), e.log("關閉 puppeteer"), await m(c, t), e.log(`下載完成 ${c}`), [null, !0];
79
+ const n = await o.$eval("video", (m) => m.getAttribute("src"));
80
+ return e.log(`取得 video url: ${n}`), await s.close(), e.log("關閉 puppeteer"), await f(n, r), e.log(`下載完成 ${n}`), [null, !0];
90
81
  } catch (e) {
91
82
  return [e, null];
92
83
  }
93
84
  }, v = async ({
94
- videoId: o,
85
+ ytdlpPath: i,
86
+ videoId: r,
95
87
  dest: t,
96
88
  debug: u = !1
97
89
  }) => {
98
- const i = f(u);
99
- i.log(`Downloading video: ${o} to: ${t}`);
100
- const e = await l("fs"), n = await l("path"), w = await l("axios"), s = async () => {
101
- const a = await w.get("https://vimeo.com/_next/viewer", {
102
- responseType: "json",
103
- headers: {
104
- "X-Requested-With": "XMLHttpRequest"
105
- }
106
- }), { jwt: g } = a.data;
107
- return g || "";
108
- }, p = async (a = "", g = "") => {
109
- const r = await w.get(`https://api.vimeo.com/videos/${a}?&fields=download`, {
110
- responseType: "json",
111
- headers: {
112
- "X-Requested-With": "XMLHttpRequest",
113
- Authorization: `jwt ${g}`
90
+ const e = d(u);
91
+ e.log(`Downloading video: ${r} to: ${t}`);
92
+ const w = await l("fs"), c = await l("path"), { exec: g } = await l("child_process"), p = (a = "") => new Promise((s, o) => {
93
+ const n = `${i} -f b -g https://vimeo.com/${a}`;
94
+ console.log(`執行命令: ${n}`), g(n, (m, h, $) => {
95
+ if (e.log(`yt-dlp stdout: ${h}`), !h.includes("https://")) {
96
+ s("");
97
+ return;
114
98
  }
115
- }), { download: c = [] } = r.data, d = c[0] || null;
116
- return (d == null ? void 0 : d.link) || "";
117
- };
118
- return e.existsSync(n.dirname(t)) || (e.mkdirSync(n.dirname(t), { recursive: !0 }, 777), i.log(`建立 ${n.dirname(t)}`)), new Promise(async (a, g) => {
99
+ s(h);
100
+ });
101
+ });
102
+ return w.existsSync(c.dirname(t)) || (w.mkdirSync(c.dirname(t), { recursive: !0 }, 777), e.log(`建立 ${c.dirname(t)}`)), new Promise(async (a, s) => {
119
103
  try {
120
- const r = await s();
121
- i.log(`取得 jwt ${r}`);
122
- const c = await p(o, r);
123
- i.log(`取得 videoUrl ${c}`), await m(c, t), i.log(`下載完成 ${t}`), a([null, !0]);
124
- } catch (r) {
125
- a([r, null]);
104
+ const o = await p(r);
105
+ if (!o)
106
+ throw new Error("無法取得 videoUrl");
107
+ e.log(`取得 videoUrl ${o}`), await f(o, t).catch((n) => {
108
+ throw e.error(`下載失敗: ${n.message}`), n;
109
+ }), e.log(`下載完成 ${t}`), a([null, !0]);
110
+ } catch (o) {
111
+ e.error(`下載失敗: ${o.message}`), a([o, null]);
126
112
  }
127
113
  });
128
- }, k = async ({
129
- videoId: o,
130
- dest: t,
131
- debug: u = !1,
132
- headless: i = !0
133
- }) => {
134
- try {
135
- const e = f(u);
136
- e.log(`Downloading video: ${o} to: ${t}`);
137
- const n = `https://www.instagram.com/reel/${o}/`, w = await l("fs"), s = await l("path"), p = await l("puppeteer");
138
- w.existsSync(s.dirname(t)) || (w.mkdirSync(s.dirname(t), { recursive: !0 }, 777), e.log(`建立 ${s.dirname(t)}`));
139
- const a = {};
140
- i || (a.headless = !1);
141
- const g = await p.launch(a);
142
- e.log("啟動 puppeteer");
143
- const r = await g.newPage();
144
- await r.setUserAgent(
145
- "'Mozilla/5.0 (iPhone; CPU iPhone OS 16_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.6 Mobile/15E148 Safari/604.1'"
146
- ), e.log("開新分頁"), e.log(`前往 ${n}`), await Promise.race([
147
- r.goto(n, {
148
- waitUntil: "networkidle0"
149
- }),
150
- r.waitForSelector("video")
151
- ]), e.log("找到 video 元素");
152
- const c = await r.$eval("video", (d) => d.getAttribute("src"));
153
- return e.log(`取得 video url: ${c}`), await g.close(), e.log("關閉 puppeteer"), await m(c, t), e.log(`下載完成 ${c}`), [null, !0];
154
- } catch (e) {
155
- return [e, null];
156
- }
157
114
  }, S = async ({
158
- videoLink: o,
115
+ ytdlpPath: i,
116
+ videoId: r,
159
117
  dest: t,
160
- debug: u = !1,
161
- headless: i = !0
118
+ debug: u = !1
119
+ }) => {
120
+ const e = d(u);
121
+ e.log(`Downloading video: ${r} to: ${t}`);
122
+ const w = await l("fs"), c = await l("path"), { exec: g } = await l("child_process"), p = (a = "") => new Promise((s, o) => {
123
+ const n = `https://www.instagram.com/reel/${a}/`, m = `${i} -f b -g ${n}`;
124
+ console.log(`執行命令: ${m}`), g(m, (h, $, b) => {
125
+ if (e.log(`yt-dlp stdout: ${$}`), !$.includes("https://")) {
126
+ s("");
127
+ return;
128
+ }
129
+ s($);
130
+ });
131
+ });
132
+ return w.existsSync(c.dirname(t)) || (w.mkdirSync(c.dirname(t), { recursive: !0 }, 777), e.log(`建立 ${c.dirname(t)}`)), new Promise(async (a, s) => {
133
+ try {
134
+ const o = await p(r);
135
+ if (!o)
136
+ throw new Error("無法取得 videoUrl");
137
+ e.log(`取得 videoUrl ${o}`), await f(o, t).catch((n) => {
138
+ throw e.error(`下載失敗: ${n.message}`), n;
139
+ }), e.log(`下載完成 ${t}`), a([null, !0]);
140
+ } catch (o) {
141
+ e.error(`下載失敗: ${o.message}`), a([o, null]);
142
+ }
143
+ });
144
+ }, U = async ({
145
+ videoLink: i,
146
+ dest: r,
147
+ debug: t = !1,
148
+ headless: u = !0
162
149
  }) => {
163
150
  try {
164
- const e = f(u);
165
- e.log(`Downloading video: ${o} to: ${t}`);
166
- const n = o, w = await l("fs"), s = await l("path"), p = await l("puppeteer");
167
- w.existsSync(s.dirname(t)) || (w.mkdirSync(s.dirname(t), { recursive: !0 }, 777), e.log(`建立 ${s.dirname(t)}`));
151
+ const e = d(t);
152
+ e.log(`Downloading video: ${i} to: ${r}`);
153
+ const w = i, c = await l("fs"), g = await l("path"), p = await l("puppeteer");
154
+ c.existsSync(g.dirname(r)) || (c.mkdirSync(g.dirname(r), { recursive: !0 }, 777), e.log(`建立 ${g.dirname(r)}`));
168
155
  const a = {};
169
- i || (a.headless = !1);
170
- const g = await p.launch(a);
156
+ u || (a.headless = !1);
157
+ const s = await p.launch(a);
171
158
  e.log("啟動 puppeteer");
172
- const r = await g.newPage();
173
- e.log("開新分頁"), e.log(`前往 ${n}`), await Promise.race([
174
- r.goto(n, {
159
+ const o = await s.newPage();
160
+ e.log("開新分頁"), e.log(`前往 ${w}`), await Promise.race([
161
+ o.goto(w, {
175
162
  waitUntil: "networkidle0"
176
163
  }),
177
- r.waitForSelector("video")
164
+ o.waitForSelector("video")
178
165
  ]), e.log("找到 video 元素");
179
- const c = await r.$eval(
166
+ const n = await o.$eval(
180
167
  'video source[src^="https://www.tiktok.com/aweme/v1/play/"]',
181
- (d) => d.getAttribute("src")
168
+ (m) => m.getAttribute("src")
182
169
  );
183
- return e.log(`取得 video url: ${c}`), await g.close(), e.log("關閉 puppeteer"), await m(c, t, {
170
+ return e.log(`取得 video url: ${n}`), await s.close(), e.log("關閉 puppeteer"), await f(n, r, {
184
171
  "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
185
172
  Referer: "https://www.tiktok.com/"
186
173
  // 設定 Referer
187
- }), e.log(`下載完成 ${c}`), [null, !0];
174
+ }), e.log(`下載完成 ${n}`), [null, !0];
188
175
  } catch (e) {
189
176
  return [e, null];
190
177
  }
191
- }, b = ({ type: o }) => {
192
- switch (o) {
178
+ }, P = ({ type: i }) => {
179
+ switch (i) {
193
180
  case "youtube":
194
181
  case "yt-short":
195
- return h;
182
+ return y;
196
183
  case "facebook":
197
184
  case "fb-reel":
198
- return $;
185
+ return k;
199
186
  case "vimeo":
200
187
  return v;
201
188
  case "instagram":
202
189
  case "ig":
203
190
  case "ig-reel":
204
- return k;
191
+ return S;
205
192
  case "tiktok":
206
193
  case "tiktok-reel":
207
- return S;
194
+ return U;
208
195
  default:
209
- throw new Error(`Invalid video type: ${o}`);
196
+ throw new Error(`Invalid video type: ${i}`);
210
197
  }
211
198
  };
212
199
  export {
213
- b as useDownloader,
214
- $ as useFacebook,
215
- k as useInstagram,
216
- S as useTiktok,
200
+ P as useDownloader,
201
+ k as useFacebook,
202
+ S as useInstagram,
203
+ U as useTiktok,
217
204
  v as useVimeo,
218
- h as useYoutube
205
+ y as useYoutube
219
206
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@pieda/video-dl",
3
3
  "private": false,
4
- "version": "1.7.0",
4
+ "version": "1.9.0",
5
5
  "type": "module",
6
6
  "main": "dist/video-dl.cjs",
7
7
  "module": "dist/video-dl.js",