Build a Video Converter Application in Node.js Using Fluent-FFmpeg Library

Hello, friend today we are going to see clearly what we are going to see in the article Build a Video Converter Application in Node.js Using Fluent-FFmpeg Library. We hope you find this article very useful.

Build a Video Converter Application in Node.js Using Fluent-FFmpeg Library

Build a Video Converter Application in Node.js Using Fluent-FFmpeg Library

const express = require('express')
const app = express()

app.get("/",(req,res) => {

res.send("Hello world")

})
app.listen(5000,() =>{
console.log("App is listening on Port 5000")
})
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Online Video Converter</title>
    <link
      rel="stylesheet"
      href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"
    />
  </head>
  <body>
    <div class="container">
      <br /><br />
      <h1 style="text-align: center;">Online Video Converter</h1>
      <form action="/convert" method="post" enctype="multipart/form-data">
        <div class="form-group">
          <input type="file" name="file"/>
        </div>
        <div class="form-group">
          <label for="to">To:</label>
          <select class="form-control" name="to">
            <option>mp4</option>
            <option>flv</option>
            <option>avi</option>
            <option>webm</option>
            <option>mov</option>
          </select>
        </div>
        <br />
        <div class="form-group">
          <button class="btn btn-danger btn-block">
            Convert
          </button>
        </div>
      </form>
    </div>
  </body>
</html>
const express = require('express')
 const app = express() 

app.get("/",(req,res) => {
 res.sendFile(__dirname + "/index.html");
 })

 app.listen(5000,() =>{
 console.log("App is listening on Port 5000")
 })
const express = require("express");

const ffmpeg = require("fluent-ffmpeg");

const app = express();

ffmpeg.setFfmpegPath("C:/ffmpeg/bin/ffmpeg.exe");

ffmpeg.setFfprobePath("C:/ffmpeg/bin");

ffmpeg.setFlvtoolPath("C:/flvtool");

console.log(ffmpeg);

app.get("/", (req, res) => {
  res.sendFile(__dirname + "/index.html");
});

app.listen(5000);


const app = express();

// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }));

// parse application/json
app.use(bodyParser.json());
const fileUpload = require("express-fileupload");

const app = express();

app.use(
  fileUpload({
    useTempFiles: true,
    tempFileDir: "/tmp/",
  })
);
app.post("/convert", (req, res) => {

  let to = req.body.to;
  let file = req.files.file;
  let fileName = `output.${to}`;
  console.log(to);
  console.log(file);

  file.mv("tmp/" + file.name, function (err) {
    if (err) return res.sendStatus(500).send(err);
    console.log("File Uploaded successfully");
  });

  ffmpeg("tmp/" + file.name)
    .withOutputFormat(to)
    .on("end", function (stdout, stderr) {
      console.log("Finished");
      res.download(__dirname + fileName, function (err) {
        if (err) throw err;

        fs.unlink(__dirname + fileName, function (err) {
          if (err) throw err;
          console.log("File deleted");
        });
      });
      fs.unlink("tmp/" + file.name, function (err) {
        if (err) throw err;
        console.log("File deleted");
      });
    })
    .on("error", function (err) {
      console.log("an error happened: " + err.message);
      fs.unlink("tmp/" + file.name, function (err) {
        if (err) throw err;
        console.log("File deleted");
      });
    })
    .saveToFile(__dirname + fileName);
});

Read Also: Smoke.js – Javascript Library for Making Alert Boxes

Final Words

Build a Video Converter Application in Node.js Using Fluent-FFmpeg Library We hope you find the article useful and will see you in the next article.

Hi, I'm Selva a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Deekshi. Here, I post about programming to help developers.

Share on:

Leave a Comment