【问题标题】:How to write to an outputStream using xuggler?如何使用 xuggler 写入 outputStream?
【发布时间】:2012-10-04 16:37:07
【问题描述】:

所以我正在尝试将我的编码缓冲图像写入输出流,但我无法从流中获取任何数据...谁能告诉我我做错了什么以及为什么我不能没有看到任何输出?

我希望当我调用 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();

【问题讨论】:

  • 你找到答案了吗?
  • 我不认为我做到了?对不起
  • @PhaniRahul 如果您仍然希望编码到 OutputStream,我已经提交了答案。

标签: java outputstream xuggle xuggler


【解决方案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),那么它不适用于简单的 @ 987654325@ 正如我所展示的。您必须写入文件而不是 OutputStream

【讨论】:

    猜你喜欢
    • 2012-04-26
    • 2013-05-11
    • 1970-01-01
    • 1970-01-01
    • 2019-09-13
    • 2022-09-30
    • 1970-01-01
    • 1970-01-01
    • 2020-10-16
    相关资源
    最近更新 更多