ffmpeg::avcodec_encode_video setting PTS h264

Martin Beckett picture Martin Beckett · Jul 7, 2011 · Viewed 17.5k times · Source

I'm trying to encode video as H264 using libavcodec

ffmpeg::avcodec_encode_video(codec,output,size,avframe);

returns an error that I don't have the avframe->pts value set correctly.
I have tried setting it to 0,1, AV_NOPTS_VALUE and 90khz * framenumber but still get the error non-strictly-monotonic PTS

The ffmpeg.c example sets the packet.pts with ffmpeg::av_rescale_q() but this is only called after you have encoded the frame !

When used with the MP4V codec the avcodec_encode_video() sets the pts value correctly itself.

Answer

Ozone picture Ozone · Feb 24, 2012

I had the same problem, solved it by calculating pts before calling avcodec_encode_video as follows:

//Calculate PTS: (1 / FPS) * sample rate * frame number
//sample rate 90KHz is for h.264 at 30 fps
picture->pts = (1.0 / 30) * 90 * frame_count;
out_size = avcodec_encode_video(c, video_outbuf, video_outbuf_size, picture);

Solution stolen from this helpful blog post

(Note: Changed sample rate to khz, expressed in hz was far too long between frames, may need to play with this value - not a video encoding expert here, just wanted something that worked and this did)