auto-tts 0.1.1 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -59,6 +59,7 @@ auto-tts --version # 查看版本
59
59
  - **长文本**:自动分块合成,长度不限
60
60
  - **批量队列**:用空行分隔多段文本,每段成为一个独立任务
61
61
  - **任务面板**:实时进度、失败原因、试听、下载 mp3
62
+ - **字幕导出**:任务完成后可下载与音频逐句对齐的 SRT 字幕
62
63
 
63
64
  ### 关于语气
64
65
 
@@ -105,6 +106,7 @@ cd auto-tts
105
106
  | GET | `/api/jobs/{id}` | 单个任务 |
106
107
  | DELETE | `/api/jobs/{id}` | 删除任务 |
107
108
  | GET | `/api/audio/{filename}` | 获取音频文件 |
109
+ | GET | `/api/subtitles/{filename}` | 获取 SRT 字幕文件 |
108
110
 
109
111
  ## 技术栈
110
112
 
Binary file
@@ -0,0 +1,97 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg" width="1146" height="717"
2
+ viewBox="0 0 1146 717" role="img"
3
+ aria-label="auto-tts 无限量文本转语音">
4
+ <title>auto-tts · 无限量文本转语音</title>
5
+
6
+ <rect width="1146" height="717" fill="#E8E6E1"/>
7
+
8
+ <!-- eyebrow -->
9
+ <rect x="72" y="74" width="13" height="13" fill="#E23A2E"/>
10
+ <text x="96" y="87" font-family="'IBM Plex Mono',ui-monospace,'SFMono-Regular',monospace" font-size="19"
11
+ letter-spacing="2.4" fill="#63666B">OPEN SOURCE · EDGE-TTS · AGPL-3.0</text>
12
+
13
+ <!-- wordmark -->
14
+ <text id="title" x="72" y="300" font-family="'Space Grotesk','PingFang SC','Noto Sans CJK SC','Microsoft YaHei',sans-serif" font-size="176"
15
+ font-weight="700" letter-spacing="-6.5" fill="#191B1D">auto-tts</text>
16
+ <rect x="732.0" y="221" width="34" height="34" fill="#E23A2E"/>
17
+
18
+ <!-- claim: the hook, with the differentiator picked out in the signal colour -->
19
+ <text x="72" y="412" font-family="'Space Grotesk','PingFang SC','Noto Sans CJK SC','Microsoft YaHei',sans-serif" font-size="98"
20
+ font-weight="700" letter-spacing="1"><tspan fill="#E23A2E">无限量</tspan><tspan fill="#191B1D">文本转语音</tspan></text>
21
+ <text x="74" y="452" font-family="'Space Grotesk','PingFang SC','Noto Sans CJK SC','Microsoft YaHei',sans-serif" font-size="24"
22
+ fill="#63666B">选音色与语气 · 批量排队 · 一键合成 MP3,可在线试听下载</text>
23
+
24
+ <!-- waveform band -->
25
+ <line x1="72" y1="496" x2="1074" y2="496" stroke="#CFCCC5" stroke-width="1"/>
26
+ <line x1="72" y1="590" x2="1074" y2="590" stroke="#CFCCC5" stroke-width="1"/>
27
+ <rect x="72.00" y="538.00" width="11.72" height="10" rx="1.5" fill="#191B1D"/>
28
+ <rect x="87.72" y="535.50" width="11.72" height="15" rx="1.5" fill="#191B1D"/>
29
+ <rect x="103.44" y="531.00" width="11.72" height="24" rx="1.5" fill="#191B1D"/>
30
+ <rect x="119.16" y="524.00" width="11.72" height="38" rx="1.5" fill="#191B1D"/>
31
+ <rect x="134.88" y="515.50" width="11.72" height="55" rx="1.5" fill="#191B1D"/>
32
+ <rect x="150.59" y="507.00" width="11.72" height="72" rx="1.5" fill="#191B1D"/>
33
+ <rect x="166.31" y="513.00" width="11.72" height="60" rx="1.5" fill="#191B1D"/>
34
+ <rect x="182.03" y="524.00" width="11.72" height="38" rx="1.5" fill="#191B1D"/>
35
+ <rect x="197.75" y="532.50" width="11.72" height="21" rx="1.5" fill="#191B1D"/>
36
+ <rect x="213.47" y="536.50" width="11.72" height="13" rx="1.5" fill="#191B1D"/>
37
+ <rect x="229.19" y="533.50" width="11.72" height="19" rx="1.5" fill="#191B1D"/>
38
+ <rect x="244.91" y="527.50" width="11.72" height="31" rx="1.5" fill="#191B1D"/>
39
+ <rect x="260.62" y="519.00" width="11.72" height="48" rx="1.5" fill="#191B1D"/>
40
+ <rect x="276.34" y="510.00" width="11.72" height="66" rx="1.5" fill="#191B1D"/>
41
+ <rect x="292.06" y="503.50" width="11.72" height="79" rx="1.5" fill="#191B1D"/>
42
+ <rect x="307.78" y="511.50" width="11.72" height="63" rx="1.5" fill="#191B1D"/>
43
+ <rect x="323.50" y="522.50" width="11.72" height="41" rx="1.5" fill="#191B1D"/>
44
+ <rect x="339.22" y="531.00" width="11.72" height="24" rx="1.5" fill="#191B1D"/>
45
+ <rect x="354.94" y="535.50" width="11.72" height="15" rx="1.5" fill="#191B1D"/>
46
+ <rect x="370.66" y="532.00" width="11.72" height="22" rx="1.5" fill="#191B1D"/>
47
+ <rect x="386.38" y="525.50" width="11.72" height="35" rx="1.5" fill="#191B1D"/>
48
+ <rect x="402.09" y="516.00" width="11.72" height="54" rx="1.5" fill="#191B1D"/>
49
+ <rect x="417.81" y="507.50" width="11.72" height="71" rx="1.5" fill="#191B1D"/>
50
+ <rect x="433.53" y="502.00" width="11.72" height="82" rx="1.5" fill="#191B1D"/>
51
+ <rect x="449.25" y="510.00" width="11.72" height="66" rx="1.5" fill="#191B1D"/>
52
+ <rect x="464.97" y="521.00" width="11.72" height="44" rx="1.5" fill="#191B1D"/>
53
+ <rect x="480.69" y="530.00" width="11.72" height="26" rx="1.5" fill="#191B1D"/>
54
+ <rect x="496.41" y="534.50" width="11.72" height="17" rx="1.5" fill="#191B1D"/>
55
+ <rect x="512.12" y="537.00" width="11.72" height="12" rx="1.5" fill="#191B1D"/>
56
+ <rect x="527.84" y="533.50" width="11.72" height="19" rx="1.5" fill="#191B1D"/>
57
+ <rect x="543.56" y="528.00" width="11.72" height="30" rx="1.5" fill="#191B1D"/>
58
+ <rect x="559.28" y="519.50" width="11.72" height="47" rx="1.5" fill="#191B1D"/>
59
+ <rect x="575.00" y="509.00" width="11.72" height="68" rx="1.5" fill="#191B1D"/>
60
+ <rect x="590.72" y="500.00" width="11.72" height="86" rx="1.5" fill="#191B1D"/>
61
+ <rect x="606.44" y="506.00" width="11.72" height="74" rx="1.5" fill="#191B1D"/>
62
+ <rect x="622.16" y="518.00" width="11.72" height="50" rx="1.5" fill="#191B1D"/>
63
+ <rect x="637.88" y="528.00" width="11.72" height="30" rx="1.5" fill="#191B1D"/>
64
+ <rect x="653.59" y="533.50" width="11.72" height="19" rx="1.5" fill="#191B1D"/>
65
+ <rect x="669.31" y="536.00" width="11.72" height="14" rx="1.5" fill="#191B1D"/>
66
+ <rect x="685.03" y="531.50" width="11.72" height="23" rx="1.5" fill="#191B1D"/>
67
+ <rect x="700.75" y="523.50" width="11.72" height="39" rx="1.5" fill="#E23A2E"/>
68
+ <rect x="716.47" y="512.50" width="11.72" height="61" rx="1.5" fill="#E23A2E"/>
69
+ <rect x="732.19" y="504.00" width="11.72" height="78" rx="1.5" fill="#E23A2E"/>
70
+ <rect x="747.91" y="499.00" width="11.72" height="88" rx="1.5" fill="#E23A2E"/>
71
+ <rect x="763.62" y="507.00" width="11.72" height="72" rx="1.5" fill="#E23A2E"/>
72
+ <rect x="779.34" y="517.00" width="11.72" height="52" rx="1.5" fill="#E23A2E"/>
73
+ <rect x="795.06" y="526.50" width="11.72" height="33" rx="1.5" fill="#E23A2E"/>
74
+ <rect x="810.78" y="532.50" width="11.72" height="21" rx="1.5" fill="#E23A2E"/>
75
+ <rect x="826.50" y="535.50" width="11.72" height="15" rx="1.5" fill="#E23A2E"/>
76
+ <rect x="842.22" y="530.00" width="11.72" height="26" rx="1.5" fill="#E23A2E"/>
77
+ <rect x="857.94" y="520.50" width="11.72" height="45" rx="1.5" fill="#E23A2E"/>
78
+ <rect x="873.66" y="509.50" width="11.72" height="67" rx="1.5" fill="#191B1D"/>
79
+ <rect x="889.38" y="501.00" width="11.72" height="84" rx="1.5" fill="#191B1D"/>
80
+ <rect x="905.09" y="508.00" width="11.72" height="70" rx="1.5" fill="#191B1D"/>
81
+ <rect x="920.81" y="519.00" width="11.72" height="48" rx="1.5" fill="#191B1D"/>
82
+ <rect x="936.53" y="528.50" width="11.72" height="29" rx="1.5" fill="#191B1D"/>
83
+ <rect x="952.25" y="534.00" width="11.72" height="18" rx="1.5" fill="#191B1D"/>
84
+ <rect x="967.97" y="536.50" width="11.72" height="13" rx="1.5" fill="#191B1D"/>
85
+ <rect x="983.69" y="532.00" width="11.72" height="22" rx="1.5" fill="#191B1D"/>
86
+ <rect x="999.41" y="524.50" width="11.72" height="37" rx="1.5" fill="#191B1D"/>
87
+ <rect x="1015.12" y="514.00" width="11.72" height="58" rx="1.5" fill="#191B1D"/>
88
+ <rect x="1030.84" y="505.00" width="11.72" height="76" rx="1.5" fill="#191B1D"/>
89
+ <rect x="1046.56" y="512.00" width="11.72" height="62" rx="1.5" fill="#191B1D"/>
90
+ <rect x="1062.28" y="526.00" width="11.72" height="34" rx="1.5" fill="#191B1D"/>
91
+
92
+ <!-- footer -->
93
+ <text x="72" y="656" font-family="'IBM Plex Mono',ui-monospace,'SFMono-Regular',monospace" font-size="20"
94
+ letter-spacing="0.5" fill="#191B1D">npm i -g auto-tts</text>
95
+ <text x="1074" y="656" font-family="'IBM Plex Mono',ui-monospace,'SFMono-Regular',monospace" font-size="20"
96
+ letter-spacing="0.5" fill="#63666B" text-anchor="end">github.com/kms413/auto-tts</text>
97
+ </svg>
@@ -27,18 +27,22 @@ class Job:
27
27
  created_at: float = field(default_factory=time.time)
