opencv-python保存视频
时间: 2023-05-31 10:06:46 浏览: 118
要使用OpenCV-Python保存视频,需要执行以下步骤:
1. 导入OpenCV库
```python
import cv2
```
2. 创建一个VideoWriter对象
```python
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi', fourcc, 20.0, (640, 480))
```
- fourcc:是一个四字符代码,用于指定视频编解码器。例如,XVID表示使用Xvid编码器。
- out:是一个VideoWriter对象,用于写入视频文件。
- 第三个参数:是帧速率,表示视频的帧数。
- 第四个参数:是视频帧的大小,即宽度和高度。
3. 捕获视频帧并将其写入文件
```python
while True:
ret, frame = cap.read()
if ret == True:
# 从视频捕获器中读取一帧
# process frame
out.write(frame)
else:
break
```
- cap.read():从视频捕获器中读取一帧。
- out.write(frame):将帧写入视频文件。
4. 释放资源
```python
cap.release()
out.release()
```
完整代码示例:
```python
import cv2
cap = cv2.VideoCapture(0)
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi', fourcc, 20.0, (640, 480))
while True:
ret, frame = cap.read()
if ret == True:
# process frame
out.write(frame)
else:
break
cap.release()
out.release()
```
阅读全文
相关推荐


















