Node.js Express Mp4 to Mp3 Converter FFMPEG App Hosted on Heroku | Installing FFMPEG Library in Heroku Tutorial 2020

Node.js Express Mp4 to Mp3 Converter FFMPEG App Hosted on Heroku | Installing FFMPEG Library in Heroku Tutorial 2020

 

 

 

 

 

 

Live Demo

 

The Live demo of the app can be seen at this link here

 

 

Adding FFMPEG inside Heroku

 

 

In order to add FFMPEG Library inside your heroku app. Go to the app settings as shown below and add a buildpack

 

 

 

 

 

 

 

 

 

 

https://github.com/jonathanong/heroku-buildpack-ffmpeg-latest.git

 

 

 

And after that ffmpeg is successfully installed once you deploy our node.js app to heroku. Now to make the node.js app you will require these dependencies which are listed below

 

 

npm i express

 

npm i multer

 

 

Now make the index.js file and copy paste the following code to it

 

 

const express = require('express')

const fs = require('fs')

const { exec } = require('child_process')

const path = require('path')

const multer = require('multer')

const bodyParser = require('body-parser')

const app = express()

var dir = 'public';
var subDirectory = 'public/uploads'

if (!fs.existsSync(dir)){
    fs.mkdirSync(dir);

    fs.mkdirSync(subDirectory)

}

var storage = multer.diskStorage({
    destination: function (req, file, cb) {
      cb(null, 'public/uploads')
    },
    filename: function (req, file, cb) {
      cb(null, file.fieldname + '-' + Date.now() + path.extname(file.originalname))
    }
})

var upload = multer({storage:storage})

app.use(bodyParser.urlencoded({extended:false}))
app.use(bodyParser.json())
app.use(express.static('public'))

const PORT = process.env.PORT || 3000

app.get('/',(req,res) => {
    res.sendFile(__dirname +'/home.html')
})

app.post('/convert',upload.single('file'),(req,res,next) => {
    if(req.file){
        console.log(req.file.path)

        var output = Date.now() + "output.mp3"

        exec(`ffmpeg -i ${req.file.path} ${output}`, (error, stdout, stderr) => {
            if (error) {
                console.log(`error: ${error.message}`);
                return;
            }
            else{
                console.log("file is converted")
            res.download(output,(err) => {
                if(err) throw err
                
                fs.unlinkSync(req.file.path)
                fs.unlinkSync(output)

                next()

            })
        }
        })
    }
})

app.listen(PORT,() => {
    console.log(`App is listening on Port ${PORT}`)
})

 

See also  Build a HTML5 Canvas Bubble Shooter Game in Browser Using CSS3 and Javascript Full Project For Beginners

 

 

home.html

 

 

 

<!DOCTYPE html>
<html>
  <head>
    <title>Mp4 to Mp3 in Node.js</title>
    <link
      rel="stylesheet"
      href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
    />
  </head>
  <body>
      <div class="container">
          <br><br>
          <h1 class="text-center">
              Mp4 to Mp3 in Node.js
          </h1>
          <br><br>
          <form action="/convert" method="POST" enctype="multipart/form-data">
            <div class="form-group">
                <input type="file" class="form-control" name="file" required>
            </div>
            <div class="form-group">
                <button class="btn btn-block btn-danger">
                    Convert to Mp3
                </button>
            </div>
        </form>
      </div>
</body>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
</html>

 

 

 

 

 

 

DOWNLOAD SOURCE CODE

 

 

Leave a Reply