【问题标题】:How do I integrate the FFMPEG commands with Python Script?如何将 FFMPEG 命令与 Python 脚本集成?
【发布时间】:2021-01-12 15:57:34
【问题描述】:

我正在尝试在 .mp4 视频中的场景发生变化时提取帧。 我使用的包是 FFMPEG。 FFMPEG 主要在 CLI 上工作,我正在尝试将其与 Python3.x 集成

我在 CLI 中使用的命令是:

ffmpeg -i {0} -vf  "select=gt(scene\,0.5), scale=640:360" -vsync vfr frame%d.png

执行 CLI 后输出就很好了。

但我想在 Python 脚本中使用相同的命令,我该怎么做?代码应该是什么?

作为该领域的业余爱好者,目前正在努力解决这个问题!

【问题讨论】:

标签: python ffmpeg video-capture


【解决方案1】:

当然,您可以通过 subprocess 模块从 Python 执行该命令,但最好使用像 https://github.com/kkroening/ffmpeg-python 这样的库

【讨论】:

  • 就像os.system() 文档本身所建议的那样,更好的解决方案是subprocess 包。但当然,原生 Python 库会更好。
【解决方案2】:

我会推荐PyAV。它是 ffmpeg 库的适当包装器。

其他提到的包使用“子流程”方法,这是有限且低效的。这些库可能比普通的 ffmpeg API 更方便。

【讨论】:

    【解决方案3】:

    感谢您的帮助! 这是我目前正在使用的代码的 sn-p,它给出了我需要的结果。 除了使用场景变化检测的帧形成之外,我还添加了一个用于生成帧时间戳的功能

    ================================================ ==============================

    >     # FFMPEG Package call through script
    >     # need to change the location in the cmd post -vsync vfr to the location where the frames are to be stored 
    >     # the location should be same as where the videos are located
    

    ================================================ ==============================

    inputf = []
    for filename in os.listdir(path):
        file= filename.split('.')[0]             # Splits the file at the extension and stores it without .mp4 extension
        input_file = path + filename
        inputf.append(input_file)                # Creates a list of all the files read
        for x in range (0, len(inputf)):
            cmd = f'ffmpeg -i {inputf[x]} -filter_complex "select=gt(scene\,0.2), scale=640:360, metadata=print:file=time_{file}.txt" -vsync vfr {path where the videos are located}\\{file}_frame%d.jpg'
            os.system(cmd)
            x=x+1
        print("Done")                            # Takes time will loop over all the videos
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-11
      • 1970-01-01
      • 2013-09-23
      • 2023-02-07
      • 2023-03-24
      • 2021-09-23
      • 2017-08-19
      • 2012-07-11
      相关资源
      最近更新 更多