python打开MP4
时间: 2023-08-13 08:04:12 浏览: 130
使用Python OpenCV可以保存视频为MP4格式。具体步骤如下:
1. 导入OpenCV库
```
import cv2
```
2. 创建VideoWriter对象
```
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter('output.mp4', fourcc, 25., (640, 480))
```
其中,fourcc是视频编码格式,这里使用mp4v;'output.mp4'是保存的文件名;25.是帧率;(640, 480)是视频的分辨率。
3. 读取视频帧并写入文件
```
while True:
ret, frame = cap.read()
if ret == True:
out.write(frame)
else:
break
```
其中,cap是读取视频的对象,可以使用cv2.VideoCapture()函数创建。
4. 释放资源
```
cap.release()
out.release()
cv2.destroyAllWindows()
```
完整代码如下:
```
import cv2
cap = cv2.VideoCapture()
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter('output.mp4', fourcc, 25., (640, 480))
while True:
ret, frame = cap.read()
if ret == True:
out.write(frame)
else:
break
cap.release()
out.release()
cv2.destroyAllWindows()
```
阅读全文
相关推荐


