28
28
  finished_at: float | None = None
29
29
  size_bytes: int = 0
30
+ has_subtitles: bool = False
30
31
 
31
32
  @property
32
33
  def filename(self) -> str:
33
34
  return f"{self.id}.mp3"
34
35
 
36
+ @property
37
+ def srt_filename(self) -> str:
38
+ return f"{self.id}.srt"
39
+
35
40
  def to_dict(self) -> dict:
36
41
  return {
37
42
  "id": self.id,
38
43
  "text": self.text,
39
44
  "voice": self.voice,
40
45
  "tone": self.tone,
41
- "tone_label": tts.tone_label(self.tone),
42
46
  "rate": self.rate,
43
47
  "volume": self.volume,
44
48
  "pitch": self.pitch,
@@ -50,6 +54,11 @@ class Job:
50
54
  "size_bytes": self.size_bytes,
51
55
  "chars": len(self.text),
52
56
  "audio_url": f"/api/audio/{self.filename}" if self.status == "completed" else None,
57
+ "srt_url": (
58
+ f"/api/subtitles/{self.srt_filename}"
59
+ if self.status == "completed" and self.has_subtitles
60
+ else None
61
+ ),
53
62
  }
54
63
 
55
64
 
@@ -102,6 +111,7 @@ class JobManager:
102
111
  if job is None:
