Skip to content

Pure-Java Screen Recorder (obsolete)

by werner on May 5th, 2011

I am working on a new AVIWriter class, which provides a higher level API than the  AVIOutputStream class.

There is still a long way to go, so I decided to write a screen recorder as a demo for the upcoming class.

Screen Recorder

This recorder uses java.awt.Robot to capture the screen, and java.awt.MouseInfo for capturing the mouse cursor.

Screen changes are captured with a frame rate of up to 15 fps. Mouse cursor movements with up to 30 fps.

For encoding the videos with AVI, I have implemented the Techsmith Screen Capture Codec in pure Java. This codec is essentially the Microsoft RLE encoder with ZIP compression applied to each video frame.
To play back a recorded video, you may have to install the free codec from www.techsmith.com.

For QuickTime, the videos are encoded with the Apple Animation codec, which I already had implemented some time ago.

You can download the file ScreenRecorder.jar. Double click the file to start the screen recorder. The .jar file also includes the source code of the recorder. The code is preliminary, and may substantially change in future releases.

Update 2012-02-04:

A newer version of the screen recorder is available at http://www.randelshofer.ch/monte

Update 2012-01-13:

The code described in this article is obsolete. Stay tuned for a new recorder which also supports audio capture in AVI files.

Update 2011-07-05:

The rate of screen and mouse captures can now be adjusted individually.

Update 2011-05-13:

I have added support for audio capture. This feature is currently only available with QuickTime videos, since AVIWriter is not finished yet. Audio is recorded from line-in. Line-in is usually the microphone of the computer.

Update 2011-05-09:

I have changed the code so that it will record into the home folder “Videos” on Windows, and into  “Movies” on Mac OS X and everywhere else. In the prior version, recording silently failed if this folder did not exist. Now, an error message is displayed.

Update 2014-01-22:
I have closed the comment section for this article, because the screen recorder class is obsolete. Take a look at the Monte Media Library for newer versions.

From → Image & Video, Java

