簡體   English   中英

如何將 ffmpeg complex_filter 轉換為 ffmpeg-python

[英]How to convert ffmpeg complex_filter to ffmpeg-python

我正在嘗試學習將 ffmpeg 命令行背景模糊過濾器轉換為ffmpeg-python格式。 '-lavfi'[0:v]scale=ih*16/9:-1,boxblur=luma_radius=min(h\,w)/20:luma_power=1:chroma_radius=min(cw\,ch)/20:chroma_power=1[bg];[bg][0:v]overlay=(Ww)/2:(Hh)/2,crop=h=iw*9/16

https://github.com/kkroening/ffmpeg-python中的基本示例很適合學習簡單的技巧,但是如何學習完整的轉換語法?

不確定您是否想到了這一點……但這是一種對我有用的方法。

提示 1:使用庫“編碼任何過濾器”的先決條件是了解 ffmpeg 命令行語法。

Tip2 一般情況下, ffmpeg.filter()將過濾器名稱作為第一個參數。 緊隨其后的是所有過濾條件。 這個 function 將下游的 stream 返回到您剛剛創建的過濾器節點。

例如:在問題的示例 ffmpeg 命令行中...閱讀它告訴我您要縮放視頻,然后應用 boxblur 過濾器,然后進行裁剪。

因此,您可以用ffmpeg-python術語將其表示為

# create a stream object, Note that any supplied kwargs are passed to ffmpeg verbatim
my_vid_stream = ffmpeg.input(input_file, "lavfi")
# The input() returns a stream object has what is called 'base_object' which represents the outgoing edge of an upstream node and can be used to create more downstream nodes. That is what we will do. This stream base_object has two properties, audio and video .. assign the video stream to a new variable, we will be creating filters to only video stream, as indicated by [0:v] in ffmpeg command line.
my_vid_stream = mystream.video
# ffmpeg.filter() takes the upstream node followed by the name of the filter, followed by the configuration of the filter
# first filter you wanted to apply is 'scale' filter. So...
my_vid_stream = ffmpeg.filter(my_vid_stream,"scale","ih*16/9:-1")
# next to the upstream node create a new filter which does the boxblur operation per your specs. so .. 
my_vid_stream = ffmpeg.filter(my_vid_stream,"boxblur", "min(h\,w)/20:luma_power=1:chroma_radius=min(cw\,ch)/20:chroma_power=1[bg];[bg][0:v]overlay=(W-w)/2:(H-h)/2")
# finally apply the crop filter to it's upstream node and assign the output stream back to the same variable. so ... 
my_vid_stream = ffmpeg.filter(my_vid_stream, "crop", h="iw*9/16")
# now generate the output node and write it to an output file.
my_vid_stream = ffmpeg.output(my_vid_stream,output_file)
## to see your pipeline in action. call the ffmpeg.run(my_vid_stream)

希望這可以幫助您或其他任何努力有效利用此庫的人。

我正在研究FFmpeg-python ,在添加自定義命令方面有很大的靈活性。 在這里,我提到了一個示例,在該示例中,我將循環添加到覆蓋視頻並添加連接過濾器,您可以從此處了解如何添加重置過濾器。

        audios = []
        inputs = []
        #generate an empty audio
        e_aud_src = rendering_helper.generate_empty_audio(0.1)
        e_aud = (
            ffmpeg.input(e_aud_src)
                .audio
        )

        for k, i in enumerate(videos):
            inp = ffmpeg.input(i['src'], ss=i['start'],  t=(i['end'] - i['start']))

            inp_f = (inp.filter_multi_output('split')[k]
                        .filter_('scale', width=(i['width'] * Factors().factors['w_factor']), height=(i['height'] * Factors().factors['h_factor'])).filter('setsar', '1/1')
                        .setpts(f"PTS-STARTPTS+{i['showtime']}/TB"))

            audio = ffmpeg.probe(i['src'], select_streams='a')
            if audio['streams'] and i['muted'] == False:
                a = (inp.audio.filter('adelay', f"{i['showtime'] * 1000}|{i['showtime'] * 1000}"))
            else: 
                a = e_aud
            audios.append(a) 

            e_frame = (e_frame.overlay(inp_f, x=(i['xpos'] * Factors().factors['w_factor']), y=(i['ypos'] * Factors().factors['h_factor']), eof_action='pass'))

        
        mix_audios = ffmpeg.filter_(audios, 'amix') if len(audios) > 1 else audios[0]
        inp_con = ffmpeg.concat(e_frame, mix_audios, v=1, a=1)
        return inp_con

我的 2 美分:在這里,我們有 3 個相互褪色的視頻。 我發現具有多個輸入的過濾器將接受一個元組作為第一個參數,也可能是一個列表。

並且 y="-y" 的東西也剛剛嘗試過——看起來這個庫足夠直觀,至少考慮到我扭曲(而不是扭曲)的想法。

import ffmpeg
infile = "test-video.mp4"
outfile = str(infile) +'.crossfade.mp4'

if __name__ == '__main__':
    faded = ffmpeg.input(infile, ss=10, to=21)
    into = ffmpeg.input(infile, ss=30, to=41)
    faded = ffmpeg.filter((faded, into), 'xfade', transition="fadeblack", duration=1, offset=10)
    into = ffmpeg.input(infile, ss=60, to=71)
    faded = ffmpeg.filter((faded, into), 'xfade', transition="fadeblack", duration=1, offset=20)
    # overwrite: n="-n" means never , same for y="-y" always
    written = ffmpeg.output(faded, outfile, y="-y")
    ffmpeg.run(written)

暫無
暫無

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

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