-->

如何使用xuggler写入的OutputStream?(How to write to an out

2019-09-23 12:34发布

所以我想我的编码缓冲的图像写入到输出流,但我不能让任何数据来通过在流......谁能告诉我什么,我做错了,为什么我看不到任何输出?

我希望,当我称之为write.encodeVideo方法,它编码的视频到我ByteArrayOutputStream ......是假设错了吗?

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

Answer 1:

我相信你所创建的方式IMediaWriter不正确。 你不想给自己或者打开作家的容器。 当您正在使用OutputStream ,而不是一个文件,你可以通过做com.xuggle.xuggler.io.XugglerIO映射是这样的:

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

请记住,如果你想创建一个具有能够输出字节,以“求”作为其一部分的创作过程中的格式(如.MOV),那么它不会有一个简单的工作OutputStream ,因为我有所示。 你将不得不写入文件,而不是一个OutputStream



文章来源: How to write to an outputStream using xuggler?