import osdesktop_path = os.path.join(os.path.expanduser('~'), 'Desktop')filename = os.path.join(desktop_path, '视频_{}.avi'.format(datetime.datetime.now().strftime('%Y%m%d_%H%M%S')))video = cv2.VideoWriter(filename, fourcc, 28, (a, b))把这段代码加到上面总代码里
时间: 2024-01-30 18:03:34 浏览: 51
python 获取文件下所有文件或目录os.walk()的实例
好的,这是更新后的完整代码:
```python
from PIL import ImageGrab
import numpy as np
import cv2
from pynput import keyboard
import threading
import datetime
import os
flag = False
def video_record():
p = ImageGrab.grab()
a, b = p.size
fourcc = cv2.VideoWriter_fourcc(*'XVID')
desktop_path = os.path.join(os.path.expanduser('~'), 'Desktop')
filename = os.path.join(desktop_path, '视频_{}.avi'.format(datetime.datetime.now().strftime('%Y%m%d_%H%M%S')))
video = cv2.VideoWriter(filename, fourcc, 28, (a, b))
while True:
im = ImageGrab.grab()
inn = cv2.cvtColor(np.array(im), cv2.COLOR_RGB2BGR)
video.write(inn)
if flag:
print('录制结束!')
break
video.release()
def on_press(key):
global flag
if key == keyboard.Key.esc:
flag = True
return False
if __name__ == '__main__':
print('开始录制!')
th = threading.Thread(target=video_record)
th.start()
with keyboard.Listener(on_press=on_press) as listener:
listener.join()
```
这个代码将录制的视频保存在桌面上,并以“视频_年月日_时分秒.avi”的格式命名。
阅读全文