Search code examples
pythonvideoffmpeg

Split video into images with ffmpeg-python


As far as I understand ffmpeg-python is main package in Python to operate ffmpeg directly.

Now I want to take a video and save it's frames as separate files at some fps.

There are plenty of command line ways to do it, e.g. ffmpeg -i video.mp4 -vf fps=1 img/output%06d.png described here

But I want to do it in Python. Also there are solutions [1] [2] that use Python's subprocess to call ffmpeg CLI, but it looks dirty for me.

Is there any way to to make it using ffmpeg-python?


Solution

  • I'd suggest you try imageio module and use the following code as a starting point:

    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)