输入视频路径获取视频长宽python opencv
时间: 2024-10-16 10:15:43 浏览: 20
Python OpenCV获取视频的方法
在Python中,使用OpenCV库可以很容易地获取视频文件的长度(帧数)和每一帧的宽度和高度。以下是一个简单的例子:
```python
import cv2
def get_video_info(video_path):
# 读取视频
cap = cv2.VideoCapture(video_path)
# 检查是否成功打开
if not cap.isOpened():
print(f"无法打开视频文件: {video_path}")
return None
# 获取视频的基本信息
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) # 帧宽
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) # 帧高
fps = cap.get(cv2.CAP_PROP_FPS) # 帧率
frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) # 总帧数
# 关闭视频流
cap.release()
return width, height, fps, frame_count
# 示例
video_path = 'path_to_your_video.mp4'
width, height, fps, frames = get_video_info(video_path)
if width and height:
print(f"Video Information: Width: {width}, Height: {height}, FPS: {fps}, Total Frames: {frames}")
阅读全文