Ffmpeg-Python: Python bindings for FFmpeg – with complex filtering support
github.com
github.com
For actual python bindings check out PyAV [0]
> 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=n=2[v2];[1]hflip[v3];\
[v2][v3]overlay=eof_action=repeat[v4];[v4]drawbox=50:50:120:120:red:t=5[v5]"\
-map [v5] output.mp4
I have actually written scripts that would generate such horrors but north of 2000 characters :].> 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=10, end_frame=20),
in_file.trim(start_frame=30, end_frame=40),
)
.overlay(overlay_file.hflip())
.drawbox(50, 50, 120, 120, color='red', thickness=5)
.output('out.mp4')
.run()
)[0] But not all
While the command line seems horrible, once you start playing with them it can be really fun. As always, start simple and build!
Are there already existing, more polished wrappers around ffmpeg that include enough escape hatches to be able to fully leverage everything ffmpeg supports? I probably just didn't search and evaluate options long enough.
I used MoviePy a few years ago to add subtitles to a short video. It was fairly easy to write a script to place bits of text at specific frames.
I'm interested to learn more about the differences and similarities of ffmpeg-python and MoviePy and will put some energy into that.
I spent 3 days to write a python script, 2 weeks ago, to generate a FFMPEG command that is able to take N videos, of different sizes, duration and framerate, and use the xstack filter to generate a tiled video, with a 2x2 layout.
Of course one problem was generating 4 series of video, to have the smallest period of time at the end. I solved this pretty easily, although there are more optimal ways of doing it.
One issue to consider is that I had to re-encode all video to a same framerate AND add a proper letterbox (I wasted a lot of time on this), but the filter system handled this pretty well since it worked in a way that did not buffer too much.
Also had to mix the audio, and I did this step separately, and there are no desync. This was very easy!
The generated command was pretty gnarly but I made it work for 30+ videos!
I wanted to try a 4x4, with reddit mp4 short gifs (I have about 500 of them), but powershell stopped me because the ffmpeg command was so large. So I gave up! I wonder if those bindings solve that problem. Nevertheless I should obviously use a unix shell to solve this command length problem.
For those concerned about extensibility, you can always use .global_args() to pass arbitrary arguments to FFMPEG if necessary. However this library is well maintained.
I wonder about ffmpeg architecture, does it run these graphs completely on CPU, completely on GPU, or does it send the video between them?
If this gets you halfway-workable tab completion for the various options of things it's worth it for that alone, vs. diving through the big long page that otherwise contains all the names and orders of arguments to filters.
As an Ex VFX wonk, and someone still working with videos, FFMPEG is both a joy and a massive pain at the same time. Having a simple interface in python makes life a whole boatload easier
Anyone know about such a project?