# How to sample n frames from a movie file?

I'm looking to import 100 image frames from an mp4 video file at roughly evenly spaced intervals. As \$ImportFormats shows, Mathematica can handle

• Movie files: .avi, .mov, .qt, .flv
• Multiframe images: .swf, .tiff, .gif

If I convert the video it to a supported type, what would the syntax be to achieve this import, I don't want to import all the frames and then take a few!

You can use Import directly as follows:

Import["MVI_9855-183.mp4", {"AVI", "ImageList", Range[1, 110, 20]}]


This will import one frame every 20, between the first and 100th, and return them as a list of pictures.

As you can see above, at least in the case of the mp4 files I have at hand I was able to "convince" Mathematica to import them directly by specifying the AVI format for the file. I am no expert in video file format, however, so I can't claim that I understand why that worked, or whether it will work for you as well.

If you are on Windows, you may also be interested in Simon Woods's MathMF package that carried out video input output functions using Windows Media Foundation. See it described in his answer to this question: Importing videos in Mathematica

• Weird! Nice trick though, do you know how to get the total frame count? – M.R. Jul 6 '15 at 0:04
• Oh it's just "FrameCount" – M.R. Jul 6 '15 at 0:14
• And the "AVI" is not necessary. – M.R. Jul 6 '15 at 0:14
• @M.R. so it works without the "AVI" on your side even if the file extension is not *.avi? That's interesting: if I try to feed an *.mp4 file directly to Import it fails, saying "Cannot import data as ImageList". – MarcoB Jul 6 '15 at 0:43