103
112
  return False
104
113
  (config.AUDIO_DIR / job.filename).unlink(missing_ok=True)
114
+ (config.AUDIO_DIR / job.srt_filename).unlink(missing_ok=True)
105
115
  return True
106
116
 
107
117
  async def _worker(self) -> None:
@@ -121,6 +131,7 @@ class JobManager:
121
131
  async def on_progress(value: float) -> None:
122
132
  job.progress = value
123
133
 
134
+ srt_path = config.AUDIO_DIR / job.srt_filename
124
135
  try:
125
136
  size = await tts.synthesize(
126
137
  job.text,
@@ -129,9 +140,11 @@ class JobManager:
129
140
  tts.format_volume(job.volume),
130
141
  tts.format_pitch(job.pitch),
131
142
  config.AUDIO_DIR / job.filename,
132
- on_progress,
143
+ srt_path=srt_path,
144
+ on_progress=on_progress,
133
145
  )
134
146
  job.size_bytes = size
147
+ job.has_subtitles = srt_path.is_file()
135
148
  job.progress = 1.0
136
149
  job.status = "completed"
137
150
  except asyncio.CancelledError:
@@ -110,7 +110,20 @@ async def get_audio(filename: str) -> FileResponse:
110
110
  path = config.AUDIO_DIR / filename
