[英]How to write to an outputStream using xuggler?
所以我正在嘗試將我的編碼緩沖圖像寫入輸出流,但我無法通過流來獲取任何數據......任何人都可以告訴我我做錯了什么以及為什么我看不到任何數據輸出?
我希望當我調用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();
我相信你創建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
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.