如何使用xuggler写入outputStream? [英] How to write to an outputStream using xuggler?

查看:109
本文介绍了如何使用xuggler写入outputStream?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在尝试将编码的缓冲图像写入输出流,但我无法在该流上传递任何数据...谁能告诉我我做错了什么,为什么我可以这样做?看不到任何输出吗?

So I'm trying to write my encoded buffered image to an output stream but I can't get any data to come through on the stream... Could anyone tell me what I'm doing wrong and why I can't see any output?

我希望当我调用将其视频编码到我的ByteArrayOutputStream中的write.encodeVideo方法时...这个假设是错误的吗?

I would expect that when I call the write.encodeVideo method that it encode's video into my ByteArrayOutputStream... is that 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();

推荐答案

我认为您创建IMediaWriter的方式不正确.您也不想自己打开作者的容器.当使用OutputStream而不是文件时,可以使用com.xuggle.xuggler.io.XugglerIO映射器来做到这一点:

I believe the way you are creating the IMediaWriter is incorrect. You don't want to open the writer's container yourself either. When you are using an OutputStream instead of a file you can do it by using the com.xuggle.xuggler.io.XugglerIO mapper 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);

请记住,如果您尝试创建一种格式,而该格式必须在创建过程中必须在输出字节中查找"(例如.mov),则它不能与简单的OutputStream如我所显示.您将必须写入文件而不是OutputStream.

Keep in mind that if you are trying to create a format that has to be able to "seek" within the output bytes as part of it's creation process (e.g. .mov), then it won't work with a simple OutputStream as I have shown. You would have to write to a file instead of an OutputStream.

这篇关于如何使用xuggler写入outputStream?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