33import json
44import re
55from datetime import datetime
6- import wave
76from pathlib import Path
87
98import pyautogui
@@ -63,6 +62,7 @@ def __init__(self, context: AppContext, parent=None):
6362 self .notes_list = [] # 每块讲稿
6463 self .notes_duration_list = []
6564 self .note_cache_keys = []
65+ self .note_cache_exts = []
6666 self .cache_hit_count = 0 # 新增:缓存命中数
6767 self .is_play_notes = False
6868 self .is_import = False
@@ -317,6 +317,7 @@ def clean_and_reset(self):
317317 self .current_index = 0
318318 self .notes_duration_list = []
319319 self .note_cache_keys = []
320+ self .note_cache_exts = []
320321 self .cache_hit_count = 0
321322 self .is_import = False
322323 self .check_import ()
@@ -327,8 +328,7 @@ def refresh_notes_duration_list(self):
327328 self .load_audio_files ()
328329 duration_list = []
329330 for path in self .media_list :
330- with wave .open (str (path ), 'rb' ) as wav_file :
331- duration = wav_file .getnframes () / float (wav_file .getframerate ())
331+ duration = AudioGenerationTask .get_audio_duration (path )
332332 duration_list .append (duration )
333333 self .notes_duration_list = duration_list
334334
@@ -397,13 +397,14 @@ def mark_split(self):
397397
398398 @staticmethod
399399 def clean_temp_folder (path : Path ):
400- """清理缓存 wav"""
401- for file_path in path .glob ('*.wav' ):
402- try :
403- file_path .unlink ()
404- print (f'已清理 { file_path .name } ' )
405- except Exception as e :
406- print (f'清理文件失败: { file_path .name } , 原因: { e } ' )
400+ """清理临时音频(wav/mp3)"""
401+ for pattern in ('*.wav' , '*.mp3' ):
402+ for file_path in path .glob (pattern ):
403+ try :
404+ file_path .unlink ()
405+ print (f'已清理 { file_path .name } ' )
406+ except Exception as e :
407+ print (f'清理文件失败: { file_path .name } , 原因: { e } ' )
407408 print ('转换完成' )
408409
409410 def thread_print_index (self , import_index ):
@@ -449,6 +450,10 @@ def save_session_record(self):
449450 for item in self .notes_list
450451 ]
451452
453+ if len (self .note_cache_exts ) != len (self .notes_list ):
454+ output_ext = self .ctx .tts_engine .get_output_extension ()
455+ self .note_cache_exts = [output_ext ] * len (self .notes_list )
456+
452457 durations = self .notes_duration_list [:]
453458 if len (durations ) != len (self .notes_list ):
454459 durations = [0.0 ] * len (self .notes_list )
@@ -461,6 +466,7 @@ def save_session_record(self):
461466 'text' : note ['text' ],
462467 'duration' : float (durations [index ]),
463468 'cache_key' : self .note_cache_keys [index ],
469+ 'cache_ext' : self .note_cache_exts [index ],
464470 })
465471
466472 now = datetime .now ()
@@ -531,6 +537,7 @@ def load_session_record(self, record_path: Path):
531537 notes_list = []
532538 duration_list = []
533539 cache_keys = []
540+ cache_exts = []
534541
535542 for idx , item in enumerate (items ):
536543 page = int (item .get ('page' , 0 ))
@@ -540,16 +547,29 @@ def load_session_record(self, record_path: Path):
540547 profile = record .get ('generation_profile' , {})
541548 cache_key = self .ctx .tts_engine .build_audio_cache_key (text , profile )
542549
543- cache_path = self .audio_cache_path / f'{ cache_key } .wav'
544-
545- if not cache_path .exists () or cache_path .stat ().st_size <= 0 :
550+ cache_ext = str (item .get ('cache_ext' , '' )).strip ().lower ().lstrip ('.' )
551+ ext_candidates = [cache_ext ] if cache_ext else []
552+ for ext in ('wav' , 'mp3' ):
553+ if ext not in ext_candidates :
554+ ext_candidates .append (ext )
555+
556+ cache_path = None
557+ for ext in ext_candidates :
558+ candidate = self .audio_cache_path / f'{ cache_key } .{ ext } '
559+ if candidate .exists () and candidate .stat ().st_size > 0 :
560+ cache_path = candidate
561+ cache_ext = ext
562+ break
563+
564+ if cache_path is None :
546565 missing_list .append (f'第{ page } 页-第{ idx + 1 } 条' )
547566 continue
548567
549568 media_list .append (cache_path )
550569 notes_list .append ({'page' : page , 'text' : text })
551570 duration_list .append (float (item .get ('duration' , 0.0 )))
552571 cache_keys .append (cache_key )
572+ cache_exts .append (cache_ext )
553573
554574 if missing_list :
555575 missing_text = '、' .join (missing_list [:10 ])
@@ -572,6 +592,7 @@ def load_session_record(self, record_path: Path):
572592 self .notes_list = notes_list
573593 self .notes_duration_list = duration_list
574594 self .note_cache_keys = cache_keys
595+ self .note_cache_exts = cache_exts
575596
576597 self .media_list = media_list
577598 self .current_index = 0
@@ -608,9 +629,10 @@ def play_notes(self):
608629 self .play_audio ()
609630
610631 def load_audio_files (self ):
611- """查找所有 wav,添加到 media_list 中"""
632+ """查找所有正文音频(wav/mp3),添加到 media_list 中"""
633+ audio_files = list (self .wav_temp_path .glob ('*.wav' )) + list (self .wav_temp_path .glob ('*.mp3' ))
612634 audio_files = sorted (
613- self . wav_temp_path . glob ( '*.wav' ) ,
635+ audio_files ,
614636 key = lambda path : [int (part ) if part .isdigit () else part for part in path .stem .split ('_' )]
615637 )
616638 self .media_list = audio_files
0 commit comments