synop 0.1.0__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.
- synop-0.1.0/.github/workflows/publish.yml +34 -0
- synop-0.1.0/.gitignore +39 -0
- synop-0.1.0/LICENSE +21 -0
- synop-0.1.0/PKG-INFO +13 -0
- synop-0.1.0/README.md +179 -0
- synop-0.1.0/logo.png +0 -0
- synop-0.1.0/pyproject.toml +24 -0
- synop-0.1.0/src/synop/__init__.py +1 -0
- synop-0.1.0/src/synop/ai_client.py +214 -0
- synop-0.1.0/src/synop/cli.py +506 -0
- synop-0.1.0/src/synop/formatter.py +99 -0
- synop-0.1.0/src/synop/settings.py +81 -0
- synop-0.1.0/src/synop/templates/example.j2 +14 -0
- synop-0.1.0/src/synop/thumbnailer.py +468 -0
- synop-0.1.0/src/synop/video.py +229 -0
- synop-0.1.0/uv.lock +454 -0
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
name: Publish Package
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
release:
|
|
5
|
+
types: [published]
|
|
6
|
+
workflow_dispatch:
|
|
7
|
+
|
|
8
|
+
permissions:
|
|
9
|
+
contents: read
|
|
10
|
+
id-token: write
|
|
11
|
+
|
|
12
|
+
jobs:
|
|
13
|
+
build-and-publish:
|
|
14
|
+
runs-on: ubuntu-latest
|
|
15
|
+
environment:
|
|
16
|
+
name: pypi
|
|
17
|
+
url: https://pypi.org/p/thumby-cli
|
|
18
|
+
steps:
|
|
19
|
+
- name: Check out repository
|
|
20
|
+
uses: actions/checkout@v4
|
|
21
|
+
|
|
22
|
+
- name: Set up Python
|
|
23
|
+
uses: actions/setup-python@v5
|
|
24
|
+
with:
|
|
25
|
+
python-version: "3.12"
|
|
26
|
+
|
|
27
|
+
- name: Install uv
|
|
28
|
+
uses: astral-sh/setup-uv@v5
|
|
29
|
+
|
|
30
|
+
- name: Build distributions
|
|
31
|
+
run: uv build
|
|
32
|
+
|
|
33
|
+
- name: Publish to PyPI
|
|
34
|
+
uses: pypa/gh-action-pypi-publish@release/v1
|
synop-0.1.0/.gitignore
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# Python
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
*.pyo
|
|
5
|
+
*.pyd
|
|
6
|
+
*.so
|
|
7
|
+
|
|
8
|
+
# Packaging / build artifacts
|
|
9
|
+
build/
|
|
10
|
+
dist/
|
|
11
|
+
*.egg-info/
|
|
12
|
+
|
|
13
|
+
# Virtual environments
|
|
14
|
+
.venv/
|
|
15
|
+
venv/
|
|
16
|
+
env/
|
|
17
|
+
|
|
18
|
+
# Tooling caches
|
|
19
|
+
.ruff_cache/
|
|
20
|
+
.mypy_cache/
|
|
21
|
+
.pytest_cache/
|
|
22
|
+
|
|
23
|
+
# Editors / OS
|
|
24
|
+
.vscode/
|
|
25
|
+
.idea/
|
|
26
|
+
.DS_Store
|
|
27
|
+
Thumbs.db
|
|
28
|
+
|
|
29
|
+
# Local media files (avoid accidental commits)
|
|
30
|
+
*.ts
|
|
31
|
+
*.mp4
|
|
32
|
+
*.mkv
|
|
33
|
+
*.mov
|
|
34
|
+
*.avi
|
|
35
|
+
*.flv
|
|
36
|
+
*.webm
|
|
37
|
+
|
|
38
|
+
# Temporary artifacts produced by app
|
|
39
|
+
filelist.txt
|
synop-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026
|
|
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.
|
synop-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: synop
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: AI-powered CLI tool to generate structured video descriptions from sampled frames and metadata.
|
|
5
|
+
License-File: LICENSE
|
|
6
|
+
Requires-Python: >=3.10
|
|
7
|
+
Requires-Dist: av>=9.0.0
|
|
8
|
+
Requires-Dist: httpx>=0.27.0
|
|
9
|
+
Requires-Dist: jinja2>=3.1.0
|
|
10
|
+
Requires-Dist: pillow>=9.2.0
|
|
11
|
+
Requires-Dist: pymediainfo>=4.3
|
|
12
|
+
Requires-Dist: rich>=13.0.0
|
|
13
|
+
Requires-Dist: typer>=0.15.0
|
synop-0.1.0/README.md
ADDED
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+

