0

这一定是一个非常愚蠢的问题,但是如何用 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))

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

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

4

2 回答 2

1

为了调整内容大小以及重新编码,您需要创建一个 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);
于 2012-04-04T08:18:16.700 回答
0

@Muhammad Umar,也许他的意思是:

@Override
    public void onVideoPicture(IVideoPictureEvent event) {
//        Logger.info("onAddStream(): now I am in VideoConverter.onVideoPicture().....");
        IVideoPicture pic = event.getPicture();
        if (videoResampler == null) {
            videoResampler = IVideoResampler.make(VIDEO_WIDTH, VIDEO_HEIGHT,
                    pic.getPixelType(), pic.getWidth(), pic.getHeight(),
                    pic.getPixelType());
        }

        IVideoPicture out = IVideoPicture.make(pic.getPixelType(), VIDEO_WIDTH, VIDEO_HEIGHT);

        videoResampler.resample(out, pic);
        IVideoPictureEvent asc = new VideoPictureEvent(event.getSource(), out, event.getStreamIndex());
        super.onVideoPicture(asc);
        out.delete();
    }
于 2017-01-17T07:36:16.537 回答