视频处理基本技术(取帧、合帧)

2022-11-16 14:17:50 浏览数 (1)

目前暂时更新

1 逐帧拆解

代码语言:javascript复制
def video2frame(videos_path,frames_save_path,time_interval):
 
  '''
  :param videos_path: 视频的存放路径
  :param frames_save_path: 视频切分成帧之后图片的保存路径
  :param time_interval: 保存间隔
  :return:
  '''
  vidcap = cv2.VideoCapture(videos_path)
  success, image = vidcap.read()
  count = 0
  while success:
    success, image = vidcap.read()
    count  = 1
    if count % time_interval == 0:
        if isinstance(image, np.ndarray) :
            cv2.imencode('.jpg', image)[1].tofile(frames_save_path   "/frame%d.jpg" % count)
    # if count == 20:
    #   break
  print(count)

其中time_interval一般取1

2 帧合成视频

代码语言:javascript复制
def Composite_video(im_list,video_dir,fps = 30):
    # fourcc = cv2.cv.CV_FOURCC('M','J','P','G') #opencv版本是2
    fourcc = cv2.VideoWriter_fourcc(*'XVID') #opencv版本是3
    
    img = Image.open(im_list[0])
    img_size = img.size #获得图片分辨率,im_dir文件夹下的图片分辨率需要一致
    
    
    videoWriter = cv2.VideoWriter(video_dir, fourcc, fps, img_size)
    # count = 1
    for il in im_list:
        im_name = il
        frame = cv2.imdecode(np.fromfile(im_name, dtype=np.uint8), -1)
        videoWriter.write(frame)
        # count =1
        # if (count == 200):
        #     print(im_name)
        #     break
    videoWriter.release()
    # print('finish')

im_list:要合成视频,帧图片列表,需要按照顺序; video_dir:图像存放地址

0 人点赞