From 10d637ea3f60aa4d30d9e6984e2048c8e194643f Mon Sep 17 00:00:00 2001 From: majmongoose Date: Sun, 23 Aug 2026 07:01:14 -0400 Subject: [PATCH] Update pagebot.py --- pagebot.py | 67 +++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 49 insertions(+), 18 deletions(-) diff --git a/pagebot.py b/pagebot.py index 0a79732..07e2dab 100644 --- a/pagebot.py +++ b/pagebot.py @@ -10,7 +10,7 @@ from watchdog.observers import Observer from watchdog.events import FileSystemEventHandler import speech_recognition as sr -def wait_for_file_ready(filepath, stable_seconds=2, timeout=60): +def wait_for_file_ready(filepath, stable_seconds=1, timeout=60): elapsed = 0 last_size = -1 stable_time = 0 @@ -31,6 +31,8 @@ def wait_for_file_ready(filepath, stable_seconds=2, timeout=60): return False last_file_time = time.time() +_processing_lock = threading.Lock() +_processing_files = set() class MyHandler(FileSystemEventHandler): def on_created(self, event): @@ -41,33 +43,62 @@ class MyHandler(FileSystemEventHandler): filepath = event.src_path filename, file_extension = os.path.splitext(filepath) if file_extension.lower() == '.amr': - wait_for_file_ready(filepath) - os.remove(filepath) - print("Removing AMR.") + with _processing_lock: + if filepath in _processing_files: + return + _processing_files.add(filepath) + try: + wait_for_file_ready(filepath) + os.remove(filepath) + print("Removing AMR.") + finally: + with _processing_lock: + _processing_files.discard(filepath) if file_extension.lower() == '.mp3': - print("New MP3!") - text = "" - if secrets_file.speech_to_text: - print("Converting To Text") - text = convert_to_text(filepath, filename) - print("Converting to MP4") - mp4_file = convert_to_mp4(filepath) - print("Sending to Discord") - asyncio.run_coroutine_threadsafe(upload_to_discord(mp4_file, text), client.loop) + with _processing_lock: + if filepath in _processing_files: + return + _processing_files.add(filepath) + try: + print("New MP3!") + wait_for_file_ready(filepath) + text = "" + if secrets_file.speech_to_text: + print("Converting To Text and MP4") + text_result = [""] + def do_text(): + text_result[0] = convert_to_text(filepath, filename) + text_thread = threading.Thread(target=do_text) + text_thread.start() + mp4_file = convert_to_mp4(filepath, delete_source=False) + text_thread.join() + text = text_result[0] + try: + os.remove(filepath) + except OSError: + pass + else: + print("Converting to MP4") + mp4_file = convert_to_mp4(filepath) + print("Sending to Discord") + asyncio.run_coroutine_threadsafe(upload_to_discord(mp4_file, text), client.loop) + finally: + with _processing_lock: + _processing_files.discard(filepath) -def convert_to_mp4(mp3_file): +def convert_to_mp4(mp3_file, delete_source=True): try: - wait_for_file_ready(mp3_file) mp4_file = os.path.splitext(mp3_file)[0] + '.mp4' subprocess.run([ - 'ffmpeg', '-loop', '1', + 'ffmpeg', '-y', '-loop', '1', '-i', secrets_file.image_path, '-i', mp3_file, '-c:a', 'aac', '-b:a', '192k', '-c:v', 'libx264', '-pix_fmt', 'yuv420p', '-shortest', mp4_file ]) - os.remove(mp3_file) + if delete_source: + os.remove(mp3_file) return mp4_file except Exception as e: print(f"Error during conversion: {e}") @@ -77,7 +108,7 @@ def convert_to_text(mp3_path, mp3_name): print(mp3_path) try: wav_file = f"{mp3_name}.wav" - subprocess.run(['ffmpeg', '-i', mp3_path, wav_file]) + subprocess.run(['ffmpeg', '-y', '-i', mp3_path, wav_file]) r = sr.Recognizer() with sr.AudioFile(wav_file) as source: data = r.record(source)