Xuggle编解码器识别失败

时间:2010-05-03 19:15:49

标签: java codec xuggle

我正在尝试运行以下Xuggle代码:

public static boolean convert(String stream) throws IOException, InterruptedException {

  IMediaReader reader = ToolFactory.makeReader(stream + ".flv");

  IMediaWriter writer = ToolFactory.makeWriter(stream + ".mp3", reader);
  reader.addListener(writer);

  while (reader.readPacket() != null)
   ;

  return true;
}

其中stream是文件路径。但我得到以下例外:

java.lang.IllegalArgumentException: could not find input codec id
 at com.xuggle.xuggler.IContainerFormat.establishOutputCodecId(IContainerFormat.java:393)
 at com.xuggle.xuggler.IContainerFormat.establishOutputCodecId(IContainerFormat.java:327)
 at com.xuggle.xuggler.IContainerFormat.establishOutputCodecId(IContainerFormat.java:300)
 at com.xuggle.mediatool.MediaWriter.addStreamFromContainer(MediaWriter.java:1141)
 at com.xuggle.mediatool.MediaWriter.getStream(MediaWriter.java:1046)
 at com.xuggle.mediatool.MediaWriter.encodeAudio(MediaWriter.java:837)
 at com.xuggle.mediatool.MediaWriter.onAudioSamples(MediaWriter.java:1448)
 at com.xuggle.mediatool.AMediaToolMixin.onAudioSamples(AMediaToolMixin.java:89)
 at com.xuggle.mediatool.MediaReader.dispatchAudioSamples(MediaReader.java:628)
 at com.xuggle.mediatool.MediaReader.decodeAudio(MediaReader.java:555)
 at com.xuggle.mediatool.MediaReader.readPacket(MediaReader.java:469)
 at <package_name>MediaFile.convert(MediaFile.java:66)
 at <package_name>.MediaFileTest.shouldConvertExistingFLV(MediaFileTest.java:32)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
 at java.lang.reflect.Method.invoke(Unknown Source)
 at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
 at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
 at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
 at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
 at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
 at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
 at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:73)
 at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:46)
 at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:180)
 at org.junit.runners.ParentRunner.access$000(ParentRunner.java:41)
 at org.junit.runners.ParentRunner$1.evaluate(ParentRunner.java:173)
 at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
 at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
 at org.junit.runners.ParentRunner.run(ParentRunner.java:220)
 at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
 at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

单元测试如下:

 @Test
 public void shouldConvertExistingFLV() throws IOException, InterruptedException {
  String str = "C:\\Program Files\\Wowza Media Systems\\Wowza Media Server 2\\content\\Extremists";
  boolean result = MediaFile.convert(str);
  assertTrue(result);
 }

那么,为什么他不能找到编解码器,因为我正在测试一个简单的flv到mp3的转换?

2 个答案:

答案 0 :(得分:0)

如果您的源是真正的flv流(没有奇怪的编解码器信息),那么这应该有效。我能想到的问题只有两个:

  • 在编写器开始编码之前,没有足够的元数据信息进入。例如,尚未检测到编解码器。
  • 由于传入视频对mp3容器类型无效,导致失败。编写媒体工具以忽略视频。

希望有所帮助

答案 1 :(得分:0)

诀窍在于在Writer上添加音频流。这段代码片段适用于从flv中提取音频流并将其转码为mp3。

     IMediaReader reader = ToolFactory.makeReader(filename + ".flv");
     IMediaWriter writer = ToolFactory.makeWriter(filename + ".mp3", reader);
     int sampleRate = 22050;
     int channels = 1;
     writer.addAudioStream(0, 0, ICodec.ID.CODEC_ID_MP3, channels, sampleRate);

     reader.addListener(writer);
     while (reader.readPacket() == null)
               ;