cv2-视频读取和解码
cv2.VideoCapture()
opencv对视频取帧
import cv2
def getFrame(videoPath, svPath):
cap = cv2.VideoCapture(videoPath)
numFrame = 0
while True:
if opened, frame = cap.read():
if opened:
#cv2.imshow('video', frame)
numFrame += 1
newPath = svPath + str(numFrame) + ".jpg"
cv2.imencode('.jpg', frame)[1].tofile(newPath)
else:
continue
if cv2.waitKey(10) == 27:
break
getFrame(videoPath,savePicturePath)
关于跳帧解码函数
import cv2
cap = cv2.VideoCapture("./test.mp4")
idx = 0
freq = 5
While True:
idx += 1
ret = cap.grab()
if not ret:
break
if idx % freq == 1:
ret, frame = cap.retrieve()
if frame is None: # exist broken frame
break
# do something else
...
cap.release()
指定时间解码读取视频
import cv2
from tqdm import tqdm
from termcolor import cprint
import time
cv2.namedWindow("image", cv2.WINDOW_NORMAL)
cv2.moveWindow("image", 0, 0)
cv2.resizeWindow("image", 960, 540)
def readvideo_startend(path, start=0, end=None, wait=1):
"""
opencv读取视频
:param path: 视频路径
:param start: 视频开始时间
:param end: 视频结束时间
:param wait: 每帧等待时间
:return: None
"""
video = cv2.VideoCapture(path)
opened = video.isOpened()
if not opened:
cprint("视频打开错误", "red")
return
# vc.set(cv2.CAP_PROP_FPS, 25)
# vc.set(cv2.CAP_PROP_FRAME_WIDTH, 1920)
# vc.set(cv2.CAP_PROP_FRAME_HEIGHT, 1080)
fps = video.get(cv2.CAP_PROP_FPS) # 获取视频帧率
count = int(video.get(cv2.CAP_PROP_FRAME_COUNT)) # 获取视频总帧数
width = int(video.get(cv2.CAP_PROP_FRAME_WIDTH)) # 获取视频宽度
height = int(video.get(cv2.CAP_PROP_FRAME_HEIGHT)) # 获取视频高度
print(f"视频宽度: {width}, 视频高度: {height}, 视频总帧数: {count}, 视频帧率: {fps}")
time.sleep(0.01)
if start:
start_frame = min(int(start * fps), count) # 开始帧
video.set(cv2.CAP_PROP_POS_FRAMES, start_frame) # 设置视频的开始帧位置
count -= start_frame
if end:
count -= count - min(int((end - start) * fps), count)
pbar = tqdm(total=count, desc="播放进度") ## 进度条
f_num = 0 ## 读取视频的帧数
while opened:
f_num += 1
opened, frame = video.read()
if opened:
# frame = cv2.resize(frame, (0, 0), None, 0.5, 0.5)
cv2.imshow('image', frame)
pbar.update(1)
if cv2.waitKey(wait) == 27 or f_num == count:
break
else:
break
pbar.close()
video.release()
cv2.destroyAllWindows()
if __name__ == '__main__':
path = r"rtsp://admin:abcd1234@192.168.12.65/h264/ch1/main/av_stream"
readvideo_startend(path, 60, 100)
get、set参数列表
参数 |
值 |
描述 |
cv2.CAP_PROP_POS_MSEC |
0 |
当前视频文件的时间位置(返回毫秒)或视频捕获时间戳 |
cv2.CAP_PROP_POS_FRAMES |
1 |
从0开始的解码/捕获时间帧 |
cv2.CAP_PROP_POS_AVI_RATIO |
2 |
返回视频文件的相关位置:0,视频开始。1,视频结束 |
cv2.CAP_PROP_FRAME_WIDTH |
3 |
视频流中的帧宽 |
cv2.CAP_PROP_FRAME_HEIGHT |
4 |
视频流中的帧高 |
cv2.CAP_PROP_FPS |
5 |
帧率 |
cv2.CAP_PROP_FOURCC |
6 |
返回解码方式中的四字符 |
cv2.CAP_PROP_FRAME_COUNT |
7 |
视频文件的总帧数 |
cv2.CAP_PROP_FORMAT |
8 |
由retrieve()函数返回的矩阵对象的格式 |
cv2.CAP_PROP_MODE |
9 |
用于预测当前捕获模式的后端专用值 |
cv2.CAP_PROP_BRIGHTNESS |
10 |
图像的亮度(仅用于摄像头) |
cv2.CAP_PROP_CONTRAST |
11 |
图像的对比度(仅用于摄像头) |
cv2.CAP_PROP_SATURATION |
12 |
图像的饱和度(仅用于摄像头) |
cv2.CAP_PROP_HUE |
13 |
图像的色调(仅用于摄像头) |
cv2.CAP_PROP_GAIN |
14 |
图像增益(仅用于摄像头) |
cv2.CAP_PROP_EXPOSURE |
15 |
曝光度(仅用于摄像头) |
cv2.CAP_PROP_CONVERT_RGB |
16 |
用于预测图像是否应该被转换为RGB的布尔位 |
cv2.CAP_PROP_WHITE_BALANCE |
17 |
白平衡(当前不支持) |
cv2.CAP_PROP_RECTIFICATION |
18 |
立体相机的纠正位 |