save_path=r"E:\myTool\appium_xiangmu\test_video" ts=calendar.timegm(time.gmtime()) videoname = str(ts)+ ".mp4" save_file_path = '{}\{}'.format(save_path, videoname) #保存视频 fourcc = cv2.VideoWriter_fourcc(*'mp4v') # 不同视频编码对应不同视频格式(例:'I','4','2','0' 对应avi格式) video = cv2.VideoWriter(save_file_path, fourcc, 20, (int(1080/3),int(2340/3))) while True: image=get_imgdecdoe() video.write(image) video.release() #释不完全如何修改代码
时间: 2024-03-13 21:42:43 浏览: 70
Vc开发的IE(源码).rar_IE_VC IE
为了确保视频释放完全,可以将 `video.release()` 放在一个 `try...finally` 语句块中,即使发生异常也能够释放视频。修改后的代码如下所示:
```
import cv2
import calendar
import time
save_path = r"E:\myTool\appium_xiangmu\test_video"
ts = calendar.timegm(time.gmtime())
videoname = str(ts) + ".mp4"
save_file_path = '{}\{}'.format(save_path, videoname)
# 保存视频
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
video = cv2.VideoWriter(save_file_path, fourcc, 20, (int(1080/3),int(2340/3)))
try:
while True:
image = get_imgdecdoe()
video.write(image)
finally:
video.release()
```
这样即使程序发生异常退出,视频也能够完全释放。
阅读全文