Videos in Matlab and Linux

If you do computer vision research (as some of you may). Or any other type of research for that matter… Its a good idea to make nice videos of the results that you obtain. This is a simple disaster-proof way of demoing your work. I’ve been doing this quite a bit lately (as a result of all the video tracking work). I’ve come up with some tips, tricks, and tools that will help out the aspiring video-creator (who uses Matlab and Linux… otherwise you’re out of luck).

Personally, I find Matlab’s built-in avi code terrible. It’s just bad. Instead, I prefer to make figures that show what I want to show on each frame, save those out, and then compile them into a movie later. This seems to be the nicest command to save the frames out:

>>for i=1:last_frame
>> %% code to create the frame in a figure
>> f = getframe;
>> imwrite(f.cdata, sprintf('./video/%04d.png',i));

This should leave you with a directory full of .png files. From here you have to assemble these into a movie. This is a fantastic script called mkmpeg4 (download). The way to run it is as such (by the way, this only works in Linux with mplayer installed)

$mkmpeg4 -o output.avi -f 30 `ls *.png`
$mplayer output.avi

Note the ` quotation marks as opposed to your typical ‘ marks. This makes compressed videos that look good and will still play on Linux, Windows, and Mac. (Also, they work in PowerPoint). One final tip. If you want to go the other way; take a video and convert it to frames, here’s how:

$mplayer -vo png movie_to_unpack.avi

If you video file is interlaced (looks good in a player, extracted images look bad), try this instead:

$mplayer -nosound -vf pp=ci -vo png:z=0 movie_to_unpack.avi

Happy Matlab Video-ing. Feel free to post your own tips or correct mine in the comments.