溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Python OpenCV實(shí)現(xiàn)視頻分幀

發(fā)布時(shí)間:2020-10-22 15:03:42 來源:腳本之家 閱讀:161 作者:等一杯咖啡 欄目:開發(fā)技術(shù)

本文實(shí)例為大家分享了Python OpenCV實(shí)現(xiàn)視頻分幀的具體代碼,供大家參考,具體內(nèi)容如下

# 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)

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI