Skip to content

How to seek a specific frame quickly? #1113

Answered by Akascape
Akascape asked this question in Q&A
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 6 replies

Comment options

You must be logged in to vote
6 replies
@Akascape
Comment options

@Jafner
Comment options

@Akascape
Comment options

@Jafner
Comment options

@Jafner
Comment options

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