tags:

views:

164

answers:

3

Anyone know of a Java video encoder for ScreenVideo (v1 or v2) which is free? I know ffmpeg has a C++ version and Lee Felarca wrote one in AS3; but I really would like to have one in Java.
AS3: http://www.zeropointnine.com/blog/assets_code/SimpleFlvWriter.as.txt

+6  A: 

I believe the Xuggle library does what you want -- although it may actually be a wrapper around native libraries such as ffmpeg.

Here's a snippet of example code encoding desktop screenshots to a flv (mp4):

 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("output.mp4");

 // 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 to stream #0
   writer.encodeVideo(0,bgrScreen,
       System.nanoTime()-startTime, TimeUnit.NANOSECONDS);
    System.out.println("encoded image: " +index);

   // sleep for framerate milliseconds
   Thread.sleep((long) (1000 / FRAME_RATE.getDouble()));
 }
 // Finally we tell the writer to close and write the trailer if
 // needed
 writer.close();

This code is from this tutorial on the Xuggle website.

More advanced encoding, also on the Xuggle website here.

If a native wrapper is what you wanted, run a web search for "IContainerFormat flv" for other bits of example code.

Also, there is already a very similar question


Update: Native java implementation

Check out ScreenVideoEncoder.java from the bigbluebutton project on github.

Andy
Good answer but Xuggle is "native" it provides access to ffmpeg via JNI. I'm looking for pure java code.
Mondain
Btw your code sample doesnt encode into "screenvideo" or for ffmpeg "flashsv". The output of this code would most likely be h.264 in an mp4 container.
Mondain
I'm not really familiar with "screenvideo", can you provide a link to more info? Google searches for just the term do not reveal much.
Andy
And yeah, Xuggle appear to be a wrapper for the native libs, but it's the best I got. I've also found yet another native wrapper, but this one uses the JMF api, so it may be more ideal if you can ultimately settle with using native implementation: http://fobs.sourceforge.net/ If you can't use a native lib I think you may be SOL (unless you write your own implementation, of course).
Andy
A: 

I believe BigBlueButton implemented one, but I don't know if they open sourced it. Check there.

Xuggle
Didn't think about those guys, but good idea.
Mondain
A: 

I don't know if you find anything good written in pure Java, without using native code. Video encoding is a very time-consuming task, so it's usually written in 'fast' native code, in languages like C or even Assembler. Video encoding often uses special CPU and GPU instructions to improve the speed - it all is unavailable from Java, so it makes very little sense to write production-use video encoders in Java. If I were you, I would just take some native solution and embed it with JNI, JNA or Swig (popular Java-to-native connectors). If you need high portability (eg. 32-bit Windows, 64-bit Windows, 32-bit Linux, 64-bit Linux), just compile this native library for for those four platforms and embed in your JARs. If you just need to write uncompressed video, it can be easily done in Java, and it will be as fast as native code. Just take this SimpleFlvWriter.as you posted and rewrite it to Java - it shouldn't be a hard task.

iirekm