feat(screen-recorder): support --once parameter

This commit is contained in:
arkohut 2024-08-24 14:37:04 +08:00
parent 4b9b2ce8e1
commit 5f998db989
3 changed files with 120 additions and 30 deletions

View File

@ -0,0 +1,6 @@
@echo off
call C:\Users\arkoh\miniconda3\Scripts\activate.bat memos
:loop
python -m screen_recorder.record-for-win --once
timeout /t 5 /nobreak >nul
goto loop

View File

@ -53,9 +53,9 @@ def take_screenshot(base_dir, previous_hashes, threshold, screen_sequences, date
img = ImageGrab.grab(bbox=(monitor.x, monitor.y, monitor.x + monitor.width, monitor.y + monitor.height))
img = img.convert("RGB")
current_hash = imagehash.phash(img)
current_hash = str(imagehash.phash(img))
if safe_monitor_name in previous_hashes and current_hash - previous_hashes[safe_monitor_name] < threshold:
if safe_monitor_name in previous_hashes and imagehash.hex_to_hash(current_hash) - imagehash.hex_to_hash(previous_hashes[safe_monitor_name]) < threshold:
logging.info(f"Screenshot for {safe_monitor_name} is similar to the previous one. Skipping.")
worklog.write(f"{timestamp} - {safe_monitor_name} - Skipped (similar to previous)\n")
continue
@ -84,15 +84,37 @@ def is_screen_locked():
user32 = ctypes.windll.User32
return user32.GetForegroundWindow() == 0
def main():
parser = argparse.ArgumentParser(description="Screen Recorder for Windows")
parser.add_argument("--threshold", type=int, default=4, help="Threshold for image similarity")
parser.add_argument("--base-dir", type=str, default="~/tmp", help="Base directory for screenshots")
args = parser.parse_args()
def load_previous_hashes(base_dir):
date = time.strftime("%Y%m%d")
hash_file = os.path.join(base_dir, date, ".previous_hashes")
try:
with open(hash_file, "r") as f:
return json.load(f)
except FileNotFoundError:
return {}
base_dir = os.path.expanduser(args.base_dir)
previous_hashes = {}
def save_previous_hashes(base_dir, previous_hashes):
date = time.strftime("%Y%m%d")
hash_file = os.path.join(base_dir, date, ".previous_hashes")
os.makedirs(os.path.dirname(hash_file), exist_ok=True)
with open(hash_file, "w") as f:
json.dump(previous_hashes, f)
def run_screen_recorder_once(args, base_dir, previous_hashes):
if not is_screen_locked():
date = time.strftime("%Y%m%d")
timestamp = time.strftime("%Y%m%d-%H%M%S")
screen_sequences = load_screen_sequences(base_dir, date)
screenshot_files = take_screenshot(
base_dir, previous_hashes, args.threshold, screen_sequences, date, timestamp
)
for screenshot_file in screenshot_files:
logging.info(f"Screenshot saved: {screenshot_file}")
save_previous_hashes(base_dir, previous_hashes)
else:
logging.info("Screen is locked. Skipping screenshot.")
def run_screen_recorder(args, base_dir, previous_hashes):
while True:
try:
if not is_screen_locked():
@ -103,7 +125,7 @@ def main():
base_dir, previous_hashes, args.threshold, screen_sequences, date, timestamp
)
for screenshot_file in screenshot_files:
logging.info(f"Screenshot taken: {screenshot_file}")
logging.info(f"Screenshot saved: {screenshot_file}")
else:
logging.info("Screen is locked. Skipping screenshot.")
except Exception as e:
@ -111,5 +133,27 @@ def main():
time.sleep(5)
def main():
parser = argparse.ArgumentParser(description="Screen Recorder for Windows")
parser.add_argument("--threshold", type=int, default=4, help="Threshold for image similarity")
parser.add_argument("--base-dir", type=str, default="~/tmp", help="Base directory for screenshots")
parser.add_argument("--once", action="store_true", help="Run once and exit")
args = parser.parse_args()
base_dir = os.path.expanduser(args.base_dir)
previous_hashes = load_previous_hashes(base_dir)
if args.once:
run_screen_recorder_once(args, base_dir, previous_hashes)
else:
while True:
try:
run_screen_recorder(args, base_dir, previous_hashes)
except Exception as e:
logging.error(f"Critical error occurred, program will restart in 10 seconds: {str(e)}")
time.sleep(10)
if __name__ == "__main__":
main()

