public static void run(String[] args)
  {
    try
    {
      final String outFile;
      if (args.length > 0)
        outFile = args[0];
      else
        outFile = "out/output.mp4";
      // This is the robot for taking a snapshot of the
      // screen.  It's part of Java AWT
      final Robot robot = new Robot();
      final Toolkit toolkit = Toolkit.getDefaultToolkit();
      final Rectangle screenBounds = new Rectangle(toolkit.getScreenSize());
      
      // First, let's make a IMediaWriter to write the file.
      final IMediaWriter writer = ToolFactory.makeWriter(outFile);
      
      // We tell it we're going to add one video stream, with id 0,
      // at position 0, and that it will have a fixed frame rate of
      // FRAME_RATE.
      writer.addVideoStream(0, 0,
          FRAME_RATE,
          screenBounds.width, screenBounds.height);
      
      // Now, we're going to loop
      long startTime = System.nanoTime();
      for (int index = 0; index < SECONDS_TO_RUN_FOR*FRAME_RATE.getDouble(); index++)
      {
        // take the screen shot
        BufferedImage screen = robot.createScreenCapture(screenBounds);
        
        // convert to the right image type
        BufferedImage bgrScreen = convertToType(screen,
            BufferedImage.TYPE_3BYTE_BGR);
        
        // encode the image
        writer.encodeVideo(0,bgrScreen,
            System.nanoTime()-startTime, TimeUnit.NANOSECONDS);

        System.out.println("encoded image: " +index);
        
        // sleep for frame rate milliseconds
        Thread.sleep((long) (1000 / FRAME_RATE.getDouble()));

      }
      // Finally we tell the writer to close and write the trailer if
      // needed
      writer.close();
    }
    catch (Throwable e)
    {
      System.err.println("an error occurred: " + e.getMessage());
    }
  }