1

我正在尝试使用 Xuggler 将电影 (.mp4) 转换为音频 (.mp3)。我在 OS X 上使用处理。

 IMediaReader reader = ToolFactory.makeReader("/Users/nouv/Desktop/video1.mp4");
 IMediaWriter writer = ToolFactory.makeWriter("/Users/nouv/Desktop/audioOutput.mp3", reader);
 int sampleRate = 22050;
 int channels = 1;
 writer.addAudioStream(0, 0, ICodec.ID.CODEC_ID_MP3, channels, sampleRate);

 reader.addListener(writer);
 try {
   while (reader.readPacket() == null)
           ;
 } finally {}

我有这个错误:IllegalArgumentException:流 [0] 不是视频

Exception in thread "Animation Thread" java.lang.IllegalArgumentException: stream[0] is not video
at com.xuggle.mediatool.MediaWriter.encodeVideo(MediaWriter.java:754)
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 xuggle.setup(xuggle.java:135)
at processing.core.PApplet.handleDraw(PApplet.java:2117)
at processing.core.PGraphicsJava2D.requestDraw(PGraphicsJava2D.java:193)
at processing.core.PApplet.run(PApplet.java:2020)
at java.lang.Thread.run(Thread.java:680)

我尝试使用 .mp4 、 .mov 和 .flv 电影,但我遇到了同样的错误。

4

1 回答 1

3

IContainer 容器 = IContainer.make();

    int result = container.open(inputFilename, IContainer.Type.READ, null);
     // check if the operation was successful
      if (result<0)
          throw new RuntimeException("Failed to open media file");

      int numStreams = container.getNumStreams();

      int audioStreamId = -1;

      IContainer writer = IContainer.make();
      writer.open(outputFilename, IContainer.Type.WRITE, IContainerFormat.make());



      for (int i=0; i<numStreams; i++) {

          IStream stream = container.getStream(i);
          IStreamCoder coder = stream.getStreamCoder();
          IStreamCoder audioCoder = IStreamCoder.make(IStreamCoder.Direction.ENCODING, coder);

          if( coder.getCodecType() == ICodec.Type.CODEC_TYPE_AUDIO){
              coder.open(IMetaData.make(), IMetaData.make());
              audioStreamId = i;

              ICodec inputCodec = ICodec.findDecodingCodec(ICodec.ID.CODEC_ID_MP3);
              if (inputCodec == null)
                  throw new IllegalArgumentException("could not find input codec id");

              if (audioStreamId == -1)
                  throw new RuntimeException("could not find audio stream in container: "+inputFilename);

              writer.addNewStream(audioCoder);
              if(writer.writeHeader() == 0)
              {

                  IPacket packet = IPacket.make();
                  while(container.readNextPacket(packet) >= 0){

                      if(packet.getStreamIndex() == audioStreamId)
                      {
                          if(coder.isOpen()){

                              writer.writePacket(packet);

                          } else {throw new RuntimeException("Could not open Coder"); }
                      }
                  }
              }else {throw new RuntimeException("Header not Written for writer container.");}
          }

          coder.close(); audioCoder.close();
      }
      writer.writeTrailer();
      writer.close();

希望这可以帮助... :)

于 2013-02-24T08:06:02.503 回答