Skip to content

How to seek a specific frame quickly? #1113

Answered by Akascape
Akascape asked this question in 1. Help
Discussion options

You must be logged in to vote

So, I somehow managed to fix the lag issue:

frame_num = 1000 # the frame I want
framerate = container.streams.video[0].average_rate # get the frame rate
time_base = container.streams.video[0].time_base # get the time base
sec = int(frame_num/framerate) # timestamp for that frame_num
container.seek(sec*1000000, whence='time', backward=True)  # seek to that nearest timestamp
frame = next(container.decode(video=0)) # get the next available frame
sec_frame = int(frame.pts * time_base * framerate) # get the proper key frame number of that timestamp

for _ in range(sec_frame, frame_num):
    frame = next(container.decode(video=0))
return frame

Now it returns the required frame in less than a se…

Replies: 1 comment 12 replies

Comment options

You must be logged in to vote
12 replies
@pauljurczak
Comment options

@Scr44gr
Comment options

@pauljurczak
Comment options

@robonrrd
Comment options

@Max-Thuc
Comment options

Answer selected by Akascape
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
8 participants