mirror of
https://github.com/Nighthawk42/MioTTS.git
synced 2026-08-30 09:42:27 +00:00
96 lines
2.8 KiB
Python
96 lines
2.8 KiB
Python
import argparse
|
|
import csv
|
|
import os
|
|
from typing import Iterable, TextIO
|
|
|
|
|
|
def _iter_rows_with_header(
|
|
fin: TextIO,
|
|
file_col: str,
|
|
text_col: str,
|
|
) -> Iterable[tuple[str, str]]:
|
|
reader = csv.DictReader(fin)
|
|
if reader.fieldnames is None or file_col not in reader.fieldnames or text_col not in reader.fieldnames:
|
|
msg = (
|
|
"Input CSV must have a header with at least the columns "
|
|
f"{file_col!r} and {text_col!r}. Found: {reader.fieldnames!r}"
|
|
)
|
|
raise ValueError(msg)
|
|
|
|
for row in reader:
|
|
file_name = (row.get(file_col) or "").strip()
|
|
text = (row.get(text_col) or "").strip()
|
|
if not file_name or not text:
|
|
continue
|
|
yield file_name, text
|
|
|
|
|
|
def _convert(
|
|
input_path: str,
|
|
output_path: str,
|
|
file_col: str,
|
|
text_col: str,
|
|
) -> None:
|
|
with open(input_path, encoding="utf-8-sig", newline="") as fin, open(
|
|
output_path,
|
|
"w",
|
|
encoding="utf-8",
|
|
newline="\n",
|
|
) as fout:
|
|
for file_name, text in _iter_rows_with_header(fin, file_col=file_col, text_col=text_col):
|
|
base = os.path.basename(file_name)
|
|
file_id, _ = os.path.splitext(base)
|
|
file_id = file_id.strip()
|
|
if not file_id:
|
|
continue
|
|
|
|
normalized_text = text
|
|
original_text = text
|
|
fout.write(f"{file_id}|{normalized_text}|{original_text}\n")
|
|
|
|
|
|
def main() -> None:
|
|
parser = argparse.ArgumentParser(
|
|
description=(
|
|
"Convert a simple CSV (e.g. file_name,text) into an LJSpeech-style metadata.csv "
|
|
"with lines of the form '<file_id>|<normalized_text>|<original_text>'."
|
|
)
|
|
)
|
|
parser.add_argument("--input", required=True, help="Path to the input CSV file.")
|
|
parser.add_argument("--output", required=True, help="Path to write the LJSpeech-style metadata.csv.")
|
|
parser.add_argument(
|
|
"--file-col",
|
|
default="file_name",
|
|
help="Name of the column containing the audio file path (default: file_name).",
|
|
)
|
|
parser.add_argument(
|
|
"--text-col",
|
|
default="text",
|
|
help="Name of the column containing the transcript text (default: text).",
|
|
)
|
|
args = parser.parse_args()
|
|
|
|
input_path = os.path.abspath(args.input)
|
|
output_path = os.path.abspath(args.output)
|
|
|
|
if not os.path.isfile(input_path):
|
|
msg = f"input CSV file not found: {input_path}"
|
|
raise FileNotFoundError(msg)
|
|
|
|
os.makedirs(os.path.dirname(output_path) or ".", exist_ok=True)
|
|
|
|
print(f"Reading CSV : {input_path}")
|
|
print(f"Writing LJS : {output_path}")
|
|
|
|
_convert(
|
|
input_path=input_path,
|
|
output_path=output_path,
|
|
file_col=args.file_col,
|
|
text_col=args.text_col,
|
|
)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|
|
|