Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

7/8/24, 11:22 PM 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/)

FFmpeg in Python Script


 (//) > HowTo (/howto/) > Python How-To's (/howto/python/)
> FFmpeg in Python Script (/howto/python/ffmpeg-python/)
 Rohan Timalsina  Oct 10, 2023
 Python (/tags/python/) Python FFMPEG (/tags/python-ffmpeg/)  (http://www.facebook.com/sharer.php?
src=bm&u=https%3a%2f%2fwww.delftstack.com%2fhowto%2fpython%2fffmpeg-
python%2f&t=FFmpeg%20in%20Python%20Script)  (https://twitter.com/intent/tweet/?text=Python%20How-
To%27s-
FFmpeg%20in%20Python%20Script&url=https%3a%2f%2fwww.delftstack.com%2fhowto%2fpython%2fffmpeg-
python%2f&hashtags=web,development)  (https://www.linkedin.com/shareArticle?
mini=true&url=https%3a%2f%2fwww.delftstack.com%2fhowto%2fpython%2fffmpeg-
python%2f&title=Python%20How-To%27s-
FFmpeg%20in%20Python%20Script&source=https%3a%2f%2fwww.delftstack.com%2fhowto%2fpython%2fffmpeg-
python%2f&summary=Short%20summary)

TABLE OF CONTENT

1. Install the FFmpeg Python Package


2. Use FFmpeg to Trim a Video in Python
3. Use FFmpeg to Get the Width and Height of a Video in Python
4. Use FFmpeg to Save the Thumbnail From a Video in Python
5. Use FFmpeg to Flip a Video in Python

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.

This tutorial will teach you to use FFMPEG commands in Python.

 Install the FFmpeg Python Package


First, you have to install FFmpeg on your system. Open the command prompt as an administrator and run the
following command to install FFmpeg using choco .

choco install ffmpeg

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 .

pip install ffmpeg-python

Output:

Successfully built ffmpeg


Installing collected packages: ffmpeg
Successfully installed ffmpeg-1.4

https://www.delftstack.com/howto/python/ffmpeg-python/ 2/5
7/8/24, 11:22 PM FFmpeg in Python Script | Delft Stack

 Use FFmpeg to Trim a Video in Python


Since we have already configured FFmpeg on our system, let’s use some FFmpeg commands to work with videos in
Python.

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)

 Use FFmpeg to Get the Width and Height of a Video in Python


The following example prints the width and height of a specified video in Python.

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

 Use FFmpeg to Save the Thumbnail From a Video in Python


You can also save the thumbnail from a video using FFmpeg in Python.

The following example generates the thumbnail of width 500px from time 4s in a video.

import ffmpeg

video = ffmpeg.input("Pencil.mp4", ss=4)


video = video.filter("scale", 500, -1)
video = ffmpeg.output(video, "output.png", vframes=1)
ffmpeg.run(video)

The height is automatically determined by the aspect ratio.

Output image:

https://www.delftstack.com/howto/python/ffmpeg-python/ 3/5
7/8/24, 11:22 PM FFmpeg in Python Script | Delft Stack

 Use FFmpeg to Flip a Video in Python


You can use ffmpeg.hflip() to flip the video horizontally and ffmpeg.vflip() to flip the video vertically in
Python.

Flip the video horizontally:

import ffmpeg

video = ffmpeg.input("Pencil.mp4")
video = ffmpeg.hflip(video)
video = ffmpeg.output(video, "horizontal.mp4")
ffmpeg.run(video)

Flip the video vertically:

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.

Author: Rohan Timalsina (/author/rohan-timalsina/)

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.

 LinkedIn (https://www.linkedin.com/in/rohantimalsina/)  Website (https://www.rohantimalsina.com.np/)

Copyright © 2024. All right reserved


About US (/about-us/) Write For Us (/write-for-us/) Privacy Policy (/privacy-policy/) Advertising (/advertising/) Contact (/contact/)

https://www.delftstack.com/howto/python/ffmpeg-python/ 5/5

You might also like