stt2vtt 0.0.1__tar.gz
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.
- stt2vtt-0.0.1/.github/workflows/publish.yml +29 -0
- stt2vtt-0.0.1/.github/workflows/test.yml +32 -0
- stt2vtt-0.0.1/.gitignore +50 -0
- stt2vtt-0.0.1/.python-version +1 -0
- stt2vtt-0.0.1/LICENSE +22 -0
- stt2vtt-0.0.1/MANIFEST.in +7 -0
- stt2vtt-0.0.1/PKG-INFO +108 -0
- stt2vtt-0.0.1/README.md +57 -0
- stt2vtt-0.0.1/pyproject.toml +51 -0
- stt2vtt-0.0.1/setup.cfg +4 -0
- stt2vtt-0.0.1/src/__init__.py +5 -0
- stt2vtt-0.0.1/src/cli.py +85 -0
- stt2vtt-0.0.1/src/stt_to_vtt.py +193 -0
- stt2vtt-0.0.1/stt2vtt.egg-info/PKG-INFO +108 -0
- stt2vtt-0.0.1/stt2vtt.egg-info/SOURCES.txt +27 -0
- stt2vtt-0.0.1/stt2vtt.egg-info/dependency_links.txt +1 -0
- stt2vtt-0.0.1/stt2vtt.egg-info/entry_points.txt +2 -0
- stt2vtt-0.0.1/stt2vtt.egg-info/requires.txt +6 -0
- stt2vtt-0.0.1/stt2vtt.egg-info/top_level.txt +1 -0
- stt2vtt-0.0.1/tests/__init__.py +0 -0
- stt2vtt-0.0.1/tests/test_cli.py +11 -0
- stt2vtt-0.0.1/tests/test_data/fast-whisper/earth-expected.vtt +97 -0
- stt2vtt-0.0.1/tests/test_data/fast-whisper/earth-input.json +1 -0
- stt2vtt-0.0.1/tests/test_data/fast-whisper/jp-expected.vtt +55 -0
- stt2vtt-0.0.1/tests/test_data/fast-whisper/jp-input.json +1 -0
- stt2vtt-0.0.1/tests/test_data/fast-whisper/street-expected.vtt +109 -0
- stt2vtt-0.0.1/tests/test_data/fast-whisper/street-input.json +1 -0
- stt2vtt-0.0.1/tests/test_stt_to_vtt.py +108 -0
- stt2vtt-0.0.1/uv.lock +586 -0
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
name: Publish
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
tags:
|
|
6
|
+
- '*'
|
|
7
|
+
|
|
8
|
+
jobs:
|
|
9
|
+
pypi:
|
|
10
|
+
name: "Publish to PyPI"
|
|
11
|
+
runs-on: ubuntu-latest
|
|
12
|
+
|
|
13
|
+
steps:
|
|
14
|
+
- uses: actions/checkout@v4
|
|
15
|
+
|
|
16
|
+
- name: Install uv
|
|
17
|
+
uses: astral-sh/setup-uv@v3
|
|
18
|
+
with:
|
|
19
|
+
enable-cache: true
|
|
20
|
+
cache-dependency-glob: uv.lock
|
|
21
|
+
|
|
22
|
+
- name: Set up Python
|
|
23
|
+
run: uv python install 3.11
|
|
24
|
+
|
|
25
|
+
- name: Build
|
|
26
|
+
run: uv build
|
|
27
|
+
|
|
28
|
+
- name: Publish
|
|
29
|
+
run: uv publish -t ${{ secrets.PYPI_TOKEN }}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
name: Tests
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: [ main, master ]
|
|
6
|
+
pull_request:
|
|
7
|
+
branches: [ main, master ]
|
|
8
|
+
|
|
9
|
+
jobs:
|
|
10
|
+
test:
|
|
11
|
+
runs-on: ubuntu-latest
|
|
12
|
+
strategy:
|
|
13
|
+
matrix:
|
|
14
|
+
python-version: ["3.9", "3.10", "3.11", "3.12"]
|
|
15
|
+
|
|
16
|
+
steps:
|
|
17
|
+
- uses: actions/checkout@v4
|
|
18
|
+
|
|
19
|
+
- name: Set up Python ${{ matrix.python-version }}
|
|
20
|
+
uses: actions/setup-python@v5
|
|
21
|
+
with:
|
|
22
|
+
python-version: ${{ matrix.python-version }}
|
|
23
|
+
|
|
24
|
+
- name: Install dependencies
|
|
25
|
+
run: |
|
|
26
|
+
python -m pip install --upgrade pip
|
|
27
|
+
pip install -e .[dev]
|
|
28
|
+
|
|
29
|
+
- name: Run tests
|
|
30
|
+
run: |
|
|
31
|
+
pytest tests/ -v --tb=short
|
|
32
|
+
|
stt2vtt-0.0.1/.gitignore
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# Python
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
*$py.class
|
|
5
|
+
*.so
|
|
6
|
+
.Python
|
|
7
|
+
build/
|
|
8
|
+
develop-eggs/
|
|
9
|
+
dist/
|
|
10
|
+
downloads/
|
|
11
|
+
eggs/
|
|
12
|
+
.eggs/
|
|
13
|
+
lib/
|
|
14
|
+
lib64/
|
|
15
|
+
parts/
|
|
16
|
+
sdist/
|
|
17
|
+
var/
|
|
18
|
+
wheels/
|
|
19
|
+
*.egg-info/
|
|
20
|
+
.installed.cfg
|
|
21
|
+
*.egg
|
|
22
|
+
|
|
23
|
+
# Virtual Environment
|
|
24
|
+
venv/
|
|
25
|
+
ENV/
|
|
26
|
+
env/
|
|
27
|
+
|
|
28
|
+
# IDE
|
|
29
|
+
.vscode/
|
|
30
|
+
.idea/
|
|
31
|
+
*.swp
|
|
32
|
+
*.swo
|
|
33
|
+
*~
|
|
34
|
+
|
|
35
|
+
# Environment
|
|
36
|
+
.env
|
|
37
|
+
.env.local
|
|
38
|
+
|
|
39
|
+
# Testing
|
|
40
|
+
.pytest_cache/
|
|
41
|
+
.coverage
|
|
42
|
+
htmlcov/
|
|
43
|
+
|
|
44
|
+
# Models (large files)
|
|
45
|
+
models/
|
|
46
|
+
|
|
47
|
+
# Temporary files
|
|
48
|
+
*.tmp
|
|
49
|
+
*.log
|
|
50
|
+
.DS_Store
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
3.12
|
stt2vtt-0.0.1/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 audio-subtitler contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
22
|
+
|
stt2vtt-0.0.1/PKG-INFO
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: stt2vtt
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: Convert STT result (Azure or fast-whisper JSON) to WebVTT
|
|
5
|
+
Author: Gary Lab
|
|
6
|
+
License: MIT License
|
|
7
|
+
|
|
8
|
+
Copyright (c) 2025 audio-subtitler contributors
|
|
9
|
+
|
|
10
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
11
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
12
|
+
in the Software without restriction, including without limitation the rights
|
|
13
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
14
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
15
|
+
furnished to do so, subject to the following conditions:
|
|
16
|
+
|
|
17
|
+
The above copyright notice and this permission notice shall be included in all
|
|
18
|
+
copies or substantial portions of the Software.
|
|
19
|
+
|
|
20
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
21
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
22
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
23
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
24
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
25
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
26
|
+
SOFTWARE.
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
Project-URL: Homepage, https://github.com/garylab/stt2vtt
|
|
30
|
+
Project-URL: Author Blog, https://garymeng.com
|
|
31
|
+
Keywords: stt,vtt,webvtt,subtitles,speech-to-text,azure,whisper
|
|
32
|
+
Classifier: Development Status :: 4 - Beta
|
|
33
|
+
Classifier: Intended Audience :: Developers
|
|
34
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
35
|
+
Classifier: Programming Language :: Python :: 3
|
|
36
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
37
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
38
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
39
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
40
|
+
Classifier: Topic :: Multimedia :: Sound/Audio :: Speech
|
|
41
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
42
|
+
Requires-Python: >=3.9
|
|
43
|
+
Description-Content-Type: text/markdown
|
|
44
|
+
License-File: LICENSE
|
|
45
|
+
Provides-Extra: dev
|
|
46
|
+
Requires-Dist: pytest>=7.0.0; extra == "dev"
|
|
47
|
+
Requires-Dist: pytest-cov>=4.0.0; extra == "dev"
|
|
48
|
+
Requires-Dist: black>=23.0.0; extra == "dev"
|
|
49
|
+
Requires-Dist: ruff>=0.1.0; extra == "dev"
|
|
50
|
+
Dynamic: license-file
|
|
51
|
+
|
|
52
|
+
# stt2vtt
|
|
53
|
+
|
|
54
|
+
Convert STT (speech-to-text) results with timestamps to WebVTT. Input can be JSON from **Azure Speech-to-Text** or **fast-whisper** (or compatible segment + word timestamps). Output is VTT text.
|
|
55
|
+
|
|
56
|
+
No audio processing or ML models — this repo only converts existing STT JSON to VTT.
|
|
57
|
+
|
|
58
|
+
## Installation
|
|
59
|
+
|
|
60
|
+
```bash
|
|
61
|
+
pip install stt2vtt
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
Dev dependencies:
|
|
65
|
+
|
|
66
|
+
```bash
|
|
67
|
+
pip install stt2vtt[dev]
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
## Usage
|
|
71
|
+
|
|
72
|
+
### CLI
|
|
73
|
+
|
|
74
|
+
```bash
|
|
75
|
+
# From file
|
|
76
|
+
stt2vtt result.json -o output.vtt
|
|
77
|
+
|
|
78
|
+
# To stdout
|
|
79
|
+
stt2vtt result.json
|
|
80
|
+
|
|
81
|
+
# From stdin
|
|
82
|
+
cat result.json | stt2vtt -o output.vtt
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
### Python
|
|
86
|
+
|
|
87
|
+
```python
|
|
88
|
+
from src.stt_to_vtt import stt_to_vtt
|
|
89
|
+
|
|
90
|
+
# JSON string or parsed list/dict
|
|
91
|
+
vtt = stt_to_vtt('{"phrases": [...]}') # Azure style
|
|
92
|
+
vtt = stt_to_vtt([{"id": 1, "start": 0, "end": 1, "text": "...", "words": [...]}]) # fast-whisper style
|
|
93
|
+
|
|
94
|
+
print(vtt) # "WEBVTT\n\n00:00:00.000 --> ..."
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
## Input formats
|
|
98
|
+
|
|
99
|
+
- **fast-whisper**: List of segments, each with `start`, `end`, `text`, and `words` (list of `{start, end, word}`).
|
|
100
|
+
- **Azure**: Object with `phrases` (or `recognizedPhrases`), each phrase with `offsetMilliseconds`, `durationMilliseconds`, and `words` with `text`, `offsetMilliseconds`, `durationMilliseconds`.
|
|
101
|
+
|
|
102
|
+
## Output
|
|
103
|
+
|
|
104
|
+
WebVTT subtitle content: `WEBVTT` header plus timestamped cues with capitalized text.
|
|
105
|
+
|
|
106
|
+
## License
|
|
107
|
+
|
|
108
|
+
MIT — see [LICENSE](LICENSE).
|
stt2vtt-0.0.1/README.md
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# stt2vtt
|
|
2
|
+
|
|
3
|
+
Convert STT (speech-to-text) results with timestamps to WebVTT. Input can be JSON from **Azure Speech-to-Text** or **fast-whisper** (or compatible segment + word timestamps). Output is VTT text.
|
|
4
|
+
|
|
5
|
+
No audio processing or ML models — this repo only converts existing STT JSON to VTT.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
pip install stt2vtt
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Dev dependencies:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
pip install stt2vtt[dev]
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
|
|
21
|
+
### CLI
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
# From file
|
|
25
|
+
stt2vtt result.json -o output.vtt
|
|
26
|
+
|
|
27
|
+
# To stdout
|
|
28
|
+
stt2vtt result.json
|
|
29
|
+
|
|
30
|
+
# From stdin
|
|
31
|
+
cat result.json | stt2vtt -o output.vtt
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
### Python
|
|
35
|
+
|
|
36
|
+
```python
|
|
37
|
+
from src.stt_to_vtt import stt_to_vtt
|
|
38
|
+
|
|
39
|
+
# JSON string or parsed list/dict
|
|
40
|
+
vtt = stt_to_vtt('{"phrases": [...]}') # Azure style
|
|
41
|
+
vtt = stt_to_vtt([{"id": 1, "start": 0, "end": 1, "text": "...", "words": [...]}]) # fast-whisper style
|
|
42
|
+
|
|
43
|
+
print(vtt) # "WEBVTT\n\n00:00:00.000 --> ..."
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## Input formats
|
|
47
|
+
|
|
48
|
+
- **fast-whisper**: List of segments, each with `start`, `end`, `text`, and `words` (list of `{start, end, word}`).
|
|
49
|
+
- **Azure**: Object with `phrases` (or `recognizedPhrases`), each phrase with `offsetMilliseconds`, `durationMilliseconds`, and `words` with `text`, `offsetMilliseconds`, `durationMilliseconds`.
|
|
50
|
+
|
|
51
|
+
## Output
|
|
52
|
+
|
|
53
|
+
WebVTT subtitle content: `WEBVTT` header plus timestamped cues with capitalized text.
|
|
54
|
+
|
|
55
|
+
## License
|
|
56
|
+
|
|
57
|
+
MIT — see [LICENSE](LICENSE).
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "stt2vtt"
|
|
3
|
+
dynamic = ["version"]
|
|
4
|
+
description = "Convert STT result (Azure or fast-whisper JSON) to WebVTT"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
requires-python = ">=3.9"
|
|
7
|
+
license = {file = "LICENSE"}
|
|
8
|
+
authors = [
|
|
9
|
+
{name = "Gary Lab"},
|
|
10
|
+
]
|
|
11
|
+
keywords = ["stt", "vtt", "webvtt", "subtitles", "speech-to-text", "azure", "whisper"]
|
|
12
|
+
classifiers = [
|
|
13
|
+
"Development Status :: 4 - Beta",
|
|
14
|
+
"Intended Audience :: Developers",
|
|
15
|
+
"License :: OSI Approved :: MIT License",
|
|
16
|
+
"Programming Language :: Python :: 3",
|
|
17
|
+
"Programming Language :: Python :: 3.9",
|
|
18
|
+
"Programming Language :: Python :: 3.10",
|
|
19
|
+
"Programming Language :: Python :: 3.11",
|
|
20
|
+
"Programming Language :: Python :: 3.12",
|
|
21
|
+
"Topic :: Multimedia :: Sound/Audio :: Speech",
|
|
22
|
+
"Topic :: Software Development :: Libraries :: Python Modules",
|
|
23
|
+
]
|
|
24
|
+
|
|
25
|
+
dependencies = []
|
|
26
|
+
|
|
27
|
+
[project.scripts]
|
|
28
|
+
stt2vtt = "src.cli:main"
|
|
29
|
+
|
|
30
|
+
[project.optional-dependencies]
|
|
31
|
+
dev = [
|
|
32
|
+
"pytest>=7.0.0",
|
|
33
|
+
"pytest-cov>=4.0.0",
|
|
34
|
+
"black>=23.0.0",
|
|
35
|
+
"ruff>=0.1.0",
|
|
36
|
+
]
|
|
37
|
+
|
|
38
|
+
[project.urls]
|
|
39
|
+
Homepage = "https://github.com/garylab/stt2vtt"
|
|
40
|
+
"Author Blog" = "https://garymeng.com"
|
|
41
|
+
|
|
42
|
+
[build-system]
|
|
43
|
+
requires = ["setuptools>=61.0", "setuptools-scm"]
|
|
44
|
+
build-backend = "setuptools.build_meta"
|
|
45
|
+
|
|
46
|
+
[tool.setuptools_scm]
|
|
47
|
+
version_scheme = "post-release"
|
|
48
|
+
local_scheme = "no-local-version"
|
|
49
|
+
|
|
50
|
+
[tool.setuptools]
|
|
51
|
+
packages = ["src"]
|
stt2vtt-0.0.1/setup.cfg
ADDED
stt2vtt-0.0.1/src/cli.py
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""Command-line interface: read STT JSON, write VTT."""
|
|
3
|
+
|
|
4
|
+
import sys
|
|
5
|
+
import argparse
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
|
|
8
|
+
from src.stt_to_vtt import stt_to_vtt
|
|
9
|
+
|
|
10
|
+
__version__ = "0.1.0"
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def main():
|
|
14
|
+
parser = argparse.ArgumentParser(
|
|
15
|
+
description="Convert STT result (Azure or fast-whisper JSON) to WebVTT",
|
|
16
|
+
formatter_class=argparse.RawDescriptionHelpFormatter,
|
|
17
|
+
epilog="""
|
|
18
|
+
Examples:
|
|
19
|
+
stt2vtt result.json -o output.vtt
|
|
20
|
+
stt2vtt result.json
|
|
21
|
+
cat result.json | stt2vtt -o output.vtt
|
|
22
|
+
""",
|
|
23
|
+
)
|
|
24
|
+
parser.add_argument(
|
|
25
|
+
"input",
|
|
26
|
+
nargs="?",
|
|
27
|
+
type=str,
|
|
28
|
+
default=None,
|
|
29
|
+
help="Input JSON file (STT result). Omit to read from stdin.",
|
|
30
|
+
)
|
|
31
|
+
parser.add_argument(
|
|
32
|
+
"-o", "--output",
|
|
33
|
+
type=str,
|
|
34
|
+
default=None,
|
|
35
|
+
help="Output VTT file (default: stdout)",
|
|
36
|
+
)
|
|
37
|
+
parser.add_argument(
|
|
38
|
+
"-q", "--quiet",
|
|
39
|
+
action="store_true",
|
|
40
|
+
help="Suppress messages",
|
|
41
|
+
)
|
|
42
|
+
parser.add_argument(
|
|
43
|
+
"-v", "--version",
|
|
44
|
+
action="version",
|
|
45
|
+
version=f"%(prog)s {__version__}",
|
|
46
|
+
)
|
|
47
|
+
args = parser.parse_args()
|
|
48
|
+
|
|
49
|
+
try:
|
|
50
|
+
if args.input:
|
|
51
|
+
path = Path(args.input)
|
|
52
|
+
if not path.exists():
|
|
53
|
+
print(f"Error: Input file not found: {args.input}", file=sys.stderr)
|
|
54
|
+
sys.exit(1)
|
|
55
|
+
if not path.is_file():
|
|
56
|
+
print(f"Error: Not a file: {args.input}", file=sys.stderr)
|
|
57
|
+
sys.exit(1)
|
|
58
|
+
content = path.read_text(encoding="utf-8")
|
|
59
|
+
else:
|
|
60
|
+
content = sys.stdin.read()
|
|
61
|
+
|
|
62
|
+
vtt = stt_to_vtt(content)
|
|
63
|
+
|
|
64
|
+
if args.output:
|
|
65
|
+
Path(args.output).write_text(vtt, encoding="utf-8")
|
|
66
|
+
if not args.quiet:
|
|
67
|
+
print(f"Written: {args.output}", file=sys.stderr)
|
|
68
|
+
else:
|
|
69
|
+
print(vtt)
|
|
70
|
+
except ValueError as e:
|
|
71
|
+
print(f"Error: {e}", file=sys.stderr)
|
|
72
|
+
sys.exit(1)
|
|
73
|
+
except KeyboardInterrupt:
|
|
74
|
+
print("\nInterrupted", file=sys.stderr)
|
|
75
|
+
sys.exit(130)
|
|
76
|
+
except Exception as e:
|
|
77
|
+
print(f"Error: {e}", file=sys.stderr)
|
|
78
|
+
if not args.quiet:
|
|
79
|
+
import traceback
|
|
80
|
+
traceback.print_exc(file=sys.stderr)
|
|
81
|
+
sys.exit(1)
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
if __name__ == "__main__":
|
|
85
|
+
main()
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
"""Convert STT (speech-to-text) results with timestamps to WebVTT.
|
|
2
|
+
|
|
3
|
+
Supports input from Azure Speech-to-Text or fast-whisper style JSON.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
import json
|
|
7
|
+
from types import SimpleNamespace
|
|
8
|
+
from typing import Any, List, Union
|
|
9
|
+
|
|
10
|
+
STOP_CHARS = set(
|
|
11
|
+
".!?,:;…‥"
|
|
12
|
+
"。!?,、;:"
|
|
13
|
+
"।"
|
|
14
|
+
"܀።፧"
|
|
15
|
+
"؟؛"
|
|
16
|
+
"၊။"
|
|
17
|
+
"⸮⁇⁈⁉"
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def _seconds_to_vtt_time(seconds: float) -> str:
|
|
22
|
+
hours = int(seconds // 3600)
|
|
23
|
+
seconds = seconds % 3600
|
|
24
|
+
minutes = int(seconds // 60)
|
|
25
|
+
milliseconds = int(seconds * 1000) % 1000
|
|
26
|
+
secs = int(seconds % 60)
|
|
27
|
+
return f"{hours:02d}:{minutes:02d}:{secs:02d}.{milliseconds:03d}"
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def _capitalize_text(text: str) -> str:
|
|
31
|
+
text = text.strip()
|
|
32
|
+
return text[0].upper() + text[1:] if text else text
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def _end_with_stop_char(text: str) -> bool:
|
|
36
|
+
if not text:
|
|
37
|
+
return False
|
|
38
|
+
return any(text.endswith(c) for c in STOP_CHARS)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def _normalize_fast_whisper(segments: List[Any]) -> List[SimpleNamespace]:
|
|
42
|
+
"""Normalize fast-whisper style segments to internal format."""
|
|
43
|
+
out = []
|
|
44
|
+
for seg in segments:
|
|
45
|
+
words = []
|
|
46
|
+
for w in seg.get("words", []):
|
|
47
|
+
start = float(w.get("start", 0))
|
|
48
|
+
end = float(w.get("end", 0))
|
|
49
|
+
word = w.get("word", "")
|
|
50
|
+
words.append(SimpleNamespace(start=start, end=end, word=word))
|
|
51
|
+
start = float(seg.get("start", 0))
|
|
52
|
+
end = float(seg.get("end", 0))
|
|
53
|
+
if words and start == 0 and end == 0:
|
|
54
|
+
start = words[0].start
|
|
55
|
+
end = words[-1].end
|
|
56
|
+
out.append(
|
|
57
|
+
SimpleNamespace(
|
|
58
|
+
start=start,
|
|
59
|
+
end=end,
|
|
60
|
+
text=seg.get("text", ""),
|
|
61
|
+
words=words,
|
|
62
|
+
)
|
|
63
|
+
)
|
|
64
|
+
return out
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def _normalize_azure(data: Any) -> List[SimpleNamespace]:
|
|
68
|
+
"""Normalize Azure Speech-to-Text result to internal format."""
|
|
69
|
+
phrases = data.get("phrases") or data.get("recognizedPhrases") or []
|
|
70
|
+
if not phrases and isinstance(data, list):
|
|
71
|
+
phrases = data
|
|
72
|
+
out = []
|
|
73
|
+
for p in phrases:
|
|
74
|
+
words = []
|
|
75
|
+
for w in p.get("words", []):
|
|
76
|
+
offset_ms = w.get("offsetMilliseconds", w.get("offset", 0))
|
|
77
|
+
duration_ms = w.get("durationMilliseconds", w.get("duration", 0))
|
|
78
|
+
start = offset_ms / 1000.0
|
|
79
|
+
end = start + duration_ms / 1000.0
|
|
80
|
+
word = w.get("text", w.get("word", ""))
|
|
81
|
+
words.append(SimpleNamespace(start=start, end=end, word=word))
|
|
82
|
+
offset_ms = p.get("offsetMilliseconds", p.get("offset", 0))
|
|
83
|
+
duration_ms = p.get("durationMilliseconds", p.get("duration", 0))
|
|
84
|
+
start = offset_ms / 1000.0
|
|
85
|
+
end = start + duration_ms / 1000.0
|
|
86
|
+
if words and (duration_ms == 0 or end <= start):
|
|
87
|
+
start = words[0].start
|
|
88
|
+
end = words[-1].end
|
|
89
|
+
out.append(
|
|
90
|
+
SimpleNamespace(
|
|
91
|
+
start=start,
|
|
92
|
+
end=end,
|
|
93
|
+
text=p.get("text", p.get("display", "")),
|
|
94
|
+
words=words,
|
|
95
|
+
)
|
|
96
|
+
)
|
|
97
|
+
return out
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def _detect_and_normalize(data: Any) -> List[SimpleNamespace]:
|
|
101
|
+
"""Detect input format and return normalized segments."""
|
|
102
|
+
if isinstance(data, str):
|
|
103
|
+
data = json.loads(data)
|
|
104
|
+
if isinstance(data, list) and data and isinstance(data[0], dict):
|
|
105
|
+
seg = data[0]
|
|
106
|
+
if "words" in seg and seg["words"]:
|
|
107
|
+
w = seg["words"][0]
|
|
108
|
+
if "start" in w and "end" in w and "word" in w:
|
|
109
|
+
return _normalize_fast_whisper(data)
|
|
110
|
+
if "offsetMilliseconds" in seg or "offset" in seg:
|
|
111
|
+
return _normalize_azure(data)
|
|
112
|
+
return _normalize_fast_whisper(data)
|
|
113
|
+
if isinstance(data, dict):
|
|
114
|
+
if "phrases" in data or "recognizedPhrases" in data:
|
|
115
|
+
return _normalize_azure(data)
|
|
116
|
+
if "segments" in data:
|
|
117
|
+
return _detect_and_normalize(data["segments"])
|
|
118
|
+
raise ValueError("Unsupported STT result format: expected list of segments or Azure-style dict with 'phrases'.")
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
def _segments_to_subtitle(segments: List[SimpleNamespace]) -> List[dict]:
|
|
122
|
+
subtitles = []
|
|
123
|
+
for segment in segments:
|
|
124
|
+
words_idx = 0
|
|
125
|
+
words_len = len(segment.words)
|
|
126
|
+
seg_start = 0.0
|
|
127
|
+
seg_end = 0.0
|
|
128
|
+
seg_text = ""
|
|
129
|
+
|
|
130
|
+
if segment.words:
|
|
131
|
+
is_segmented = False
|
|
132
|
+
for word in segment.words:
|
|
133
|
+
if not is_segmented:
|
|
134
|
+
seg_start = word.start
|
|
135
|
+
is_segmented = True
|
|
136
|
+
seg_end = word.end
|
|
137
|
+
seg_text += word.word
|
|
138
|
+
|
|
139
|
+
if _end_with_stop_char(word.word):
|
|
140
|
+
seg_text = seg_text[:-1]
|
|
141
|
+
if not seg_text:
|
|
142
|
+
continue
|
|
143
|
+
if seg_start < seg_end and seg_text.strip():
|
|
144
|
+
subtitles.append(
|
|
145
|
+
{"msg": seg_text, "start_time": seg_start, "end_time": seg_end}
|
|
146
|
+
)
|
|
147
|
+
is_segmented = False
|
|
148
|
+
seg_text = ""
|
|
149
|
+
|
|
150
|
+
if words_idx == 0 and segment.start < word.start:
|
|
151
|
+
seg_start = word.start
|
|
152
|
+
if words_idx == (words_len - 1) and segment.end > word.end:
|
|
153
|
+
seg_end = segment.end
|
|
154
|
+
words_idx += 1
|
|
155
|
+
|
|
156
|
+
if not seg_text:
|
|
157
|
+
continue
|
|
158
|
+
if seg_start < seg_end and seg_text.strip():
|
|
159
|
+
subtitles.append(
|
|
160
|
+
{"msg": seg_text, "start_time": seg_start, "end_time": seg_end}
|
|
161
|
+
)
|
|
162
|
+
return subtitles
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
def _format_vtt(subtitles: List[dict]) -> str:
|
|
166
|
+
lines = []
|
|
167
|
+
for sub in subtitles:
|
|
168
|
+
msg = sub.get("msg")
|
|
169
|
+
if not msg:
|
|
170
|
+
continue
|
|
171
|
+
start = sub.get("start_time", 0)
|
|
172
|
+
end = sub.get("end_time", 0)
|
|
173
|
+
start_str = _seconds_to_vtt_time(start)
|
|
174
|
+
end_str = _seconds_to_vtt_time(end)
|
|
175
|
+
text = _capitalize_text(msg)
|
|
176
|
+
lines.append(f"{start_str} --> {end_str}\n{text}\n")
|
|
177
|
+
return "WEBVTT\n\n" + "\n".join(lines)
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
def stt_to_vtt(stt_result: Union[str, bytes, list, dict]) -> str:
|
|
181
|
+
"""Convert STT result (Azure or fast-whisper style) to WebVTT text.
|
|
182
|
+
|
|
183
|
+
Args:
|
|
184
|
+
stt_result: JSON string, parsed list of segments, or Azure-style dict.
|
|
185
|
+
|
|
186
|
+
Returns:
|
|
187
|
+
WebVTT subtitle content as a string.
|
|
188
|
+
"""
|
|
189
|
+
if isinstance(stt_result, bytes):
|
|
190
|
+
stt_result = stt_result.decode("utf-8")
|
|
191
|
+
segments = _detect_and_normalize(stt_result)
|
|
192
|
+
subtitles = _segments_to_subtitle(segments)
|
|
193
|
+
return _format_vtt(subtitles)
|