javaxuggler

Encoded video's tempo is high in xuggler


I am trying to convert mp4 to ts using IMediaWriter. Video encoded successfully but tempo is very high.

IContainer container = IContainer.make();
container.open(input, IContainer.Type.READ,null);
mediaReader = ToolFactory.makeReader(container);
mediaReader.setBufferedImageTypeToGenerate(BufferedImage.TYPE_3BYTE_BGR);
mediaReader.addListener(new MediaListener());
for(int i=0; i< container.getNumStreams();i++){
    if(container.getStream(i).getStreamCoder().getCodecType() == Type.CODEC_TYPE_VIDEO){
        FRAME_RATE = container.getStream(i).getFrameRate();

    }
    else if(container.getStream(i).getStreamCoder().getCodecType() == Type.CODEC_TYPE_AUDIO){
        sampleRate = container.getStream(i).getStreamCoder().getSampleRate();
    }
}
mediaWriter = ToolFactory.makeWriter(output);
mediaWriter.addVideoStream(0, 0,
          FRAME_RATE,
          640,480);

startTime = System.nanoTime();
while (mediaReader.readPacket() == null) ;
mediaWriter.close();
mediaReader.close();

Listener

public void onVideoPicture(IVideoPictureEvent event) {
    mediaWriter.encodeVideo(0,event.getImage(),System.nanoTime()-startTime, TimeUnit.NANOSECONDS);

}

Note: I'm aware of this: mediaReader.addListener(mediaWriter). But I want to convert it manually like my code.


Solution

  • I have solved this problem and writing the answer for future reference. Problem was the time stamp on video encoding. We need to read time stamp from IVideoPictureEvent.

    public void onVideoPicture(IVideoPictureEvent event) {
        mediaWriter.encodeVideo(0,event.getImage(),event.getTimeStamp(TimeUnit.MICROSECONDS).longValue(), TimeUnit.NANOSECONDS);
    
    }