111
111
  if not path.is_file():
112
112
  raise HTTPException(status_code=404, detail="Audio not found")
113
- return FileResponse(path, media_type="audio/mpeg", filename=filename)
113
+ # Served without a Content-Disposition header: browsers only stream media
114
+ # inline when the response is not flagged as a download. The UI's own
115
+ # download link supplies the filename.
116
+ return FileResponse(path, media_type="audio/mpeg")
117
+
118
+
119
+ @app.get("/api/subtitles/{filename}")
120
+ async def get_subtitles(filename: str) -> FileResponse:
121
+ if Path(filename).name != filename:
122
+ raise HTTPException(status_code=404, detail="Subtitles not found")
123
+ path = config.AUDIO_DIR / filename
124
+ if not path.is_file():
125
+ raise HTTPException(status_code=404, detail="Subtitles not found")
126
+ return FileResponse(path, media_type="application/x-subrip")
114
127
 
115
128
 
116
129
  # Serve the built frontend from the same origin so a single command can run the
@@ -27,7 +27,6 @@ class JobView(BaseModel):
27
27
  text: str
28
28
  voice: str
29
29
  tone: str
30
- tone_label: str
31
30
  rate: int
32
31
  volume: int
33
32
  pitch: int
@@ -38,4 +37,5 @@ class JobView(BaseModel):
38
37
  finished_at: float | None = None
