import cv2
import numpy as np
import pyautogui
import time
#from record_audio import start_audio_recording, stop_audio_recording
from datetime import datetime
from threading import Thread



# Global flag to control the recording state
is_recording = False

def record_screen(output_video_file, fps=10):
    """
    Records the screen and saves it as a video file.
    The recording continues until stop_recording() is called.
    """
    global is_recording
    screen_width, screen_height = pyautogui.size()

    # Define the codec and create VideoWriter object
    fourcc = cv2.VideoWriter_fourcc(*"XVID")
    out = cv2.VideoWriter(output_video_file, fourcc, fps, (screen_width, screen_height))

    print("Screen recording started!")

    # Start the recording loop
    is_recording = True
    while is_recording:
        # Capture the screen
        screenshot = pyautogui.screenshot()

        # Convert screenshot to a numpy array
        frame = np.array(screenshot)

        # Convert colors from RGB to BGR (OpenCV uses BGR format)
        frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)

        # Write the frame to the video file
        out.write(frame)

        # Delay for the next frame
        time.sleep(1 / fps)

    print("Screen recording finished.")
    out.release()

def stop_video_recording():
    """
    Stops the screen recording.
    """
    global is_recording
    is_recording = False


