How to install and use ffmpeg + node js with ubuntu
FFmpeg is an open source library which you can use to convert any video or audio into another format, create thumbnails and resize video or audio. In this article I would cover how to install ffmpeg on Ubuntu and use it with nodejs. So lets start.
- Install ffmpeg on ubuntu
- Create a project with node js
- Use of ffmpeg with node js
Install ffmpeg on ubuntu:
For install ffmpeg on ubuntu you needs to follow these steps:
- Update the package list with this command
1 |
sudo apt update |
- Install ffmpeg with this command
1 |
sudo apt install ffmpeg |
- For check ffmpeg is installed or not run this command:
1 |
ffmpeg -version |
Now ffmpeg is ready to run on your system, the next step is how would we use ffmpeg with node js for covert or manipulate video.
Create project for node js:
Nodejs is the server side javascript environment for creating web applications. For nodejs you need npm to install the nodejs project. For creating a nodejs project you needs to follow these steps:
- mkdir foldername
- cd /foldername
- npm install
- npm install express –save
Now create server.js in the root of the project folder. And paste this code in it
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
var express = require('express'); var app = express(); app.get('/', function (req, res) { res.send('Hello World!'); }); app.listen(3000, function () { console.log('Example app listening on port 3000!'); }); |
And save this file.
Now come on cmd run this command for run application on browser. You can access your application with http://localhost:3000 here 3000 is the port you can use another instead. Your application is ready. Now comes on ffmpeg usage.
How to use ffmpeg with nodejs
For use of ffmpeg you need an npm extension called “node-fluent-ffmpeg”. Firstly you would install this extension follow this command for install it
1 |
npm install fluent-ffmpeg |
To start using this you must include it in your project so we would open server.js file and place it on the top of file. And use the code below i have mentioned. It is the easiest way to use the ffmpeg with node 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 |
var express = require('express'); var app = express(); var ffmpeg = require('fluent-ffmpeg'); ffmpeg.setFfmpegPath("/usr/bin/ffmpeg"); ffmpeg.setFfprobePath("/usr/bin/ffprobe"); ffmpeg('video.mp4') .on('end', function() { console.log('Screenshots taken'); }) .on('error', function(err) { console.error('this error:'); console.error(err); }) .screenshots({ // Will take screenshots at 20%, 40%, 60% and 80% of the video count: 4, folder: 'uploads' }); app.get('/', function (req, res) { res.send('Hello World!'); }); app.listen(3000, function () { console.log('Example app listening on port 3000!'); }); |
That’s it.
Conclusion: This is the tutorial on how to install ffmpeg and use it in nodejs. This is very simple. There is one step I have faced little problem in when including the ffmpeg path. So if you are facing this problem too then you need to check ffmpeg installed directory and change in this code.
Happy learning.