好,所以尽我所能来理解jcodec。基本上,我想将屏幕上的内容记录为BufferedImages,将缓冲的图像编码为H264帧,然后对其进行解码,然后将它们转换回缓冲的图像。
这是我拥有的代码,ScreenRecorder类扩展了线程并基本上在while true循环中捕获了屏幕。那部分起作用。然后,似乎也可以将ScreenRecorder中的BufferedImage编码为H264帧,但是当我尝试对其进行解码时,我得到了AIOBE
ScreenRecorder sc = new ScreenRecorder();
sc.start();
H264Encoder enc = H264Encoder.createH264Encoder();
ColorSpace cs = enc.getSupportedColorSpaces()[0];
System.out.println("cs= " + cs);
H264Decoder dec = new H264Decoder();
byte[][] buff = new byte[4096][4096];
while (true) {
BufferedImage bi = sc.getSnapshot();
System.out.println("Got bi " + bi);
if (bi != null) {
Picture pi = AWTUtil.fromBufferedImage(bi, cs);
int bufSize = enc.estimateBufferSize(pi);
ByteBuffer bb = ByteBuffer.allocate(bufSize);
VideoEncoder.EncodedFrame ef = enc.encodeFrame(pi, bb);
System.out.println("Data = " + Arrays.toString(ef.getData().array()));
Frame f = dec.decodeFrame(ef.getData(), buff);
System.out.println("Decoded frame = " + f);
}
}
产生:
Got bi BufferedImage@506e1b77: type = 1 DirectColorModel: rmask=ff0000 gmask=ff00 bmask=ff amask=0 IntegerInterleavedRaster: width = 1920 height = 1080 #Bands = 3 xOff = 0 yOff = 0 dataOffset[0] 0
org.jcodec.common.VideoEncoder$EncodedFrame@1a93a7ca
Data = [0, 0, 0, 1, 103, 66, 0, 40, -83, 0, -16, 4, 75, -54, -128, 0, 0, 0, 1, 104, -50, 56, -128, 0, 0, 0, 1, 101, -72, 32, 13, 12, 98, -128, 0, -111, 127, -1, -3, 127, -61, 24, 0, 36, 9, 112, -96, 28, 13, -108, -43, 45, -92, -57, 7, 101, 39, 31, -4, 0, 121, -104, 17, -86, 9, 84, -111, -94, 39, 125, 94, -57, 64, 2, 67, 56, 28, 0, 4, 0, 118, -97, -128, 40, 124, 20, 39, 122, 111, -117, -124, 25, 72, -76, -107, 90, -1, -122, 48, 0, 67, 41, 10, -64, 0, 32, 106, 0, 5, -128, -108, -13, 80, 51, 112, 113, 112, 107, -1, -17, -32, 32, 85, -60, -76, 52, 70, -67, 124, 3, 113, 111, 51, 0, 1, 0, -64, 0, 16, 15, -5, -48, 3, -80, -21, -2, 24, -64, 25, -48, 0, 16, 39, 0, 1, 0, 105, -112, 13, 9, 11, -35, -16, 56, 0, 8, 6, -128, 0, -128, 80, 0, -69, -112, -32, 0, 32, 26, 0, 2, 1, 64, 2, -18, 124, 56, 0, 8, 6, -128, 0, -128, 80, 0, -69, -112, -32, 0, 32, 26, 0, 2, 1, 64, 2, -18, 47, -8, 98, 56, 0, -112, 15, 21, 96, 3, 102, 59, -124, 48, 83, 96, 55, -10, 1, 55, -6, 27, 0, 67, 19, 47, -12, 90, 112, -125, -108, 64, 38, 9, 83, 29, 101, -56, 75, -126, -85, -96, 113, 17, -105, 2, 57, 98, 56, -66, 28, 88, -105, ...
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
at java.lang.System.arraycopy(Native Method)
at org.jcodec.codecs.h264.decode.SliceDecoder.putMacroblock(SliceDecoder.java:198)
at org.jcodec.codecs.h264.decode.SliceDecoder.decodeMacroblocks(SliceDecoder.java:104)
at org.jcodec.codecs.h264.decode.SliceDecoder.decodeFromReader(SliceDecoder.java:73)
at org.jcodec.codecs.h264.H264Decoder$FrameDecoder.decodeFrame(H264Decoder.java:152)
at org.jcodec.codecs.h264.H264Decoder.decodeFrameFromNals(H264Decoder.java:103)
at org.jcodec.codecs.h264.H264Decoder.decodeFrame(H264Decoder.java:99)
at uno.anahata.satgyara.screen.ScreenRecorder.main(ScreenRecorder.java:148)
只是想弄清楚jcodec是如何工作的。关于我在做什么错的任何想法吗?任何帮助将不胜感激