小編給大家分享一下Python OpenCV對本地視頻文件進行分幀保存的示例分析,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
創(chuàng)新互聯(lián)一直秉承“誠信做人,踏實做事”的原則,不欺瞞客戶,是我們最起碼的底線! 以服務(wù)為基礎(chǔ),以質(zhì)量求生存,以技術(shù)求發(fā)展,成交一個客戶多一個朋友!為您提供做網(wǎng)站、網(wǎng)站制作、成都網(wǎng)頁設(shè)計、微信平臺小程序開發(fā)、成都網(wǎng)站開發(fā)、成都網(wǎng)站制作、成都軟件開發(fā)、App定制開發(fā)是成都本地專業(yè)的網(wǎng)站建設(shè)和網(wǎng)站設(shè)計公司,等你一起來見證!如下所示:
# 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)
以上是“Python OpenCV對本地視頻文件進行分幀保存的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!