Benjamin Barrois
Benjamin Barrois

Reputation: 2686

How to use multi-input filters with ffmpeg-python

I want to mimic the following ffmpeg command with ffmpeg-python

ffmpeg -y -i in.mp4 -t 30 -filter_complex "fps=10,scale=-1:-1:flags=lanczos[x];[0:v]palettegen[y];[x][y]paletteuse" out.gif

So far, this is what I've got:

in_stream = ffmpeg.input(src, ss=start_time, t=(stop_time-start_time))
scale_input = in_stream
if fps >= 1:
    stream = ffmpeg.filter(in_stream['v'], 'fps', fps)
    scale_input = stream

stream = ffmpeg.filter(scale_input, 'scale', output_width, output_height, 'lanczos')

palette = ffmpeg.filter(in_stream['v'], 'palettegen')
#stream = ffmpeg.filter(stream, palette, 'paletteuse') ???
stream.output(dst).run()

I checked, the palette generates well if I output it as a png. However, I can't find how to use it through the multi-input command paletteuse, as filters only take one stream as an input in ffmpeg-python. I tried concatenating them with ffmpeg.concat() which is the only method I found to make one stream from two but I think it is non-sense (and it doesn't work anyway).

Any idea?

Upvotes: 3

Views: 3023

Answers (1)

Ecen Cronzeton
Ecen Cronzeton

Reputation: 81

I encountered the same problem, and this question was one of the top search results. I eventually figured out how to do it.

The following function takes a stream (for example stream = ffmpeg.input('input.mp4')) and rescales it. It then splits the stream. The first split is used to generate a palette. Finally, the second split is used with the palette to output a .gif.

# stream: input stream
# output: name of output file
def output_lowres_gif(stream, output):
  split = (
    stream
    .filter('scale', 512, -1, flags='lanczos') # Scale width to 512px, retain aspect ratio
    .filter('fps', fps=12)
    .split()
  )

  palette = (
    split[0]
    .filter('palettegen')
  )

  (
    ffmpeg
    .filter([split[1], palette], 'paletteuse')
    .output(f'{output}.gif')
    .overwrite_output()
    .run()
  )

Tweak resolution, fps or add other filters as needed.

Upvotes: 6

Related Questions