python 的 opencv 包能够非常方便的处理计算机视频相关的应用问题。利用 opencv 可以容易的获取视频的基本信息。本篇给予介绍。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import cv2 as cv

video_path = '/home/jinzhongxu/Dataset/test.mp4'

video = cv.VideoCapture(video_path)

# 获取视频的总帧数
video.get(cv.CAP_PROP_FRAME_COUNT)
# 获取视频的帧率
video.get(cv.CAP_PROP_FPS)
# 获取视频的帧高
video.get(cv.CAP_PROP_FRAME_HEIGHT)
# 获取视频的帧宽
video.get(cv.CAP_PROP_FRAME_WIDTH)

其他可获取的参数列表如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
0 	CV_CAP_PROP_POS_MSEC Current position of the video file in milliseconds or video capture timestamp.
视频文件的当前位置(播放)以毫秒为单位
1 CV_CAP_PROP_POS_FRAMES 0-based index of the frame to be decoded/captured next.
基于以0开始的被捕获或解码的帧索引
2 CV_CAP_PROP_POS_AVI_RATIO Relative position of the video file: 0 - start of the film, 1 - end of the film.
视频文件的相对位置(播放):0=电影开始,1=影片的结尾。
3 CV_CAP_PROP_FRAME_WIDTH
视频帧宽度
4 CV_CAP_PROP_FRAME_HEIGHT
视频帧高度
5 CV_CAP_PROP_FPS
视频帧速率
6 CV_CAP_PROP_FOURCC 4-character code of codec.
编解码的4字-字符代码
7 CV_CAP_PROP_FRAME_COUNT
视频总帧数
8 CV_CAP_PROP_FORMAT Format of the Mat objects returned by retrieve() .
返回对象的格式
9 CV_CAP_PROP_MODE Backend-specific value indicating the current capture mode.
返回后端特定的值,该值指示当前捕获模式
10 CV_CAP_PROP_BRIGHTNESS Brightness of the image (only for cameras).
图像的亮度(仅适用于照相机)
11 CV_CAP_PROP_CONTRAST Contrast of the image (only for cameras).
图像的对比度(仅适用于照相机)
12 CV_CAP_PROP_SATURATION Saturation of the image (only for cameras).
图像的饱和度(仅适用于照相机)
13 CV_CAP_PROP_HUE Hue of the image (only for cameras).
色调图像(仅适用于照相机)
14 CV_CAP_PROP_GAIN Gain of the image (only for cameras).
图像增益(仅适用于照相机)(Gain在摄影中表示白平衡提升)
15 CV_CAP_PROP_EXPOSURE Exposure (only for cameras).
曝光(仅适用于照相机)
16 CV_CAP_PROP_CONVERT_RGB Boolean flags indicating whether images should be converted to RGB.
指示是否应将图像转换为RGB布尔标志
17 CV_CAP_PROP_WHITE_BALANCE_U The U value of the whitebalance setting (note: only supported by DC1394 v 2.x backend currently)
18 CV_CAP_PROP_WHITE_BALANCE_V The V value of the whitebalance setting (note: only supported by DC1394 v 2.x backend currently)
立体摄像机的矫正标注(目前只有DC1394 v.2.x后端支持这个功能)
19 CV_CAP_PROP_RECTIFICATION Rectification flag for stereo cameras (note: only supported by DC1394 v 2.x backend currently)
20 CV_CAP_PROP_ISO_SPEED The ISO speed of the camera (note: only supported by DC1394 v 2.x backend currently)
21 CV_CAP_PROP_BUFFERSIZE Amount of frames stored in internal buffer memory (note: only supported by DC1394 v 2.x backend currently)

也可以使用编号,获取视频信息,方法如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import cv2 as cv

video_path = '/home/jinzhongxu/Dataset/test.mp4'

video = cv.VideoCapture(video_path)

# 获取视频的总帧数
video.get(7)
# 获取视频的帧率
video.get(5)
# 获取视频的帧高
video.get(4)
# 获取视频的帧宽
video.get(3)

参考文献

  1. python3 opencv获取视频的总帧数介绍