4

有人知道免费的 ScreenVideo(v1 或 v2)Java 视频编码器吗?我知道 ffmpeg 有一个 C++ 版本,Lee Felarca 用 AS3 写了一个;但我真的很想拥有一个Java。
AS3:http ://www.zeropointnine.com/blog/assets_code/SimpleFlvWriter.as.txt

4

4 回答 4

8

我相信Xuggle库可以满足您的需求——尽管它实际上可能是原生库(如 ffmpeg)的包装器。

这是将桌面屏幕截图编码为 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();

此代码来自Xuggle 网站上的本教程。

更高级的编码,也在 Xuggle 网站

如果您需要本机包装器,请在网络上搜索“IContainerFormat flv”以获取其他示例代码。

另外,已经有一个非常相似的问题


更新:本机 java 实现

从 github 上的 bigbluebutton 项目中查看ScreenVideoEncoder.java 。

于 2010-10-01T19:16:04.397 回答
2

Werner Randelshofer posted a pure java screen recorder on his blog and was kind enough to publish the source : http://www.randelshofer.ch/blog/2011/05/pure-java-screen-recorder/ It looks to do what you want.

于 2011-08-01T22:12:43.307 回答
0

我相信 BigBlueButton 实现了一个,但我不知道他们是否开源了它。检查那里。

于 2010-10-02T04:40:21.053 回答
0

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.

于 2010-10-08T14:12:48.810 回答