python视频帧文件互转

python视频帧文件互转,第1张

文章目录
  • python视频帧文件互转
  • 一、视频转帧文件
  • 二、帧文件转视频
  • 注释


python视频帧文件互转
一、视频转帧文件

代码如下:

import cv2
 
def video2frame(videos_path, frames_save_path, time_interval=1, type='.jpg', start_time=0, stop_time=-1):
 
  '''
  Args:
    videos_path: 视频的存放路径
    frames_save_path: 视频切分成帧之后图片的保存路径
    time_interval: 保存间隔
  
  '''
  
  vidcap = cv2.VideoCapture(videos_path)
  frames = vidcap.get(7)#获取总帧数
  rate = vidcap.get(5)#获取帧率
  if stop_time == -1:
    print('每秒'+str(int(rate))+'帧','共'+str(int(frames))+'帧','打印'+str(int(frames)//time_interval)+'帧')
    for i in range(int(frames)):
      ret, image = vidcap.read()
      if ret is False:
        break
      if i % time_interval == 0:#time_interval换位帧率可按秒截取
        cv2.imencode(type, image)[1].tofile(frames_save_path + "%d.jpg" % i)
  else:
    segment = [int(rate * start_time), int(rate * stop_time)]
    print('每秒'+str(int(rate))+'帧','共'+str(int(frames))+'帧', '打印'+str((segment[1]-segment[0])//time_interval)+'帧')
    for i in range(segment[0], segment[1]):
      if i < segment[0]:
        continue
      elif i > segment[1]:
        break
      _, image = vidcap.read()
      if i % time_interval == 0:#time_interval换位帧率可按秒截取
        cv2.imencode(type, image)[1].tofile(frames_save_path + "%d.jpg" % i)


 
if __name__ == '__main__':
   videos_path = 
   frames_save_path = 
   time_interval = 1 #帧间隔,1为无间隔
   video2frame(videos_path, frames_save_path, time_interval,start_time=2, stop_time=-1)#可以设置帧读取截止时间,以秒计
二、帧文件转视频
import cv2
import os
import numpy as np
from PIL import Image
 
 
def frame2video(im_dir,video_dir,fps,start_frame=0,stop_frame=-1):
    '''
        im_dir:合成视频所需帧的路径
        video_dir:合成视频存放的路径
        fps:帧率,每秒钟帧数越多,越流畅
        start_frame:开始转的帧编号
        stop_frame:停止转的帧编号
    '''
 
    im_list = os.listdir(im_dir)
    im_list.sort(key=lambda x: int(x.split('.')[0]))  #图片顺序按序号排列
    img = Image.open(os.path.join(im_dir,im_list[0]))
    img_size = img.size #获得图片分辨率,im_dir文件夹下的图片分辨率需要一致
    image_type = im_list[0].split('.')[-1]
    fourcc = cv2.VideoWriter_fourcc(*'XVID') 
    videoWriter = cv2.VideoWriter(video_dir, fourcc, fps, img_size)
    
    if stop_frame == -1:
        total_frame = len(im_list)
        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)
            
    else:
        total_frame = stop_frame - start_frame
        for i in range(start_frame, stop_frame):
            im_name = os.path.join(im_dir, str(i) + '.' + image_type)
            frame = cv2.imdecode(np.fromfile(im_name, dtype=np.uint8), -1)
            videoWriter.write(frame)
        
    videoWriter.release()
    print('视频共'+str(total_frame/fps)+'秒')
 
if __name__ == '__main__':
    im_dir =  
    video_dir =  
    fps = 20 #帧率,每秒钟帧数越多,越流畅
    frame2video(im_dir, video_dir, fps, start_frame=20, stop_frame=-1) #指定一定长度的帧
注释

VideoCapture.get()参数及意义

截图来源

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/792960.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-05
下一篇 2022-05-05

发表评论

登录后才能评论

评论列表(0条)

保存