Make conversion to text smarter

Avoids issue where multiple pages happen back to back
This commit is contained in:
majmongoose 2024-02-12 14:05:40 -05:00
parent 41bb8756fd
commit df4ca14b9c
2 changed files with 7 additions and 7 deletions

2
.gitignore vendored
View File

@ -3,7 +3,7 @@ __pycache__/
# Ignore ffmpeg executable # Ignore ffmpeg executable
ffmpeg ffmpeg
ffmpeg.exe ffmpeg.exe
output_audio.wav *.wav
# Ignore secrets file # Ignore secrets file
secrets_file.py secrets_file.py

View File

@ -25,7 +25,7 @@ class MyHandler(FileSystemEventHandler):
text = "" text = ""
if secrets_file.speech_to_text: if secrets_file.speech_to_text:
print("Converting To Text") print("Converting To Text")
text = convert_to_text(filepath) text = convert_to_text(filepath,filename)
print("Converting to MP4") print("Converting to MP4")
mp4_file = convert_to_mp4(filepath) mp4_file = convert_to_mp4(filepath)
print("Sending to Discord") print("Sending to Discord")
@ -46,18 +46,18 @@ def convert_to_mp4(mp3_file):
print(f"Error during conversion: {e}") print(f"Error during conversion: {e}")
return None return None
def convert_to_text(mp3_file): def convert_to_text(mp3_path,mp3_name):
print(mp3_file) print(mp3_path)
try: try:
command = f'ffmpeg -i "{mp3_file}" output_audio.wav' command = f'ffmpeg -i "{mp3_path}" {mp3_name}.wav'
subprocess.run(command, shell=True) subprocess.run(command, shell=True)
r = sr.Recognizer() r = sr.Recognizer()
# Load the audio file # Load the audio file
with sr.AudioFile("output_audio.wav") as source: with sr.AudioFile(f"{mp3_name}.wav") as source:
data = r.record(source) data = r.record(source)
# Convert speech to text # Convert speech to text
text = r.recognize_google(data) text = r.recognize_google(data)
os.remove("output_audio.wav") os.remove("{mp3_name}.wav")
return (text) return (text)
except Exception as e: except Exception as e:
print(f"Error during conversion: {e}") print(f"Error during conversion: {e}")