39
38
  size_bytes: int
40
39
  chars: int
41
- audio_url: str | None = None
40
+ audio_url: str | None = None
41
+ srt_url: str | None = None
@@ -13,6 +13,12 @@ from . import config
13
13
 
14
14
  ProgressCallback = Callable[[float], Awaitable[None]]
15
15
 
16
+ # edge-tts streams 48 kbps CBR mono mp3 and reports boundaries in 100-nanosecond
17
+ # ticks. Each Communicate counts from zero, so the bytes already written are used
18
+ # to shift later chunks onto a single timeline.
19
+ _TICKS_PER_SECOND = 10_000_000
20
+ _MP3_BITRATE_BPS = 48_000
21
+
16
22
  _voices_cache: list[dict] | None = None
17
23
  _voices_lock = asyncio.Lock()
18
24
 
@@ -37,21 +43,20 @@ def format_pitch(value: int) -> str:
37
43
 
38
44
  # Speaking-tone presets. edge-tts does not expose emotion styles, so every tone
39
45
  # is approximated with a rate/volume/pitch delta triple that the frontend can
40
- # apply to the prosody controls.
46
+ # apply to the prosody controls. Display names live in the frontend locale
47
+ # catalogues, keyed by the `key` below.
41
48
  TONE_PRESETS: tuple[dict, ...] = (
42
- {"key": "natural", "label": "自然", "hint": "默认语速与音高", "rate": 0, "volume": 0, "pitch": 0},
43
- {"key": "gentle", "label": "温柔", "hint": "放慢、压低,像在耳边说话", "rate": -10, "volume": -6, "pitch": 4},
44
- {"key": "cheerful", "label": "愉悦", "hint": "略快、上扬,带一点笑意", "rate": 8, "volume": 4, "pitch": 8},
45
- {"key": "excited", "label": "兴奋", "hint": "更快、更亮,情绪外放", "rate": 16, "volume": 8, "pitch": 14},
46
- {"key": "serious", "label": "严肃", "hint": "沉稳、低音,适合声明", "rate": -6, "volume": 2, "pitch": -8},
47
- {"key": "sad", "label": "悲伤", "hint": "缓慢、低沉,语气下沉", "rate": -14, "volume": -8, "pitch": -6},
48
- {"key": "newscast", "label": "新闻播报", "hint": "清晰、稳定,字正腔圆", "rate": 6, "volume": 6, "pitch": -2},
49
- {"key": "whisper", "label": "低语", "hint": "轻声细语,音量很轻", "rate": -8, "volume": -40, "pitch": 2},
50
- {"key": "custom", "label": "自定义", "hint": "自己微调语速、音调与音量", "rate": 0, "volume": 0, "pitch": 0},
49
+ {"key": "natural", "rate": 0, "volume": 0, "pitch": 0},
50
+ {"key": "gentle", "rate": -10, "volume": -6, "pitch": 4},
51
+ {"key": "cheerful", "rate": 8, "volume": 4, "pitch": 8},
52
+ {"key": "excited", "rate": 16, "volume": 8, "pitch": 14},
53
+ {"key": "serious", "rate": -6, "volume": 2, "pitch": -8},
54
+ {"key": "sad", "rate": -14, "volume": -8, "pitch": -6},
55
+ {"key": "newscast", "rate": 6, "volume": 6, "pitch": -2},
56
+ {"key": "whisper", "rate": -8, "volume": -40, "pitch": 2},
57
+ {"key": "custom", "rate": 0, "volume": 0, "pitch": 0},
51
58
  )
52
59
 
53
- TONE_INDEX: dict[str, dict] = {preset["key"]: preset for preset in TONE_PRESETS}
54
-
55
60
  DEFAULT_TONE = "natural"
