Welcome folks today in this blog post we will be using the youtube-dl
library to extract youtube video information
such as title and description in browser using javascript. All the full source code of the application is shown below.
Get Started
In order to get started you need to initialize a new node.js
project using the below command as shown below
npm init -y
npm i express
Now we need to make the index.js
file and copy paste the following code
index.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
const express = require('express'); const bodyParser = require('body-parser'); const {exec} = require('child_process'); const app = express(); app.use(bodyParser.urlencoded({ extended: false })) app.use(bodyParser.json()) app.get('/', (req, res) => { res.sendFile(__dirname + "/index.html") }) app.listen(5000, () => { console.log('listening on port 5000') }) |
As you can see we are importing the express
library and then we are applying the bodyparser
middleware and then we are loading the index.html
file when user goes to the /
home route.
index.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Advanced Youtube Video Thumbnail Downloader in Node.js & Express</title> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"> </head> <body> <div class="container"> <h1 class="text-center"> Extract Information About Youtube Video </h1> <form action="/fetchdata" method="post"> <div class="form-group"> <input type="text" class="form-control" name="url" placeholder="Enter Youtube Video URL" required id=""> </div> <div class="form-group"> <button class="btn btn-danger btn-block"> Extract Information </button> </div> </form> </div> </body> </html> |
As you can see we are importing the bootstrap
library cdn and then we have the simple html5 form
where we allow the user to enter the youtube video url
and then we have the button to extract the video information. Here we are making the post
request to the /fetchinfo
route. Now we need to define this route inside the index.js
file as shown below
index.js
1 2 3 4 5 6 7 8 9 10 11 12 13 |
app.post('/fetchdata', (req, res) => { var url = req.body.url exec(`youtube-dl --skip-download --write-info-json ${url}`, (err, stdout, stderr) => { if (err) { console.log(err) } else { console.log(stdout) } }) }) |
As you can see we are getting the url
of the youtube video and then we are executing the command using the exec()
method and inside that we are using the youtube-dl
library to extract the information about the video and save it in json
file.