How To Install Ffmpeg In Python
ffmpeg-python: Python bindings for FFmpeg
Overview
In that location are tons of Python FFmpeg wrappers out there but they seem to lack complex filter back up. ffmpeg-python
works well for simple as well as complex signal graphs.
Quickstart
Flip a video horizontally:
import ffmpeg stream = ffmpeg.input('input.mp4') stream = ffmpeg.hflip(stream) stream = ffmpeg.output(stream, 'output.mp4') ffmpeg.run(stream)
Or if y'all prefer a fluent interface:
import ffmpeg ( ffmpeg .input('input.mp4') .hflip() .output('output.mp4') .run() )
API reference
Complex filter graphs
FFmpeg is extremely powerful, but its control-line interface gets really complicated rather quickly - specially when working with signal graphs and doing anything more than trivial.
Take for example a signal graph that looks like this:
The corresponding command-line arguments are pretty gnarly:
ffmpeg -i input.mp4 -i overlay.png -filter_complex "[0]trim=start_frame=10:end_frame=20[v0];\ [0]trim=start_frame=30:end_frame=40[v1];[v0][v1]concat=due north=2[v2];[i]hflip[v3];\ [v2][v3]overlay=eof_action=repeat[v4];[v4]drawbox=50:fifty:120:120:red:t=five[v5]" \ -map [v5] output.mp4
Perhaps this looks great to you, but if yous're not an FFmpeg command-line skillful, it probably looks alien.
If you're like me and find Python to be powerful and readable, it's easier with ffmpeg-python
:
import ffmpeg in_file = ffmpeg.input('input.mp4') overlay_file = ffmpeg.input('overlay.png') ( ffmpeg .concat( in_file.trim(start_frame = ten, end_frame = twenty), in_file.trim(start_frame = 30, end_frame = 40), ) .overlay(overlay_file.hflip()) .drawbox(50, fifty, 120, 120, color = 'scarlet', thickness = five) .output('out.mp4') .run() )
ffmpeg-python
takes care of running ffmpeg
with the command-line arguments that correspond to the to a higher place filter diagram, in familiar Python terms.
Real-world signal graphs can go a heck of a lot more than complex, but ffmpeg-python
handles arbitrarily big (directed-acyclic) bespeak graphs.
Installation
Installing ffmpeg-python
The latest version of ffmpeg-python
can be acquired via a typical pip install:
pip install ffmpeg-python
Or the source tin be cloned and installed from locally:
git clone git@github.com:kkroening/ffmpeg-python.git pip install -e ./ffmpeg-python
Annotation:
ffmpeg-python
makes no endeavor to download/install FFmpeg, asffmpeg-python
is only a pure-Python wrapper - whereas FFmpeg installation is platform-dependent/surroundings-specific, and is thus the responsibility of the user, equally described beneath.
Installing FFmpeg
Before using ffmpeg-python
, FFmpeg must be installed and accessible via the $PATH
environment variable.
In that location are a variety of means to install FFmpeg, such as the official download links, or using your package managing director of choice (due east.grand. sudo apt install ffmpeg
on Debian/Ubuntu, mash install ffmpeg
on Bone X, etc.).
Regardless of how FFmpeg is installed, you can check if your environs path is set correctly by running the ffmpeg
control from the concluding, in which case the version data should appear, as in the post-obit example (truncated for brevity):
$ ffmpeg ffmpeg version four.2.4-1ubuntu0.1 Copyright (c) 2000-2020 the FFmpeg developers built with gcc 9 (Ubuntu nine.3.0-10ubuntu2)
Notation: The actual version information displayed here may vary from one system to another; but if a message such as
ffmpeg: control not institute
appears instead of the version information, FFmpeg is not properly installed.
Examples
When in doubt, have a await at the examples to meet if there's something that's shut to any you're trying to do.
Here are a few:
-
Catechumen video to numpy array
-
Generate thumbnail for video
-
Read raw PCM audio via pipe
-
JupyterLab/Notebook stream editor
- Tensorflow/DeepDream streaming
Run across the Examples README for boosted examples.
Custom Filters
Don't come across the filter y'all're looking for? While ffmpeg-python
includes shorthand note for some of the most usually used filters (such as concat
), all filters can be referenced via the .filter
operator:
stream = ffmpeg.input('dummy.mp4') stream = ffmpeg.filter(stream, 'fps', fps = 25, round = 'upwardly') stream = ffmpeg.output(stream, 'dummy2.mp4') ffmpeg.run(stream)
Or fluently:
( ffmpeg .input('dummy.mp4') .filter('fps', fps = 25, round = 'upwards') .output('dummy2.mp4') .run() )
Special choice names:
Arguments with special names such as -qscale:v
(variable bitrate), -b:v
(abiding bitrate), etc. tin can be specified as a keyword-args lexicon as follows:
( ffmpeg .input('in.mp4') .output('out.mp4', **{'qscale:five': 3}) .run() )
Multiple inputs:
Filters that accept multiple input streams can be used by passing the input streams as an assortment to ffmpeg.filter
:
principal = ffmpeg.input('main.mp4') logo = ffmpeg.input('logo.png') ( ffmpeg .filter([main, logo], 'overlay', 10, x) .output('out.mp4') .run() )
Multiple outputs:
Filters that produce multiple outputs can exist used with .filter_multi_output
:
split up = ( ffmpeg .input('in.mp4') .filter_multi_output('divide') # or `.split()` ) ( ffmpeg .concat(split[0], split up[1].reverse()) .output('out.mp4') .run() )
(In this particular instance, .split()
is the equivalent autograph, just the full general approach works for other multi-output filters)
String expressions:
Expressions to be interpreted by ffmpeg can be included as string parameters and reference whatsoever special ffmpeg variable names:
( ffmpeg .input('in.mp4') .filter('crop', 'in_w-2*10', 'in_h-2*20') .input('out.mp4') )
When in doubt, refer to the existing filters, examples, and/or the official ffmpeg documentation.
Oft asked questions
Why do I become an import/attribute/etc. error from import ffmpeg
?
Brand sure you lot ran pip install ffmpeg-python
and non pip install ffmpeg
(incorrect) or pip install python-ffmpeg
(also incorrect).
Why did my sound stream get dropped?
Some ffmpeg filters driblet audio streams, and care must exist taken to preserve the audio in the last output. The .audio
and .video
operators can be used to reference the sound/video portions of a stream and so that they can be processed separately and and then re-combined later in the pipeline.
This dilemma is intrinsic to ffmpeg, and ffmpeg-python tries to stay out of the way while users may refer to the official ffmpeg documentation equally to why certain filters driblet audio.
As usual, have a look at the examples (Audio/video pipeline in particular).
How tin I discover out the used control line arguments?
You can run stream.get_args()
before stream.run()
to recollect the command line arguments that will be passed to ffmpeg
. You lot can also run stream.compile()
that also includes the ffmpeg
executable as the first statement.
How exercise I exercise XYZ?
Take a look at each of the links in the Additional Resources department at the end of this README. If you wait everywhere and can't find what you're looking for and have a question that may exist relevant to other users, you may open up an issue asking how to do it, while providing a thorough caption of what you lot're trying to do and what you've tried so far.
Issues not directly related to ffmpeg-python
or issues asking others to write your lawmaking for you or how to do the piece of work of solving a complex betoken processing problem for you lot that'south not relevant to other users will exist closed.
That said, we hope to continue improving our documentation and provide a community of support for people using ffmpeg-python
to do cool and exciting things.
Contributing
1 of the best things yous can do to help brand ffmpeg-python
better is to answer open up questions in the issue tracker. The questions that are answered will exist tagged and incorporated into the documentation, examples, and other learning resources.
If yous notice things that could be better in the documentation or overall development experience, delight say so in the effect tracker. And of course, feel gratis to report any bugs or submit feature requests.
Pull requests are welcome as well, only it wouldn't hurt to touch base in the issue tracker or hop on the Matrix chat aqueduct start.
Anyone who fixes any of the open bugs or implements requested enhancements is a hero, but changes should include passing tests.
Running tests
git clone git@github.com:kkroening/ffmpeg-python.git cd ffmpeg-python virtualenv venv . venv/bin/activate # (Bone X / Linux) venv\bin\activate # (Windows) pip install -e .[dev] pytest
Special thank you
- Fabrice Bellard
- The FFmpeg team
- Arne de Laat
- Davide Depau
- Dim
- Noah Stier
Additional Resources
- API Reference
- Examples
- Filters
- FFmpeg Homepage
- FFmpeg Documentation
- FFmpeg Filters Documentation
- Test cases
- Outcome tracker
- Matrix Chat: #ffmpeg-python:matrix.org
Source: https://github.com/kkroening/ffmpeg-python
Posted by: woolleyuple1987.blogspot.com
0 Response to "How To Install Ffmpeg In Python"
Post a Comment