56
61
 
57
62
 
@@ -60,12 +65,6 @@ def list_tones() -> list[dict]:
60
65
  return [dict(preset) for preset in TONE_PRESETS]
61
66
 
62
67
 
63
- def tone_label(key: str) -> str:
64
- """Resolve a tone key to its display label, falling back to the raw key."""
65
- preset = TONE_INDEX.get(key)
66
- return preset["label"] if preset else key
67
-
68
-
69
68
  async def list_voices() -> list[dict]:
70
69
  """Return the edge-tts voice catalogue, cached for the process lifetime."""
71
70
  global _voices_cache
@@ -106,6 +105,20 @@ def split_text(text: str, limit: int) -> list[str]:
106
105
  return chunks
107
106
 
108
107
 
108
+ def _audio_ticks(audio_bytes: int) -> int:
109
+ """Convert a count of 48 kbps mp3 bytes into edge-tts timing ticks."""
110
+ return audio_bytes * 8 * _TICKS_PER_SECOND // _MP3_BITRATE_BPS
111
+
112
+
113
+ def _write_subtitles(submaker: edge_tts.SubMaker, srt_path: Path) -> bool:
114
+ """Write an SRT file, returning False when no boundaries were reported."""
115
+ srt = submaker.get_srt()
116
+ if not srt.strip():
117
+ return False
118
+ srt_path.write_text(srt, encoding="utf-8")
119
+ return True
120
+
121
+
109
122
  async def synthesize(
110
123
  text: str,
111
124
  voice: str,
@@ -113,28 +126,48 @@ async def synthesize(
113
126
  volume: str,
114
127
  pitch: str,
115
128
  out_path: Path,
129
+ srt_path: Path | None = None,
116
130
  on_progress: ProgressCallback | None = None,
117
131
  ) -> int:
118
- """Synthesise *text* into an mp3 file and return the number of audio bytes written."""
132
+ """Synthesise *text* into an mp3 file and return the number of audio bytes written.
133
+
134
+ When *srt_path* is provided, sentence-level subtitles derived from the
135
+ service's boundary events are written alongside the audio.
136
+ """
119
137
  chunks = split_text(text, config.CHUNK_SIZE)
120
138
  if not chunks:
121
139
  raise ValueError("Text must not be empty")
122
140
 
123
141
  out_path.parent.mkdir(parents=True, exist_ok=True)
124
142
  tmp_path = out_path.with_name(f"{out_path.name}.part")
143
+ submaker = edge_tts.SubMaker()
125
144
  total = len(chunks)
126
145
  written = 0
127
146
  try:
128
147
  with tmp_path.open("wb") as handle:
129
148
  for index, chunk in enumerate(chunks):
130
- communicate = edge_tts.Communicate(chunk, voice, rate=rate, volume=volume, pitch=pitch)
149
+ # Boundary offsets from a fresh Communicate are relative to its own
150
+ # audio, so shift them by the duration of everything written so far.
151
+ base = _audio_ticks(written)
152
+ communicate = edge_tts.Communicate(
153
+ chunk,
154
+ voice,
155
+ rate=rate,
156
+ volume=volume,
157
+ pitch=pitch,
158
+ boundary="SentenceBoundary",
159
+ )
131
160
  async for message in communicate.stream():
132
161
  if message["type"] == "audio":
133
162
  handle.write(message["data"])
134
163
  written += len(message["data"])
164
+ elif message["type"] in ("WordBoundary", "SentenceBoundary"):
165
+ submaker.feed({**message, "offset": message["offset"] + base})
135
166
  if on_progress is not None:
136
167
  await on_progress((index + 1) / total)
137
168
  tmp_path.replace(out_path)
169
+ if srt_path is not None:
170
+ _write_subtitles(submaker, srt_path)
138
171
  except BaseException:
139
172
  tmp_path.unlink(missing_ok=True)
140
173
  raise