【问题标题】:How to convert ffmpeg complex_filter to ffmpeg-python如何将 ffmpeg complex_filter 转换为 ffmpeg-python
【发布时间】:2020-09-04 10:28:25
【问题描述】:

我正在尝试学习将 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=(W-w)/2:(H-h)/2,crop=h=iw*9/16

来自https://github.com/kkroening/ffmpeg-python 的基本示例很好学习简单的技巧,但是如何学习完整的转换语法?

【问题讨论】:

  • 首先,我对ffmpeg一无所知。但是在简要阅读了 API 文档之后,似乎很多您想要使用的过滤器功能都没有实现(即 boxblur)。您可以使用ffmpeg.filter() 函数自己实现它们以应用自定义过滤器。 Check it out。希望这会有所帮助!
  • 如果您可以发布一个示例命令行参数,也许我可以提供帮助

标签: python ffmpeg scripting video-editing


【解决方案1】:

我正在研究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】:

    不确定您是否想到了这一点……但这是一种对我有用的方法。

    提示1:使用库“编码任何过滤器”的先决条件是 理解 ffmpeg 命令行语法。

    提示2:一般情况下,ffmpeg.filter() 将过滤器名称作为第一个参数。紧随其后的是所有过滤条件。该函数将下游的流返回到您刚刚创建的过滤器节点。

    例如:在问题的示例 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)
    

    希望这可以帮助您或其他任何努力有效利用此库的人。

    【讨论】:

      猜你喜欢
      • 2023-02-07
      • 2021-09-23
      • 2015-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-28
      • 2018-08-12
      • 2013-01-07
      相关资源
      最近更新 更多