我正在尝试使用 Python 获取视频中所有 I 帧的索引列表(稍后将其中一部分保存为 JPEG
)。现在,我可以在终端中使用 FFProbe
遍历所有帧,并查看哪一个是 I 帧:
ffprobe -select_streams v -show_frames -show_entries frame=pict_type -of csv 18s.mp4
这给了我这样的东西:
frame,I
frame,B
frame,P
frame,B
frame,P
frame,B
但是我如何在 Python 中做到这一点(我在 Windows 中)并获取所有索引的列表?
最佳答案
从 here 获取见解,我能够使用 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()
关于python - 使用 Python 获取视频中的 I 帧列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51661864/