簡體   English   中英

如何在 ffmpeg-python 中使用多輸入過濾器

[英]How to use multi-input filters with ffmpeg-python

我想用 ffmpeg-python 模仿以下 ffmpeg 命令

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

到目前為止,這就是我所擁有的:

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()

我檢查過,如果我將它輸出為 png,調色板會生成得很好。 但是,我無法通過多輸入命令paletteuse找到如何使用它,因為過濾器僅將一個流作為 ffmpeg-python 的輸入。 我嘗試將它們與ffmpeg.concat()連接起來,這是我發現從兩個流中生成一個流的唯一方法,但我認為這是沒有意義的(無論如何它都不起作用)。

任何的想法?

我遇到了同樣的問題,這個問題是最熱門的搜索結果之一。 我最終想出了如何做到這一點。

以下函數接受一個流(例如stream = ffmpeg.input('input.mp4') )並對其進行stream = ffmpeg.input('input.mp4') 然后它拆分流。 第一個拆分用於生成調色板。 最后,第二個拆分與調色板一起使用以輸出 .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()
  )

根據需要調整分辨率、fps 或添加其他過濾器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM