Share
Sign In
Code / Tip 공유 게시판
[python] Recording
김영호_메가존
import cv2 import pyautogui import numpy as np import os import tkinter as tk from datetime import datetime import threading # 파일 이름 설정 now = datetime.now() formatted_now = now.strftime('%Y%m%d%H%M%S') fileName = "Result" + formatted_now + ".mp4" # 화면 녹화를 계속할지 여부를 저장하는 변수 recording = True # 녹화 함수 def record_screen(out, screen_size): global recording while recording: try: # 두 개의 모니터의 화면 캡처 img = pyautogui.screenshot(region=(0,0,1920,1080)) # numpy 배열로 변환 frame1 = np.array(img) # BGR 형식으로 변환 frame2 = cv2.cvtColor(frame1, cv2.COLOR_BGR2RGB) # Get the mouse position x, y = pyautogui.position() # Draw the mouse cursor position. cv2.circle(frame2, (x, y), 5, (0, 0, 255), -1) # 녹화 out.write(frame2) except Exception as e: print(e) break # 버튼 클릭 이벤트 핸들러 def stop_recording(): global recording recording = False # GUI 생성 root = tk.Tk() frame = tk.Frame(root) frame.pack() button = tk.Button(frame, text="STOP", width=10, command=stop_recording) button.pack(side=tk.LEFT) # 현재 스크립트의 경로를 얻음 current_dir = os.getcwd() # 녹화 파일을 저장할 폴더의 경로 folder_path = os.path.join(current_dir, 'Video') # 해당 폴더가 없다면 폴더를 생성 if not os.path.exists(folder_path): os.makedirs(folder_path) # 녹화 파일의 경로 output_path = os.path.join(folder_path, fileName) # 'test.mp4' 파일 녹화 시작 screen_size = (1920, 1080) videofps = 30.0 fourcc = cv2.VideoWriter_fourcc(*"XVID") out = cv2.VideoWriter(output_path, fourcc, videofps, screen_size) while True: try: # 화면 캡처 img = pyautogui.screenshot() # numpy 배열로 변환 frame = np.array(img) # BGR 형식으로 변환 frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # Get the mouse position x, y = pyautogui.position() # Draw the mouse cursor position. cv2.circle(frame, (x, y), 5, (0, 0, 255), -1) # 녹화 out.write(frame) # GUI 업데이트 root.update() except Exception as e: print(e) break out.release() cv2.destroyAllWindows() root.destroy()
👍