Search code examples
pythonffmpegpipedisk

ffmpeg - output images in memory instead of disk


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)

Solution

  • 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)