For Loop覆盖之前的迭代



我尝试提取视频列表中每个视频的第二帧,并将每帧保存为单个图像文件。

frame_no = 2
dir_list = ['a', 'b']
dir_lust_full = ['C:/User/video/a.mp4','c:/User/video/b.mp4']
# Loop currently overwrites each iteration. 
for i in list(dir_list_full): 
cap = cv2.VideoCapture(i) 
cap.set(1,frame_no)  
ret, frame = cap.read()  
for x in dir_list:
isWritten = cv2.imwrite('C:/User/Frames/frame_'+ x +'.png', frame) 

if isWritten:
print('Frames are successfully saved as individual files.')

但是,循环将当前迭代的确切帧保存到它创建的每个图像文件。最终,当循环结束时,每个文件都有相同的帧(来自上一个视频)。

谁能告诉我我哪里做错了?谢谢!=)

也许你想这样做:

frame_no = 2
dir_list_source = ["C:/User/video/a.mp4", "c:/User/video/b.mp4"]
dir_list_target = ["a", "b"]
for i in range(len(dir_list_source)): 
cap = cv2.VideoCapture(dir_list_source[i])
if not cap.isOpened():
print(f"Cannot open {dir_list_source[i]}")
exit()
cap.set(1, frame_no)
ret, frame = cap.read()
if not ret:
print(f"Error reading frame from {dir_list_source[i]}")
exit()
cv2.imwrite(f"C:/User/Frames/frame_{dir_list_target[i]}.png", frame)
cap.release()
print("Frames successfully saved as individual files.")

相关内容

  • 没有找到相关文章

最新更新