3

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).

blackgreen
  • 34,072
  • 23
  • 111
  • 129
Igor
  • 355
  • 1
  • 4
  • 12

1 Answers1

2

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.

the kamilz
  • 1,860
  • 1
  • 15
  • 19
  • 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? – Igor Sep 18 '18 at 13:04
  • 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. – the kamilz Sep 19 '18 at 06:48
  • This simple project has some idea about pts implementation: https://github.com/kmsquire/split_video – the kamilz Sep 19 '18 at 07:19
  • 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. – Igor Sep 19 '18 at 12:00