-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtranscribe_wav.py
More file actions
150 lines (129 loc) · 5.9 KB
/
Copy pathtranscribe_wav.py
File metadata and controls
150 lines (129 loc) · 5.9 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# Trascrive automaticamente i file audio .wav in testo utilizzando il modello Whisper, salvando le trascrizioni e saltando quelle già esistenti.
import os
import subprocess
import sys
import importlib
def upgrade_pip_and_install_whisper():
"""
Aggiorna pip e installa o reinstalla correttamente whisper.
"""
user_home = os.environ.get('USERPROFILE')
python_path = os.path.join(user_home, "AppData", "Local", "Programs", "Python", "Python310", "python.exe")
if not os.path.exists(python_path):
print(f"Errore: Python 3.10 non trovato in {python_path}.")
sys.exit(1)
print("Aggiornamento di pip in corso...")
try:
subprocess.check_call([python_path, "-m", "pip", "install", "--upgrade", "pip"])
except subprocess.CalledProcessError as e:
print(f"Errore durante l'aggiornamento di pip: {e}")
sys.exit(1)
print("Disinstallazione di vecchie versioni di whisper...")
try:
subprocess.check_call([python_path, "-m", "pip", "uninstall", "whisper", "-y"])
except subprocess.CalledProcessError:
# Ignora errori se whisper non è installato
pass
print("Installazione di openai-whisper...")
try:
subprocess.check_call([python_path, "-m", "pip", "install", "-U", "openai-whisper"])
except subprocess.CalledProcessError as e:
print(f"Errore durante l'installazione di openai-whisper: {e}")
sys.exit(1)
def ensure_python_3_10():
"""
Verifica se Python 3.10 è in uso, altrimenti forza l'esecuzione con Python 3.10.
"""
if sys.version_info[0] != 3 or sys.version_info[1] != 10:
print("Forzando l'esecuzione con Python 3.10...")
# Recupera il percorso della home directory reale
user_home = os.environ.get('USERPROFILE')
# Costruisci il percorso di Python 3.10 dinamicamente
python_path = os.path.join(user_home, "AppData", "Local", "Programs", "Python", "Python310", "python.exe")
if not os.path.exists(python_path):
print(f"Errore: Python 3.10 non trovato in {python_path}. Verifica che Python 3.10 sia installato correttamente.")
sys.exit(1)
# Verifica se python3.10 è disponibile
try:
subprocess.check_call([python_path, "--version"])
except subprocess.CalledProcessError:
print("Errore: Python 3.10 non trovato o non configurato correttamente.")
sys.exit(1)
# Esegui lo script con Python 3.10
subprocess.check_call([python_path, os.path.abspath(__file__)] + sys.argv[1:])
sys.exit() # Termina il processo attuale, in modo che non venga eseguito altro codice
def import_whisper():
"""
Importa il modulo whisper in modo sicuro.
"""
try:
import whisper
return whisper
except ImportError:
print("Modulo whisper non trovato. Installazione in corso...")
upgrade_pip_and_install_whisper()
# Riprova ad importare dopo l'installazione
try:
import whisper
return whisper
except ImportError as e:
print(f"Impossibile importare whisper anche dopo l'installazione: {e}")
sys.exit(1)
def transcribe_podcast(file_path, model_name='medium', language='it'):
"""
Trascrive un file audio in formato .wav utilizzando il modello Whisper.
"""
whisper = import_whisper()
model = whisper.load_model(model_name)
result = model.transcribe(file_path, language=language)
return result['text']
def save_transcription(transcription, output_path):
"""
Salva la trascrizione in un file di testo.
"""
with open(output_path, 'w', encoding='utf-8') as f:
f.write(transcription)
def main(podcast_dir):
for root, dirs, files in os.walk(podcast_dir):
for file_name in files:
file_path = os.path.join(root, file_name)
base_name, ext = os.path.splitext(file_name)
# Supportati formati audio (solo .wav ora)
if ext.lower() == '.wav':
output_file_name = base_name + '.txt'
output_path = os.path.join(root, output_file_name)
# Verifica se la trascrizione esiste già
if os.path.exists(output_path) and os.path.getsize(output_path) > 1:
print(f'Saltato {file_name}, il file di trascrizione esiste già.')
continue
try:
print(f'Trascrizione in corso per {file_name}...')
transcription = transcribe_podcast(file_path)
save_transcription(transcription, output_path)
print(f'Trascrizione completata per {file_name}, salvata in {output_path}')
except Exception as e:
print(f'Errore durante la trascrizione di {file_name}: {e}')
if __name__ == "__main__":
# Verifica che Python 3.10 sia utilizzato
ensure_python_3_10()
# Aggiorna pip e installa correttamente whisper
upgrade_pip_and_install_whisper()
podcast_dir = input("Inserisci il percorso della cartella contenente i podcast: ").strip()
if os.path.isdir(podcast_dir):
main(podcast_dir)
print("Trascrizione completata.")
else:
print("Il percorso inserito non è valido. Per favore riprova.")
while True:
scelta = input("\nUtilizza di nuovo lo script digitando 1 o premi 0 per ritornare a main.py: ").strip()
if scelta == '1':
podcast_dir = input("Inserisci il percorso della cartella contenente i podcast: ").strip()
if os.path.isdir(podcast_dir):
main(podcast_dir)
print("Trascrizione completata.")
else:
print("Il percorso inserito non è valido. Per favore riprova.")
elif scelta == '0':
break
else:
print("Scelta non valida. Inserire 1 o 0.")