Xuggle-Xuggler无法将.MOV或.AVI转换为.MP4。

3

我一直在尝试使用Xuggle将.MOV文件转换为.MP4。以下是我的原始代码:

    // create a media reader  
    IMediaReader mediaReader = ToolFactory.makeReader(path);  

    // create a media writer  
    IMediaWriter mediaWriter = ToolFactory.makeWriter(outpath, mediaReader);  

    // add a writer to the reader, to create the output file  
    mediaReader.addListener(mediaWriter);  

    // create a media viewer with stats enabled  
    IMediaViewer mediaViewer = ToolFactory.makeViewer(true);  

    // add a viewer to the reader, to see the decoded media  
    mediaReader.addListener(mediaViewer);  

    // read and decode packets from the source file and  
    // and dispatch decoded audio and video to the writer  
    while (mediaReader.readPacket() == null) ;  

这段代码适用于.WMV格式文件,但不适用于.AVI或.MOV格式文件。 以下是使用.MOV格式文件时出现的错误:
19:36:37.270 [video stream 0 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10]   DEBUG com.xuggle.mediatool.MediaViewer - thread started
19:36:37.407 [audio stream 1 PCM signed 16-bit little-endian] DEBUG   com.xuggle.mediatool.MediaViewer - thread started
19:36:37.640 [AWT-EventQueue-0] WARN  com.xuggle.xuggler - Got error:   picture is not of the same PixelType as this Coder expected   (../../../../../../../csrc/com/xuggle/xuggler/StreamCoder.cpp:1430)
Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: failed to   encode video
at com.xuggle.mediatool.MediaWriter.encodeVideo(MediaWriter.java:764)
at com.xuggle.mediatool.MediaWriter.encodeVideo(MediaWriter.java:783)
at com.xuggle.mediatool.MediaWriter.onVideoPicture(MediaWriter.java:1434)
at com.xuggle.mediatool.AMediaToolMixin.onVideoPicture(AMediaToolMixin.java:166)
at com.xuggle.mediatool.MediaReader.dispatchVideoPicture(MediaReader.java:610)
at com.xuggle.mediatool.MediaReader.decodeVideo(MediaReader.java:519)
at com.xuggle.mediatool.MediaReader.readPacket(MediaReader.java:475)
at Drivers.beginProgram(Drivers.java:146)
at Drivers.start(Drivers.java:182)
at Drivers$3.actionPerformed(Drivers.java:100)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

关于 .AVI 文件的错误:

19:39:32.205 [video stream 0 DV (Digital Video)] DEBUG com.xuggle.mediatool.MediaViewer - thread started
19:39:32.331 [audio stream 1 PCM signed 16-bit little-endian] DEBUG com.xuggle.mediatool.MediaViewer - thread started
19:39:32.336 [audio stream 2 PCM signed 16-bit little-endian] DEBUG com.xuggle.mediatool.MediaViewer - thread started
19:39:32.390 [AWT-EventQueue-0] WARN  com.xuggle.xuggler - Got error: picture is not of the same PixelType as this Coder expected (../../../../../../../csrc/com/xuggle/xuggler/StreamCoder.cpp:1430)
Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: failed to  encode video
at com.xuggle.mediatool.MediaWriter.encodeVideo(MediaWriter.java:764)
at com.xuggle.mediatool.MediaWriter.encodeVideo(MediaWriter.java:783)
at com.xuggle.mediatool.MediaWriter.onVideoPicture(MediaWriter.java:1434)
at com.xuggle.mediatool.AMediaToolMixin.onVideoPicture(AMediaToolMixin.java:166)
at com.xuggle.mediatool.MediaReader.dispatchVideoPicture(MediaReader.java:610)
at com.xuggle.mediatool.MediaReader.decodeVideo(MediaReader.java:519)
at com.xuggle.mediatool.MediaReader.readPacket(MediaReader.java:475)
at Drivers.beginProgram(Drivers.java:146)
at Drivers.start(Drivers.java:182)
at Drivers$3.actionPerformed(Drivers.java:100)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

然后我尝试了这段代码:

IMediaReader reader = ToolFactory.makeReader(path);
IMediaWriter writer = ToolFactory.makeWriter(outpath, reader);
writer.open();
writer.setForceInterleave(true);
IContainerFormat outFormat = IContainerFormat.make();
outFormat.setOutputFormat("mp4", outpath, null);
IContainer container = writer.getContainer();
container.open(outpath, IContainer.Type.WRITE, outFormat);
writer.addVideoStream(0, 0, ICodec.findEncodingCodecByName("h264"), 1920, 1080);
writer.addAudioStream(1, 0, ICodec.findEncodingCodecByName("mp3"), 0, 0);
reader.addListener(writer);
while (reader.readPacket() == null);

它仍然无法工作。我听说需要使用IAudioResampler,但是对于像我这样的初学者来说,如何做到这一点非常困惑,并且找不到足够的文档。非常感谢您的帮助!请尽量解释清楚。谢谢!
编辑:我认为第二个代码的问题在于编解码器。当我运行它时,我会得到一个“null codec”异常。(如果有人知道如何解决“Closing dangling Container (../../../../../../../csrc/com/xuggle/xuggler/Container.cpp:146)”问题,请告诉我。
谢谢!

Xuggler已经不再更新,建议使用ffmpeg来转换您的文件。 - thibsc
1个回答

0
你可以尝试使用 ICodec.ID.CODEC_ID_H264
writer.addVideoStream(0, 0, ICodec.ID.CODEC_ID_H264, 1920, 1080);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接