import os import logging import sys import time # Ensure project root is in path sys.path.append(r"d:\dsWork\aiData") from Util.ASRClient import ASRClient # Configure logging logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s', stream=sys.stdout) logger = logging.getLogger("Transcriber") def transcribe_all(): audio_dir = r"d:\dsWork\aiData\DouYin\Audios" transcript_dir = r"d:\dsWork\aiData\DouYin\Transcripts" if not os.path.exists(transcript_dir): os.makedirs(transcript_dir) client = ASRClient() files = [f for f in os.listdir(audio_dir) if f.endswith(".mp3")] # Sort files to ensure deterministic order files.sort() logger.info(f"Found {len(files)} audio files.") for filename in files: audio_path = os.path.join(audio_dir, filename) name, _ = os.path.splitext(filename) txt_filename = f"{name}.txt" txt_path = os.path.join(transcript_dir, txt_filename) if os.path.exists(txt_path): # Check if file is empty if os.path.getsize(txt_path) > 0: logger.info(f"Skipping (already exists): {txt_filename}") continue else: logger.info(f"Re-processing empty file: {txt_filename}") logger.info(f"Processing: {filename}") try: # Direct local file transcription using synchronous method text = client.transcribe_file_sync(audio_path) if text: with open(txt_path, 'w', encoding='utf-8') as f: f.write(text) logger.info(f"Saved transcript to: {txt_filename}") else: logger.error(f"Failed to transcribe: {filename}") # Add a small delay between files time.sleep(1) except Exception as e: logger.error(f"Error processing {filename}: {str(e)}", exc_info=True) if __name__ == "__main__": try: transcribe_all() except KeyboardInterrupt: logger.info("Stopped by user") except Exception as e: logger.error(f"Fatal error: {str(e)}", exc_info=True)