Skip to content

Writing AVI videos in pure Java

by werner on August 12th, 2008

Since writing QuickTime videos in pure Java turned out to be relatively easy, I gave today a try at an AVI encoder for PNG and Motion-JPEG encoded videos.

The AVIOuputStream class is freely available as part of my CubeTwister source code. You can download a small demo which only contains the AVIOutputStream and all necessary sources here: AVIDemo.jar. Running the demo will create a Motion JPEG-encoded and a PNG-encoded AVI video.

I am not sure, if AVI supports PNG-encoded videos at all, because Microsoft Media Player 11 can’t play them. The QuickTime player works fine with them though.

The source code is available under the terms of the Creative Commons Attribution 3.0 license.

Enjoy. πŸ™‚

Update 2012-02-04:Β The demo code described in this article is obsolete. The latest version of the code is available on the following page:

Update 2011-01-17: AVIOutputStream can now also write RLE-encoded videos with a depth ofΒ 8-bits per pixel.

Update 2009-08-30: AVIOutputStream can now also write RAW-encoded video.

From → Image & Video, Java

  1. Mario Z permalink

    When I use the demo it creates the jpg avi and png avi (obviously), but when I use Windows Media Player and Media Player from Mega Codec, the playback is playing 1 frame (actually worse) per second…effectively it thinks the 3 second avi is 50 minutes long. Quicktime plays it in 3 seconds.

    Do you know why that would be?


  2. Hi Mario,

    You are right, the frame rate and time scale values are not computed properly. I had mixed up AVI “frame rate” with QuickTime “frame duration”.

    I have fixed this now, and uploaded a new version.
    It should play back now correctly in Windows Media Player as well.


  3. Thanks for this; I didn’t even realize AVI’s could be written with JPEG’s. This works great. πŸ™‚

  4. Hi Jeremy,

    Didn’t you mean “PNG’s”?
    Because, I thought, I got the idea for writing JPEG’s into AVI from you. πŸ˜‰

  5. I haven’t tried the class yet but I am sure that many Java developers out there really want this kind of functionality and it was pretty difficult to find πŸ™‚ I develop Java games and I figured that this could be a nice way for players to generate in-game videos.
    Thanks for developing this!

  6. Hi Per,

    Just keep in mind that the video files may quickly become huge.

    After generating an in-game video from Java (I highly recommend generating AVI-PNG or QuickTime-PNG), you may want to run it through an MPEG-4 H.264 baseline encoder. The final video will be very small, and look very crisp. Best thing is, it will work seamlessly with Flash, QuickTime, iPod/iPhone and Windows MediaPlayer.

  7. Chris Dallimore permalink

    Hi just wondering which H.264 encode you are using, I haven’t been able to convert the files using ffmpeg


  8. Hi Chris,

    I use QuickTime Pro.

    I just tried with ffmpeg. It works fine with the JPEG-encoded sample video, but it doesn’t support the PNG-encoded sample video.

    I also tried ffmpeg with my QuickTime sample videos. Its the same here: JPEG-encoded video works, PNG-encoded video does not.


  9. Hello!
    I just wanted to tell you that I have integrated your software into my game engine and it works great!

    Do you plan to add audio as well?

    If you don’t do it, I will eventually do it myself and you can get my code if you want to. I just have to read those RIFF specs first…

  10. Hi Per,

    Do you have a link to your game engine?

    I have currently no need for audio support in AVIOutputStream.

    If you add it to it, I would be very pleased to get your code. 8)


  11. The game engine/tool is currently under development, but I use your AVIOutputStream for every ingame Youtube video I make, except when the actual sound from the game is required. I will give you a link when I release my next game which will happen within 6 months (I hope πŸ™‚ ).

    One huge benefit I get when I use your software is that I get very smooth videos which is sometimes not possible when I use external software such as Fraps, Taksi and CamStudio due to slowdowns.

  12. Hi Per,

    I have recently added audio support to my QuickTimeWriter class. Since you re-compress your videos for YouTube anyway, it shouldn’t make a difference if you use QuickTime instead of AVI as an intermediate format.

  13. Dear Werner,

    thanks for an excellent piece of code. It really helped me to add a nice video output feature to a visualisation module for a simulation model. And after running it through ffmpeg with H264, we get beautiful and compact video files. Very nice!

  14. djtoon permalink

    hi all πŸ™‚
    im trying the raw format and i get word colors from the avi
    i use tga’s ….or…what image format i need for raw avi encoding ?


  15. Hi djtoon,

    AVI RAW images are encoded as Microsoft DIB (device independent bitmap). The pixels of a frame are written row by row from bottom to top and from the left to the right. 24-bit pixels are encoded as BGR.


  16. This looks really interesting. Do you provide pre-compiled jar files? I tried building your latest stuff with netbeans and I’m getting a pile of errors (GPLLicense interface frequently) that break the build, particularly: C:\Users\Alexander\Desktop\Media\nbproject\build-impl.xml:263: srcdir “C:\Users\Alexander\Desktop\Media\src\jai.core” does not exist!
    We could really use this tool, but I can’t seem to get it working.

  17. Marc permalink

    Thank you so much! I just want you to know that, after trying many free and proprietary tools to try simply to capture video from my screen, all of which failed in one way or another, you little Java app works perfectly! Awesome.

  18. Fernando permalink

    Well done!!! Saved me a lot of work

Trackbacks & Pingbacks

  1. I was wondering if it is possible to record with Java

Comments are closed.