I want to extract video frames and save them as image.
import os, sys
from PIL import Image
a, b, c = os.popen3(\"ffmpeg -i test.avi\")
out = c.read()
dp =
import numpy as np
import cv2
capture = cv2.VideoCapture(0)
size = (int(capture.get(3)),int(capture.get(4)))
fourcc = cv2.VideoWriter_fourcc(*'XVID')
video = cv2.VideoWriter('output.avi',fourcc,30,size)
print capture.isOpened()
num=0
while True:
ret, img = capture.read()
video.write(img)
cv2.imshow('video',img)
cv2.imwrite("image" + str(num) + ".jpg",img)
num = num + 1
key= cv2.waitKey(1)
if key == ord('q')
num=0
break
video.release()
capture.release()
cv2.destroyAllWindows()
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
There are ffmpeg python modules are available
ffmpeg: https://code.google.com/p/pyffmpeg/
pyAV: https://github.com/mikeboers/PyAV
import av
container = 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)
Easy way, use Open CV.
import cv2
vc = cv2.VideoCapture('Test.mp4')
c=1
if vc.isOpened():
rval , frame = vc.read()
else:
rval = False
while rval:
rval, frame = vc.read()
cv2.imwrite(str(c) + '.jpg',frame)
c = c + 1
cv2.waitKey(1)
vc.release()
Your ffmpeg does not support png File Format. You should use jpg or gif instead. Also follow this post (first posted by 'yesterday').
There is much easier way to do this. Just use OpenCV
and get the all the frame required.
refer to the answer here: How to extract frames from Videos
It's done using Python3
and OpenCV 3+