I am generating an animation in Matlab, composed of simple diagrams. The images are characterized by fine structures, strong contrasts and mostly discrete colors, e.g. there are few-pixels wide black lines, dots or small patches of uniform color, few-pixels high antialiased text, etc. Here's a screen shot of a typical frame:

enter image description here

What changes from frame to frame are the colored patches on the left, the time ("t = "), and the position of the black dot in the right panel.

Using a standard video codec on this which was designed for full-frame moving photographic imagery generates strong artefacts even with weak compression. Without compression however, the video file becomes unmanageably huge.

My question: Is there a video codec that is optimized for this kind of material? If possible, it would be optimal if an encoder is included in ffmpeg, and if there is a decoder (codec implementation) available for Windows.

  • Will need some details: what ffmpeg command did you use to generate the bad looking video? What is the target bitrate or file size you're aiming for? Do you need a codec whose decoder can be freely distributed or is a commercial solution Ok? – Gyan Aug 19 '15 at 5:37
  • @Mulvya, thanks for your comment. bad looking video: I tried H.264/mp4, and increasing the bitrate only helped when the compressed video got almost as large as the original file. file size: my uncompressed video has a size of 25 GB, which cannot e.g. conveniently transferred via internet. As size below 500 MB would be nice. Decoder: Optimally it would be something that can be freely distributed, e.g. something that VLC supports. – Apart from the problem I'm facing right now, I'd be interested in some general pointers in how to deal with this kind of material. – A. Donda Aug 19 '15 at 15:16
  • I made some progress myself and will post a tentative answer soon. I'd be very interested in comments on how to improve upon that. – A. Donda Aug 19 '15 at 15:16

I read up a bit and made some experiments with lossless codecs, getting decent results. I'd be interested in comments on this, especially if there are lossless or lossy alternatives that I overlooked.

I tried the following codecs / formats in ffmpeg:

  • Lossless Motion JPEG2000 / AVI

    ffmpeg -i test.avi -vcodec jpeg2000 -strict -2 -pred 1 test_jpeg2000.avi

    compression ratio 19.59%

  • Lossless VP9 / WebM

    ffmpeg -i test.avi -vcodec vp9 -lossless 1 test_vp9.webm

    compression ratio 0.52%

  • HuffYUV / AVI

    ffmpeg -i test.avi -vcodec huffyuv test_huffyuv.avi

    compression ratio 37.42%

  • Lossless H.264 / MP4

    ffmpeg -i test.avi -vcodec h264 -qp 0 -preset veryslow test_h264.mp4

    compression ratio 0.53%

Since HuffYUV did not perform well, I excluded it from further tests.

I then tested playback support on Linux and Windows, trying mplayer, xine, vlc, chrome, and firefox on Linux (Debian testing) and vlc, windows media player, chrome, and firefox on Windows (8.1). I included browsers because WebM is specifically designed for web video. Results:

  • Lossless Motion JPEG2000 / AVI

    plays on Linux with mplayer

  • Lossless VP9 / WebM

    plays on Linux with vlc and chrome and on Windows with chrome and firefox

  • Lossless H.264 / MP4

    plays on Linux with mplayer, vlc, and chrome, and on Windows with vlc and chrome

That a video "plays" means it is supported by the player out of the box, and playback is correct. I found several cases where the video was "supported" but playback was buggy.

Though Lossless VP9 has the best compression ratio (by a tiny margin), for the time being I decided to use Lossless H.264 because it has better playback support.

  • Have you looked into H.265 (aka x265)? I've never encoded with it, but the results look very promising. Animated video compresses about 50% more than H.264 (results will vary of course). The quality is sill phenomenal. Playback support will probably be worse, since its a newer codec. – Doug.McFarlane Aug 19 '15 at 19:11
  • I am having good results with .flv and codec On2 VP6 in Adobe Media Encoder. Roughly 200 Mb for 40 minutes of 640x480 1fs slideshow – Rho Phi Feb 27 '18 at 18:00
  • Probably not relevant anymore but what about the PNG-based formats APNG and MNG? And what about WebP? – Christian Dec 5 '19 at 0:40
  • I haven't really looked into any more it since then. If I remember correctly, I considered MNG, but it never really got off the ground. I hadn't heard about APNG. And I didn't know that WebP supports lossless animation. – A. Donda Dec 5 '19 at 2:15
  • @Christian, this littlesvr.ca/apng/gif_apng_webp5.html might interest you. If you happen to look into this further, I'd welcome an alternative answer! – A. Donda Dec 5 '19 at 2:16

If you're OK with formats requiring proprietary encoders, I suggest you check out RealVideo. I haven't been using this format for a while, but I remember 10 years ago I could compress DVD cartoons into < 500 MB RMVB files with very little quality loss, while XviD files of similar bitrate were extremely blocky.

I suppose your MATLAB animation will be similar to cartoons: few colors used, sharp borders between colors, only a small part of the frame actively changing, etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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