欢迎来到代码驿站!

Python代码

当前位置:首页 > 软件编程 > Python代码

Python OpenCV对本地视频文件进行分帧保存的实例

时间:2020-11-04 11:20:48|栏目:Python代码|点击:

如下所示:

# coding=utf-8
 
import os
import cv2
 
videos_src_path = "/home/wgp/视频/"
video_formats = [".MP4", ".MOV"]
frames_save_path = "/home/wgp/视频/"
width = 320
height = 240
time_interval = 50
 
 
def video2frame(video_src_path, formats, frame_save_path, frame_width, frame_height, interval):
 """
 将视频按固定间隔读取写入图片
 :param video_src_path: 视频存放路径
 :param formats: 包含的所有视频格式
 :param frame_save_path: 保存路径
 :param frame_width: 保存帧宽
 :param frame_height: 保存帧高
 :param interval: 保存帧间隔
 :return: 帧图片
 """
 videos = os.listdir(video_src_path)
 
 def filter_format(x, all_formats):
  if x[-4:] in all_formats:
   return True
  else:
   return False
 
 videos = filter(lambda x: filter_format(x, formats), videos)
 
 for each_video in videos:
  print "正在读取视频:", each_video
 
  each_video_name = each_video[:-4]
  os.mkdir(frame_save_path + each_video_name)
  each_video_save_full_path = os.path.join(frame_save_path, each_video_name) + "/"
 
  each_video_full_path = os.path.join(video_src_path, each_video)
 
  cap = cv2.VideoCapture(each_video_full_path)
  frame_index = 0
  frame_count = 0
  if cap.isOpened():
   success = True
  else:
   success = False
   print("读取失败!")
 
  while(success):
   success, frame = cap.read()
   print "---> 正在读取第%d帧:" % frame_index, success
 
   if frame_index % interval == 0:
    resize_frame = cv2.resize(frame, (frame_width, frame_height), interpolation=cv2.INTER_AREA)
    # cv2.imwrite(each_video_save_full_path + each_video_name + "_%d.jpg" % frame_index, resize_frame)
    cv2.imwrite(each_video_save_full_path + "%d.jpg" % frame_count, resize_frame)
    frame_count += 1
 
   frame_index += 1
 
 cap.release()
 
 
if __name__ == '__main__':
 video2frame(videos_src_path, video_formats, frames_save_path, width, height, time_interval)

上一篇:Tensorflow之Saver的用法详解

栏    目:Python代码

下一篇:Python语言技巧之三元运算符使用介绍

本文标题:Python OpenCV对本地视频文件进行分帧保存的实例

本文地址:http://www.codeinn.net/misctech/19036.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有