使用ffmpeg python将视频分割为图像

使用ffmpeg python将视频分割为图像,python,video,ffmpeg,Python,Video,Ffmpeg,据我所知,Python的主包是直接操作ffmpeg 现在,我想拍摄一段视频,并将其帧以fps格式保存为单独的文件 有很多命令行方法可以做到这一点,例如,ffmpeg-i video.mp4-vf fps=1 img/output%06d.png 但是我想用Python来做。还有一些解决方案使用Python的子流程调用ffmpegCLI,但对我来说,它看起来很脏 有没有办法使用ffmpeg-python?我建议您尝试使用以下代码作为起点: import imageio reader = imag

据我所知,Python的主包是直接操作
ffmpeg

现在,我想拍摄一段视频,并将其帧以fps格式保存为单独的文件

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

但是我想用Python来做。还有一些解决方案使用Python的
子流程
调用
ffmpeg
CLI,但对我来说,它看起来很脏

有没有办法使用
ffmpeg-python

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

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)
你也可以用它

参考代码:

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.")
导入cv2
视频捕获=cv2.VideoCapture(“您的视频路径”)
视频捕获集(cv2.CAP\u PROP\u FPS)
已保存的\u帧\u名称=0
当视频_capture.isopend()时:
frame_是_read,frame=video_capture.read()
如果第_帧被_读取:
imwrite(f“frame{str(saved_frame_name)}.jpg”,frame)
已保存的\u帧\u名称+=1
其他:
打印(“无法读取帧。”)

以下内容对我很有用:

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

看起来像是您要查找的。方法
。set
返回
False
。这意味着FPS不能直接设置为视频捕获。我只是用
continue
跳过了多余的帧