cv2.VideoWriter
时间: 2023-02-23 08:48:41 浏览: 46
cv2.VideoWriter 是一个 Python 库,它主要用于在 OpenCV 中写入视频文件。它接受一个视频文件名,编码器名称,帧速率,视频帧大小和是否是彩色视频的参数,并返回一个 cv2.VideoWriter 对象,该对象可以用于向视频文件写入帧。例如:
```
import cv2
# 创建 VideoWriter 对象
video_writer = cv2.VideoWriter("video.avi", cv2.VideoWriter_fourcc(*"MJPG"), 30, (640, 480), True)
# 在循环中写入帧
for i in range(100):
frame = ... # 获取帧
video_writer.write(frame)
# 释放 VideoWriter 对象
video_writer.release()
```
相关问题
fourcc = cv2.VideoWriter_fourcc(*'XVID') out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))
This code sets up a VideoWriter object to write video frames to a file named "output.avi" with the XVID codec, a frame rate of 20 frames per second, and a resolution of 640x480 pixels. The asterisk before 'XVID' is used to unpack the string into individual characters, as expected by the VideoWriter_fourcc function.
if save_vid: if vid_path != save_path: # new video print("save=", save_path) vid_path = save_path if isinstance(vid_writer, cv2.VideoWriter): vid_writer.release() # release previous video writer if vid_cap: # video fps = vid_cap.get(cv2.CAP_PROP_FPS) w = int(vid_cap.get(cv2.CAP_PROP_FRAME_WIDTH)) h = int(vid_cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) else: # stream fps, w, h = 30, im0.shape[1], im0.shape[0] save_path += '.mp4' vid_writer = cv2.VideoWriter(save_path, cv2.VideoWriter_fourcc(*'mp4v'), fps, (w, h)) vid_writer.write(im0)
这段代码是用来保存视频的。它会检查是否需要保存视频,并检查保存路径是否有更改。如果路径改变了,则释放之前的视频写入器,创建一个新的写入器来写入新的视频。如果视频是从文件中读取的,则会从视频中获取帧率、宽度和高度。如果视频是从流中获取的,则会设置默认的帧率为30,宽度和高度为输入图像的宽度和高度。最后,它使用cv2.VideoWriter将帧写入视频。
阅读全文