4

How can I get timestamp of a frame in a video or rtmp stream from pts and time_base or duration? Thanks a lot!

import av
def init_input(file_name):
    global a
    container = av.open(file_name)
    a = container.duration
    return container.decode(video=0)
url = "rtsp://184.72.239.149/vod/mp4:BigBuckBunny_115k.mov"
stream1 = init_input(url)
for frame1 in stream1:
    print(frame1.pts)
    print(frame1.time_base)

PS: frame.time is incorrect with actual time

HelloGoodbye
  • 3,624
  • 8
  • 42
  • 57
Thanh Nguyen
  • 41
  • 1
  • 2

1 Answers1

6

As of writing, this bug was just fixed on GitHub.

If you need this to work with the currently released PyAV (i.e. on the PyPI), then you can use the time_base on the video Stream:

import av

url = "rtsp://184.72.239.149/vod/mp4:BigBuckBunny_115k.mov"

container = av.open(url, options={'rtsp_transport': 'tcp'})
stream = container.streams.video[0]

for frame in container.decode(stream):
    print(float(frame.pts * stream.time_base))
Mike Boers
  • 6,665
  • 3
  • 31
  • 40