Get the list of I-Frames in a video in FFMPEG / Python
Solution 1
Getting insights from here, I was able to do it with ffprobe
:
def iframes():
if not os.path.exists(iframe_path):
os.mkdir(iframe_path)
command = 'ffprobe -v error -show_entries frame=pict_type -of default=noprint_wrappers=1'.split()
out = subprocess.check_output(command + [filename]).decode()
f_types = out.replace('pict_type=','').split()
frame_types = zip(range(len(f_types)), f_types)
i_frames = [x[0] for x in frame_types if x[1]=='I']
if i_frames:
cap = cv2.VideoCapture(filename)
for frame_no in i_frames:
cap.set(cv2.CAP_PROP_POS_FRAMES, frame_no)
ret, frame = cap.read()
outname = iframe_path+'i_frame_'+str(frame_no)+'.jpg'
cv2.imwrite(outname, frame)
cap.release()
print("I-Frame selection Done!!")
if __name__ == '__main__':
iframes()
Solution 2
This seems like an X/Y problem, so I'll propose several different commands:
List of timestamps
If you want to output a list of timestamps for each keyframe:
ffprobe -v error -skip_frame nokey -show_entries frame=pkt_pts_time -select_streams v -of csv=p=0 input
0.000000
2.502000
3.795000
6.131000
10.344000
12.554000
Note the -skip_frame nokey
.
select filter
Another method is to use the select filter with the scene
option to output your thumbnails:
ffmpeg -i input -vf "select=gt'(scene,0.4)',scale=160:-1" -vsync vfr %04d.png
Solution 3
This will output all the i frames as PNG images.
ffmpeg -i 2.flv -vf "select=eq(pict_type\,I)" -vsync vfr frame-%02d.png
Credit to this comment a similar superuser.com question. How to extract all key frames from a video clip?
Hope that helps. Cheers.
Ian
Related videos on Youtube
Tina J
Updated on September 18, 2022Comments
-
Tina J over 1 year
I'm trying to select say 10 frames from a given video, possibly with highest variety and scenes. I would like to experiment with various selection scenarios, but the good thing is that the notion of
I-frame
inherently means scene changes! So I would like to get the I-frames. But maybe there are many I-frames, so probably I have to sample them.How can I get the list of all I-frames's frame_number in a video in FFMpeg or Python? I would like to use the list to select only 10 of them and save them as PNG/JPEG.
-
Tina J over 5 yearsThanks, but I know this. What I asked is the array of the I-frame indexes in code!