69 Comments
  1. mittin permalink

    Hi there, this is exactly what I need. My question is when i record, the video gets saved, but not the sound, is there anything i need to do?

  2. Hi mittin,

    The AVIWriter class does not yet support audio. But QuickTimeWriter does.

    The recommended approach for recording audio, is to create a separate thread, which reads samples from AudioSystem.getLine(…), and then writes them into an audio track.

    I have updated the Screen Recorder demo. It can now record audio into QuickTime files.

  3. Kostya permalink

    Werner,

    I’d like to do a video recording of my java-based UI tests and would like to use your screen recorder. Is it possible to share complete source codes of your recorder? At the moment in jar file quite a few source files are missing.

    Thanks,
    Kostya

  4. Hi Kostya,

    Oops. I have now uploaded a new version of the .jar file. It should now include all source files.

    Best,
    Werner

  5. Dav permalink

    Hi, would it be possible to add a gui element so we can choose the frame rate. I use a slow frame rate of about 3 fps, it reduces file size and cpu usage. Thanks.

  6. Hi Dav,

    I have added fields for the screen capture rate and mouse capture rate.

    Best,
    Werner

  7. TSCC currently is not the best screen codec our there. I’ve study this for a long time and found some competitor perform better:

    NSCC at Nanoback Screen Capture Codec always created file smaller than TSCC. yes, ALWAYS!

    SCPR at Screen Pressor sometimes perform better than NSCC.

    I also test a lot of other lossless code and found most are far behind from TSCC. I test these codec by re-compressing the same video with the specific codec and compare the file size. I use VirtualDub.

    My reply maybe a little bit off-topic, but I think it may help for those interesting in screencasting software.

  8. Hi Alex,

    Yes, your reply is a little bit off-topic – nevertheless very interesting.

    The program presented here is just a proof of concept. So, I am trying to keep everything very basic. I chose TSCC because it was extremely simple to implement on top of the Microsoft RLE codec which I had implemented before.

  9. Apos permalink

    Did you have to do anything special to make java.awt.Robot to capture the screen faster? Usually, I can only get it to capture a few frames be second. I’ve searched and it seems like I’m not the only one that has this problem, yet, your screen recorder doesn’t seem to suffer from this slowness.

  10. Hi Apos,

    My screen recorder uses separate threads for screen capture and mouse capture. This allows to capture mouse movements at a higher rate than screen changes.

  11. Stefan Ettrup permalink

    Hello Werner, the solution you have made in the file ScreenRecorder.jar, would that be okay to use for internal use in a company, or do you any requirements regarding that?

    Thanks in advance.

    Stefan

  12. Hi Stefan,

    The code can freely be used under the terms of Creative Commons Attribution 3.0. The only requirement is, that you must keep an attribution to me in the source files.

  13. Stefan Ettrup permalink

    Hey Werner.

    Thanks for a quick response. The attribution shouldn’t be a problem, and the license I will look into,

    Have a great day.

    Regards Stefan

  14. Stefan Ettrup permalink

    Hey Werner.

    Du you have any plan for, when you’re having audio implemented together with the avi recording format. There is a huge different in size between the two formats when recording, an I would of course prefer the small one:)

    Thanks in advance.
    Regards Stefab

  15. Stefan – I have no time schedule for this library. I add features when I need them. If this is worth to you a small PayPal donation or an Amazon gift card, I can add audio support for AVI right away.

  16. Marc permalink

    Hi Werner.

    I am trying to use ur recorder without GUI. I just need to call using the main API.
    My idea is copy your ScreenRecorderMain.java to a ScreenTestRecorder.java and remover all the Swing code.
    Then I wrote a unit test like:

    public void proveOfConcptRecordTestVideo() throws Exception {
            ScreenTestRecorder screenTestRecorder = 
                 new ScreenTestRecorder();
            screenTestRecorder.startStopPerformed();
            Thread.sleep(5000);
            screenTestRecorder.startStopPerformed();
        }
    

    It works and creates a movie file, but when I try to play the file I get an error like “The document “ScreenRecording 2011-08-05 at 16.24.01.avi” could not be opened. The movie is not in a format that QuickTime Player understands.” However I can play the movies created using the original GUI.

    If I create Quicktime format files I get the same mistake.

    Do you have any advise about this problem? Do I am missing something essential while removing the Swing code? I My class still extends javax.swing.JFrame, but I removed all the Swing code that generates the GUI.

    Kind regards.
    Marc Andreu.

  17. Hi Marc,

    You get a corrupt movie file, because the unit test ends before the movie file has been fully written.

    This happens because method startStopPerformed() is designed as a GUI method. This method always returns immediately and performs all time consuming tasks asynchronously in a worker thread.

    There is no need to strip code away from ScreenRecorderMain, because you can use class ScreenRecorder directly. I have already designed this class, so that it can be used without GUI.

    Here is how your test method could look like:

    public void proveOfConcptRecordTestVideo() throws Exception {
    ScreenTestRecorder screenTestRecorder =
            GraphicsConfiguration gc = GraphicsEnvironment//
                    .getLocalGraphicsEnvironment()//
                    .getDefaultScreenDevice()//
                    .getDefaultConfiguration();
            ScreenRecorder sr = new ScreenRecorder(
                    gc,
                    "QuickTime", 24,
                    ScreenRecorder.CursorEnum.BLACK,
                    30, 15,
                    44100);
            sr.start();
            Thread.sleep(5000);
            sr.stop();
        }
    

    Best,
    Werner

  18. alvin permalink

    After I have record the movie (to avi format), I am not able to play the video with window media player 11. But I am able to play it with VLC player.
    Is there a way that I can play the file from window media player 11?
    Maybe after running a converter?

  19. Hi Alvin,

    To play back a recorded AVI video with WMP 11, you can install the Techsmith codec from http://www.techsmith.com/codecs.asp or any other codec pack which includes this codec.

    Alternatively, you can record in the QuickTime format. This format is supported out-of-the-box by WMP 11.

    hth,
    Werner

  20. alvin permalink

    After installing the codec, I am able to play the avi files. But for the quicktime files, I am still not able to play them on WMP 11. The quicktime files is not corrupted because I can play that from QuickTime player (version 7.6.9).

    I tried both (one suppose to have no audio recording and the other have audio recording):

    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    recorder = new ScreenRecorder(ge.getDefaultScreenDevice()
    .getDefaultConfiguration(), "QuickTime", 16, CursorEnum.NONE,8000);
    

    and

    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    recorder = new ScreenRecorder(ge.getDefaultScreenDevice()
    .getDefaultConfiguration(), "QuickTime", 16, CursorEnum.NONE,0f);
    

    Any other suggestion?

  21. Hi alvin,

    You are right. I tried now with WMP 12, and it can’t play back the QuickTime files out-of-the-box. Although Microsoft claims that QuickTime files 2.0 or earlier are supported.

    http://support.microsoft.com/kb/316992

    The knowledge base article does not list which QuickTime codecs are supported, so I assumed that WMP would support all codecs that QuickTime 2.0 supports. But this is apparently not the case.

    Looking at the codecs listed on this knowledge base article, I would say there is no suitable codec for screen recording provided by WMP 12 out-of-the-box. I guess the same is true for WMP 11. I suggest that you install a codec pack.

  22. Marc permalink

    Hello Werner,

    Many thanks for your reply.

    It works perfectly fine now.

    Thanks,
    Marc Andreu.

  23. Marc permalink

    Hi Werner,

    Yes as you said my problem was trying to remove Swing code. I did not know how to get a GraphicsConfiguration so I was extending JFrame and removing the other Swing code but still using the threaded methods to start and stop. I guess this caused problems while finishing the test was faster than waiting for the ScheduledThreadPoolExecutor instances to finish or some other thread related.

    By using your example is more clear how to get the GraphicsConfiguration and now looks all good.

    Many thanks,
    Marc Andreu

  24. Anup Mondal permalink

    please send me the source code…. i need it…. thanks in advance…:)

  25. Anup – The source is included in the .jar file. You can change the filename extension to .zip, and then unzip the file.

  26. tux permalink

    Hi,

    great job.
    I’d like capture audio in avi too.
    Please, check out how they do it, just using java.

    AVIWriter.java
    http://www.koders.com/java/fid470DE295CEDD264B73F59156C7B25CC5DCAD1609.aspx?s=Dirname
    From Jlipsync
    http://sourceforge.net/projects/jlipsync/

  27. tux permalink

    Hi werner,

    today I took a look at the web and found “videojs” player for html5 and I thought.
    You could use you tool to capture the video on desktop and save as mp4 to publish on html5 new video player.
    What do you thing? It seems to be useful to publish on youtube or make tutorials.
    Or else, could also do a java server to capture the desktop using vnc protocol and use noVnc on client to show.

    Check out:
    videoJs player html5
    http://videojs.com/

    noVNC html5
    http://kanaka.github.com/noVNC/

  28. Hi tux,

    Thank you for the links to AVIWriter from Jlipsync.
    I will certainly add audio support to my AVIWriter, when I need it. See my reply to stefan if you want me to add it earlier.

    As far as I know, there is no common video format in html5 that would work in all browsers. So, a recorded video would need to be converted into multiple formats – into H.264 for IE and Safari and into WebM for Chrome, Firefox and Opera. I think the conversion could be done with ffmpeg. I don’t know of a pure Java tool which could generate these formats.

    I haven’t tried if videos recorded with this tool are supported by youtube.

  29. tux permalink

    I understand.
    About H264 I found it.
    However, it’s integrated with jmf
    http://sourceforge.net/projects/h264avcjavaenco/
    And to decode there is pure java:
    http://code.google.com/p/h264j/
    and another
    http://code.google.com/p/jcodec/

    I’ll try to implement it too.
    Another idea that I’ve had about your tool, is to provide the option of capturing just a selected area, instead of fullscreen.
    It’s would make the final file size, extremely smaller.
    If I do it how can i repass the new features for you?

  30. Hm. I think the first two projects that you mentioned have not produced any code yet. jcodec might have working code, but they have not made any releases yet.

    I deliberately try keeping the screen recorder simple. Its sole purpose is to exercise my media library. If you do make changes that you think should go into the library, you can send them to me by e-mail.

  31. Hi Werner,

    I’m working on fully JWS deployment for both recording & playback but keep running into either native library dependancy issues or unsupported format during playback. Is there a pure java player that can handle the encoding format you’ve implmented? Or is there an alternate AVI writer that uses an encoding format that’s compatible with a JMF player?

    Thanks in advance.

  32. Hi JP,

    I have no pure Java player for these formats yet. I currently use QuickTime with the Perian plugin, MPlayer or VLC Player for playback.

    However, the formats that I used for this proof-of-concept screen recorder are extremely simple to implement with JMF.

    You can look up the descriptions for TSCC, Microsoft RLE or QuickTime RLE in the Multimedia Wiki.
    I would say, that a JMF decoder for one of these formats can be written in about three hours.

  33. Hi,
    thank you for providing that screen capturing in pure java.
    This is exactly what i was searching for.
    Maybe you want to include sound in the AVIs.
    If, for example you want to embed mp3 encoded audio
    you can get the converted sources of the lame project implemented in pure java here:
    http://jsidplay2.sourceforge.net/jump3r.jar
    Sources can be found on my project web-site.

    Ciao,
    Kenchis

  34. tux permalink

    That’s true.
    The api that Kenchis said is mp3spi

    http://www.javazoom.net/mp3spi/mp3spi.html

  35. Where does it save files to on Windows?

  36. Hi Vitaly,

    The screen recordings are stored in the “Videos” folder inside of the home directory.

  37. Jinfy permalink

    Hi, werner

    How can I write lines of subtitle on the movies. If yes, please give me an example.

    Thanks

  38. Shashikiran permalink

    Hello Werner,

    Thanks a lot for the wonderful application…Great Job…!

    I had experience in J2ME and I need to experience with Java Media Library so I thought to add two functionalities to your application as listed below

    1. Audio support to AVI file format
    2. Region selector to select the region on a screen for recording – ALREADY STARTED WORKING

    I started analyzing how AVI file is getting created so could you guide me how to add AUDIO functionality to AVI FORMAT so that I will implement it in your application.

    Because I will not happy until unless I learn something new and I want to try new things…

    Thanks a lot in Advance,
    Shashikiran

  39. Hi Jinfy,

    There is no support for sub-title tracks in the underlying media library (yet).

  40. Hi Shashikiran,

    In the meantime, I have implemented audio support for AVI. Integrating audio support into an AVI screen-recorder is non-trivial, since AVI lacks support for a variable frame rate. I had to do several changes in the screen-recorder to keep audio and video in sync.
    I did not have time to publish a new article yet. Depending on my work-load, I might publish a new article over the Christmas/New Year period.

    Region selection is trivial. I am not going to add this to the user interface, because this is just supposed to be a technology demo – not a fully blown application. I might extend the ScreenRecorder class, so that a rectangle can be passed as an argument to the constructor.

    Best,
    Werner

  41. Shashikiran permalink

    Hi Werner,

    Thanks for your replay.

    Could you share the source code of Audio logic implemented for AVI so that I will analyze it? Also please let me know if anything I can help you to achieve the same.

    I am doing some RnD to play the recorded video and audio in native java application and here is my approach,
    1. To render the BuffredImage in MediaTracker control of AWT package
    2. To send the audio data to speakers using sound API
    OR
    Using JMF to play the recorded video. Which approach is correct to play the recorded video?

    Thanks,
    Shashikiran

  42. Alexey Romanov permalink

    Hi! The AVIOutputStream link is broken.

  43. Shashirikan,

    I will publish a new version of the AVI classes over the Christmas period – if time permits.

    The simplest way for playing back a recorded video is with JMF.
    If you recorded a video with the Techsmith Codec, you will also need the same codec in JMF, programmed against the JMF API. I will publish these classes also.

  44. Shashikiran permalink

    Hi Werner,

    Thanks for your replay.

    Till that time I will try at my end by first analyzing your application in depth and I will try adding audio for AVI format if it is possible from me (as media library is new to me so I started studying about video file formats).

    Also I came to know about FFMpeg to convert media files from one format to another format. And I tried to convert both AVI and MOV generated from your recorder tool and here is result.
    a. AVI gets converted to FLV and working it is working fine
    b. MOV is NOT GETTING CONVERTED

    Any idea why MOV is not getting converted?

    Thanks in Advance,
    Shashikiran

  45. shashikiran permalink

    Hi Werner,

    I tried again by converting MOV file generated from your recorder tool to FLV and it took bit more time and get converted to FLV. Below is the result,

    a. It takes long time to convert
    b. File size is too big like 80 KB of MOV file will become 210 KB of FLV file

    My doubt is that even though apple codec has been applied to QuickTime format the file size of MOV is too large but this is not the case with AVI. Please share your concern on the same.

    Thanks in Advance,
    Shashikiran

  46. Hi Shashirikan,

    I think you should ask these questions at the ffmpeg forum.

    a. Conversion time consists of decoding time and encoding time. The codecs used by the screen recorder can be decoded very fast. If conversion time takes too long, you may want to select an encoder with less complexity or tune the encoding parameters in ffmpeg.

    b. FLV with Sörenson codec or with On2 codec is not suited for screen recordings. You may want to use MP4 with H.264 encoding instead.

    (c.) The filesize of recordings in QuickTime format in this demo is significantly larger than files recorded in AVI format, because I chose to demonstrate different codecs in this demo! In the next version of the screen recorder, the TSCC codec will be selectable for both container formats. In general, the QuickTime format has less overhead than the AVI format, and will generate slightly smaller files than AVI.

  47. Shashikiran permalink

    Hi Werner,

    I wish you happy Christmas and prosperous New Year 🙂

    Thanks for your replay. I will check in FFMpeg forum. Please suggest me some good books on Java Graphics OR URL’s so that it helps me to analyze your project and to know more about Graphics concepts in java.

    Thanks Again,
    Shashikiran

  48. Priyanka permalink

    Hello Werner,

    I downloaded the file but I didn’t get the source code for the screen recorder as mentioned.

  49. Bhargava permalink

    Hai Werner,

    The Application is very nice and working great but i want to record audio which is playing in music player. Is it possible with this application..
    Can you help me out.

    Thank you
    Bhargava Naluvala

  50. Bhargava permalink

    Hai Priyanka,

    As you mentioned that you have a problem on getting the sorce code for the jar file.
    You extract that jar file and you can see the java files in those extracted files, or else you can directly import that jar file in to your working eclipse.

    Is this explanation is helpfull to you..

Comments are closed.