-
I want to get a particular video frame using pyav. I found that Perhaps, I used this method to get the required frame: container.seek(0, whence='time', backward=True) # seek to the starting
frame_num = 30 # the frame I want to seek
time.sleep(0.2) # limit cpu usage
for _ in range(frame_num):
frame = next(container.decode(video=0))
return frame This method works properly but it is too slow while seeking the last frames of any long video file. (with frames>1000) I tried to solve this issue by modifying the seek method : frame_num = 30
framerate = container.streams.video[0].average_rate # get the frame rate
sec = int(frame_num/framerate) # timestamp (sec) for that frame_num
container.seek(sec*1000000, whence='time', backward=True) # seek to that nearest timestamp
sec_frame = sec * framerate # get the key frame number of that timestamp
for _ in range(sec_frame, frame_num):
frame = next(container.decode(video=0))
return frame This reduces the time while seeking any frame of the video, but it doesn't return the proper frame because Is there any other way to get the required frame with pyav? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 12 replies
-
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 second. |
Beta Was this translation helpful? Give feedback.
So, I somehow managed to fix the lag issue:
Now it returns the required frame in less than a se…