npm init -y
npm i express
npm i multer
npm i ejs
index.js
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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
const express = require("express"); const multer = require("multer"); const { getVideoDurationInSeconds } = require("get-video-duration"); const app = express(); app.set("view engine", "ejs"); const bodyparser = require("body-parser"); const path = require("path"); app.use(express.static(path.join(__dirname + "uploads"))); app.use(bodyparser.urlencoded({ extended: false })); app.use(bodyparser.json()); const storage = multer.diskStorage({ destination: function (req, file, cb) { cb(null, "uploads"); }, filename: function (req, file, cb) { cb(null, Date.now() + path.extname(file.originalname)); //Appending extension }, }); const upload = multer({ storage: storage }).single("file"); const PORT = 3000; app.get("/", (req, res) => { res.render("index", { duration: "" }); }); app.post("/getduration", (req, res) => { console.log(req.body.url) getVideoDurationInSeconds(req.body.url).then((duration) => { console.log(duration); res.render("index", { duration: duration }); }); }); app.listen(PORT, () => { console.log("App is listening on port " + PORT); }); |
Now create a views/index.ejs
file and copy paste the following code
index.ejs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<!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>Get Video Duration in Seconds</title> </head> <body> <form action="/getduration" method="POST"> <input type="text" name="url" placeholder="Enter Video URL" required /> <button type="submit">Get Duration in Seconds</button> <div id="result"> <% if(duration){ %> <%=duration %> <% } %> </div> </form> </body> </html> |
Now create a uploads
directory to store all the video
files