ffprobe get pts of last audio/video packet
Asked Answered
H

1

10

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.

Helsinki answered 17/11, 2018 at 1:29 Comment(0)
J
16

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.

Jerz answered 17/11, 2018 at 5:32 Comment(4)
Holy crap you rock Gyan! This is perfect!Helsinki
This seems to fail with M2TS files if the latest packet of a stream is not the last packet in that stream in the physical file; in other words, if the packets in the physical file are not ordered by PTS. I have just tested it with a few M2TS files that are arranged that way. I have no clue whether other container formats (e.g. mp4, mov, mkv and so on) can suffer from that problem, too.Mortal
Yes, for streams with B-frames, that's a possibility. You can change the | tail -1 to | tail -8 | sort -n | tail -1 to account for that.Jerz
I already did something similar. Lucky me that I have installed Cygwin on my Windows machines ;-) But thanks for the -8; I didn't know that number yet. And I have to leave away the -read_intervals part as it seems, because otherwise ffprobe emits the last frame only (before tailing the output). I'll look into this again.Mortal

© 2022 - 2024 — McMap. All rights reserved.