Skip to content

Latest commit

 

History

History
282 lines (205 loc) · 9.11 KB

_i26ua6t3r_1.md

File metadata and controls

282 lines (205 loc) · 9.11 KB

Code Review (Requirements,Features,In Progress,Finished,Code,Exception Handling)

#SCREEN RECORDER

REQUIREMENTS:

Python Modules:
from tkinter import *
from time import sleep,time
from threading import Thread
from tkinter.font import Font
import cv2
import numpy as np
import pyautogui
from pyautogui import size
from ttkbootstrap import Style
from PIL import ImageDraw,Image,ImageTk
import random
from os import mkdir,listdir,getlogin
from datetime import datetime,date
import keyboard

FEATURES:

I. Play

II. Stop

III. Mouse Tracker/Mouse Pointer

IV. Maximum Hours To Record(1 Hr)

V. Add "green" (0,255,0) as mouse color with size 5

VI. Make a circle shape to represent mouse movements

IN PROGRESS:


FINISHED:

I. Play

II. Stop

III. Mouse Tracker/Mouse Pointer

IV. Maximum Hours To Record(1 Hr)

V. Add "green" (0,255,0) as mouse color with size 5

VI. Make a circle shape to represent mouse movements

CODE:

from tkinter import 
from time import sleep,time
from threading import Thread
from tkinter.font import Font
import cv2
import numpy as np
import pyautogui
from pyautogui import size
from ttkbootstrap import Style
from PIL import ImageDraw,Image,ImageTk
import random
from os import mkdir,listdir,getlogin
from datetime import datetime,date
import keyboard

SCREENWIDTH,SCREENHEIGHT=size()

day=date.today()
hours=datetime.now().hour
minutes=datetime.now().minute

GETPATH,GETUSERS=[ r"C:\Users",f"\{getlogin()}"]
USERS=GETPATH+GETUSERS

try:
SCREEN_RECORDER,Videos=[f"{USERS}\Videos\SCREEN RECORDER",f"{USERS}\Videos"]

    create=[mkdir(SCREEN_RECORDER)]

    if "SCREEN RECORDER" in listdir(Videos):
        print(f"Found: {SCREEN_RECORDER}")
    else:
        print(f"{SCREEN_RECORDER} Not Found...")

except( WindowsError,IOError) as e:
print("Folders Already Exists...")

RANDOM=random.randrange(start=1,stop=1000)
OUTPUT=f"{SCREEN*RECORDER}\Recorder N{RANDOM} {day} {hours}*{minutes}GMT.avi"

