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