improve(tts-voice): use spawnSync args to avoid shell quoting issues

This commit is contained in:
2026-03-13 12:02:57 +08:00
parent 9df7c7c4cb
commit 36e0d349b2

View File

@@ -8,7 +8,7 @@
* - curl CLI
*/
import { execSync } from 'child_process';
import { execSync, spawnSync } from 'child_process';
import { readFileSync, existsSync, unlinkSync } from 'fs';
const LUXTTS_BASE = 'http://localhost:7860';
@@ -113,19 +113,28 @@ function generateSpeech(text: string, params: TtsParams): string | null {
const outPath = `/tmp/tts_output_${timestamp}.wav`;
try {
const httpCode = execSync(
`curl -s -o ${outPath} -w "%{http_code}" ` +
`-b ${COOKIE_JAR} ` +
`-X POST ${LUXTTS_BASE}/luxtts/api/tts ` +
`-F "ref_audio=@${REF_AUDIO}" ` +
`-F "text=${text.replace(/"/g, '\\"')}" ` +
`-F "num_steps=${params.numSteps}" ` +
`-F "t_shift=${params.tShift}" ` +
`-F "speed=${params.speed}"`,
{ timeout: 120000 } // 2 min timeout for CPU synthesis
).toString().trim();
const args = [
'-s',
'-o', outPath,
'-w', '%{http_code}',
'-b', COOKIE_JAR,
'-X', 'POST',
`${LUXTTS_BASE}/luxtts/api/tts`,
'-F', `ref_audio=@${REF_AUDIO}`,
'-F', `text=${text}`,
'-F', `num_steps=${params.numSteps}`,
'-F', `t_shift=${params.tShift}`,
'-F', `speed=${params.speed}`,
];
if (httpCode === '200' && existsSync(outPath)) {
const result = spawnSync('curl', args, {
timeout: 120000, // 2 min timeout for CPU synthesis
encoding: 'utf-8',
});
const httpCode = (result.stdout || '').trim();
if (result.status === 0 && httpCode === '200' && existsSync(outPath)) {
return outPath;
}