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

时间:2011-12-08 15:17:44

标签: java video-encoding xuggle xuggler

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

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

任何帮助都会非常感激!!!

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分: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();
    }