Extract Video Frames In Python Extract Video Frames In Python python python

Extract Video Frames In Python


ffmpeg is complaining about there being a missing %d in the filename because you've asked it to convert multiple frames.

This post suggests this would be a better way of using ffmpeg to extract single frames

ffmpeg -i n.wmv -ss 00:00:20 -t 00:00:1 -s 320×240 -r 1 -f singlejpeg myframe.jpg

[edit]

After a bit more research, here is a command line which works outputing single png frames

ffmpeg -i test.avi -vcodec png -ss 10 -vframes 1 -an -f rawvideo test.png

Tested on my ubuntu 12.04 laptop


Easy way, use Open CV.

import cv2vc = cv2.VideoCapture('Test.mp4')c=1if vc.isOpened():    rval , frame = vc.read()else:    rval = Falsewhile rval:    rval, frame = vc.read()    cv2.imwrite(str(c) + '.jpg',frame)    c = c + 1    cv2.waitKey(1)vc.release()


There are ffmpeg python modules are available

ffmpeg: https://code.google.com/p/pyffmpeg/

pyAV: https://github.com/mikeboers/PyAV

import avcontainer = av.open('/path/to/video.mp4')for packet in container.demux():    for frame in packet.decode():        if frame.type == 'video':            frame.to_image().save('/path/to/frame-%04d.jpg' % frame.index)