def PIL_IMAGES(height,width): # PLAY TRIANGLE 2
top=(height/8,width/8)
left=(height/8,width)
right=(height,width/2)

    PLAY_TRIANGLE2=Image.new(mode="RGBA",size=[height,width],color=(255,255,255,0))
    draw=ImageDraw.Draw(PLAY_TRIANGLE2)
    draw.polygon(xy=[top,left,right],fill="black",width=4)

    # STOP ROUNDED SQUARE ICON
    topleft=(height/1.5,width/2)
    topright=(height/3,width/2)

    STOP_ICON=Image.new(mode="RGBA",size=[height,width],color=(255,255,255,0))
    draw=ImageDraw.Draw(STOP_ICON)
    draw.rounded_rectangle(xy=[topleft,topright],fill="black",radius=0.8)

    #OPERATIONAL ICON
    lefthalf=((height/3.6)/8,(width/3.6)/8) # *
    righthalf=((height/3.6)/1.2,(width/3.6)/1.2) # *

    OPERATIONAL_STATUS=Image.new(mode="RGBA",size=[int(height/4),int(width/4)],color=(255,255,255,0)) # *
    draw=ImageDraw.Draw(OPERATIONAL_STATUS)
    draw.ellipse(xy=[lefthalf,righthalf],fill="green")

    #RIGHT NAVIGATION
    x1=((height/height)/4,(width)/4)
    x2=((height/1.2)/4,(width/2)/4)
    x3=((height/1.2)/4,(width/2)/4)
    x4=((height/height)/4,(width/width)/4)

    RIGHT_NAV=Image.new(mode="RGBA",size=[int(height/4),int(width/4)],color=(255,255,255,0))
    draw=ImageDraw.Draw(RIGHT_NAV)
    draw.line(xy=[x1,x2],fill="black",width=3)
    draw.line(xy=[x3,x4],fill="black",width=3)

    #LEFT NAVIGATION
    x1=(height,width/width)
    x2=(height/6,width/2)
    x3=(height/6,width/2)
    x4=(height,width)

    LEFT_NAV=Image.new(mode="RGBA",size=[height,width],color="white")
    draw=ImageDraw.Draw(LEFT_NAV)
    draw.line(xy=[x1,x2],fill="black",width=3)
    draw.line(xy=[x3,x4],fill="black",width=3)

    root=Tk()
    style=Style(theme="darkly")
    root.title("Screen Recorder")
    root.geometry(f"{int(SCREENWIDTH/4)}x{int(SCREENHEIGHT/8)}+{int(SCREENWIDTH/1.34)}+{int(SCREENHEIGHT/SCREENHEIGHT)}")
    root.wm_maxsize(int(SCREENWIDTH/4),int(SCREENHEIGHT/8))
    root.wm_minsize(int(SCREENWIDTH/4),int(SCREENHEIGHT/8))

    def screen_recorder():
        fourcc = cv2.VideoWriter_fourcc(*"XVID")
        out = cv2.VideoWriter(OUTPUT,fourcc,12.0,(SCREENWIDTH,SCREENHEIGHT))

    fps = 60
        prev = 0

    while True:
            mouse_x,mouse_y=pyautogui.position() # Get X,Y coords of mouse

    time_elapsed = time() - prev

    img = pyautogui.screenshot()

    if time_elapsed > 1.0/fps:
                prev = time()
                frame = np.array(img)

    cv2.circle(frame, (mouse_x, mouse_y), 5, (0, 255, 0), -1) # Use circle with color of green and size of 5 as a representation of a mouse

    frame = cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)
                out.write(frame)
            cv2.waitKey(10)

    rec=Thread(target=screen_recorder,name="screen recorder",daemon=True) # Initiate screen recorder in a thread to prevent it from freezing and also blocking controls

    OPERATIONAL_STATUS=ImageTk.PhotoImage(OPERATIONAL_STATUS)

    # Start screen recorder
    def start():

    def countdowntimer2(timer,checkhour):
            global s

    s=timer
            def countdownthread():
                def time(sec):
                    global s
                    while 1:
                        s=s+sec

    min,seconds=divmod(s,60)
                        hours,minutes=divmod(min,60)

    if int(hours)==int(checkhour):
                            root.destroy() # *

    break
                        else:
                            print(f"{hours} hrs {minutes} min {seconds} seconds")
                            CountdownTimer.configure(text=f"{hours} hrs {minutes} min {seconds} seconds")

    sleep(1)
                time(1)
            countdownthd=Thread(target=countdownthread,name="countdownthread",daemon=True)
            countdownthd.start()

    countdowntimer2(0,1)

    Recording.configure(text=" Recording",image=OPERATIONAL_STATUS,compound="left")
        rec.start() # Start screen recording

    Recording=Label(root)
    Recording.place(x=f"{int(SCREENWIDTH)/10}",y=f"{int(SCREENHEIGHT)/25}")

    CountdownTimer=Label(root)
    CountdownTimer.place(x=f"{int(SCREENWIDTH)/11}",y=f"{int(SCREENHEIGHT)/13}")

    # Stops screen recorder and close it
    def stop():
        rec.join(timeout=0)
        root.destroy()  # Make sure that in Development mode the "root.quit() or "root.destroy()" method is used to close the window if and only if the application is built for use publicly.
    PLAY_TRIANGLE2=ImageTk.PhotoImage(PLAY_TRIANGLE2)
    button_play=Button(root,command=start,image=PLAY_TRIANGLE2)
    button_play.place(x=f"{int(SCREENWIDTH)/50}",y=f"{int(SCREENHEIGHT)/55}") # *

    STOP_ICON=ImageTk.PhotoImage(STOP_ICON)
    button_stop=Button(root,command=stop,image=STOP_ICON)
    button_stop.place(x=f"{int(SCREENWIDTH)/5}",y=f"{int(SCREENHEIGHT)/55}") # *

    def maximize():

    root.geometry(f"{int(SCREENWIDTH/4)}x{int(SCREENHEIGHT/8)}+{int(SCREENWIDTH/1.34)}+{int(SCREENHEIGHT/SCREENHEIGHT)}")
        root.wm_maxsize(int(SCREENWIDTH/4),int(SCREENHEIGHT/8))
        root.wm_minsize(int(SCREENWIDTH/4),int(SCREENHEIGHT/8))

    root.overrideredirect(False)
        minimize_button.place_configure(x=f"{int(SCREENWIDTH)/4.5}",y=f"{int(SCREENHEIGHT)/10}")
        button_stop.place_configure(x=f"{int(SCREENWIDTH)/5}",y=f"{int(SCREENHEIGHT)/55}")
        Recording.place_configure(x=f"{int(SCREENWIDTH)/10}",y=f"{int(SCREENHEIGHT)/25}")
        CountdownTimer.place_configure(x=f"{int(SCREENWIDTH)/11}",y=f"{int(SCREENHEIGHT)/13}")
        CountdownTimer.configure(font=Font(size=9))
        minimize_button.configure(command=minimize)

    def minimize():
        root.geometry(f"{int(SCREENWIDTH/7)}x{int(SCREENHEIGHT/8)}+{int(SCREENWIDTH/1.17)}+{int(SCREENHEIGHT/SCREENHEIGHT)}")
        root.wm_maxsize(int(SCREENWIDTH/7),int(SCREENHEIGHT/8))
        root.wm_minsize(int(SCREENWIDTH/7),int(SCREENHEIGHT/8))

    root.overrideredirect(True)
        minimize_button.place_configure(x=f"{int(SCREENWIDTH)/7.5}",y=f"{int(SCREENHEIGHT)/9.45}")
        button_stop.place_configure(x=f"{int(SCREENWIDTH)/10}",y=f"{int(SCREENHEIGHT)/55}") # *
        Recording.place_configure(x=f"{int(SCREENWIDTH/SCREENWIDTH)}",y=f"{int(SCREENHEIGHT)/10}")
        CountdownTimer.configure(font=Font(size=7))
        CountdownTimer.place_configure(x=f"{int(SCREENWIDTH)/18}",y=f"{int(SCREENHEIGHT)/9.5}")
        minimize_button.configure(command=maximize)

    RIGHT_NAV=ImageTk.PhotoImage(RIGHT_NAV)
    minimize_button=Button(root,image=RIGHT_NAV,command=minimize)
    minimize_button.place(x=f"{int(SCREENWIDTH)/4.5}",y=f"{int(SCREENHEIGHT)/10}")

    def show_window():
            root.deiconify()
    keyboard.add_hotkey("Alt+S", show_window) # Use the shortcut provided to show app when hidden

    def hide_window():
            root.withdraw()
    keyboard.add_hotkey("Alt+H", hide_window) # Use the shortcut provided to hide app when shown

    root.mainloop()

PIL_IMAGES(60,60)

EXCEPTION HANDLING:

  • WindowsError,IOError:

    • I. Create a folder "SCREEN RECORDER"

    • II. If it exist,exit and continue with operation