Introduction
YouTube is the world’s largest video-sharing platform, home to millions of videos. Sometimes, you may want to download these videos for offline viewing or other purposes. In this tutorial, we will demonstrate how to create a simple YouTube video downloader using Node.js and the ytdl-core package.
Prerequisites
- Basic knowledge of JavaScript and Node.js.
- Node.js installed on your computer. You can download it from the official Node.js website.
Setting Up the Project
First, we will create a new directory for our project and initialize a new Node.js application.
mkdir youtube-downloader
cd youtube-downloader
npm init -y
Next, we need to install the ytdl-core package. Run the following command:
npm install ytdl-core
Creating the Downloader
Now, let’s create a file named download.js:
touch download.js
Open download.js in your favorite code editor and add the following code:
const ytdl = require('ytdl-core');
const fs = require('fs');
// Replace with the youtube video URL you want to download
const videoURL = 'ENTER_YOUTUBE_VIDEO_ID_OR_URL_HERE';
// Get video info from YouTube
ytdl.getInfo(videoURL).then((info) => {
// Select video format (change quality here if needed)
const format = ytdl.chooseFormat(info.formats, { quality: '248' });
// Create a write stream to save the video file
const outputFilePath = `${info.videoDetails.title}.${format.container}`;
const outputStream = fs.createWriteStream(outputFilePath);
// Download the video file
ytdl.downloadFromInfo(info, { format: format }).pipe(outputStream);
// When the download is complete, show a message
outputStream.on('finish', () => {
console.log(`Finished downloading: ${outputFilePath}`);
});
}).catch(err => {
console.error(err);
});
In this code, you replace the placeholder ENTER_YOUTUBE_VIDEO_ID_OR_URL_HERE with the actual YouTube video ID or URL you wish to download. The script will download the video in whichever quality you specify through the `itag` code (here it is set for a 1080p video, itag 248, which is VP9 codec).
Understanding the Code
1. Importing Required Modules:
We use require to load the ytdl-core and fs (file system) modules. The fs module is needed to handle our output file streams.
2. Getting Video Info:
The ytdl.getInfo function fetches metadata about the specified YouTube video, giving us access to properties such as format, title, etc.
3. Selecting Format:
Using the ytdl.chooseFormat method, we can select the desired video format from the available options.
4. Saving the Video:
We create a writable stream to a file, naming it after the video’s title and extension based on the format we chose. The video is then downloaded and piped into this writable stream.
5. Finishing Up:
Finally, we listen for the finish event on our writable stream to confirm that the download is complete.
Running the Downloader
To run your downloader, execute the following command in your terminal:
npm start download.js
You should see a message indicating that the download is complete.
Conclusion
In this blog post, we have learned how to use Node.js and the ytdl-core module to create a basic YouTube video downloader. This is just the tip of the iceberg, as the ytdl-core package has many more features and options, such as downloading audio only or specific formats.
Feel free to explore the ytdl-core documentation to learn more and expand upon this simple project!