|
|
2
|
+
# synop
|
|
3
|
+
|
|
4
|
+
**synop** is a uv-first CLI utility that generates structured video descriptions using OpenAI-compatible vision APIs (OpenRouter by default).
|
|
5
|
+
|
|
6
|
+
It samples key frames across your video timeline, combines them with technical media metadata, and writes generated output as JSON, plaintext, or Jinja-rendered BBCode.
|
|
7
|
+
|
|
8
|
+

|
|
9
|
+

|
|
10
|
+
|
|
11
|
+
## Features
|
|
12
|
+
|
|
13
|
+
- Smart frame sampling based on runtime length (4/8/12 frames)
|
|
14
|
+
- Metadata extraction (duration, resolution, codec)
|
|
15
|
+
- OpenRouter by default, with configurable OpenAI-compatible API base URL
|
|
16
|
+
- Persistent API key storage in user settings
|
|
17
|
+
- Custom output templates with Jinja2
|
|
18
|
+
|
|
19
|
+
## Installation
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
uv tool install synop-cli
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Quick Start
|
|
26
|
+
|
|
27
|
+
1) Save your API key once:
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
synop config set-key
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
2) Generate a description:
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
synop "path/to/video.mp4"
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
This writes `video_name_desc.json` next to the source video.
|
|
40
|
+
|
|
41
|
+
## Usage
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
synop "path/to/video.mp4" [OPTIONS]
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
Options:
|
|
48
|
+
|
|
49
|
+
- `-m, --model` Model ID (default: `x-ai/grok-4.20-beta`)
|
|
50
|
+
- `--api-base-url` OpenAI-compatible API base URL (default: `https://openrouter.ai/api/v1`)
|
|
51
|
+
- `-f, --format` Output format: `json` (default), `plaintext`, `bbcode`
|
|
52
|
+
- `-t, --template` Path to custom `.j2` or `.txt` template (only used with `--format bbcode`)
|
|
53
|
+
- `-o, --output` Output path for generated description
|
|
54
|
+
- `--thumb` Thumbnail URL/path to inject into `{{ video_thumb }}`
|
|
55
|
+
- `--generate-thumbnail` Generate thumby-style thumbnail sheet image
|
|
56
|
+
- `--thumbnail-output` Output path for generated thumbnail sheet
|
|
57
|
+
- `--screens-dir` Directory to save thumby-grid extracted screen images
|
|
58
|
+
- `--thumb-rows` Rows in thumbnail/screen grid (default: `9`)
|
|
59
|
+
- `--thumb-cols` Columns in thumbnail/screen grid (default: `3`)
|
|
60
|
+
- `--thumb-width` Grid tile width in pixels (default: `400`)
|
|
61
|
+
- `--thumb-skip` Seconds to skip from start for grid extraction (default: `10.0`)
|
|
62
|
+
- `--thumb-quality` JPEG quality for thumbnail/screens (default: `95`)
|
|
63
|
+
|
|
64
|
+
## Output Formats
|
|
65
|
+
|
|
66
|
+
By default, synop writes JSON output:
|
|
67
|
+
|
|
68
|
+
```json
|
|
69
|
+
{
|
|
70
|
+
"title": "...",
|
|
71
|
+
"short_summary": "...",
|
|
72
|
+
"generated_description": "...",
|
|
73
|
+
"duration": "01:42:33",
|
|
74
|
+
"full_resolution": "1920x1080",
|
|
75
|
+
"short_resolution": "1080p",
|
|
76
|
+
"codec": "H.264",
|
|
77
|
+
"video_thumb": ""
|
|
78
|
+
}
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
Use `--format plaintext` to write plain text output.
|
|
82
|
+
|
|
83
|
+
Use `--format bbcode` to render Jinja templates (defaults to bundled `example.j2` if `--template` is not set).
|
|
84
|
+
|
|
85
|
+
When thumbnail/screens generation is enabled, JSON output also includes:
|
|
86
|
+
|
|
87
|
+
- `thumbnail_path` (when `--generate-thumbnail` is used)
|
|
88
|
+
- `screens_dir`, `screens_count`, `screens` (when `--screens-dir` is used)
|
|
89
|
+
|
|
90
|
+
## Thumbnail and Screens
|
|
91
|
+
|
|
92
|
+
Generate a thumby-style thumbnail sheet:
|
|
93
|
+
|
|
94
|
+
```bash
|
|
95
|
+
synop "path/to/video.mp4" --generate-thumbnail
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
Save thumby-grid extracted frames into a screens folder:
|
|
99
|
+
|
|
100
|
+
```bash
|
|
101
|
+
synop "path/to/video.mp4" --screens-dir "./screens"
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
Generate both thumbnail and screens with custom grid settings:
|
|
105
|
+
|
|
106
|
+
```bash
|
|
107
|
+
synop "path/to/video.mp4" --generate-thumbnail --screens-dir "./screens" --thumb-rows 6 --thumb-cols 4 --thumb-width 360
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
Notes:
|
|
111
|
+
|
|
112
|
+
- `--screens-dir` always saves the thumby-grid frames (`rows * cols`) with ordered filenames.
|
|
113
|
+
- By default, saved screens are resized to 50% of the original video dimensions.
|
|
114
|
+
- When `--generate-thumbnail` is used and `--thumb` is not set, `video_thumb` is auto-filled with the generated thumbnail path.
|
|
115
|
+
|
|
116
|
+
## API Key Resolution
|
|
117
|
+
|
|
118
|
+
Synop resolves the key in this order:
|
|
119
|
+
|
|
120
|
+
1. `SYNOP_API_KEY` environment variable
|
|
121
|
+
2. Persisted settings file
|
|
122
|
+
|
|
123
|
+
## API Provider
|
|
124
|
+
|
|
125
|
+
By default, synop calls OpenRouter (`https://openrouter.ai/api/v1`).
|
|
126
|
+
|
|
127
|
+
You can point synop to another OpenAI-compatible endpoint:
|
|
128
|
+
|
|
129
|
+
```bash
|
|
130
|
+
synop "path/to/video.mp4" --api-base-url "https://your-provider.example/v1" --model "your-vision-model"
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
Settings path:
|
|
134
|
+
|
|
135
|
+
- Windows: `%APPDATA%\synop\settings.json`
|
|
136
|
+
- Linux/macOS: `$XDG_CONFIG_HOME/synop/settings.json` or `~/.config/synop/settings.json`
|
|
137
|
+
|
|
138
|
+
## Example BBCode Template
|
|
139
|
+
|
|
140
|
+
The previous default BBCode output is now provided as an example Jinja template in `src/synop/templates/example.j2`:
|
|
141
|
+
|
|
142
|
+
```text
|
|
143
|
+
[cast]
|
|
144
|
+
[font=Arial Black][size=4]{{ title }}[/size][/font]
|
|
145
|
+
{{ short_summary }}
|
|
146
|
+
|
|
147
|
+
[info]
|
|
148
|
+
[b]Duration:[/b] {{ duration }}
|
|
149
|
+
[b]Resolution:[/b] {{ full_resolution }} ({{ short_resolution }})
|
|
150
|
+
[b]Codec:[/b] {{ codec }}
|
|
151
|
+
|
|
152
|
+
[plot]
|
|
153
|
+
{{ generated_description }}
|
|
154
|
+
|
|
155
|
+
[screens]
|
|
156
|
+
[img]{{ video_thumb }}[/img]
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
Use it directly:
|
|
160
|
+
|
|
161
|
+
```bash
|
|
162
|
+
synop "path/to/video.mp4" --format bbcode --template src/synop/templates/example.j2
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
Template variables available in Jinja context:
|
|
166
|
+
|
|
167
|
+
- `title`
|
|
168
|
+
- `short_summary`
|
|
169
|
+
- `generated_description`
|
|
170
|
+
- `duration`
|
|
171
|
+
- `full_resolution`
|
|
172
|
+
- `short_resolution`
|
|
173
|
+
- `codec`
|
|
174
|
+
- `video_thumb`
|
|
175
|
+
|
|
176
|
+
## Requirements
|
|
177
|
+
|
|
178
|
+
- Python 3.10+
|
|
179
|
+
- FFmpeg runtime libraries (required by `av`)
|
synop-0.1.0/logo.png
ADDED
|
Binary file
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "synop"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "AI-powered CLI tool to generate structured video descriptions from sampled frames and metadata."
|
|
5
|
+
requires-python = ">=3.10"
|
|
6
|
+
dependencies = [
|
|
7
|
+
"typer>=0.15.0",
|
|
8
|
+
"av>=9.0.0",
|
|
9
|
+
"pymediainfo>=4.3",
|
|
10
|
+
"Pillow>=9.2.0",
|
|
11
|
+
"rich>=13.0.0",
|
|
12
|
+
"httpx>=0.27.0",
|
|
13
|
+
"jinja2>=3.1.0",
|
|
14
|
+
]
|
|
15
|
+
|
|
16
|
+
[project.scripts]
|
|
17
|
+
synop = "synop.cli:run"
|
|
18
|
+
|
|
19
|
+
[build-system]
|
|
20
|
+
requires = ["hatchling"]
|
|
21
|
+
build-backend = "hatchling.build"
|
|
22
|
+
|
|
23
|
+
[tool.hatch.build.targets.wheel]
|
|
24
|
+
packages = ["src/synop"]
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "0.1.0"
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import base64
|
|
4
|
+
import json
|
|
5
|
+
import re
|
|
6
|
+
from dataclasses import dataclass
|
|
7
|
+
from urllib.parse import urljoin
|
|
8
|
+
|
|
9
|
+
import httpx
|
|
10
|
+
|
|
11
|
+
OPENROUTER_BASE_URL = "https://openrouter.ai/api/v1"
|
|
12
|
+
DEFAULT_MODEL = "x-ai/grok-4.20-beta"
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
@dataclass(slots=True)
|
|
16
|
+
class DescriptionResult:
|
|
17
|
+
title: str
|
|
18
|
+
short_summary: str
|
|
19
|
+
generated_description: str
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class OpenRouterError(RuntimeError):
|
|
23
|
+
pass
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class OpenRouterClient:
|
|
27
|
+
def __init__(
|
|
28
|
+
self,
|
|
29
|
+
api_key: str,
|
|
30
|
+
model: str = DEFAULT_MODEL,
|
|
31
|
+
timeout: float = 120.0,
|
|
32
|
+
api_base_url: str = OPENROUTER_BASE_URL,
|
|
33
|
+
):
|
|
34
|
+
self.api_key = api_key
|
|
35
|
+
self.model = model
|
|
36
|
+
self.timeout = timeout
|
|
37
|
+
self.api_base_url = api_base_url.rstrip("/")
|
|
38
|
+
|
|
39
|
+
@property
|
|
40
|
+
def chat_completions_url(self) -> str:
|
|
41
|
+
if self.api_base_url.lower().endswith("/chat/completions"):
|
|
42
|
+
return self.api_base_url
|
|
43
|
+
return urljoin(f"{self.api_base_url}/", "chat/completions")
|
|
44
|
+
|
|
45
|
+
@property
|
|
46
|
+
def is_openrouter(self) -> bool:
|
|
47
|
+
return "openrouter.ai" in self.api_base_url.lower()
|
|
48
|
+
|
|
49
|
+
async def generate_description(self, frame_jpegs: list[bytes]) -> DescriptionResult:
|
|
50
|
+
if not frame_jpegs:
|
|
51
|
+
raise ValueError("No frame images were provided to the AI client")
|
|
52
|
+
|
|
53
|
+
content: list[dict[str, object]] = [
|
|
54
|
+
{
|
|
55
|
+
"type": "text",
|
|
56
|
+
"text": (
|
|
57
|
+
"You are an expert film archiver. Analyze these sequential frames "
|
|
58
|
+
"from a video and respond with valid JSON only. You are allowed to use explicit/adult language in case of NSFW context. "
|
|
59
|
+
'Schema: {"title":"...", "short_summary":"...", "generated_description":"..."}. '
|
|
60
|
+
"Requirements: title should be catchy and concise, short_summary must be exactly 2 sentences, "
|
|
61
|
+
"generated_description must be 3 paragraphs with rich plot/detail context."
|
|
62
|
+
),
|
|
63
|
+
}
|
|
64
|
+
]
|
|
65
|
+
|
|
66
|
+
for frame in frame_jpegs:
|
|
67
|
+
encoded = base64.b64encode(frame).decode("ascii")
|
|
68
|
+
content.append(
|
|
69
|
+
{
|
|
70
|
+
"type": "image_url",
|
|
71
|
+
"image_url": {"url": f"data:image/jpeg;base64,{encoded}"},
|
|
72
|
+
}
|
|
73
|
+
)
|
|
74
|
+
|
|
75
|
+
payload = {
|
|
76
|
+
"model": self.model,
|
|
77
|
+
"messages": [{"role": "user", "content": content}],
|
|
78
|
+
"temperature": 0.4,
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
headers = {
|
|
82
|
+
"Authorization": f"Bearer {self.api_key}",
|
|
83
|
+
"Content-Type": "application/json",
|
|
84
|
+
}
|
|
85
|
+
if self.is_openrouter:
|
|
86
|
+
headers["HTTP-Referer"] = "https://github.com/h4nz4/synop"
|
|
87
|
+
headers["X-Title"] = "synop"
|
|
88
|
+
|
|
89
|
+
async with httpx.AsyncClient(timeout=self.timeout) as client:
|
|
90
|
+
response = await client.post(
|
|
91
|
+
self.chat_completions_url,
|
|
92
|
+
headers=headers,
|
|
93
|
+
json=payload,
|
|
94
|
+
)
|
|
95
|
+
|
|
96
|
+
if response.status_code >= 400:
|
|
97
|
+
self._raise_http_error(response)
|
|
98
|
+
|
|
99
|
+
data = response.json()
|
|
100
|
+
raw_content = _extract_assistant_content(data)
|
|
101
|
+
return _parse_description(raw_content)
|
|
102
|
+
|
|
103
|
+
def _raise_http_error(self, response: httpx.Response) -> None:
|
|
104
|
+
body_text = response.text
|
|
105
|
+
status = response.status_code
|
|
106
|
+
lowered = body_text.lower()
|
|
107
|
+
|
|
108
|
+
if status == 401:
|
|
109
|
+
provider = "OpenRouter" if self.is_openrouter else "API"
|
|
110
|
+
raise OpenRouterError(
|
|
111
|
+
f"{provider} authentication failed. Check SYNOP_API_KEY or saved key."
|
|
112
|
+
)
|
|
113
|
+
if status == 429:
|
|
114
|
+
provider = "OpenRouter" if self.is_openrouter else "API"
|
|
115
|
+
raise OpenRouterError(
|
|
116
|
+
f"{provider} rate limit reached. Retry later or use another model."
|
|
117
|
+
)
|
|
118
|
+
if (
|
|
119
|
+
status == 400
|
|
120
|
+
and "image" in lowered
|
|
121
|
+
and ("unsupported" in lowered or "not support" in lowered)
|
|
122
|
+
):
|
|
123
|
+
raise OpenRouterError(
|
|
124
|
+
f"Model '{self.model}' does not appear to support vision input."
|
|
125
|
+
)
|
|
126
|
+
|
|
127
|
+
raise OpenRouterError(f"OpenRouter request failed ({status}): {body_text}")
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
def _extract_assistant_content(payload: dict[str, object]) -> str:
|
|
131
|
+
choices = payload.get("choices")
|
|
132
|
+
if not isinstance(choices, list) or not choices:
|
|
133
|
+
raise OpenRouterError("OpenRouter response is missing choices")
|
|
134
|
+
|
|
135
|
+
first_choice = choices[0]
|
|
136
|
+
if not isinstance(first_choice, dict):
|
|
137
|
+
raise OpenRouterError("OpenRouter response has invalid choice format")
|
|
138
|
+
|
|
139
|
+
message = first_choice.get("message")
|
|
140
|
+
if not isinstance(message, dict):
|
|
141
|
+
raise OpenRouterError("OpenRouter response is missing message payload")
|
|
142
|
+
|
|
143
|
+
content = message.get("content")
|
|
144
|
+
if isinstance(content, str):
|
|
145
|
+
return content.strip()
|
|
146
|
+
|
|
147
|
+
if isinstance(content, list):
|
|
148
|
+
parts: list[str] = []
|
|
149
|
+
for item in content:
|
|
150
|
+
if isinstance(item, dict):
|
|
151
|
+
text = item.get("text")
|
|
152
|
+
if isinstance(text, str):
|
|
153
|
+
parts.append(text)
|
|
154
|
+
if parts:
|
|
155
|
+
return "\n".join(parts).strip()
|
|
156
|
+
|
|
157
|
+
raise OpenRouterError("OpenRouter response did not include text content")
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
def _parse_description(content: str) -> DescriptionResult:
|
|
161
|
+
data = _parse_json_maybe(content)
|
|
162
|
+
if data is not None:
|
|
163
|
+
title = str(data.get("title") or "Untitled")
|
|
164
|
+
short_summary = str(data.get("short_summary") or "No short summary provided.")
|
|
165
|
+
generated_description = str(
|
|
166
|
+
data.get("generated_description") or "No detailed description provided."
|
|
167
|
+
)
|
|
168
|
+
return DescriptionResult(
|
|
169
|
+
title=title.strip(),
|
|
170
|
+
short_summary=short_summary.strip(),
|
|
171
|
+
generated_description=generated_description.strip(),
|
|
172
|
+
)
|
|
173
|
+
|
|
174
|
+
plain = content.strip()
|
|
175
|
+
short_summary = plain[:220].strip()
|
|
176
|
+
if short_summary and not short_summary.endswith("."):
|
|
177
|
+
short_summary += "."
|
|
178
|
+
return DescriptionResult(
|
|
179
|
+
title="Generated Video Description",
|
|
180
|
+
short_summary=short_summary or "No short summary provided.",
|
|
181
|
+
generated_description=plain or "No detailed description provided.",
|
|
182
|
+
)
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
def _parse_json_maybe(content: str) -> dict[str, object] | None:
|
|
186
|
+
stripped = content.strip()
|
|
187
|
+
try:
|
|
188
|
+
loaded = json.loads(stripped)
|
|
189
|
+
if isinstance(loaded, dict):
|
|
190
|
+
return loaded
|
|
191
|
+
except json.JSONDecodeError:
|
|
192
|
+
pass
|
|
193
|
+
|
|
194
|
+
fenced_match = re.search(r"```(?:json)?\s*(\{.*?\})\s*```", stripped, re.DOTALL)
|
|
195
|
+
if fenced_match:
|
|
196
|
+
candidate = fenced_match.group(1)
|
|
197
|
+
try:
|
|
198
|
+
loaded = json.loads(candidate)
|
|
199
|
+
if isinstance(loaded, dict):
|
|
200
|
+
return loaded
|
|
201
|
+
except json.JSONDecodeError:
|
|
202
|
+
pass
|
|
203
|
+
|
|
204
|
+
bracket_match = re.search(r"\{.*\}", stripped, re.DOTALL)
|
|
205
|
+
if bracket_match:
|
|
206
|
+
candidate = bracket_match.group(0)
|
|
207
|
+
try:
|
|
208
|
+
loaded = json.loads(candidate)
|
|
209
|
+
if isinstance(loaded, dict):
|
|
210
|
+
return loaded
|
|
211
|
+
except json.JSONDecodeError:
|
|
212
|
+
return None
|
|
213
|
+
|
|
214
|
+
return None
|