Process a single transcript file and extract metadata. Args: file_path: Path to the transcript file audio_extension: Extension for associated audio file (default: .m4a) Returns: Dictionary containing transcript metadata Raises: FileNotFoundError: I
(
file_path: FilePath, audio_extension: str = ".m4a"
)
| 91 | |
| 92 | |
| 93 | def process_transcript_file( |
| 94 | file_path: FilePath, audio_extension: str = ".m4a" |
| 95 | ) -> TranscriptDict: |
| 96 | """ |
| 97 | Process a single transcript file and extract metadata. |
| 98 | |
| 99 | Args: |
| 100 | file_path: Path to the transcript file |
| 101 | audio_extension: Extension for associated audio file (default: .m4a) |
| 102 | |
| 103 | Returns: |
| 104 | Dictionary containing transcript metadata |
| 105 | |
| 106 | Raises: |
| 107 | FileNotFoundError: If the transcript file doesn't exist |
| 108 | ValueError: If the transcript format is invalid |
| 109 | """ |
| 110 | if not os.path.exists(file_path): |
| 111 | raise FileNotFoundError(f"Transcript file not found: {file_path}") |
| 112 | |
| 113 | try: |
| 114 | # Read file content |
| 115 | with open(file_path, "r", encoding="utf-8") as f: |
| 116 | content = f.read() |
| 117 | |
| 118 | # Extract file information |
| 119 | file_info = extract_file_info(file_path) |
| 120 | |
| 121 | # Parse transcript using TranscriptReader |
| 122 | reader = TranscriptReader( |
| 123 | file_path=None, transcript_string=content, ext=file_info["extension"] |
| 124 | ) |
| 125 | transcript, transcript_start, transcript_end = reader.read() |
| 126 | |
| 127 | # Calculate transcript length |
| 128 | length = calculate_transcript_length(transcript_start, transcript_end) |
| 129 | |
| 130 | # Construct audio file path |
| 131 | audio_file = file_info["base_path"] + audio_extension |
| 132 | |
| 133 | return { |
| 134 | "subtitle_file": file_path, |
| 135 | "content": content, |
| 136 | "length": length, |
| 137 | "audio_file": audio_file, |
| 138 | "id": file_info["base_name"], |
| 139 | } |
| 140 | |
| 141 | except Exception as e: |
| 142 | print(f"Error processing {file_path}: {e}") |
| 143 | # Return a minimal dictionary for failed files |
| 144 | file_info = extract_file_info(file_path) |
| 145 | return { |
| 146 | "subtitle_file": file_path, |
| 147 | "content": "", |
| 148 | "length": 0.0, |
| 149 | "audio_file": file_info["base_path"] + audio_extension, |
| 150 | "id": file_info["base_name"], |
no test coverage detected