UNPKG

@lordxdd/yt-dl

Version:

YouTube-dl

71 lines (67 loc) 2.07 kB
const { spawn } = require("child_process"); const fs = require('fs'); const path = require("path"); const ytdlpPath = path.join(__dirname, "../bin/yt-dlp"); const cookiesPath = path.join(__dirname, "../bin/cookies.txt"); const tempPath = path.join(__dirname, "../temp"); //audio async function ytadl(vid) { const fileP = path.join(__dirname, "../temp/song.m4a"); if (fs.existsSync(fileP)) fs.unlinkSync(fileP); return new Promise((resolve, reject) => { const output = path.join(tempPath, `song.m4a`); const args = [ "-f", "bestaudio[ext=m4a]", "--cookies", cookiesPath, "-o", output, `https://www.youtube.com/watch?v=${vid}`, ]; const process = spawn(ytdlpPath, args); process.on("error", (err) => reject(`yt-dlp error: ${err.message}`)); process.stderr.on("data", (data) => { console.error(`yt-dlp error: ${data}`); }); process.on("close", (code) => { if (code === 0) { resolve(output); } else { reject(`yt-dlp failed with exit code ${code}`); } }); }); } //video async function ytvdl(vid, quality='720') { const filePath = path.join(__dirname, "../temp/video.mp4"); if (fs.existsSync(filePath)) fs.unlinkSync(filePath); return new Promise((resolve, reject) => { const output = path.join(tempPath, `video.mp4`); const args = [ "-f", `bestvideo[height<=${quality}]+bestaudio/best`, "--merge-output-format", "mp4", "--cookies", cookiesPath, "-o", output, `https://www.youtube.com/watch?v=${vid}`, ]; const process = spawn(ytdlpPath, args); process.on("close", (code) => { if (code === 0) { resolve(output); } else { reject(`yt-dlp failed with exit code ${code}`); } }); process.on("error", (err) => { console.error(`yt-dlp process failed: ${err}`); reject(`yt-dlp error: ${err.message}`); }); }); } module.exports = { ytadl, ytvdl };