[Python] 纯文本查看 复制代码
import cv2
import os
# 从.mp4 数据类型的视频中提取图像
def splitFrames_mp4(sourceFileName,zhenshu):
# 在这里把后缀接上
video_path = os.path.join(path, sourceFileName + '.mp4')
times = 0 #照片标号从多少开始
# 提取视频的频率,每25帧提取一个
frameFrequency = zhenshu
# 输出图片到当前目录vedio文件夹下
outPutDirName = path +'/'+ sourceFileName + '/'
# 如果文件目录不存在则创建目录
if not os.path.exists(outPutDirName):
os.makedirs(outPutDirName)
camera = cv2.VideoCapture(video_path)
while True:
times+=1
res, image = camera.read()
if not res:
# print('not res , not image')
break
if times%frameFrequency==0:
#cv2.imwrite(outPutDirName + str(times)+'.jpg', image) #复制的时候带的方法
#cv2.imwrite(outPutDirName + str(times)+'.jpg', image) #该方法不成功
cv2.imencode('.jpg', image)[1].tofile(outPutDirName + str(times)+'.jpg') ##正确方法
print(outPutDirName + str(times)+'.jpg')
#cv2.imwrite(outPutDirName + str(times) + '.jpg', image)
#print(times)
print(sourceFileName+'图片提取结束')
camera.release()
if __name__ == '__main__':
path = input('输入地址,将会截取所有视频截图')
zhenshu = int(input('多少帧截图一次'))
im_file = path
# for im_name in im_names:
for im_name in os.listdir(im_file):
suffix_file = os.path.splitext(im_name)[-1]
print('开始截取'+im_name)
sourceFileName = os.path.splitext(im_name)[0]
splitFrames_mp4(sourceFileName,zhenshu)
自己打包一下吧,打包的exe太大了