在java中播放视频质量差

时间:2014-03-04 17:41:11

标签: java video video-processing flv xuggler

我正在尝试使用xuggler在Java中播放视频,但是当我运行它时,质量会显着降低。以下是2个屏幕截图:

Normal quality Playback quality

从第二个屏幕截图中您可以看到颜色都不同。我不是视频专家,有人能告诉我问题出在哪里?视频解码器?颜色编码?有人能帮助我吗?

编辑:这是我为播放视频而编写的代码。它与Xuggler的例子相同。

 if (container.open("temp.flv",IContainer.Type.READ,null) < 0) {
       throw new IllegalArgumentException("could not open file: " + whatToPlay);
      }
  int numStreams = container.getNumStreams();

  int videoStreamId = -1;
  int audioStreamId = -1;

for (int i = 0; i < numStreams; i++) {
      IStream stream = container.getStream(i);
      IStreamCoder coder = stream.getStreamCoder();

      if (coder.getCodecType() == ICodec.Type.CODEC_TYPE_VIDEO) {
          videoStreamId = i;
          videoCoder = coder;
      }
      if (coder.getCodecType() == ICodec.Type.CODEC_TYPE_AUDIO) {
          audioStreamId = i;
          audioCoder = coder;
      }
  }

  if (videoStreamId == -1 && audioStreamId == -1) {
      throw new RuntimeException("could not find audio or video stream in container: " + whatToPlay);
  }
  if(videoCoder != null) {
  if (videoCoder.open() < 0) {
      throw new RuntimeException("could not open video decoder for container: " + whatToPlay);
  }
  }
  if(audioCoder != null) {
      if(audioCoder.open() < 0) {
          throw new RuntimeException("could not open audio decoder for container: " + whatToPlay);
      }

      try {
          openJavaSound(audioCoder);
      } catch(LineUnavailableException reason) {
          throw new RuntimeException("unable to open sound device on your system when playing back container: " + whatToPlay);
      }
  }
  IVideoResampler resampler = null;
  if (videoCoder.getPixelType() != IPixelFormat.Type.BGR24) {
      resampler = IVideoResampler.make(videoCoder.getWidth(),
              videoCoder.getHeight(), IPixelFormat.Type.BGR24,
              videoCoder.getWidth(), videoCoder.getHeight(), videoCoder.getPixelType());
      if (resampler == null) {
          throw new RuntimeException("could not create color space resampler for: " + whatToPlay);
      }
  }
 IPacket packet = IPacket.make();
  while (container.readNextPacket(packet) >= 0) {
      if (packet.getStreamIndex() == audioStreamId) {
          IAudioSamples samples = IAudioSamples.make(1024, audioCoder.getChannels());

          int offset = 0;

          while (offset < packet.getSize()) {
              int bytesDecoded = audioCoder.decodeAudio(samples, packet, offset);
              if (bytesDecoded < 0) {
                  throw new RuntimeException("got error decoding audio in: " + whatToPlay);
              }
              offset += bytesDecoded;
              if (samples.isComplete() && (isMute == false)) {
                  playJavaSound(samples);
              }
          }
      } else if (packet.getStreamIndex() == videoStreamId) {
          picture = IVideoPicture.make(videoCoder.getPixelType(), videoCoder.getWidth(), videoCoder.getHeight());
          int offset = 0;
          while (offset < packet.getSize()) {
              int bytesDecoded = videoCoder.decodeVideo(picture, packet, offset);
              offset += bytesDecoded;
              if (picture.isComplete()) {
                  IVideoPicture newPic = picture;
                  if (resampler != null) {
                      // we must resample
                      newPic = IVideoPicture.make(resampler.getOutputPixelFormat(), picture.getWidth(), picture.getHeight());
                      if (resampler.resample(newPic, picture) < 0) {
                          throw new RuntimeException("could not resample video from: " + whatToPlay);
                      }
                  }

                  if (newPic.getPixelType() != IPixelFormat.Type.BGR24) {
                      throw new RuntimeException("could not decode video as BGR 24 bit data in: " + whatToPlay);
                  }
                  updatePanelImage(Utils.videoPictureToImage(newPic));                                         
              }
          }
      } else {
          do {
          } while (false);
      }
  }  

0 个答案:

没有答案