View File

@ -94,12 +94,12 @@ def take_screenshot(
)
# 计算当前截图的哈希值
current_hash = imagehash.phash(img)
current_hash = str(imagehash.phash(img))
# 检查当前截图与前一次截图的哈希值是否相似
if (
screen_name in previous_hashes
and current_hash - previous_hashes[screen_name] < threshold
and imagehash.hex_to_hash(current_hash) - imagehash.hex_to_hash(previous_hashes[screen_name]) < threshold
):
logging.info(f"Screenshot for {screen_name} is similar to the previous one. Skipping.")
os.remove(temp_filename)
@ -148,20 +148,40 @@ def is_screen_locked():
return False
def main():
parser = argparse.ArgumentParser(description="Screen Recorder")
parser.add_argument(
"--threshold", type=int, default=4, help="Threshold for image similarity"
)
parser.add_argument(
"--base-dir", type=str, default="~/tmp", help="Base directory for screenshots"
)
args = parser.parse_args()
def load_previous_hashes(base_dir):
date = time.strftime("%Y%m%d")
hash_file = os.path.join(base_dir, date, ".previous_hashes")
try:
with open(hash_file, "r") as f:
return json.load(f)
except FileNotFoundError:
return {}
base_dir = os.path.expanduser(args.base_dir)
previous_hashes = {}
def save_previous_hashes(base_dir, previous_hashes):
date = time.strftime("%Y%m%d")
hash_file = os.path.join(base_dir, date, ".previous_hashes")
os.makedirs(os.path.dirname(hash_file), exist_ok=True)
with open(hash_file, "w") as f:
json.dump(previous_hashes, f)
def run_screen_recorder_once(args, base_dir, previous_hashes):
if not is_screen_locked():
date = time.strftime("%Y%m%d")
timestamp = time.strftime("%Y%m%d-%H%M%S")
screen_sequences = load_screen_sequences(base_dir, date)
screenshot_files = take_screenshot(
base_dir, previous_hashes, args.threshold, screen_sequences, date, timestamp
)
for screenshot_file in screenshot_files:
logging.info(f"Screenshot saved: {screenshot_file}")
save_previous_hashes(base_dir, previous_hashes)
else:
logging.info("Screen is locked. Skipping screenshot.")
def run_screen_recorder(args, base_dir, previous_hashes):
while True:
try:
if not is_screen_locked():
@ -169,15 +189,10 @@ def main():
timestamp = time.strftime("%Y%m%d-%H%M%S")
screen_sequences = load_screen_sequences(base_dir, date)
screenshot_files = take_screenshot(
base_dir,
previous_hashes,
args.threshold,
screen_sequences,
date,
timestamp,
base_dir, previous_hashes, args.threshold, screen_sequences, date, timestamp
)
for screenshot_file in screenshot_files:
logging.info(f"Screenshot taken: {screenshot_file}")
logging.info(f"Screenshot saved: {screenshot_file}")
else:
logging.info("Screen is locked. Skipping screenshot.")
except Exception as e:
@ -186,5 +201,30 @@ def main():
time.sleep(5)
def main():
parser = argparse.ArgumentParser(description="Screen Recorder")
parser.add_argument(
"--threshold", type=int, default=4, help="Threshold for image similarity"
)
parser.add_argument(
"--base-dir", type=str, default="~/tmp", help="Base directory for screenshots"
)
parser.add_argument("--once", action="store_true", help="Run once and exit")
args = parser.parse_args()
base_dir = os.path.expanduser(args.base_dir)
previous_hashes = load_previous_hashes(base_dir)
if args.once:
run_screen_recorder_once(args, base_dir, previous_hashes)
else:
while True:
try:
run_screen_recorder(args, base_dir, previous_hashes)
except Exception as e:
logging.error(f"Critical error occurred, program will restart in 10 seconds: {str(e)}")
time.sleep(10)
if __name__ == "__main__":
main()