const ytdl = require('ytdl'); function downloadVideo(videoURL) { // Extract the video ID from the URL const videoID = ytdl.getURLVideoID(videoURL); // Get the video information ytdl.getInfo(videoID, (info) => { // Get the video title const videoTitle = info.videoDetails.title; // Create a filename with the video title and MP4 extension const filename = `${videoTitle}.mp4`; // Start downloading the video ytdl(videoURL, { format: 'mp4' }) .pipe(fs.createWriteStream(filename)) .on('progress', (progress) => { console.log(`Downloading... ${progress.percent}%`); }) .on('error', (error) => { console.error(`Error downloading video: ${error}`); }) .on('end', () => { console.log(`Video downloaded successfully: ${filename}`); }); }); } downloadVideo('https://www.youtube.com/watch?v=VIDEO_ID');

Comments