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
1 |
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
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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
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}`) }) |
home.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 28 29 30 |
<!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