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