ffprobe get pts of last audio/video packet

11.9k views Asked by At

I have an mov file that I need to get the ending pts for both the audio and video streams. I'm able to do this by doing the following (manually):

ffprobe -show_packets file.mov

Which gives me an output like (with many more packets of course):

[PACKET]
codec_type=audio
stream_index=0
pts=221184
pts_time=5.015510
dts=221184
dts_time=5.015510
duration=580
duration_time=0.013152
convergence_duration=N/A
convergence_duration_time=N/A
size=304
pos=4833575
flags=K_
[/PACKET]
[PACKET]
codec_type=video
stream_index=1
pts=29800
pts_time=4.966667
dts=29400
dts_time=4.900000
duration=200
duration_time=0.033333
convergence_duration=N/A
convergence_duration_time=N/A
size=20707
pos=4837376
flags=__
[/PACKET]

In the scenario above, the ending pts is 221764 for audio and 30000 for video (pts + duration).

I was wondering if there was an easy way to either get the final audio/video packet pts directly via ffprobe flags or by intelligently parsing the output.

1

There are 1 answers

4
Gyan On BEST ANSWER

Run

ffprobe -v 0 -show_entries packet=pts,duration -of compact=p=0:nk=1 -read_intervals 999999 -select_streams v video.mp4  | tail -1

This will print values in the form of 118231200|3600 where the first value is the pts and the second the duration of the last packet of the video stream.

You just need to make sure the value of -read_intervals, in seconds, is equal to or greater than the duration of the file. 999999 is a safe value. The tail is required for video streams since ffprobe will print the entire last GOP.