webvtt-python


Namewebvtt-python JSON
Version 0.2.0 PyPI version JSON
download
home_pageNone
SummaryPython WebVTT API Implementation
upload_time2025-02-20 19:10:29
maintainerNone
docs_urlNone
authorNone
requires_python<3.14,>=3.9
licenseNone
keywords captions subtitles webvtt
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # WebVTT Python Parser

A Python implementation of the [WebVTT (Web Video Text Tracks)](https://www.w3.org/TR/webvtt1/) format parser with strict validation and error handling.

## Features

- Full compliance with W3C WebVTT specification
- Strict and lenient parsing modes
- Comprehensive model validation:
  - Timestamp format validation
  - Cue timing consistency checks
  - Region setting validation
  - Position/size value range checks
- Detailed error reporting with context
- Support for:
  - Header metadata
  - Regions with scroll settings
  - Cue positioning and alignment
  - Multi-line cues
  - Voice spans and basic styling

## Installation

```bash
pip install webvtt-python
```

Or using `uv` for faster installation:
```bash
uv add webvtt-python
```

## Quick Start

```python
from webvtt_python import WebVTTParser, WebVTT

# Parse from string
parser = WebVTTParser(strict=True)
content = """WEBVTT

00:00:01.000 --> 00:00:02.000
Hello world!

00:00:02.500 --> 00:00:05.000 position:50%
Multi-line
subtitle
"""

webvtt: WebVTT = parser.parse(content)
for cue in webvtt.cues:
    print(f"{cue.start_time:.1f}-{cue.end_time:.1f}s: {cue.text}")
```

## Advanced Usage

### Region Handling
```python
content = """WEBVTT
REGION
id:test
width:40%
lines:3
regionanchor:0%,100%
viewportanchor:10%,90%
"""

webvtt = parser.parse(content)
region = webvtt.regions[0]
print(f"Region {region.id}: {region.width}% width, {region.lines} lines")
```

### Error Handling
```python
try:
    parser.parse("00:00:01.000 --> 00:00:00.500\nInvalid timing")
except ValueError as e:
    print(f"Validation error: {e}")
```

### Cue Settings
```python
cue = webvtt.cues[0]
print(f"Position: {cue.position}%")
print(f"Alignment: {cue.text_alignment.value}")
print(f"Writing direction: {cue.writing_direction}")
```

## Architecture

The WebVTT parser implements the [W3C WebVTT specification](https://www.w3.org/TR/webvtt1/).

```mermaid
graph TD
    subgraph "Parser Pipeline"
        A[".vtt File/String"] --> B["WebVTTParser"]
        B --> C{"Valid<br>WEBVTT?"}
        C -->|No| D["MalformedVTTError"]

        C -->|Yes| E["Header Processing"]
        E --> F["Style & Region<br>Parsing"]

        F --> G["Cue Processing"]
        G --> H{"Validation"}

        H -->|Invalid| I["ValidationError"]
        H -->|Valid| J["WebVTTFile"]

        J --> K["Output Formats"]
        K --> L1["JSON"]
        K --> L2["SRT"]
        K --> L3["WebVTT"]
    end

    classDef default fill:#2A2A2A,stroke:#666,color:#DDD
    classDef process fill:#4a90e2,stroke:#2171C7,color:white
    classDef error fill:#e74c3c,stroke:#c0392b,color:white
    classDef decision fill:#f39c12,stroke:#d35400,color:white
    classDef output fill:#2ecc71,stroke:#27ae60,color:white

    class A,B default
    class C,H decision
    class D,I error
    class E,F,G process
    class J,K,L1,L2,L3 output
```

### Key Components

1. **Input Processing**
   - File or string input
   - WEBVTT validation
   - BOM handling

2. **Content Parsing**
   - Header and metadata
   - Styles and regions
   - Cue timing and text

3. **Output Options**
   - JSON serialization
   - SRT conversion
   - WebVTT formatting

## API Reference

### WebVTTParser
```python
WebVTTParser(strict: bool = True)
```
- `strict`: Raise errors for invalid content (default True)

Methods:
- `parse(content: str | TextIO) -> WebVTT`

### WebVTT Model
```python
class WebVTT:
    cues: List[WebVTTCue]
    regions: List[WebVTTRegion]
    styles: List[str]
    header_comments: List[str]
```

### WebVTTCue
```python
class WebVTTCue:
    start_time: float
    end_time: float
    text: str
    identifier: Optional[str]
    region: Optional[str]
    position: Optional[float]
    size: float
    text_alignment: TextAlignment
    # ... other properties
```

### WebVTTRegion
```python
class WebVTTRegion:
    id: str
    width: float
    lines: int
    region_anchor: Tuple[float, float]
    viewport_anchor: Tuple[float, float]
    scroll: str
```

## Development

```bash
git clone https://github.com/yourusername/webvtt-python.git
cd webvtt-python
uv venv
source .venv/bin/activate
uv sync --system
```

### Running Tests
```bash
uv run pytest tests/ -v
```

## License

This software is licensed under the MIT License (MIT). See [LICENSE.md](LICENSE.md) for details.

## Contributing

Contributions welcome! Please open an issue first to discuss proposed changes.

## Acknowledgments
- W3C WebVTT specification team
- Python datetime module for timestamp parsing inspiration

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "webvtt-python",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<3.14,>=3.9",
    "maintainer_email": null,
    "keywords": "captions, subtitles, webvtt",
    "author": null,
    "author_email": "Brian McBrayer <brandmcbrayer@live.com>",
    "download_url": "https://files.pythonhosted.org/packages/cc/7d/6b0a941f4a06e50cc3423832254112b47969439f3085e143025443be225e/webvtt_python-0.2.0.tar.gz",
    "platform": null,
    "description": "# WebVTT Python Parser\n\nA Python implementation of the [WebVTT (Web Video Text Tracks)](https://www.w3.org/TR/webvtt1/) format parser with strict validation and error handling.\n\n## Features\n\n- Full compliance with W3C WebVTT specification\n- Strict and lenient parsing modes\n- Comprehensive model validation:\n  - Timestamp format validation\n  - Cue timing consistency checks\n  - Region setting validation\n  - Position/size value range checks\n- Detailed error reporting with context\n- Support for:\n  - Header metadata\n  - Regions with scroll settings\n  - Cue positioning and alignment\n  - Multi-line cues\n  - Voice spans and basic styling\n\n## Installation\n\n```bash\npip install webvtt-python\n```\n\nOr using `uv` for faster installation:\n```bash\nuv add webvtt-python\n```\n\n## Quick Start\n\n```python\nfrom webvtt_python import WebVTTParser, WebVTT\n\n# Parse from string\nparser = WebVTTParser(strict=True)\ncontent = \"\"\"WEBVTT\n\n00:00:01.000 --> 00:00:02.000\nHello world!\n\n00:00:02.500 --> 00:00:05.000 position:50%\nMulti-line\nsubtitle\n\"\"\"\n\nwebvtt: WebVTT = parser.parse(content)\nfor cue in webvtt.cues:\n    print(f\"{cue.start_time:.1f}-{cue.end_time:.1f}s: {cue.text}\")\n```\n\n## Advanced Usage\n\n### Region Handling\n```python\ncontent = \"\"\"WEBVTT\nREGION\nid:test\nwidth:40%\nlines:3\nregionanchor:0%,100%\nviewportanchor:10%,90%\n\"\"\"\n\nwebvtt = parser.parse(content)\nregion = webvtt.regions[0]\nprint(f\"Region {region.id}: {region.width}% width, {region.lines} lines\")\n```\n\n### Error Handling\n```python\ntry:\n    parser.parse(\"00:00:01.000 --> 00:00:00.500\\nInvalid timing\")\nexcept ValueError as e:\n    print(f\"Validation error: {e}\")\n```\n\n### Cue Settings\n```python\ncue = webvtt.cues[0]\nprint(f\"Position: {cue.position}%\")\nprint(f\"Alignment: {cue.text_alignment.value}\")\nprint(f\"Writing direction: {cue.writing_direction}\")\n```\n\n## Architecture\n\nThe WebVTT parser implements the [W3C WebVTT specification](https://www.w3.org/TR/webvtt1/).\n\n```mermaid\ngraph TD\n    subgraph \"Parser Pipeline\"\n        A[\".vtt File/String\"] --> B[\"WebVTTParser\"]\n        B --> C{\"Valid<br>WEBVTT?\"}\n        C -->|No| D[\"MalformedVTTError\"]\n\n        C -->|Yes| E[\"Header Processing\"]\n        E --> F[\"Style & Region<br>Parsing\"]\n\n        F --> G[\"Cue Processing\"]\n        G --> H{\"Validation\"}\n\n        H -->|Invalid| I[\"ValidationError\"]\n        H -->|Valid| J[\"WebVTTFile\"]\n\n        J --> K[\"Output Formats\"]\n        K --> L1[\"JSON\"]\n        K --> L2[\"SRT\"]\n        K --> L3[\"WebVTT\"]\n    end\n\n    classDef default fill:#2A2A2A,stroke:#666,color:#DDD\n    classDef process fill:#4a90e2,stroke:#2171C7,color:white\n    classDef error fill:#e74c3c,stroke:#c0392b,color:white\n    classDef decision fill:#f39c12,stroke:#d35400,color:white\n    classDef output fill:#2ecc71,stroke:#27ae60,color:white\n\n    class A,B default\n    class C,H decision\n    class D,I error\n    class E,F,G process\n    class J,K,L1,L2,L3 output\n```\n\n### Key Components\n\n1. **Input Processing**\n   - File or string input\n   - WEBVTT validation\n   - BOM handling\n\n2. **Content Parsing**\n   - Header and metadata\n   - Styles and regions\n   - Cue timing and text\n\n3. **Output Options**\n   - JSON serialization\n   - SRT conversion\n   - WebVTT formatting\n\n## API Reference\n\n### WebVTTParser\n```python\nWebVTTParser(strict: bool = True)\n```\n- `strict`: Raise errors for invalid content (default True)\n\nMethods:\n- `parse(content: str | TextIO) -> WebVTT`\n\n### WebVTT Model\n```python\nclass WebVTT:\n    cues: List[WebVTTCue]\n    regions: List[WebVTTRegion]\n    styles: List[str]\n    header_comments: List[str]\n```\n\n### WebVTTCue\n```python\nclass WebVTTCue:\n    start_time: float\n    end_time: float\n    text: str\n    identifier: Optional[str]\n    region: Optional[str]\n    position: Optional[float]\n    size: float\n    text_alignment: TextAlignment\n    # ... other properties\n```\n\n### WebVTTRegion\n```python\nclass WebVTTRegion:\n    id: str\n    width: float\n    lines: int\n    region_anchor: Tuple[float, float]\n    viewport_anchor: Tuple[float, float]\n    scroll: str\n```\n\n## Development\n\n```bash\ngit clone https://github.com/yourusername/webvtt-python.git\ncd webvtt-python\nuv venv\nsource .venv/bin/activate\nuv sync --system\n```\n\n### Running Tests\n```bash\nuv run pytest tests/ -v\n```\n\n## License\n\nThis software is licensed under the MIT License (MIT). See [LICENSE.md](LICENSE.md) for details.\n\n## Contributing\n\nContributions welcome! Please open an issue first to discuss proposed changes.\n\n## Acknowledgments\n- W3C WebVTT specification team\n- Python datetime module for timestamp parsing inspiration\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "Python WebVTT API Implementation",
    "version": "0.2.0",
    "project_urls": {
        "Homepage": "https://github.com/BrianMcBrayer/webvtt-python",
        "Issues": "https://github.com/BrianMcBrayer/webvtt-python/issues",
        "Repository": "https://github.com/BrianMcBrayer/webvtt-python"
    },
    "split_keywords": [
        "captions",
        " subtitles",
        " webvtt"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "3207e321c790bcab0fdcf59b9799bb91b4756dabc2a34d628254b357853f777a",
                "md5": "faf827310767cf145c491f34f3357266",
                "sha256": "4d4f0beb2654818a0984cd3074c698c3571bef215c593b7e0d208a5a29ceba35"
            },
            "downloads": -1,
            "filename": "webvtt_python-0.2.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "faf827310767cf145c491f34f3357266",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<3.14,>=3.9",
            "size": 9221,
            "upload_time": "2025-02-20T19:10:28",
            "upload_time_iso_8601": "2025-02-20T19:10:28.766313Z",
            "url": "https://files.pythonhosted.org/packages/32/07/e321c790bcab0fdcf59b9799bb91b4756dabc2a34d628254b357853f777a/webvtt_python-0.2.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "cc7d6b0a941f4a06e50cc3423832254112b47969439f3085e143025443be225e",
                "md5": "dc4eff9d1187e5b08060b3eba5afecbe",
                "sha256": "a1d48853895059f0b37a660774710cad7daffeecfd649067febf3eae33da3544"
            },
            "downloads": -1,
            "filename": "webvtt_python-0.2.0.tar.gz",
            "has_sig": false,
            "md5_digest": "dc4eff9d1187e5b08060b3eba5afecbe",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<3.14,>=3.9",
            "size": 104750,
            "upload_time": "2025-02-20T19:10:29",
            "upload_time_iso_8601": "2025-02-20T19:10:29.776099Z",
            "url": "https://files.pythonhosted.org/packages/cc/7d/6b0a941f4a06e50cc3423832254112b47969439f3085e143025443be225e/webvtt_python-0.2.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-02-20 19:10:29",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "BrianMcBrayer",
    "github_project": "webvtt-python",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "webvtt-python"
}
        
Elapsed time: 0.47395s