Today I’ll be talking about… exactly what the title suggests! I got an idea a couple of days back for a video processing project and I decided that I was going to use Python 3 and OpenCV 3, because I like shiny new things. I ran into a little bit of trouble getting ffmpeg support on OpenCV, and without it you can’t grab frames from a video file. But with my Google-Fu and sheer genius I eventually pulled through.

Here’s the instructions!

  1. Install ffmpeg using Homebrew (terminal command: brew install ffmpeg)
  2. Follow instructions on Luis González’s blog post EXCEPT when you’re configuring the OpenCV build, also
    • Set FFMPEG_INCLUDE_DIR as /usr/local/Cellar/ffmpeg/2.4.2/include/
    • Set FFMPEG_LIB_DIR as /usr/local/Cellar/ffmpeg/2.4.2/lib/ (You can type ffmpeg in your terminal to find the exact paths for the two values above)
    • Tick the box of WITH_FFMPEG
  3. Continue with Luis González’s instructions and you should be good!

To test whether the codec worked, you can try something like the following:

import cv2

cap = cv2.VideoCapture('my_video.mp4')

frames = []
while cap.isOpened():

    # Capture frame-by-frame
    read_success, frame = cap.read()

    if not read_success:
        break

    frames.append(frame)

print('Grabbed ', len(frames), ' frames')

Hope this was helpful!