- Reorganize source code into src/ directory with logical subdirectories: - src/servers/: MCP and REST API server implementations - src/core/: Core business logic (transcriber, model_manager) - src/utils/: Utility modules (audio_processor, formatters) - Update all import statements to use proper module paths - Configure PYTHONPATH in startup scripts and Dockerfile - Update documentation with new structure and paths - Update pyproject.toml with package configuration - Keep DevOps files (scripts, Dockerfile, configs) at root level All functionality validated and working correctly.
166 lines
4.5 KiB
Python
166 lines
4.5 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Formatting Output Module
|
|
Responsible for formatting transcription results into different output formats (VTT, SRT, JSON, TXT)
|
|
"""
|
|
|
|
import json
|
|
from typing import List, Dict, Any
|
|
|
|
def format_vtt(segments: List) -> str:
|
|
"""
|
|
Format transcription results to VTT
|
|
|
|
Args:
|
|
segments: List of transcription segments
|
|
|
|
Returns:
|
|
str: Subtitle content in VTT format
|
|
"""
|
|
vtt_content = "WEBVTT\n\n"
|
|
|
|
for segment in segments:
|
|
start = format_timestamp(segment.start)
|
|
end = format_timestamp(segment.end)
|
|
text = segment.text.strip()
|
|
|
|
if text:
|
|
vtt_content += f"{start} --> {end}\n{text}\n\n"
|
|
|
|
return vtt_content
|
|
|
|
def format_srt(segments: List) -> str:
|
|
"""
|
|
Format transcription results to SRT
|
|
|
|
Args:
|
|
segments: List of transcription segments
|
|
|
|
Returns:
|
|
str: Subtitle content in SRT format
|
|
"""
|
|
srt_content = ""
|
|
index = 1
|
|
|
|
for segment in segments:
|
|
start = format_timestamp_srt(segment.start)
|
|
end = format_timestamp_srt(segment.end)
|
|
text = segment.text.strip()
|
|
|
|
if text:
|
|
srt_content += f"{index}\n{start} --> {end}\n{text}\n\n"
|
|
index += 1
|
|
|
|
return srt_content
|
|
|
|
def format_txt(segments: List) -> str:
|
|
"""
|
|
Format transcription results to plain text
|
|
|
|
Args:
|
|
segments: List of transcription segments
|
|
|
|
Returns:
|
|
str: Plain text transcription content
|
|
"""
|
|
text_content = ""
|
|
|
|
for segment in segments:
|
|
text = segment.text.strip()
|
|
if text:
|
|
# Add the text content
|
|
text_content += text
|
|
|
|
# Add appropriate spacing between segments
|
|
# If the text doesn't end with punctuation, add a space
|
|
if not text.endswith(('.', '!', '?', ':', ';')):
|
|
text_content += " "
|
|
else:
|
|
# If it ends with punctuation, add a space for natural flow
|
|
text_content += " "
|
|
|
|
# Clean up any trailing whitespace and ensure single line breaks
|
|
text_content = text_content.strip()
|
|
|
|
# Replace multiple spaces with single spaces
|
|
while " " in text_content:
|
|
text_content = text_content.replace(" ", " ")
|
|
|
|
return text_content
|
|
|
|
def format_json(segments: List, info: Any) -> str:
|
|
"""
|
|
Format transcription results to JSON
|
|
|
|
Args:
|
|
segments: List of transcription segments
|
|
info: Transcription information object
|
|
|
|
Returns:
|
|
str: Transcription results in JSON format
|
|
"""
|
|
result = {
|
|
"segments": [{
|
|
"id": i,
|
|
"start": segment.start,
|
|
"end": segment.end,
|
|
"text": segment.text.strip(),
|
|
"words": [{
|
|
"word": word.word,
|
|
"start": word.start,
|
|
"end": word.end,
|
|
"probability": word.probability
|
|
} for word in segment.words] if hasattr(segment, 'words') and segment.words else []
|
|
} for i, segment in enumerate(segments)],
|
|
"language": info.language,
|
|
"language_probability": info.language_probability if hasattr(info, 'language_probability') else None,
|
|
"duration": info.duration,
|
|
"all_language_probs": info.all_language_probs if hasattr(info, 'all_language_probs') else None
|
|
}
|
|
return json.dumps(result, indent=2, ensure_ascii=False)
|
|
|
|
def format_timestamp(seconds: float) -> str:
|
|
"""
|
|
Format timestamp for VTT format
|
|
|
|
Args:
|
|
seconds: Number of seconds
|
|
|
|
Returns:
|
|
str: Formatted timestamp (HH:MM:SS.mmm)
|
|
"""
|
|
hours = int(seconds // 3600)
|
|
minutes = int((seconds % 3600) // 60)
|
|
seconds = seconds % 60
|
|
return f"{hours:02d}:{minutes:02d}:{seconds:06.3f}"
|
|
|
|
def format_timestamp_srt(seconds: float) -> str:
|
|
"""
|
|
Format timestamp for SRT format
|
|
|
|
Args:
|
|
seconds: Number of seconds
|
|
|
|
Returns:
|
|
str: Formatted timestamp (HH:MM:SS,mmm)
|
|
"""
|
|
hours = int(seconds // 3600)
|
|
minutes = int((seconds % 3600) // 60)
|
|
secs = int(seconds % 60)
|
|
msecs = int((seconds - int(seconds)) * 1000)
|
|
return f"{hours:02d}:{minutes:02d}:{secs:02d},{msecs:03d}"
|
|
|
|
def format_time(seconds: float) -> str:
|
|
"""
|
|
Format time into readable format
|
|
|
|
Args:
|
|
seconds: Number of seconds
|
|
|
|
Returns:
|
|
str: Formatted time (HH:MM:SS)
|
|
"""
|
|
hours = int(seconds // 3600)
|
|
minutes = int((seconds % 3600) // 60)
|
|
secs = int(seconds % 60)
|
|
return f"{hours:02d}:{minutes:02d}:{secs:02d}" |