35 lines
816 B
Python
35 lines
816 B
Python
import json
|
|
from pathlib import Path
|
|
|
|
from transcriber.merge.aligner import MergedSegment
|
|
|
|
|
|
def write_json(segments: list[MergedSegment], output_path: str) -> str:
|
|
"""Export merged segments as a structured JSON file.
|
|
|
|
Args:
|
|
segments: List of merged speaker segments.
|
|
output_path: Path to the output .json file.
|
|
|
|
Returns:
|
|
Path to the written file.
|
|
"""
|
|
path = Path(output_path)
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
|
|
data = [
|
|
{
|
|
"speaker": seg.speaker,
|
|
"start": round(seg.start, 2),
|
|
"end": round(seg.end, 2),
|
|
"text": seg.text,
|
|
}
|
|
for seg in segments
|
|
]
|
|
|
|
path.write_text(
|
|
json.dumps(data, ensure_ascii=False, indent=2),
|
|
encoding="utf-8",
|
|
)
|
|
return str(path)
|