5

I encode a new H.264 video muxed with audio in MP4 file.

How to correctly calculate PTS and DTS for AVPacket and AVFrame for video and audio?

I generate new video frames and new audio from my source. There are no original PTS/DTS information. I know frame rate which I need to use (time_base).

1 Answer 1

4

Assuming your frame rate is constant. And after setting stream time bases correctly. Start both pts's from zero (0). Audio pts will increase by 'sample per frame' for each frame. This is typically audio_sample_rate / frame_rate (i.e. 48000/60 = 800).

For the video, things are different and somewhat simpler. Video pts will increase same amount of 'Video frame duration' per frame. Use this cheat sheet to calculate the duration:

FPS Frame duration 23.98 2002 24.00 2000 25.00 2000 29.97 2002 30.00 2000 50.00 1000 59.94 1001 60.00 1000 

Yes these somewhat hacky but will work.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your reply, regrettably it doesn't work for me. I quickly get error: "Application provided invalid, non monotonically increasing dts to muxer in stream 0" For video I set time_base 1/30 and for audio 1/44100. I encode RAW video/audio data. Do you know any full example code for muxing?
This "Application provided invalid, non monotonically increasing dts to muxer in stream X" happens only when next PTS or DTS is equal or lower then previous one. It should increment similar to as I explained. I forget to mention, set PTS and DTS same value for the frame. And I'll look for examples.
This simple project has some idea about pts implementation: github.com/kmsquire/split_video
Thanks again for your advices! I will check that example code. I found at least one critical error in my code. I didn't set AVPacket.steam_index before call av_interleaved_write_frame() It seems that earlier all my video/audio AVPacets were added as to a video stream (0). Now muxing works and I see to streams - video and audio.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.