UNPKG

ytdlp-nodejs

Version:

A TypeScript wrapper for the yt-dlp executable

3 lines (2 loc) 2.44 kB
"use strict";var x=Object.create;var g=Object.defineProperty;var v=Object.getOwnPropertyDescriptor;var S=Object.getOwnPropertyNames;var k=Object.getPrototypeOf,F=Object.prototype.hasOwnProperty;var P=(e,t,r,s)=>{if(t&&typeof t=="object"||typeof t=="function")for(let i of S(t))!F.call(e,i)&&i!==r&&g(e,i,{get:()=>t[i],enumerable:!(s=v(t,i))||s.enumerable});return e};var l=(e,t,r)=>(r=e!=null?x(k(e)):{},P(t||!e||!e.__esModule?g(r,"default",{value:e,enumerable:!0}):r,e));var f=l(require("fs")),b=l(require("path"));var A=l(require("https")),D=l(require("http")),d=require("url"),n=l(require("fs"));function w(e,t={}){return new Promise((r,s)=>{let o=(new d.URL(e).protocol==="https:"?A:D).get(e,t,a=>{if(a.statusCode>=300&&a.statusCode<400&&a.headers.location){let c=new d.URL(a.headers.location,e).toString();w(c,t).then(r).catch(s);return}r(a)});o.on("error",s),o.setTimeout(3e4,()=>{o.destroy(),s(new Error("Request timed out"))})})}async function h(e,t){try{let r=n.createWriteStream(t),s=await w(e);if(s.statusCode!==200)throw r.close(),n.unlinkSync(t),new Error(`Failed to download file: ${s.statusCode} ${s.statusMessage}`);let i=parseInt(s.headers["content-length"]||"0",10),m=0;return s.on("data",o=>{m+=o.length;let a=m/i*100;process.stdout.write(`Progress: ${Math.round(a)}%\r`)}),s.pipe(r),new Promise((o,a)=>{r.on("finish",()=>{r.close(),console.log(` Download complete!`),o()}),r.on("error",c=>{r.close(),n.unlinkSync(t),a(c)}),s.on("error",c=>{r.close(),n.unlinkSync(t),a(c)})})}catch(r){throw n.existsSync(t)&&n.unlinkSync(t),r}}var y=l(require("path"));var u=y.join(__dirname,"..","bin");var O="https://github.com/yt-dlp/yt-dlp/releases/latest/download",p={win32:{x64:"yt-dlp.exe",ia32:"yt-dlp_x86.exe"},linux:{x64:"yt-dlp",armv7l:"yt-dlp_linux_armv7l",aarch64:"yt-dlp_linux_aarch64"},darwin:{x64:"dlp_macos",arm64:"dlp_macos"}};function z(){let e=process.platform,t=process.arch;if(!p[e]||!p[e][t])throw new Error(`No FFmpeg build available for ${e} ${t}`);return p[e][t]}async function T(){let e=z(),t=`${O}/${e}`,r=b.join(u,e);if(f.existsSync(r))return r;console.log("Downloading yt-dlp...",t),f.existsSync(u)||f.mkdirSync(u,{recursive:!0});try{await h(t,r),console.log(`yt-dlp downloaded successfully to: ${r}`);try{f.chmodSync(r,493)}catch{console.log("Error while chmod")}return r}catch(i){throw console.error(`Download failed: ${i}`),i}}T().catch(e=>{console.error("Failed to download yt-dlp:",e),process.exit(1)});