Node.js Fluent-FFMPEG Example to Change Video Resolution & Compress to Smaller Size in Command Line
Mar 12, 2025
Buy the full source code of application here:
https://buy.stripe.com/aEU17o7JffJe0Raba8
Visit my Online Free Media Tool Website
https://freemediatools.com/
Buy Premium Scripts and Apps Here:
https://procodestore.com/
Welcome Folks My name is Gautam and Welcome to Coding Shiksha a Place for All Programmers. You can learn Web Development and Programming Tutorials.
Donate to Our Youtube Channel at :
https://www.instamojo.com/@geekygautam1997
Subscribe for more Videos: https://www.youtube.com/channel/UCR6d0EiC3G4WA8-Rqji6a8g?sub_confirmation=1
Show More Show Less View Video Transcript
0:06
uh hello friends today in this tutorial
0:08
i will be showing you that how to
0:10
compress
0:11
the video to a smaller size and also how
0:14
to change the video resolution let's
0:16
suppose you see these video resolutions
0:18
inside youtube as well such as
0:20
1280
0:23
720
0:24
all this full hd
0:26
720 how to change video resolutions so
0:29
we will be using this library called as
0:32
fluent ffmpg which is a wrapper library
0:35
of fmpg library so if you just search
0:39
this library which is fluent fmpg you
0:41
will get to know about this library so
0:44
it's a wrapper library to fmpg library
0:48
which is a video and audio processing
0:50
library so it is a very popular library
0:52
it has got 193 000 weekly downloads
0:57
and we will be using this at the command
0:59
line using node.js
1:01
so
1:02
it is using ffmpg library in the
1:04
background so fmpg as i already told you
1:08
it's a video processing library
1:10
so just first of all you need to be have
1:12
having installed this library
1:14
so after you install it you can cross
1:16
check just type fmpg and if the command
1:19
works then you have successfully
1:22
installed fmpg so now the thing remains
1:24
that guys you need to create a brand new
1:26
project just install this using this
1:28
command which is npmi fluent ffmpg
1:32
so i have already installed it so let me
1:35
show you what we will be doing in this
1:37
tutorial so basically we will take a
1:39
video file
1:40
to compress it to a smaller size
1:43
and this will create two versions of
1:46
this video file such that youtube
1:48
creates it whenever you upload a video
1:49
to youtube
1:51
it will create it in full hd and also it
1:54
will create in 720 hd
1:57
video let me run this file here so now
1:59
to run this script it is very easy
2:01
simply just
2:02
write node index.js
2:05
followed by the input file name which is
2:07
video dot mp4
2:10
so now if you run this
2:12
you will now see it will create two
2:13
versions of this input file which is
2:16
1280 into 720 which is
2:21
this one and 1920 into 180 this is full
2:26
hd and this is hd
2:28
so now basically you will see that
2:30
if i show you
2:34
if i open this inside file explorer so
2:36
you will see the size
2:38
difference
2:39
original video was
2:41
one megabyte 863 kb and now this 1920 is
2:47
a bit smaller you can see around about
2:49
one megabyte and this 1280 into 720 it's
2:53
532 kilobytes
2:55
so it's round about very small video you
2:58
will just see here
3:01
so
3:03
it compresses the size of the video and
3:05
also it creates
3:07
two versions of the video resolutions it
3:09
also changes the resolution of the video
3:11
as well you can take a longer video
3:13
example let's suppose this contains 14
3:16
seconds of video which is 7 megabytes
3:18
old
3:19
let me copy this video
3:22
and paste it right here inside it
3:25
let me delete this
3:31
so now if i
3:33
execute this here
3:34
same command and let me change the path
3:37
to video to dot mp4
3:39
so now basically it will
3:42
you will see the progress how it is
3:44
compressing it frame by frame
3:46
so
3:48
it will take some time because this
3:49
video is larger so you just see finish
3:52
processing so now basically you will see
3:54
it is compressed this video from seven
3:58
megabytes to
4:00
896 kilobytes and this 1280 into 720 is
4:05
450 454 kilobyte so it significantly
4:09
compresses the size as well so this is a
4:12
nodejs script that you will definitely
4:14
want
4:15
so in the background it is using ffmpg
4:17
library so i have given all the source
4:20
code in the video description guys this
4:21
is the blog post i have written which is
4:24
this one
4:26
so if you just go to the video
4:27
description link right here inside it
4:30
right here
4:32
inside the description of this live
4:33
stream you will find this link so simply
4:36
go to this
4:37
and here you will land on this bla
4:39
blog post so this is the script here i
4:42
will write it step by step in this
4:44
tutorial
4:45
so just
4:47
copy this
4:48
so now let me write the script step by
4:51
step guys so first of all you need to
4:53
install this library which is
4:56
fluent ffmpg so just install this i have
4:59
already installed it
5:02
and after this guys what we need to do
5:04
is that
5:06
we need to make an anonymous function
5:07
here
5:08
which will run automatically whenever
5:10
you run the nodejscript so like this
5:14
so now
5:15
this is an anonymous function so
5:17
basically whenever you put anything here
5:19
it will run so now basically if i put
5:22
here hello world
5:24
and if i execute the script
5:26
so you will see it will execute hello
5:28
world message
5:30
so
5:31
basically you don't need to call this
5:32
function it will call by itself
5:35
so right here inside this we need to
5:37
first of all import the ffmpg library
5:40
which is
5:41
we have installed it
5:46
so we need to require it
5:49
so after requiring this library
5:52
we will be
5:55
giving the output in the command line
5:57
itself whenever we run the script
6:00
node index.js then we will give the
6:02
input file name
6:03
so now to cache this file name
6:06
we need to
6:09
catch it using the command line so we
6:11
will make use of process
6:13
dot argv
6:15
and we will use the slice and we will
6:18
here we will pass to so it will put the
6:20
second argument
6:22
and then here we will use the for each
6:24
rule and here we will have a simple
6:27
function
6:29
this will have these three parameters
6:32
and here we will cache the file name
6:34
which will be stored inside the valve
6:36
parameter so if i console of the val
6:39
you will simply see if i execute
6:43
nodeindex.js and pass the file name
6:45
which is
6:46
video.mp4 so now this file name will be
6:49
printed out you will see so we are
6:51
successfully getting the file name
6:54
so after this what we need to do is that
6:55
we need to get the extension
6:57
so we will use the base name function
7:00
and pass the file name so here we need
7:02
to create this function which is
7:05
base name this is a custom function
7:08
so this will have
7:10
we will pass the file name as a string
7:14
so now we need to extract the base of
7:16
the file name which we can do this using
7:19
string constructor we will pass this and
7:21
then we will use the substring method
7:24
and here we will get the last
7:27
string last
7:28
index
7:30
so we will use the last
7:33
index of
7:34
this is a method we would use
7:37
and then we will pass the slash symbol
7:40
like this
7:42
and
7:44
concatenate by one so this is a logic by
7:47
which you can get the base name without
7:49
the extension
7:50
so then we will run a
7:52
if condition that if base
7:55
last indexed off
7:57
and dot
7:59
is not equal to
8:02
-1
8:03
this is a condition
8:06
in that case we will
8:09
use base dot substring zero dot
8:13
base dot
8:15
last index of
8:20
dot
8:21
so you just need to copy this code guys
8:23
you don't need to understand
8:25
much about this code so basically it is
8:27
just
8:28
returning the base name of the
8:31
file name so it will now return the base
8:33
name that's it
8:36
so now if you console log it
8:40
base name
8:46
so
8:47
this needs to be base name sorry
8:50
so just
8:51
do this
8:53
so now if i
8:57
so now basically guys you will see that
9:00
it is getting the file name without
9:02
having the extension so now it is
9:03
returning only video not mp4 dot mp4
9:07
it is
9:08
splitted out
9:10
so that's exactly what we wanted now we
9:12
will use the ffmpg library which is we
9:15
have installed
9:16
import it we will pass the file name
9:18
which is stored
9:21
and then we will use the output method
9:24
to
9:25
we will use the base name
9:27
plus
9:28
and then we will say dot
9:31
1280 this is the version 720 resolution
9:36
dot mp4
9:38
and here we will use the video codec
9:41
of
9:44
which is very common codec which is lib
9:47
x 264.
9:50
so there will be no audio so i will be
9:53
having no audio function
9:55
the size will be simply 1280
9:58
into 720
10:02
so these methods are very much available
10:04
in fluent ffmpg library which makes it
10:06
very much easy
10:08
so now we will be generating
10:10
the
10:11
full hd
10:12
version
10:14
so for that we will once again use our
10:16
dot output base name plus
10:21
so here we will be having 1920 into
10:25
1080p
10:28
dot mp4
10:30
so again we will use the same codec
10:32
which is
10:34
lib
10:35
x 264
10:40
and again we will use no audio this is
10:44
not compulsory that you use no audio
10:46
function
10:47
basically
10:48
it
10:49
removes audio from the video
10:53
and the size is simply
10:55
1920
10:57
by
10:59
1080
11:02
so
11:03
now guys we will be attaching some
11:05
callbacks so if any sort of error take
11:08
place inside this conversions
11:10
the errors will be there inside this so
11:12
we can simply console log the error
11:16
like this
11:19
so
11:20
on progress is also some event is there
11:23
so if you want to show the progress you
11:26
will use this method which is progress
11:30
so inside this
11:32
week it will show you the progress
11:36
frames
11:39
progress dot frames
11:45
and lastly guys we will be having the on
11:48
end so when the
11:50
processing is finished there is this
11:53
event which will be fired
11:55
so here we can simply write a message
11:57
that finished processing
12:02
so this is the script guys and now
12:04
basically you need to run the script
12:06
also so this is the very
12:09
basic method which is run dot run that's
12:12
it this will actually run the ffmpg
12:14
command that's it this is the overall
12:17
script guys i have written step by step
12:20
so if i now execute it video dot
12:24
so now basically it will you will see
12:26
the progress in the left hand side it
12:28
will create two versions of it
12:37
you will see that
12:38
so in this way guys you can compress the
12:40
video and change the video resolution as
12:43
well inside fluent ffmpg inside nodejs
12:46
i have given all the source code in the
12:48
video description
12:49
please hit the like button subscribe the
12:51
channel and i will be seeing you in the
12:53
next video
#Video Sharing
