Reputation: 2625
I've a python script which basically converts a video into images and stores them in a folder, then all this images are read and informations are extracted from them, then images are deleted. Since the writing images step is so slow and is apparently useless for what I need, I would like to store images somehow in memory instead of the disk, read this images from there and doing my operations, this would speed up my process a lot.
Now my code look like: 1st step:
ffmpeg -i myvideo.avi -r 1 -f image2 C:\img_temp
2nd step:
for i in range(1, len(os.listdir(IMGTEMP)):
#My operations for each image
3rd step:
for image in os.listdir(IMGTEMP):
os.remove(IMGTEMP + "\\" + image)
Upvotes: 3
Views: 3728
Reputation: 3780
With MoviePy:
import moviepy.editor as mpy
clip = mpy.VideoFileClip("video.avi")
for frame in clip.iter_frames():
# do something with the frame (a HxWx3 numpy array)
Upvotes: 1
Reputation: 43495
Have a look at PyAV. (There is documentation, but it's rather sparse.)
It looks like you could just open a video, and then iterate over the frames.
Upvotes: 1
Reputation: 11174
The short version is that you could use ramdisk or tmpfs or something like that, so that the files are indeed actually stored in memory. However, I'm wondering about your "operations for each image". Do you really need an image file for them? If all you're doing is read their size, why do you need an image (with compression/decompression etc.) overhead at all? Why not just use the FFmpeg API, read the AVI file, decode frames, and do your metrics on the decoded data directly?
Upvotes: 1