Python 如何将视频帧保存到新文件夹
我想知道如何运行循环,以便将文件中的所有视频的帧提取并保存到新文件中。到目前为止,我有以下代码:Python 如何将视频帧保存到新文件夹,python,opencv,Python,Opencv,我想知道如何运行循环,以便将文件中的所有视频的帧提取并保存到新文件中。到目前为止,我有以下代码: import cv2 import os # where the videos are stored path = "/Users/harryhat/Desktop/Water Droplets/Videos of water droplets/Random" for video_path in os.listdir(path): name, ext = os.pat
import cv2
import os
# where the videos are stored
path = "/Users/harryhat/Desktop/Water Droplets/Videos of water droplets/Random"
for video_path in os.listdir(path):
name, ext = os.path.splitext(video_path)
if ext == '.avi':
video_path = os.path.join(path, video_path)
# Opens the Video file
cap = cv2.VideoCapture(video_path)
i = 0
while(cap.isOpened()):
ret, frame = cap.read()
if ret == False:
break
cv2.imwrite(str(name)+str(i)+'.jpg', frame)
i += 1
cap.release()
cv2.destroyAllWindows()
然而,在我运行它的那一刻,什么都没有发生,理想情况下,每个视频都会提取它们的帧,并放置在一个文件中,该文件中包含视频名称。关于如何实现这一点有什么建议吗?您可以这样做
import cv2
import os
count=1
vidcap = cv2.VideoCapture('video.mp4')
def getFrame(sec):
vidcap.set(cv2.CAP_PROP_POS_MSEC,sec*1000)
hasFrames,image = vidcap.read()
if hasFrames:
dim = (512, 512) # you can change image height and image width
resized = cv2.resize(image, dim, interpolation = cv2.INTER_AREA)
cv2.imwrite("images/"+str(count)+".png", resized) # image write to image folder be sure crete image folder in same dir
return hasFrames
sec = 0
frameRate = 0.1 # change frame rate as you wish, ex : 30 fps => 1/30
success = getFrame(sec)
while success:
count = count + 1
sec = sec + frameRate
sec = round(sec, 2)
success = getFrame(sec)
如果这有助于你