python中將視頻流截圖爲圖像保存

將視頻流截圖保存爲jpg文件,間隔秒數根據視頻流幀數修改,可以在任意播放器的播放信息裏面查看,例如幀速率19/s,意思是1s播放19幀,這樣你想要的每秒截圖,就需要將幀速改19

import cv2
import os.path
from tqdm import tqdm

file_path = r"D:\\FFOutput2\\"
path_dir = os.listdir(file_path)   #返回文件夾中的文件名
save_path = r"E:\\CJ_Data\\dianti_image\\image2\\"
count=1
name_count=1
for allDir in tqdm(path_dir):
    video_path = file_path+allDir
    video = cv2.VideoCapture(video_path)  # 讀入視頻文件
    if video.isOpened():  # 判斷是否正常打開
        rval, frame = video.read()
    else:
        rval = False
 
    timeF = 19  # 視頻幀計數間隔頻率

    while rval:  # 循環讀取視頻幀
        rval, frame = video.read()
        if (count % timeF == 0):  # 每隔timeF幀進行存儲操作
            # cv2.imshow('pic',frame)
            cv2.imwrite(save_path + str(name_count) + '.jpg', frame)  # imwrite在py3中無法保存中文路徑
            # cv2.imencode('.jpg', frame)[1].tofile(save_path + str(count) + '.jpg')  # 存儲爲圖像
            # print('E:\Dataset\file\數據\image/' + '%06d' % c + '.jpg')
            name_count=name_count+1
        count = count + 1
        cv2.waitKey(1)

video.release()
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章