标签:通道 std jpg 就是 ext not ram 视频格式 iter
# coding=utf-8 import shutil import cv2 import os video_path = ‘./test.mp4‘ image_path = ‘./image‘ extract_frame = 20 def extract_frames(video_path, image_path, index): """视频转图片""" video = cv2.VideoCapture() if not video.open(video_path): print("can not open the video path") exit(1) count = 1 while True: success, frame = video.read() if frame is None: break if count % extract_frame == 0: # 通过调整extract_frame的值来提取图片的帧率 save_path = ‘{}/{:>03d}.bmp‘.format(image_path, index) cv2.imwrite(save_path, frame) index += 1 count += 1 video.release() print("totall save {:d} pics".format(index-1)) def main(): try: shutil.rmtree(image_path) except OSError: pass import os os.mkdir(image_path) extract_frames(video_path, image_path, 1) if __name__ == ‘__main__‘: main()
# 图片合成视频 def picvideo(path, size): filelist = os.listdir(path) # 获取该目录下的所有文件名 filelist.sort() ‘‘‘ fps: 帧率:1秒钟有n张图片写进去[控制一张图片停留5秒钟,那就是帧率为1,重复播放这张图片5次] 如果文件夹下有50张 534*300的图片,这里设置1秒钟播放5张,那么这个视频的时长就是10秒 ‘‘‘ fps = 5 # size = (591,705) #图片的分辨率片 file_path = "./image/" + ‘result‘ + ".avi" # 导出路径 fourcc = cv2.VideoWriter_fourcc(‘M‘, ‘J‘, ‘P‘, ‘G‘) # 不同视频编码对应不同视频格式(例:‘I‘,‘4‘,‘2‘,‘0‘ 对应avi格式 ‘D‘, ‘I‘, ‘V‘, ‘X‘ mp4 png图片) video = cv2.VideoWriter(file_path, fourcc, fps, size) for item in filelist: if item.endswith(‘.jpg‘): item = path + ‘/‘ + item img = cv2.imread(item) # 使用opencv读取图像,直接返回numpy.ndarray 对象,通道顺序为BGR ,注意是BGR,通道值默认范围0-255。 video.write(img) # 把图片写进视频 video.release() # 释放 print("转换完成") picvideo(‘./image‘, (352, 640))
标签:通道 std jpg 就是 ext not ram 视频格式 iter
原文地址:https://www.cnblogs.com/hello-python2020/p/13930294.html