python如何获取一个视频的帧率_python-使用OpenCV计算视频文件中的帧数?

官方参考:https://docs.opencv.org/4.x/d4/d15/group__videoio__flags__base.html#ggaeb8dd9c89c10a5c63c139bf7c4f5704daf01bc92359d2abc9e6eeb5cbe36d9af2

python如何获取一个视频的帧率_python-使用OpenCV计算视频文件中的帧数?_第1张图片

# -*- coding: utf-8 -*-
import time
import cv2
import pandas as pd
import numpy as np
import tensorflow as tf
from PIL import Image


# 37s 94M  超清,一秒30帧,切成图片花了74S
# video_path = './video_image/video.mp4'
# video_path = './video_image/2022-08-13-10-31-45.mp4'
video_path = './video_image/2022-08-14-09-00-06.mp4'

# log_path = './log.txt'
log_path = './log1.txt'
"""
Video review
"""
cam = cv2.VideoCapture(video_path)  # 从指定的路径读取视频
frame_count = cam.get(cv2.CAP_PROP_FRAME_COUNT)
fps = cam.get(cv2.CAP_PROP_FPS)
fps_count = fps / 2
print(fps)
print(frame_count)
currentframe = 0
time_start = time.time()
fp = open(log_path, 'a+', encoding='utf-8')
while True:
    time_start1 = time.time()
    ret, frame = cam.read()
    if ret:
        if currentframe % fps_count == 0:
            path_list = video_path.rsplit(".", 1)
            output_img_path = path_list[0] + '_' + str(currentframe) + ".jpg"
            cv2.imwrite(output_img_path, frame)   # 写入提取的图像
            time_log = time.time() - time_start1
            # print(type(time_log))
            fp.write(str(pd.Timestamp(
                time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))) + '\t' + str(time_log) + '\n')
            # print(time.time() - time_start1)
        currentframe += 1  # 增加计数器,以便显示创建了多少帧
    else:
        break
cam.release()   # 一旦完成释放所有的空间和窗口
cv2.destroyAllWindows()
print(time.time() - time_start)
fp.close()


你可能感兴趣的:(NLP实战项目,笔记,python,python,opencv,音视频)