from PIL import ImageGrabimport numpy as npimport cv2from pynput import keyboardimport threadingimport datetimeimport osflag = Falsedef 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 Falseif __name__ == '__main__': print('开始录制!') th = threading.Thread(target=video_record) th.start() with keyboard.Listener(on_press=on_press) as listener: listener.join()在这段代码上把要保存的文件名修改为自定义格式
时间: 2024-03-03 11:48:08 浏览: 56
【Linux】Ubuntu18.04使用【python2】安装PIL、opencv、numpy、matplotlib、pandas、seaborn
你可以把这段代码中的文件名修改为你想要的格式,例如将以下代码中的"视频_{}.avi"修改为"my_video_{}.mp4":
```python
filename = os.path.join(desktop_path, 'my_video_{}.mp4'.format(datetime.datetime.now().strftime('%Y%m%d_%H%M%S')))
```
这样录制的视频将以"my_video_日期_时间.mp4"的格式保存在桌面上。
阅读全文