How to Create a Youtube downloader with Javascript
By: Haider Imtiaz
Tags: JavaScript, Development, YouTube, Nodejs, Programming

Youtube is the most famous video streaming website now days even 98% of people in this world are using youtube, even the android mobile has built-in youtube installed from google. I think that’s the fact youtube is the most popular product of Google, Let move to our today’s topic, Youtube Downloader in JavaScript. So we are going to walk through that how to develop a youtube downloader that downloads any youtube video or playlist on your computer, we can use any programming language for this python, C++ even java. But I love JavaScript so I will go with it, well without wasting time let’s jump into the things we need to start the coding.We need NodeJs to compile the JavaScript code, if you already had NodeJs installed that good you can move to the library installation part or not just download the latest version NodeJs and install it on your windows, Linux, or any operating system you had. Next we need a JavaScript module name youtube-dl, copy that below command and paste it in the command prompt.

npm i youtube-dl

Coding Part

Create a youtube.js file and open it in your code editor, let start with importing the library of youtube-dl in our script.

first-line we are loading the fs module in the const variable fs, that module is basically used for reading and writing video files, In the next line, we are loading our main module in const variable youtube-dl. After we loading the required modules we just use youtubedl() function in which we are passing youtube video URL, and format. Note we are setting the format number to 18 which is 320p mp4 format, if you want to set HD format or any format you loved in youtube videos visit the youtube_format_number. Let move to another part of the program is downloading the video check the code below.

If you noticed in line 4 we save the youtubedl() in the const video variable and on line 10 we used that video variable to call the function info. Next line using the javascript console.log function to printing out the youtube information on the command prompt. last we used the fs module to write our file in mp4 format, if you see we use video.pipe function and fs.createWrtierStream together to download our video. Video.pipe is just giving the video data in encoded form and the fs function will convert the video in mp4 format and saved it to your PC.

Downloading Playlist

The first part is really easy to develop, the next challenge our downloader will face will be downloading a playlist with youtube_dl. Let start developing the javascript code that downloads the whole playlist, Again we will use the same modules to check the code below.

As you saw the code we just create a function name playlist and passing the parameter “URL”, now that’s URL will be the playlist URL that we gonna pass, next we using youtubedl() function storing it in video variable, and on the next line we define an error handler using video.on() function ( which is actually youtubedl function ) in that function, if our program got any error or bug during runtime, the program will return console.log function saying error and telling the type of error occurred. From line 14 to 19 if you have seen we are using video.on() function in which we are storing the size info of the playlist in size variable and then path.join(__dirname + ‘/’, size + ‘.mp4’) will define a size.mp4 video size will replace by the size info we get from .size() and next lines we using fs stream module to output our playlist video in the directory in which our javascript file is placed. From line 21 to 31 we are actually printing something on the command line, that’s right we are making the moving bar the same like you saw in some software installation in which the bar is moving to the right telling them what percentage of installation is done, but in this case, we use it to tell how much data of the videos is downloaded. We using stdout() to make that bar which is a built-in function of javascript.

Getting Video information

We going to explore the youtube_dl library a little more, let develop a javascript code that will print out the information of video on the command prompt.

First thing first requires (“youtube_dl”) loading the library in our js file next line we use optional arguments ( you can ignore that if you don't want ) this argument hold your login details in the youtube. next line new function of youtube_dl we are using the getInfo() that function takes URL, option argument, a function which indicating an error message, if(err) throw err that javascript condiction in which we are checking that if the error occurs simply throw it and stop the program otherwise move to the if condition body which using console.log function to printing our the video information.

Using Proxies:

Some time a youtube video is limited to specific countries, so for bypassing that, we will use the optional argument of proxies in youtubedl() function, just pass the proxy and port number as an argument with the youtube video URL. You will be more if you check the below code.

Downloading Subtitles

Subtitles are very useful when you are not a native speaker of that particular language, youtube videos have multiple subtitles edit by video uploader, that’s not good that we download just the video, not with its subtitles, well the youtube_dl modules have some function for downloading the video subtitles.

let breakdown the code and understand what is happening in it, as usual, the first step to load the module in the Js file next we create a URL const variable passing a youtube video URL in it, and then we made options variable that holds the optional arguments for the youtubedl() function and if you notice I added comment after every argument in it and if you check format is ttml you can set it vtt, both ar ethe format of subtitles files next we can define the dirname ( you can add directory path here) and in the end, we call the youtubedl() function getsubs() passing all the arguments.

That its, We just create an awesome javascript youtube downloader, you can use that code in a web app or any javascript-based app or on your website also. I hope this article will help you someday. Feel free to share your opinion.