gpt4 book ai didi

java - 如何使用 Xuggler 重新编码电影文件

转载 作者:行者123 更新时间:2023-12-01 15:38:41 25 4
gpt4 key购买 nike

这肯定是一个非常愚蠢的问题,但是如何使用 Xuggler 重新编码呢?简化后我有:

IMediaReader reader = ToolFactory.makeReader(sourceUrl);      
IMediaWriter writer = ToolFactory.makeWriter(url, reader);

MediaSegmenterWriter writerListener = new MediaSegmenterWriter();
writer.open();
while (reader.readPacket() == null)
do {
}
while(false);

现在,我想将阅读器中的文件重新编码为另一个比特率和分辨率。我怎么做?在创建编写器时,我尝试添加 IMediaStreams 以及原始编码器的副本并进行必要的更改,但这不起作用:

    int numStreams = reader.getContainer().getNumStreams();
for(int i = 0; i < numStreams; i++)
{
final IStream stream = reader.getContainer().getStream(i);
final IStreamCoder coder = stream.getStreamCoder();

IStreamCoder newCoder = IStreamCoder.make(IStreamCoder.Direction.ENCODING, coder);
if(newCoder == null ){
continue;
}
writer.getContainer().addNewStream(i);

int streams = writer.getContainer().getNumStreams();
System.out.println("Current amount of streams in writer: " + streams);

System.out.println("Coder: " + coder.toString());
if (coderSetting != null && newCoder != null){
if (newCoder.getCodecType().equals(ICodec.Type.CODEC_TYPE_VIDEO)) {
newCoder.setWidth(320);
newCoder.setHeight(240);
}
IStream outputStream = writer.getContainer().getStream(i);
outputStream.setStreamCoder(newCoder);
newCoder.open();
}
}

但这只会给出与保留代码相同的结果(例如原始的 1920x1080)

还尝试向编写器添加监听器并替换编码器,但要么出现错误(编码器已打开_,要么没有效果。(onOpen、onAddStream、onOpenCoder))

我寻找教程,但似乎没有做这个简单的操作。

任何帮助将非常感激!!!

最佳答案

为了调整内容大小并重新编码,您需要创建一个 MediaToolAdapter,例如:

private static class MediaResizer extends MediaToolAdapter {
private IVideoResampler videoResampler = null;
private int mediaHeight;
private int mediaWidth;

public MediaResizer (int aHight, int aWidth) {
mediaWidth = aWidth;
mediaHeight = aHeight;
}

@Override
public void onVideoPicture(IVideoPictureEvent event) {
// In case of audio only, do not re-size as it is not needed
if(job.role == MediaRole.MediaRoleEnum.LS_AUDIO) super.onVideoPicture(event);

IVideoPicture pic = event.getPicture();

if (videoResampler == null) {
videoResampler = IVideoResampler.make(job.getCoderSettings().width, job.getCoderSettings().height, pic.getPixelType(), pic.getWidth(), pic.getHeight(), pic.getPixelType());
}

IVideoPicture out = IVideoPicture.make(pic.getPixelType(), mediaWidth, mediaHeight);
videoResampler.resample(out, pic);

IVideoPictureEvent asc = new VideoPictureEvent(event.getSource(), out, event.getStreamIndex());
super.onVideoPicture(asc);

out.delete();
}

}

您将其添加为读者的监听器,然后您的作家调整了大小。它应该是这样的:

 IMediaReader reader = ToolFactory.makeReader(sourceUrl);
MediaResizer resizer = new MediaResizer(job);
IMediaWriter currentWriter = ToolFactory.makeWriter(destinationDir, reader);

reader.addListener(resizer);
resizer.addListener(currentWriter);

关于java - 如何使用 Xuggler 重新编码电影文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8433215/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com