Xuggler以AVI格式录制

时间:2017-11-20 11:49:57

标签: java java-ee xuggler xuggle

您好我正在尝试使用Xuggler进行AVI格式的屏幕录制视频。但是我在做同样的事情时遇到以下异常。不能打开流。我也改变了宽度和高度但没有用。

Exception in thread "main" java.lang.RuntimeException: could not open stream com.xuggle.xuggler.IStream@4088416[index:0;id:0;streamcoder:com.xuggle.xuggler.IStreamCoder@4088512[codec=com.xuggle.xuggler.ICodec@4088848[type=CODEC_TYPE_VIDEO;id=CODEC_ID_H264;name=libx264;];time base=1/1000000;frame rate=0/0;pixel type=YUV420P;width=683;height=384;];framerate:0/0;timebase:1/90000;direction:OUTBOUND;]: Operation not permitted
at com.xuggle.mediatool.MediaWriter.openStream(MediaWriter.java:1192)
at com.xuggle.mediatool.MediaWriter.getStream(MediaWriter.java:1052)
at com.xuggle.mediatool.MediaWriter.encodeVideo(MediaWriter.java:799)
at com.atmecs.ep.ScreenRecording.main(ScreenRecording.java:52)

公共类ScreenRecording {

private static final double FRAME_RATE = 50;

private static final int SECONDS_TO_RUN_FOR = 30;

private static final String outputFilename = "c:/today.avi";

private static Dimension screenBounds;

public static void main(String[] args) {

    // let's make a IMediaWriter to write the file.
    final IMediaWriter writer = ToolFactory.makeWriter(outputFilename);

    screenBounds = Toolkit.getDefaultToolkit().getScreenSize();

    // 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.addListener(ToolFactory.makeDebugListener());
    writer.addVideoStream(0, 0, ICodec.ID.CODEC_ID_H264, screenBounds.width / 2, screenBounds.height / 2);
    long startTime = System.nanoTime();

    for (int index = 0; index < SECONDS_TO_RUN_FOR * FRAME_RATE; index++) {

        // take the screen shot
        BufferedImage screen = getDesktopScreenshot();

        // 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);

        // sleep for frame rate milliseconds
        try {
            Thread.sleep((long) (1000 / FRAME_RATE));
        } catch (InterruptedException e) {
            // ignore
        }
    }
    // tell the writer to close and write the trailer if needed
    writer.close();
}
public static BufferedImage convertToType(BufferedImage sourceImage, int targetType) {

    BufferedImage image;

    // if the source image is already the target type, return the source
    // image
    if (sourceImage.getType() == targetType) {
        image = sourceImage;
    }
    // otherwise create a new image of the target type and draw the new
    // image
    else {
        image = new BufferedImage(sourceImage.getWidth(), sourceImage.getHeight(), targetType);
        image.getGraphics().drawImage(sourceImage, 0, 0, null);
    }

    return image;

}

private static BufferedImage getDesktopScreenshot() {
    try {
        Robot robot = new Robot();
        Rectangle captureSize = new Rectangle(screenBounds);
        return robot.createScreenCapture(captureSize);
    } catch (AWTException e) {
        e.printStackTrace();
        return null;
    }
}

}

您能否对视频编码提出任何建议(仅限AVI)

0 个答案:

没有答案