@@ -267,32 +267,61 @@ def transcribe_audio_parallel(file_path, model_name='medium', language='it'):
267267 """
268268 import concurrent .futures
269269 import time
270+ from tqdm import tqdm
270271
271272 print ("Avvio trascrizione parallela..." )
272273
274+ # Ottieni la durata per la barra di progresso
275+ audio_duration = get_audio_duration (file_path )
276+
273277 # Dividi l'audio in chunk
274278 chunks = split_audio_into_chunks (file_path )
275279
276280 if not chunks or len (chunks ) == 1 :
277281 # Se non è stato possibile dividere o audio troppo corto, trascrizione singola
278282 print ("Esecuzione trascrizione singola (audio corto o indivisibile)" )
279- return transcribe_podcast_with_progress (file_path , model_name , language , speed_up = False )
283+ return transcribe_podcast_with_progress (file_path , model_name , language , parallel = False )
280284
281285 print (f"Trascrizione parallela di { len (chunks )} chunk..." )
282286
283287 start_time = time .time ()
284288
285289 try :
286- # Avvia trascrizione parallela dei chunk
287- with concurrent .futures .ThreadPoolExecutor (max_workers = 2 ) as executor :
288- # Invia i job per i due chunk
289- future1 = executor .submit (transcribe_chunk_parallel , chunks [0 ], model_name , language )
290- future2 = executor .submit (transcribe_chunk_parallel , chunks [1 ], model_name , language )
291-
292- # Attende i risultati
293- print ("Elaborazione chunk in corso..." )
294- chunk1_text = future1 .result (timeout = 600 ) # 10 minuti timeout
295- chunk2_text = future2 .result (timeout = 600 )
290+ # Crea barra di progresso per la trascrizione parallela
291+ with tqdm (total = 100 , desc = "Progresso parallelo" , unit = "%" , ncols = 80 ) as pbar :
292+
293+ # Avvia trascrizione parallela dei chunk
294+ with concurrent .futures .ThreadPoolExecutor (max_workers = 2 ) as executor :
295+ # Invia i job per i due chunk
296+ future1 = executor .submit (transcribe_chunk_parallel , chunks [0 ], model_name , language )
297+ future2 = executor .submit (transcribe_chunk_parallel , chunks [1 ], model_name , language )
298+
299+ # Funzione per aggiornare la barra di progresso durante l'attesa
300+ def update_progress ():
301+ """Aggiorna la barra di progresso durante l'elaborazione parallela"""
302+ while not pbar .disable :
303+ elapsed = time .time () - start_time
304+ # Calcola il progresso basato sul tempo trascorso vs tempo stimato
305+ # I chunk paralleli dovrebbero essere circa 2x più veloci
306+ processing_ratio = 0.15 # secondi di processing per secondo di audio
307+ estimated_progress = min (95 , (elapsed / (audio_duration * processing_ratio / 2 )) * 100 )
308+
309+ if estimated_progress >= pbar .n :
310+ pbar .update (estimated_progress - pbar .n )
311+ pbar .set_postfix ({
312+ "Elaborazione" : f"{ estimated_progress :.1f} %" ,
313+ "Durata" : f"{ audio_duration :.1f} s"
314+ })
315+
316+ time .sleep (0.5 ) # Aggiorna ogni 0.5 secondi
317+
318+ # Avvia il thread per l'aggiornamento del progresso
319+ progress_thread = threading .Thread (target = update_progress , daemon = True )
320+ progress_thread .start ()
321+
322+ # Attende i risultati con barra di progresso
323+ chunk1_text = future1 .result (timeout = 600 ) # 10 minuti timeout
324+ chunk2_text = future2 .result (timeout = 600 )
296325
297326 # Unisce i risultati
298327 full_transcription = chunk1_text .strip () + " " + chunk2_text .strip ()
@@ -313,10 +342,10 @@ def transcribe_audio_parallel(file_path, model_name='medium', language='it'):
313342
314343 except concurrent .futures .TimeoutError :
315344 print ("Timeout nella trascrizione parallela, fallback a trascrizione singola" )
316- return transcribe_podcast_with_progress (file_path , model_name , language , speed_up = False )
345+ return transcribe_podcast_with_progress (file_path , model_name , language , parallel = False )
317346 except Exception as e :
318347 print (f"Errore nella trascrizione parallela: { e } , fallback a trascrizione singola" )
319- return transcribe_podcast_with_progress (file_path , model_name , language , speed_up = False )
348+ return transcribe_podcast_with_progress (file_path , model_name , language , parallel = False )
320349
321350def get_audio_duration (file_path ):
322351 """
@@ -439,7 +468,7 @@ def update_progress():
439468 progress_thread .start ()
440469
441470 # Esegue la trascrizione
442- result = model .transcribe (actual_file_path , language = language )
471+ result = model .transcribe (file_path , language = language )
443472
444473 # Completa la barra di progresso
445474 elapsed = time .time () - start_time
@@ -481,11 +510,13 @@ def format_time(seconds):
481510 """
482511 return str (timedelta (seconds = int (seconds )))
483512
484- def main (podcast_dir , parallel = False ):
513+ def main (podcast_dir , model_name = 'medium' , language = 'it' , parallel = False ):
485514 """
486515 Funzione principale con barra di progresso e ETA.
487516 Args:
488517 podcast_dir: Directory contenente i file audio
518+ model_name: Nome del modello Whisper da utilizzare
519+ language: Lingua del contenuto audio
489520 parallel: Se True, utilizza processamento parallelo per velocizzare
490521 """
491522 # Importa i moduli necessari
@@ -548,7 +579,7 @@ def main(podcast_dir, parallel=False):
548579 wav_file_path = file_path
549580
550581 # Procedi con la trascrizione
551- transcription = transcribe_podcast_with_progress (wav_file_path , parallel = parallel )
582+ transcription = transcribe_podcast_with_progress (wav_file_path , model_name , language , parallel )
552583 save_transcription (transcription , output_path )
553584
554585 processed_files += 1
@@ -622,7 +653,7 @@ def main(podcast_dir, parallel=False):
622653 else :
623654 print ("Rispondi 's' per sì o 'n' per no." )
624655
625- main (podcast_dir , parallel = parallel )
656+ main (podcast_dir , model_name = 'medium' , language = 'it' , parallel = parallel )
626657 else :
627658 print ("Il percorso inserito non è valido. Per favore riprova." )
628659 continue
0 commit comments