Welcome folks today in this blog post we will be showing how to cut,trim and change the fps
value and scale mp4 video using the ffmpeg
library in python. All the full source code of the application is shown below.
Get Started
In order to get started you need to install the ffmpeg
module using the pip
command as shown below
pip install ffmpeg
After installing this library you need to make an app.py
file and copy paste the following code
app.py
1 2 3 4 |
import ffmpeg import sys sys.path.append(r'C:\ffmpeg\bin') # your ffmpeg file path |
First of all we are importing the ffmpeg
and sys
module and then we are setting the path of the ffmpeg
library where it is stored inside the local file system.
1 |
stream = ffmpeg.input('video.mp4') # video location |
And in the above line we are using the input
method to provide the output.mp4
video file as the input to ffmpeg library.
1 2 3 |
stream = stream.trim(start = 0, duration=5).filter('setpts', 'PTS-STARTPTS') stream = stream.filter('fps', fps=15, round='up').filter('scale', w=128, h=128) |
And now we are using some methods of the ffmpeg
module which includes the trim()
method which basically cuts the video from the start to end. And then we use the filter()
method to scale and change the fps
value of the video
1 2 3 |
stream = ffmpeg.output(stream, 'output.mp4') ffmpeg.run(stream) |
And lastly we are executing the ffmpeg
command using the output()
method and then we are running the command using the run()
method of ffmpeg module.