Search code examples
javah.264frame-ratexuggle

Xuggler H264 FPS encoding issue


I'm trying to encode a series of images into an MP4 video with xuggler. However, trying to wrap my head around the timebase/framerate issues is driving me insane! I can't seem to get a decent video encoded. Using the Converter.java example, I have

IRational num = IRational.make(24, 1);
outStreamCoder.setFrameRate(num);
outStreamCoder.setTimeBase(IRational.make(num.getDenominator(),  num.getNumerator()));

...

long tsOffset = 0;
if (outStream.getStartTime() != Global.NO_PTS && outStream.getStartTime() > 0
            && outStream.getTimeBase() != null)
{
        IRational defTimeBase = IRational.make(1, (int) Global.DEFAULT_PTS_PER_SECOND);
        tsOffset = defTimeBase.rescale(outStream.getStartTime(), outStream.getTimeBase());
}

....

long timeStamp = (3600 * count); // experimenting
IVideoPicture outFrame = converter.toPicture(worksWithXugglerBufferedImage, timeStamp);
if (outFrame.getTimeStamp() != Global.NO_PTS)
    outFrame.setTimeStamp(outFrame.getTimeStamp() - tsOffset);

For 30 images, the encoded duration is far less than 1s. I'd expect it to be just over a second. Can anyone please help me, this has had me perplexed for some time now!


Solution

  • So it turns out I was being an idiot! I was assigning the frame a timeStamp based on the timebase of an H.264 encoded file: (1/90,000); I should really have just been assigned it a time in microseconds from the first frame. (e.g. a multiple of (1e6/fps)). SO my code should have read:

    IRational fps = IRational.make(24, 1);
    outStreamCoder.setFrameRate(fps);
    outStreamCoder.setTimeBase(IRational.make(fps.getDenominator(),  fps.getNumerator()));
    
    ...
    
    long timeStamp = (1e6/fps.getNumerator() * count);