Search code examples
pythonvideoimage-processingvideo-capturevideo-processing

How to extract slides from a video using python


I have a video training course supplied as AVI files. Most of the screens are shown as slides with a mouse pointer moving around on them.

I'd like to capture a screenshot of the slide automatically when the screen changes (ignoring when the image changes a small amount due to the mouse pointer moving around.)

I want to do this so I can paste the images into a word or html document that I can add notes to as I learn as at the moment I'm taking screenshots but it's very slow and tedious and the course is very long (around 24 hours total play time).

I know python well but am unsure as to how I would go about extracting stills from a video file and then how to compare one still with another to see how much they differ to decide which to keep and which to discard.

Can anyone suggest how to go about doing this?


Solution

  • A tool like ffmpeg is suited for extracting images from a video. From the manual:

     ffmpeg -i foo.avi -r 1 -s WxH -f image2 foo-%03d.jpeg
    

    This will extract one video frame per second from the video and will output them in files named foo-001.jpeg, foo-002.jpeg, etc. Images will be rescaled to fit the new WxH values.

    Comparing them for differences can then perhaps be done by PIL and/or OpenCV.

    EDIT: I just realized that it probably would be more efficient to only grab the key frames (intra frame), because those occur when a drastic change in the scene happens. A quick google later we have this:

    ffmpeg -i foo.avi -vsync 0 -vf select="eq(pict_type\,PICT_TYPE_I)" -s WxH -f image2 foo-%03d.jpeg