Ffmpeg-python: Splitting video into frames

Created on 5 Jun 2019  路  2Comments  路  Source: kkroening/ffmpeg-python

Hello,

Thanks for the repo - it's very well documented!

One thing I haven't been able to find an example for is how to take a video input and split it into frames that we save to disk (e.g. as jpegs). I'm sure this is very simple and I've missed something - would appreciate if you could point me in the right direction.

Thanks,
Sam

answered question

Most helpful comment

Probably, to do it with ffmpeg-python, you can make use of something like this:

try:
    (ffmpeg.input('test.mp4')
          .filter('fps', fps=2)
          .output('test/%d.png', 
                  video_bitrate='5000k',
                  s='64x64',
                  sws_flags='bilinear',
                  start_number=0)
          .run(capture_stdout=True, capture_stderr=True))
except ffmpeg.Error as e:
    print('stdout:', e.stdout.decode('utf8'))
    print('stderr:', e.stderr.decode('utf8'))

All 2 comments

Try this. Let's assume the video you want to split is called split_this.mp4:

import cv2
import numpy as np
import os

filename = "split_this.mp4"

src = cv2.VideoCapture(filename)
fps = src.get(cv2.CAP_PROP_FPS)

# the split frames will go into the "data" folder in the current directory
os.makedirs("data")

frame_num = 0
while(True):
    # Capture frame-by-frame
    ret, frame = cap.read()

    # Saves image of the current frame in jpg file
    name = './data/frame' + str(frame_num) + '.jpg'
    print ('Creating...' + name)
    cv2.imwrite(name, frame)

    # To stop duplicate images
    currentFrame += 1

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()

Source

Probably, to do it with ffmpeg-python, you can make use of something like this:

try:
    (ffmpeg.input('test.mp4')
          .filter('fps', fps=2)
          .output('test/%d.png', 
                  video_bitrate='5000k',
                  s='64x64',
                  sws_flags='bilinear',
                  start_number=0)
          .run(capture_stdout=True, capture_stderr=True))
except ffmpeg.Error as e:
    print('stdout:', e.stdout.decode('utf8'))
    print('stderr:', e.stderr.decode('utf8'))
Was this page helpful?
0 / 5 - 0 ratings