-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpriya2.py
More file actions
53 lines (44 loc) · 2 KB
/
Copy pathpriya2.py
File metadata and controls
53 lines (44 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import speech_recognition as sr
import os
def rename_file_from_voice_command(old_name, new_name):
"""Renames a file using the provided old and new names."""
try:
old_name += ".txt"
new_name += ".txt"
if not os.path.exists(old_name):
print(f"❌ Error: '{old_name}' not found.")
return
os.rename(old_name, new_name)
print(f"✅ File successfully renamed from '{old_name}' to '{new_name}'")
except Exception as e:
print(f"❌ Error: {e}")
def listen_for_filename(prompt):
"""Listens for a single filename input via voice command."""
recognizer = sr.Recognizer()
mic = sr.Microphone()
with mic as source:
recognizer.adjust_for_ambient_noise(source, duration=3) # Increase noise adaptation
print(f"🎤 {prompt}")
try:
audio = recognizer.listen(source, timeout=10, phrase_time_limit=5) # Increased timeout
command = recognizer.recognize_google(audio, language="en-US")
print(f"📝 You said: {command}")
return command.strip().replace(" ", "_") # Replace spaces with underscores
except sr.UnknownValueError:
print("❌ Could not understand. Please try again.")
return None
except sr.WaitTimeoutError:
print("⏳ Timeout: No speech detected. Try speaking louder and clearly.")
return None
except sr.RequestError as e:
print(f"❌ Error with speech recognition service: {e}")
return None
if __name__ == "__main__":
print("🔊 Welcome to the Voice-Controlled File Renamer!")
old_name = None
while old_name is None:
old_name = listen_for_filename("Say the name of the file you want to rename (without .txt)")
new_name = None
while new_name is None:
new_name = listen_for_filename("Say the new name for the file (without .txt)")
rename_file_from_voice_command(old_name, new_name)