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
views:
164answers:
3I 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.
I believe BigBlueButton implemented one, but I don't know if they open sourced it. Check there.
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.