Files
tts-plus/commands.py
T
2026-05-24 16:42:11 +08:00

117 lines
3.1 KiB
Python

import click
from loguru import logger
@click.group()
def cli():
pass
@cli.command()
@click.option(
"--backend", default="crispasr", help="The backend to use for speech recognition"
)
@click.option(
"--model", default="./qwen3tts", help="The model to use for speech recognition"
)
@click.option(
"--output", default="./output.wav", help="The output file for the generated speech"
)
@click.option(
"--output-dir",
default="./outputs",
help="The output directory for the generated speech",
)
@click.option(
"--gpu-backend",
default="auto",
help="The GPU backend to use for speech recognition",
)
@click.option(
"--instruct",
default="用符合文本应读语气说",
help="The instruction to use for speech recognition",
)
@click.option(
"executable",
"--executable",
default="./crispasr",
help="The executable to use for speech recognition",
)
@click.option(
"--codec-model",
help="The codec model to use for speech recognition",
)
@click.option(
"--strict",
is_flag=True,
help="Use strict parsing mode,which just keep Chinese, English and numbers",
)
@click.argument("input_file")
def say(
backend: str,
model: str,
output: str,
input_file: str,
gpu_backend: str,
instruct: str,
executable: str,
output_dir: str,
codec_model: str,
strict: bool,
):
with open(input_file, "r", encoding="utf-8") as f:
html = f.read()
from parser import TTSHTMLParser
logger.info("解析中...")
parser = TTSHTMLParser(html)
result = parser.parse(strict=strict)
logger.debug(f"解析结果: {result}")
match backend:
case "crispasr":
from backend import CrispASRCLIBackend
tts_backend = CrispASRCLIBackend(
model=model,
executable=executable,
gpu_backend=gpu_backend,
codec_model=codec_model,
)
for voice, text in result:
logger.info(f"正在合成: voice={voice}, text={text}")
tts_backend.smart_say(
voice=voice,
text=text,
instruct=instruct,
output_file_prefix=f"{output_dir}/{voice}_{text[0:9]}",
)
case _:
raise ValueError(f"Unsupported backend: {backend}")
@cli.command()
@click.argument("input_dir")
@click.argument("output_file")
@click.option("--format", default="wav", help="The output format, either wav or mp3")
def combine(input_dir: str, output_file: str, format: str):
from files import FileHandler
handler = FileHandler(input_dir)
if format == "wav":
handler.combine_waves(output_file)
elif format == "mp3":
handler.combine_waves(output_file)
handler.wavs_to_mp3(output_file, output_file.replace(".wav", ".mp3"))
@cli.command()
@click.argument("input_file")
@click.argument("output_file")
def transform(input_file: str, output_file: str):
from files import FileHandler
handler = FileHandler(input_file)
handler.wavs_to_mp3(input_file, output_file)