Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 625
Thanh Nguyen Avatar asked Oct 22 '25 05:10

Thanh Nguyen


1 Answers

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))
like image 130
Mike Boers Avatar answered Oct 24 '25 19:10

Mike Boers



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!