@pieda/video-dl 1.8.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 {};
package/dist/video-dl.cjs CHANGED
@@ -1 +1 @@
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=$;
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,205 +1,206 @@
1
- const l = (r) => new Promise((o, a) => {
2
- import(r).then((u) => o(u.default)).catch(a);
3
- }), f = async (r, o, a) => {
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
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, {
5
+ return console.log(`Downloading ${i} to ${r}`), new Promise((w, c) => {
6
+ e.get(i, {
7
7
  responseType: "stream",
8
- headers: a
9
- }).then((s) => {
10
- const p = s.data, n = u.createWriteStream(o);
11
- p.pipe(n), n.on("finish", function() {
12
- n.close(() => {
8
+ headers: t
9
+ }).then((g) => {
10
+ const p = g.data, a = u.createWriteStream(r);
11
+ p.pipe(a), a.on("finish", function() {
12
+ a.close(() => {
13
13
  w();
14
14
  });
15
15
  });
16
16
  }).catch(c);
17
17
  });
18
- }, $ = (r = !1) => ({
19
- log: (...o) => {
20
- r && console.log(...o);
18
+ }, d = (i = !1) => ({
19
+ log: (...r) => {
20
+ i && console.log(...r);
21
21
  },
22
- error: (...o) => {
23
- r && console.error(...o);
22
+ error: (...r) => {
23
+ i && console.error(...r);
24
24
  }
25
- }), v = async ({
26
- ytdlpPath: r,
27
- videoId: o,
28
- dest: a,
25
+ }), y = async ({
26
+ ytdlpPath: i,
27
+ videoId: r,
28
+ dest: t,
29
29
  debug: u = !1
30
30
  }) => {
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) => {
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
36
  if (e.log(`[yt-dlp] stdout: ${h}`), !h.includes("https://")) {
37
- g("");
37
+ s("");
38
38
  return;
39
39
  }
40
- g(h);
40
+ s(h);
41
41
  });
42
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) => {
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
44
  try {
45
- const t = await p(o);
46
- if (!t)
45
+ const o = await p(r);
46
+ if (!o)
47
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]);
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]);
53
53
  }
54
54
  });
55
- }, y = async ({
56
- videoId: r,
57
- dest: o,
58
- debug: a = !1,
55
+ }, k = async ({
56
+ videoId: i,
57
+ dest: r,
58
+ debug: t = !1,
59
59
  headless: u = !0
60
60
  }) => {
61
61
  try {
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);
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)}`));
66
+ const a = {};
67
+ u || (a.headless = !1);
68
+ const s = await p.launch(a);
69
69
  e.log("啟動 puppeteer");
70
- const t = await g.newPage();
71
- await t.setUserAgent(
70
+ const o = await s.newPage();
71
+ await o.setUserAgent(
72
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'"
73
73
  ), e.log("開新分頁"), e.log(`前往 ${w}`), await Promise.race([
74
- t.goto(w, {
74
+ o.goto(w, {
75
75
  waitUntil: "networkidle0"
76
76
  }),
77
- t.waitForSelector("video")
77
+ o.waitForSelector("video")
78
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];
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];
81
81
  } catch (e) {
82
82
  return [e, null];
83
83
  }
84
- }, k = async ({
85
- ytdlpPath: r,
86
- videoId: o,
87
- dest: a,
84
+ }, v = async ({
85
+ ytdlpPath: i,
86
+ videoId: r,
87
+ dest: t,
88
88
  debug: u = !1
89
89
  }) => {
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) => {
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
95
  if (e.log(`yt-dlp stdout: ${h}`), !h.includes("https://")) {
96
- g("");
96
+ s("");
97
97
  return;
98
98
  }
99
- g(h);
99
+ s(h);
100
100
  });
101
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) => {
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) => {
103
103
  try {
104
- const t = await p(o);
105
- if (!t)
104
+ const o = await p(r);
105
+ if (!o)
106
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]);
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]);
112
112
  }
113
113
  });
114
114
  }, S = async ({
115
+ ytdlpPath: i,
115
116
  videoId: r,
116
- dest: o,
117
- debug: a = !1,
118
- headless: u = !0
117
+ dest: t,
118
+ debug: u = !1
119
119
  }) => {
120
- try {
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);
128
- e.log("啟動 puppeteer");
129
- const t = await g.newPage();
130
- await t.setUserAgent(
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'"
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];
140
- } catch (e) {
141
- return [e, null];
142
- }
143
- }, b = async ({
144
- videoLink: r,
145
- dest: o,
146
- debug: a = !1,
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,
147
148
  headless: u = !0
148
149
  }) => {
149
150
  try {
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);
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)}`));
155
+ const a = {};
156
+ u || (a.headless = !1);
157
+ const s = await p.launch(a);
157
158
  e.log("啟動 puppeteer");
158
- const t = await g.newPage();
159
+ const o = await s.newPage();
159
160
  e.log("開新分頁"), e.log(`前往 ${w}`), await Promise.race([
160
- t.goto(w, {
161
+ o.goto(w, {
161
162
  waitUntil: "networkidle0"
162
163
  }),
163
- t.waitForSelector("video")
164
+ o.waitForSelector("video")
164
165
  ]), e.log("找到 video 元素");
165
- const i = await t.$eval(
166
+ const n = await o.$eval(
166
167
  'video source[src^="https://www.tiktok.com/aweme/v1/play/"]',
167
168
  (m) => m.getAttribute("src")
168
169
  );
169
- return e.log(`取得 video url: ${i}`), await g.close(), e.log("關閉 puppeteer"), await f(i, o, {
170
+ return e.log(`取得 video url: ${n}`), await s.close(), e.log("關閉 puppeteer"), await f(n, r, {
170
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",
171
172
  Referer: "https://www.tiktok.com/"
172
173
  // 設定 Referer
173
- }), e.log(`下載完成 ${i}`), [null, !0];
174
+ }), e.log(`下載完成 ${n}`), [null, !0];
174
175
  } catch (e) {
175
176
  return [e, null];
176
177
  }
177
- }, P = ({ type: r }) => {
178
- switch (r) {
178
+ }, P = ({ type: i }) => {
179
+ switch (i) {
179
180
  case "youtube":
180
181
  case "yt-short":
181
- return v;
182
+ return y;
182
183
  case "facebook":
183
184
  case "fb-reel":
184
- return y;
185
- case "vimeo":
186
185
  return k;
186
+ case "vimeo":
187
+ return v;
187
188
  case "instagram":
188
189
  case "ig":
189
190
  case "ig-reel":
190
191
  return S;
191
192
  case "tiktok":
192
193
  case "tiktok-reel":
193
- return b;
194
+ return U;
194
195
  default:
195
- throw new Error(`Invalid video type: ${r}`);
196
+ throw new Error(`Invalid video type: ${i}`);
196
197
  }
197
198
  };
198
199
  export {
199
200
  P as useDownloader,
200
- y as useFacebook,
201
+ k as useFacebook,
201
202
  S as useInstagram,
202
- b as useTiktok,
203
- k as useVimeo,
204
- v as useYoutube
203
+ U as useTiktok,
204
+ v as useVimeo,
205
+ y as useYoutube
205
206
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@pieda/video-dl",
3
3
  "private": false,
4
- "version": "1.8.0",
4
+ "version": "1.9.0",
5
5
  "type": "module",
6
6
  "main": "dist/video-dl.cjs",
7
7
  "module": "dist/video-dl.js",