Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Mathematica's Import command is purportedly able to import .AVI files. However, I find that many files that I want to import do only result in MMA showing a blank frame or another placeholder. All or some of my video-players are able to show these AVI files.

This is an example where it works as advertised:

Import["", {"ImageList", 2}]

Mathematica graphics


Import["", {"ImageList", 2}]

doesn't work (VLC does). I get an empty white frame.

More examples:

Import["", {"ImageList", 2}]

(works on VLC and mediaplayer classic)

Import["", {"ImageList", 2}]

BIG-126 MB!!
Should have looked like this: Mathematica graphics and works on Windows Media Player, VLC and mediaplayer classic, but I get a white box in MMA.

This is on windows 7-64/MMA8.04. I have the k-lite codecs pack installed.


Responding to Thomas' comment below I found most of the sample files I linked to in my original post gone. I tried to gather a new set and found some that worked and some that don't:


Import[#, "VideoEncoding"] & /@ 

{"msvc", "MJPG", "msvc", "MJPG", "msvc", "msvc", "YUV", "Uncompressed"}

Don't import:

Import[#, "VideoEncoding"] & /@

{"XVID", "MP42", "MJPG", "DX50"}

share|improve this question
I think it has to do with how it is encoded. A lot of the video files you see floating on the internet with a .avi extension are not really AVI files. I'm don't think that mma utilizes the codecs in your system, whereas VLC does. As such, mma can only import videos that are truthful to the AVI specs (if there are any) – R. M. Jan 18 '12 at 14:53
@R.M The problem is that AVI is a container which may hold variously encoded material. Apparently, but not entirely understandable, MMA is not able to handle many codecs although they are present on my PC. However, I would have expected the Microsoft test movies to work, as they are explicitly meant as confirming to MS standards. – Sjoerd C. de Vries Jan 18 '12 at 14:59
<insert "MS has standards?" joke here?> :) – R. M. Jan 18 '12 at 15:02
I am not that familiar with Windows's multimedia playback system, but my guess is this: Windows has two big frameworks: Video for Windows (VfW) and DirectShow (DS). VfW is outdated, but seems to be still used by Mathematica. When you install codecs, most of them are made for DS, and won't work with VfW. – Szabolcs Jan 18 '12 at 15:28
This may be a Windows-specific issue: With Mathematica 8.0.4 under Mac OS X 10.6.8, I evaluated each of the Import commands you listed, and every one of them worked except for the final one -- the star collapse. But even for that one, I don't get a white box; rather, I get a black box with some variegated pixels in a band along the top of that box. I cannot test the Microsoft .avi's you cite, since the link you gave is to an .exe, and of course an .exe is not an OS X executable. – murray Jan 18 '12 at 16:01
up vote 26 down vote accepted

64-bit Windows only

Note for Mathematica version 10: The Wolfram Library has been updated in version 10 and you will need to recompile the MathMF DLL. This is most easily accomplished by evaluating "MathMF"//FindLibrary//DeleteFile prior to loading the package.

Link to package on GitHub

I have written a package called MathMF which uses a LibraryLink DLL to do frame-by-frame video import and export with Windows Media Foundation. It should be able to read a reasonable variety of movie files, including AVI, WMV and MP4. Exporting is currently limited to WMV and MP4 formats (AVI encoding is not natively supported by Media Foundation)

Here is the sort of code you can write with it. The code first opens a video file for reading, and creates a new video file for writing to. It then runs a loop in which each frame is sequentially read from the input stream, processed in Mathematica and then written to the output stream. So Mathematica is effectively being used as a video filter.

{duration, framerate, width, height} =

 width, height, "FrameRate" -> framerate]

  (image = MFGrabFrame["RealImage"]) =!= EndOfFile,
  MFSendFrame @ GradientFilter[image, 2]
  ] ~Monitor~ image


The package can be downloaded from the GitHub link at the top of this post, it is too large to include in full here.

The package includes the library source code, and on first use will attempt to compile the library locally. I believe the compilation should work if you have Visual Studio 2010 or later installed, and probably won't work if you use a different compiler. There is a pre-built DLL available if the compilation fails (see the readme on GitHub for more details)

I hope some people find this useful, it has been hovering in my mind as something to try to do for quite some time, hindered mainly by my total lack of experience with C++ and COM programming.

share|improve this answer
Excellent Simon! I had to download the Visual C++ Redistributable for Visual Studio 2012 before it worked for me, but after that, flawless. One small point: I don't think we should distribute software in the form of binaries, as that can have terrible security implications. – Sjoerd C. de Vries Apr 27 '14 at 14:07
@Sjoerd, thank you. I agree about distributing the binary, I have now uploaded a .m package and the C++ source code. The package will attempt to build the library from source when it is first loaded. There are various header files which the compiler needs, I think any system with VS2012 or the Win7 SDK will have these available but I'm not sure. I'd be grateful if you could try it out when you have the chance. – Simon Woods Apr 27 '14 at 20:34
as confirmed in chat, compiling works. Using avi to wmv converters available on the internet should allow avi import as well. – Sjoerd C. de Vries Apr 27 '14 at 22:07
@Sjoerd, avi import should just work. MF supports reading avi, just not writing. – Simon Woods Apr 27 '14 at 22:15

