Thanh Nguyen
Thanh Nguyen

Reputation: 41

Can get timestamp of frame in video with pyav - python

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

Upvotes: 4

Views: 4697

Answers (1)

Mike Boers
Mike Boers

Reputation: 6745

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

Upvotes: 6

Related Questions