当前位置:   article > 正文

python将视频切分成帧&&帧合成视频_video2frame-frame2video

video2frame-frame2video

opencv更新命令:pip3 install --upgrade opencv-python==4.5.5.64

一、视频切分成帧
import cv2
 
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:
      cv2.imencode('.jpg', image)[1].tofile(frames_save_path + "/frame%d.jpg" % count)
    # if count == 20:
    #   break
  print(count)
 
if __name__ == '__main__':
   videos_path = 'D:\测试\测试视频01.mp4'
   frames_save_path = 'D:\测试\测试视频01'
   time_interval = 2#隔一帧保存一次
   video2frame(videos_path, frames_save_path, time_interval)
二、帧合成视频
import cv2
import os
import numpy as np
from PIL import Image
 
 
def frame2video(im_dir,video_dir,fps):
 
    im_list = os.listdir(im_dir)
    im_list.sort(key=lambda x: int(x.replace("frame","").split('.')[0]))  #最好再看看图片顺序对不
    img = Image.open(os.path.join(im_dir,im_list[0]))
    img_size = img.size #获得图片分辨率,im_dir文件夹下的图片分辨率需要一致
 
 
    # fourcc = cv2.cv.CV_FOURCC('M','J','P','G') #opencv版本是2
    fourcc = cv2.VideoWriter_fourcc(*'XVID') #opencv版本是3
    videoWriter = cv2.VideoWriter(video_dir, fourcc, fps, img_size)
    # count = 1
    for i in im_list:
        im_name = os.path.join(im_dir+i)
        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')
 
if __name__ == '__main__':
    im_dir = 'E:/测试/图片/'#帧存放路径
    video_dir = 'E:\测试/test.avi' #合成视频存放的路径
    fps = 30 #帧率,每秒钟帧数越多,所显示的动作就会越流畅
    frame2video(im_dir, video_dir, fps)
 
 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/爱喝兽奶帝天荒/article/detail/944736
推荐阅读
相关标签
  

闽ICP备14008679号