FFmpeg in Python Script - Delft Stack
FFmpeg in Python Script - Delft Stack
Tutorial
(/tutorial/) Python 3 Basic (/tutorial/python-3-basic-tutorial/) Tkinter (/tutorial/tkinter-tutorial/) Python Modules (/tutorial/python-modules-tutorial/)
JavaScript (/tutorial/javascript/) Python Numpy (/tutorial/python-numpy/) Git (/tutorial/git/) Matplotlib (/tutorial/matplotlib/) PyQt5 (/tutorial/pyqt5/)
Data Structure (/tutorial/data-structure/) Algorithm (/tutorial/algorithm/)
HowTo
(/howto/) Python Scipy (/howto/python-scipy/) Python (/howto/python/) Python Tkinter (/howto/python-tkinter/) Batch (/howto/batch/)
PowerShell (/howto/powershell/) Python Pandas (/howto/python-pandas/) Numpy (/howto/numpy/) Python Flask (/howto/python-flask/) Django (/howto/django/)
Matplotlib (/howto/matplotlib/) Docker (/howto/docker/) Plotly (/howto/plotly/) Seaborn (/howto/seaborn/) Matlab (/howto/matlab/) Linux (/howto/linux/)
Git (/howto/git/) C (/howto/c/) Cpp (/howto/cpp/) HTML (/howto/html/) JavaScript (/howto/javascript/) jQuery (/howto/jquery/)
Python Pygame (/howto/python-pygame/) TensorFlow (/howto/tensorflow/) TypeScript (/howto/typescript/) Angular (/howto/angular/) React (/howto/react/)
CSS (/howto/css/) PHP (/howto/php/) Java (/howto/java/) Go (/howto/go/) Kotlin (/howto/kotlin/) Node.js (/howto/node.js/) Csharp (/howto/csharp/)
Rust (/howto/rust/) Ruby (/howto/ruby/) Arduino (/howto/arduino/) MySQL (/howto/mysql/) MongoDB (/howto/mongodb/) Postgres (/howto/postgres/)
SQLite (/howto/sqlite/) R (/howto/r/) VBA (/howto/vba/) Scala (/howto/scala/) Raspberry Pi (/howto/raspberry-pi/)
Reference
(/api/) Python (/api/python/) Python Pandas (/api/python-pandas/) Numpy (/api/numpy/) Scipy (/api/scipy/) JavaScript (/api/javascript/)
TABLE OF CONTENT
https://www.delftstack.com/howto/python/ffmpeg-python/ 1/5
7/8/24, 11:22 PM FFmpeg in Python Script | Delft Stack
FFmpeg is short for Fast Forward Moving Picture Experts Group. It is an open-source project that provides tools such
as ffmpeg , ffplay , and ffprobe to deal with multimedia files.
FFmpeg is a command-line utility that helps to convert video/audio format, compress a video, extract audio from a
video, create a GIF, cut a video, and more.
Next, install the ffmpeg-python package using the Python package manager tool pip .
Run the following command in the prompt to install the package with pip .
Output:
https://www.delftstack.com/howto/python/ffmpeg-python/ 2/5
7/8/24, 11:22 PM FFmpeg in Python Script | Delft Stack
The following example cuts the video Pencil.mp4 from 5s to 10s and saves it as output.mp4 .
import ffmpeg
video = ffmpeg.input("Pencil.mp4")
video = video.trim(start=5, duration=5)
video = ffmpeg.output(video, "output.mp4")
ffmpeg.run(video)
import ffmpeg
probe = ffmpeg.probe("output.mp4")
video = next(
(stream for stream in probe["streams"] if stream["codec_type"] == "video"), None
)
width = int(video["width"])
height = int(video["height"])
print("Width:", width)
print("Height:", height)
Output:
Width: 1280
Height: 720
The following example generates the thumbnail of width 500px from time 4s in a video.
import ffmpeg
Output image:
https://www.delftstack.com/howto/python/ffmpeg-python/ 3/5
7/8/24, 11:22 PM FFmpeg in Python Script | Delft Stack
import ffmpeg
video = ffmpeg.input("Pencil.mp4")
video = ffmpeg.hflip(video)
video = ffmpeg.output(video, "horizontal.mp4")
ffmpeg.run(video)
import ffmpeg
video = ffmpeg.input("Pencil.mp4")
video = ffmpeg.vflip(video)
video = ffmpeg.output(video, "vertical.mp4")
ffmpeg.run(video)
FFmpeg is a handy tool for performing different operations on multimedia files. It can quickly trim the video, change
file format, extract audio, create GIFs, and more.
By this point, you should have clearly understood how to use FFmpeg commands in Python script. We hope you find
this tutorial helpful.
https://www.delftstack.com/howto/python/ffmpeg-python/ 4/5
7/8/24, 11:22 PM FFmpeg in Python Script | Delft Stack
(/author/rohan-timalsina/)
(/author/rohan-timalsina/)
Rohan is a learner, problem solver, and web developer. He loves to write and share his understanding.
https://www.delftstack.com/howto/python/ffmpeg-python/ 5/5