【问题标题】:Split video into images with ffmpeg-python使用 ffmpeg-python 将视频分割成图像
【发布时间】:2021-01-16 11:14:42
【问题描述】:

据我了解ffmpeg-python是Python中的主要包,可以直接操作ffmpeg

现在我想拍摄一个视频并将其帧保存为单独的文件,以某些 fps 的速度。

有很多命令行方法可以做到这一点,例如ffmpeg -i video.mp4 -vf fps=1 img/output%06d.pngdescribed here

但我想用 Python 来做。还有一些解决方案 [1] [2] 使用 Python 的 subprocess 调用 ffmpeg CLI,但对我来说看起来很脏。

有没有办法使用ffmpeg-python 来实现它?

【问题讨论】:

  • 看起来 This 就是你要找的东西。

标签: python video ffmpeg


【解决方案1】:

我建议您尝试imageio module 并使用以下代码作为起点:

import imageio

reader = imageio.get_reader('imageio:cockatoo.mp4')

for frame_number, im in enumerate(reader):
    # im is numpy array
    if frame_number % 10 == 0:
        imageio.imwrite(f'frame_{frame_number}.jpg', im)

【讨论】:

    【解决方案2】:

    您也可以为此使用openCV

    参考代码:

    import cv2
    
    video_capture = cv2.VideoCapture("your_video_path")
    video_capture.set(cv2.CAP_PROP_FPS, <your_desired_fps_here>)
    
    saved_frame_name = 0
    
    while video_capture.isOpened():
        frame_is_read, frame = video_capture.read()
    
        if frame_is_read:
            cv2.imwrite(f"frame{str(saved_frame_name)}.jpg", frame)
            saved_frame_name += 1
    
        else:
            print("Could not read the frame.")
    

    【讨论】:

    • 方法.set 返回False。这意味着 FPS 不能直接设置为 VideoCapture。我只是用continue跳过了多余的帧
    【解决方案3】:

    以下对我有用:

    ffmpeg
    .input(url)
    .filter('fps', fps='1/60')
    .output('thumbs/test-%d.jpg', 
            start_number=0)
    .overwrite_output()
    .run(quiet=True)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-25
      • 1970-01-01
      • 1970-01-01
      • 2016-09-08
      • 1970-01-01
      • 2015-05-07
      • 1970-01-01
      • 2011-05-20
      相关资源
      最近更新 更多