If you only need to access the video frame by frame you can use ffmpeg to decode the video.

Reposting a previous answer:

If you only want to read it linearly you can tell ffmpeg to dump the video to stdout and then read width*height*bytes-per-pixel bytes at a time to get the video frame-by-frame:

openVideo[fname_, w_, h_] := 
  video["stream"] = 
    "!ffmpeg -i " ~~ fname ~~ " -loglevel quiet -f rawvideo -pix_fmt rgb24 -", 
    BinaryFormat -> True];
  video["SkipFrame", n_Integer: 1] := Skip[video["stream"], Byte, n*3*w*h];
  video["NextFrame"] := Partition[Partition[
     BinaryReadList[video["stream"], "Byte", 3*w*h]
   , 3], w];
  video["NextFrame", n_Integer] := Table[video["NextFrame"], {n}];
  video["NextImage"] := Image[video["NextFrame"], "Byte"];
  video["NextImage", n_Integer] := Table[video["NextImage"], {n}];

Here's an example:

(* Create a test movie *)
file = $TemporaryPrefix <> "testvid.avi";
Export[file,Table[Rasterize[i, ImageSize -> {352, 200}], {i, 1, 50}]];

video = openVideo[file,352,200]



For some reason Mathematica seems to read through the entire video when doing Close[video["stream"]] and it might be worth to kill -15 the ffmpeg process manually to speed it up for large files.

For a 720x404 h264 mp4 video on a modest laptop it takes 13s to skip 200 frames and 0.07s to read a frame. The skipping could possibly be sped up by starting a new ffmpeg process that begins at desired frame. I would compare speeds to Import but Mathematica can't read it (for the test video Import takes .2s for first frame compared to .02s ). So as a bonus with this way you can work with many many more video formats :)

share|improve this answer
Doesn't seem to work for me. I tested that ffmpeg is present; tried it either with specifying its full path, and without path but directory set with SetDirectory. Nothing. Also tested with ReadList["!\"c:\\Program Files (x86)\\ConvertHelper\\ffmpeg.exe\" -h \ ", String] that ffmpeg actually executes. It does. – Sjoerd C. de Vries Apr 10 '13 at 17:32
There is a minus sign without option letter at the end of the ffmpeg call. Is that intentional? – Sjoerd C. de Vries Apr 10 '13 at 19:56
@SjoerdC.deVries The last - makes it output to stdout, do you get anything interesting on stderr when running from a terminal without -loglevel quiet? (Whatever windows equivalent of ffmpeg -i video.avi -f rawvideo -pix_fmt rgb24 - >/dev/null is, maybe > nul) – ssch Apr 11 '13 at 22:47
For comparison, this is what I get for above test video: – ssch Apr 11 '13 at 22:58
I upgraded ffmpeg to a newer version. That did the trick. Thanks! An improvement would be to remove the need to enter the frame size. – Sjoerd C. de Vries Apr 12 '13 at 21:33

(Thanks to K.H. from Wolfram):

Even on Windows, if QuickTime is installed, Mathematica uses the native QuickTime install (excluding added, third-party codecs) to import AVI files, and the supported codecs are listed in:


(Without an installation of QuickTime, only uncompressed AVI files are supported by Mathematica.) For the file, QuickTime on Windows can not open it, so Mathematica has problem importing it.

share|improve this answer
Thanks, that explains a lot. So, we must hope Apple upgrades QT a bit then... – Sjoerd C. de Vries Jan 20 '12 at 20:34
Given the current legal wranglings over video formats, and the fact that AVI is considered to be inferior to current formats, and the fact that Apple doesn't like supporting other people's technology, hope is the operative word... – cormullion Jan 21 '12 at 10:50

I think the problem is the fact that you cannot open the video at

by QuickTime. Try it. Download free QuickTime and try to open it in that player and is most probably won't work. It means that to make the video a non-standard video encoding was used which is not a subset of formats understood by QuickTime. High chance this is why M. cannot open it - my best guess.

share|improve this answer
Well, I'm on windows and Quicktime player is not the first choice on that platform. Anyway, that doesn't show a thing. As I wrote above, all the movies listed can be played by one of more of the players I have on my PC. The codec pack installed on my PC makes an enormous amount of standard and exotic codecs available to Windows and any player/importer that uses the appropriate system API should be able to use them. It looks like MMA doesn't go through the codecs that are installed on the machine and instead only uses a few built-in codecs; apparently a very small set. – Sjoerd C. de Vries Jan 20 '12 at 18:47

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.