How to write output stream using xuggler?

So, I'm trying to write an encoded buffered image to the output stream, but I can’t get any data that can happen in the stream ... Can someone tell me what I am doing wrong, and why I can "No way out?

I would expect that when I call the write.encodeVideo method, which it encodes the video into my ByteArrayOutputStream ... is this assumption wrong?

ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); // video parameters final int videoStreamIndex = 0; final int videoStreamId = 0; final long frameRate = DEFAULT_TIME_UNIT.convert(15, MILLISECONDS); final int width = 512; final int height = 254; long nextFrameTime = 0; // create a media writer and specify the output file final IMediaWriter writer = ToolFactory.makeWriter("aaa.ogg"); IContainer ic = writer.getContainer(); ic.open(outputStream, writer.getContainer().getContainerFormat(), true, false); ICodec codec = ICodec.guessEncodingCodec(null, null,"aaa.ogg", null, ICodec.Type.CODEC_TYPE_VIDEO); // add the video stream writer.addVideoStream(videoStreamIndex, videoStreamId, codec, width, height); BufferedImage img = null; try { img = ImageIO.read(new File("/home/jbkreme/aaa.png")); } catch (final IOException e) { e.printStackTrace(); } for(int yy =0; yy < 2048-height; yy=yy+8) { nextFrameTime++; BufferedImage frame = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR); frame = img.getSubimage(0, yy, width, height); BufferedImage frame2 = convertToType(frame, BufferedImage.TYPE_3BYTE_BGR); //encode the video writer.encodeVideo(videoStreamIndex, frame2, nextFrameTime, DEFAULT_TIME_UNIT); nextFrameTime += frameRate; } writer.close(); outputStream.flush(); outputStream.close(); 
+4
source share
1 answer

I believe that the way you create IMediaWriter is incorrect. You also do not want to open the container for letters. When you use OutputStream instead of a file, you can do this using mapper com.xuggle.xuggler.io.XugglerIO , like this:

 // create a media writer and specify the output stream final IMediaWriter writer = ToolFactory.makeWriter(XugglerIO.map(outputStream)); // manually set the container format (because it can't detect it by filename anymore) IContainerFormat containerFormat = IContainerFormat.make(); containerFormat.setOutputFormat("ogg", null, "application/ogg"); mWriter.getContainer().setFormat(containerFormat); // add the video stream writer.addVideoStream(videoStreamIndex, videoStreamId, ICodec.ID.CODEC_ID_THEORA, width, height); 

Keep in mind that if you are trying to create a format that should "search" in the output bytes as part of its creation process (for example, .mov), then it will not work with a simple OutputStream , as I showed. You will need to write to the file instead of OutputStream .

+1
source

Source: https://habr.com/ru/post/1437843/


All Articles