import pysubs2 import os def validate_and_repair_srt(srt_path): """ Validates an SRT file and attempts to repair it using pysubs2. Args: srt_path (str): Path to the SRT file. Returns: bool: True if valid/repaired, False if critical error. """ if not os.path.exists(srt_path): return False print(f"Validating SRT: {srt_path}...") try: # Load the subtitle file. pysubs2 parser is robust and handles many errors automatically. subs = pysubs2.load(srt_path) # Save it back ensures consistent formatting and fixes minor syntax issues subs.save(srt_path) print("SRT validation passed (file re-saved with correct formatting).") return True except Exception as e: print(f"Warning: SRT validation failed: {e}") return False