Today I will teach you how to converter your video MP4 to audio with nodejs
With that, you can select your video and transfer it to audio using the library ffmpeg Repo project here!!
The libraries we will use is:
What is FFMPEG
There are many open source tools to edit, tweak and convert your files to exactly what you need. Tools like, Audacity and Handbrake is very good, but sometimes you just want to change a file from one format to another quickly.
FFmpeg is a collection of different projects for handling multimedia files. It is often used behind the scenes in many other media related projects.
Create the project
- Create the project folder.
npm init
We will need to use some lib to upload the file, and the lib chosen was the multer
npm install express multer
npm install nodemon
npm install @ffmpeg-installer/ffmpeg fluent-ffmpeg
- Configure nodemon to run inside the
package.json
file and in thestart
script addnodemon index.js
In the index.js
file we will add an endpoint called /convert
it will look like this:
const express = require('express') const multer = require('multer') const app = express() const PORT = process.env.PORT || 5000 app.use(multer({ dest: './tmp/' }).single('file')) app.post('/convert', (request, response) => { return response.send("It's works") }) app.listen(PORT)
In our multer middware (he is responsible for saving our video) we will add a small improvement to it, because if you send the same file with the same name it will cause problems. So it will look like this:
const storage = multer.diskStorage({ destination: function (request, file, callback) { callback(null, './tmp/') }, filename: (req, file, callback) => { callback(null, Date.now() + '-' + file.originalname) }, }) app.use(multer({ storage }).single('file'))
We renamed the filename to get the now date and filename.
Now let's implement FFMPEG to convert to mp3
const express = require('express') const multer = require('multer') const fs = require('fs') const ffmpeg = require('fluent-ffmpeg') const ffmpegPath = require('@ffmpeg-installer/ffmpeg').path ffmpeg.setFfmpegPath(ffmpegPath) const PORT = process.env.PORT || 5000 const app = express() const storage = multer.diskStorage({ destination: (request, file, callback) => { callback(null, './tmp/') }, filename: (request, file, callback) => { callback(null, Date.now() + '-' + file.originalname) }, }) app.use(multer({ storage }).single('file')) app.post('/convert', (request, response) => { const file = request.file const fileName = '-output.mp3' ffmpeg('tmp/' + file.filename) .toFormat('mp3') .on('end', () => { return response.download(__dirname + fileName, error => { if (error) throw error console.log('conversion success') removeFile(`${__dirname}/tmp/${file.filename}`) }) }) .on('error', error => { console.log(error) removeFile(`${__dirname}/tmp/${file.filename}`) }) .saveToFile(__dirname + fileName) }) function removeFile(directory) { fs.unlink(directory, error => { if (error) throw error console.log('File deleted') }) } app.listen(PORT)
- First we imported our necessary dependencies and made a ffmpeg.setFfmpegPath(ffmpegPath) for the operating system to get the FFMPEG path
- Inside the /convert endpoint we call some ffmpeg functions such as format it supports several formats, but as in today's blog topic and converting to audio we just call it mp3.
- When FFMPEG finishes the conversion it will call this END function and send our file to our client, and after sending the file to the client it will remove the file from our local machine.
.on('end', () => {
return response.download(__dirname + fileName, error => {
if (error) throw error
console.log('conversion success')
removeFile(`${__dirname}/tmp/${file.filename}`)
})
})
ffmpeg has several functions, start, end among others...
And that, I hope you liked it and until next time
Until later!