vided 0.1.2__py3-none-any.whl
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.
- vided/__init__.py +84 -0
- vided/__main__.py +4 -0
- vided/cli.py +347 -0
- vided/contact_sheet.py +257 -0
- vided/data/__init__.py +1 -0
- vided/data/silero_vad_16k_op15.onnx +0 -0
- vided/errors.py +21 -0
- vided/ffmpeg.py +200 -0
- vided/filtergraph.py +107 -0
- vided/frames.py +101 -0
- vided/project.py +238 -0
- vided/redactions.py +136 -0
- vided/render.py +78 -0
- vided/static/__init__.py +0 -0
- vided/static/app.js +1368 -0
- vided/static/index.html +121 -0
- vided/static/styles.css +808 -0
- vided/time_ranges.py +87 -0
- vided/trimmer.py +932 -0
- vided/ui_server.py +170 -0
- vided/vad.py +495 -0
- vided-0.1.2.dist-info/METADATA +492 -0
- vided-0.1.2.dist-info/RECORD +26 -0
- vided-0.1.2.dist-info/WHEEL +4 -0
- vided-0.1.2.dist-info/entry_points.txt +2 -0
- vided-0.1.2.dist-info/licenses/LICENSE +21 -0
vided/__init__.py
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
"""Public Python API for vided."""
|
|
2
|
+
|
|
3
|
+
from .contact_sheet import render_contact_sheet
|
|
4
|
+
from .errors import (
|
|
5
|
+
ExternalToolError,
|
|
6
|
+
OptionalDependencyError,
|
|
7
|
+
ProjectError,
|
|
8
|
+
ValidationError,
|
|
9
|
+
VidedError,
|
|
10
|
+
)
|
|
11
|
+
from .ffmpeg import ToolError, VideoInfo, probe_media
|
|
12
|
+
from .frames import generate_frames
|
|
13
|
+
from .project import (
|
|
14
|
+
ProjectPaths,
|
|
15
|
+
create_project,
|
|
16
|
+
load_project,
|
|
17
|
+
project_paths,
|
|
18
|
+
read_json,
|
|
19
|
+
save_project,
|
|
20
|
+
write_json,
|
|
21
|
+
)
|
|
22
|
+
from .redactions import (
|
|
23
|
+
Rect,
|
|
24
|
+
Redaction,
|
|
25
|
+
RedactionDocument,
|
|
26
|
+
load_redactions,
|
|
27
|
+
render_redactions,
|
|
28
|
+
save_redactions,
|
|
29
|
+
validate_redaction_document,
|
|
30
|
+
)
|
|
31
|
+
from .render import render_project
|
|
32
|
+
from .trimmer import (
|
|
33
|
+
OperationResult,
|
|
34
|
+
TrimOptions,
|
|
35
|
+
TrimPlan,
|
|
36
|
+
TrimSegment,
|
|
37
|
+
VadOptions,
|
|
38
|
+
build_trim_command,
|
|
39
|
+
plan_trim,
|
|
40
|
+
run_trim_plan,
|
|
41
|
+
)
|
|
42
|
+
from .vad import VadSettings, normalize_detector, run_vad_detection
|
|
43
|
+
|
|
44
|
+
__version__ = "0.1.0"
|
|
45
|
+
|
|
46
|
+
__all__ = [
|
|
47
|
+
"ExternalToolError",
|
|
48
|
+
"OperationResult",
|
|
49
|
+
"OptionalDependencyError",
|
|
50
|
+
"ProjectError",
|
|
51
|
+
"ProjectPaths",
|
|
52
|
+
"Rect",
|
|
53
|
+
"Redaction",
|
|
54
|
+
"RedactionDocument",
|
|
55
|
+
"ToolError",
|
|
56
|
+
"TrimOptions",
|
|
57
|
+
"TrimPlan",
|
|
58
|
+
"TrimSegment",
|
|
59
|
+
"VadOptions",
|
|
60
|
+
"VadSettings",
|
|
61
|
+
"ValidationError",
|
|
62
|
+
"VidedError",
|
|
63
|
+
"VideoInfo",
|
|
64
|
+
"__version__",
|
|
65
|
+
"build_trim_command",
|
|
66
|
+
"create_project",
|
|
67
|
+
"generate_frames",
|
|
68
|
+
"load_project",
|
|
69
|
+
"load_redactions",
|
|
70
|
+
"normalize_detector",
|
|
71
|
+
"plan_trim",
|
|
72
|
+
"probe_media",
|
|
73
|
+
"project_paths",
|
|
74
|
+
"read_json",
|
|
75
|
+
"render_contact_sheet",
|
|
76
|
+
"render_project",
|
|
77
|
+
"render_redactions",
|
|
78
|
+
"run_trim_plan",
|
|
79
|
+
"run_vad_detection",
|
|
80
|
+
"save_project",
|
|
81
|
+
"save_redactions",
|
|
82
|
+
"validate_redaction_document",
|
|
83
|
+
"write_json",
|
|
84
|
+
]
|
vided/__main__.py
ADDED
vided/cli.py
ADDED
|
@@ -0,0 +1,347 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import argparse
|
|
4
|
+
import re
|
|
5
|
+
import sys
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
|
|
8
|
+
from .contact_sheet import render_contact_sheet
|
|
9
|
+
from .errors import VidedError
|
|
10
|
+
from .ffmpeg import ToolError, ensure_tool
|
|
11
|
+
from .frames import generate_frames
|
|
12
|
+
from .project import create_project, project_paths
|
|
13
|
+
from .redactions import load_redactions, render_redactions, validate_redaction_document
|
|
14
|
+
from .render import render_project
|
|
15
|
+
from .trimmer import TrimOptions, VadOptions, build_trim_command, plan_trim, run_trim_plan
|
|
16
|
+
from .vad import run_vad_detection
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def _default_project_dir(source: Path) -> Path:
|
|
20
|
+
name = re.sub(r"[^A-Za-z0-9]+", "-", source.stem).strip("-").lower()
|
|
21
|
+
return Path(name or "video-project")
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def _add_vad_options(parser: argparse.ArgumentParser) -> None:
|
|
25
|
+
parser.add_argument("--vad-threshold", type=float, default=None)
|
|
26
|
+
parser.add_argument("--vad-min-speech-ms", type=int, default=None)
|
|
27
|
+
parser.add_argument("--vad-min-silence-ms", type=int, default=None)
|
|
28
|
+
parser.add_argument("--vad-speech-pad-ms", type=int, default=None)
|
|
29
|
+
parser.add_argument("--vad-merge-gap", type=float, default=None)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def _add_trim_detector_options(parser: argparse.ArgumentParser) -> None:
|
|
33
|
+
parser.add_argument(
|
|
34
|
+
"--detector",
|
|
35
|
+
"--engine",
|
|
36
|
+
choices=["audio", "vad"],
|
|
37
|
+
default=None,
|
|
38
|
+
help="Detector used to classify normal-speed sections.",
|
|
39
|
+
)
|
|
40
|
+
_add_vad_options(parser)
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def _add_speed_indicator_options(parser: argparse.ArgumentParser) -> None:
|
|
44
|
+
parser.add_argument(
|
|
45
|
+
"--speed-indicator",
|
|
46
|
+
action=argparse.BooleanOptionalAction,
|
|
47
|
+
default=None,
|
|
48
|
+
help="Show a small speed label on sped-up silent sections.",
|
|
49
|
+
)
|
|
50
|
+
parser.add_argument(
|
|
51
|
+
"--speed-indicator-corner",
|
|
52
|
+
choices=["top-left", "top-right", "bottom-left", "bottom-right"],
|
|
53
|
+
default=None,
|
|
54
|
+
)
|
|
55
|
+
parser.add_argument(
|
|
56
|
+
"--speed-indicator-style",
|
|
57
|
+
choices=["dark", "light"],
|
|
58
|
+
default=None,
|
|
59
|
+
)
|
|
60
|
+
parser.add_argument(
|
|
61
|
+
"--speed-indicator-min-seconds",
|
|
62
|
+
type=float,
|
|
63
|
+
default=None,
|
|
64
|
+
help="Only show the badge when the sped-up section lasts at least this long after speedup.",
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def _add_diagnostic_parser(subparsers, name: str) -> argparse.ArgumentParser:
|
|
69
|
+
parser = subparsers.add_parser(name)
|
|
70
|
+
# Keep diagnostic commands parseable without putting them in top-level help.
|
|
71
|
+
subparsers._choices_actions = [
|
|
72
|
+
action for action in subparsers._choices_actions if action.dest != name
|
|
73
|
+
]
|
|
74
|
+
return parser
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def build_parser() -> argparse.ArgumentParser:
|
|
78
|
+
parser = argparse.ArgumentParser(
|
|
79
|
+
prog="vided",
|
|
80
|
+
description="Simple local video silence speeder and rectangular blur redactor.",
|
|
81
|
+
)
|
|
82
|
+
sub = parser.add_subparsers(dest="command", metavar="command")
|
|
83
|
+
|
|
84
|
+
init = sub.add_parser("init", help="Create a one-video project folder.")
|
|
85
|
+
init.add_argument("source", type=Path, help="Input video path.")
|
|
86
|
+
init.add_argument(
|
|
87
|
+
"project",
|
|
88
|
+
type=Path,
|
|
89
|
+
nargs="?",
|
|
90
|
+
help="Project folder to create. Defaults to a folder name based on the input video.",
|
|
91
|
+
)
|
|
92
|
+
init.add_argument(
|
|
93
|
+
"-o",
|
|
94
|
+
"--output-dir",
|
|
95
|
+
type=Path,
|
|
96
|
+
default=None,
|
|
97
|
+
help="Project folder to create when using the input video shorthand.",
|
|
98
|
+
)
|
|
99
|
+
init.add_argument(
|
|
100
|
+
"--frame-interval", type=float, default=1.0, help="Default thumbnail interval."
|
|
101
|
+
)
|
|
102
|
+
init.add_argument(
|
|
103
|
+
"--symlink", action="store_true", help="Symlink instead of copying input video."
|
|
104
|
+
)
|
|
105
|
+
init.add_argument("--overwrite", action="store_true", help="Allow reusing a non-empty folder.")
|
|
106
|
+
init.set_defaults(func=cmd_init)
|
|
107
|
+
|
|
108
|
+
trim = sub.add_parser("trim", help="Run the trim renderer on the source video.")
|
|
109
|
+
trim.add_argument("project", type=Path)
|
|
110
|
+
_add_trim_detector_options(trim)
|
|
111
|
+
trim.add_argument("--mode", choices=["hybrid", "speed", "cut", "keep"], default=None)
|
|
112
|
+
trim.add_argument("--margin", default=None, help="Trim margin, e.g. 0.2s or 0.3s,1.0s")
|
|
113
|
+
trim.add_argument("--smooth", default=None, help="Trim smoothing pair, e.g. 0.2s,0.1s")
|
|
114
|
+
trim.add_argument("--silent-speed", type=float, default=None, help="Speed for silent sections.")
|
|
115
|
+
trim.add_argument(
|
|
116
|
+
"--mute-silent-audio",
|
|
117
|
+
action=argparse.BooleanOptionalAction,
|
|
118
|
+
default=None,
|
|
119
|
+
help="When speed mode is used, chain volume:0 onto silent sections.",
|
|
120
|
+
)
|
|
121
|
+
_add_speed_indicator_options(trim)
|
|
122
|
+
trim.add_argument("--overwrite", action="store_true")
|
|
123
|
+
trim.add_argument("--dry-run", action="store_true")
|
|
124
|
+
trim.set_defaults(func=cmd_trim)
|
|
125
|
+
|
|
126
|
+
vad = _add_diagnostic_parser(sub, "vad")
|
|
127
|
+
vad.add_argument("project", type=Path)
|
|
128
|
+
_add_vad_options(vad)
|
|
129
|
+
vad.set_defaults(func=cmd_vad)
|
|
130
|
+
|
|
131
|
+
frames = sub.add_parser("frames", help="Generate or refresh frame thumbnails.")
|
|
132
|
+
frames.add_argument("project", type=Path)
|
|
133
|
+
frames.add_argument("--interval", type=float, default=None, help="Seconds between thumbnails.")
|
|
134
|
+
frames.add_argument("--thumbnail-width", type=int, default=None)
|
|
135
|
+
frames.add_argument("--overwrite", action="store_true")
|
|
136
|
+
frames.add_argument("--dry-run", action="store_true")
|
|
137
|
+
frames.set_defaults(func=cmd_frames)
|
|
138
|
+
|
|
139
|
+
ui = sub.add_parser("ui", help="Start the local annotation UI, generating frames if needed.")
|
|
140
|
+
ui.add_argument("project", type=Path)
|
|
141
|
+
ui.add_argument("--host", default="127.0.0.1")
|
|
142
|
+
ui.add_argument("--port", type=int, default=8765)
|
|
143
|
+
ui.add_argument("--no-open", action="store_true", help="Do not open the browser automatically.")
|
|
144
|
+
ui.set_defaults(func=cmd_ui)
|
|
145
|
+
|
|
146
|
+
render = sub.add_parser("render", help="Render final or debug preview video.")
|
|
147
|
+
render.add_argument("project", type=Path)
|
|
148
|
+
render.add_argument(
|
|
149
|
+
"--debug", action="store_true", help="Render visible rectangles instead of blur."
|
|
150
|
+
)
|
|
151
|
+
render.add_argument(
|
|
152
|
+
"--contact-sheet",
|
|
153
|
+
action="store_true",
|
|
154
|
+
help="Render a contact sheet from the final video.",
|
|
155
|
+
)
|
|
156
|
+
render.add_argument(
|
|
157
|
+
"--final-video",
|
|
158
|
+
type=Path,
|
|
159
|
+
default=None,
|
|
160
|
+
help="Final video to sample when rendering a contact sheet.",
|
|
161
|
+
)
|
|
162
|
+
render.add_argument("--output", type=Path, default=None)
|
|
163
|
+
render.add_argument("--overwrite", action="store_true")
|
|
164
|
+
render.add_argument("--dry-run", action="store_true")
|
|
165
|
+
render.set_defaults(func=cmd_render)
|
|
166
|
+
|
|
167
|
+
validate = _add_diagnostic_parser(sub, "validate")
|
|
168
|
+
validate.add_argument("project", type=Path)
|
|
169
|
+
validate.set_defaults(func=cmd_validate)
|
|
170
|
+
|
|
171
|
+
doctor = sub.add_parser("doctor", help="Check external tool availability.")
|
|
172
|
+
doctor.set_defaults(func=cmd_doctor)
|
|
173
|
+
|
|
174
|
+
preview = _add_diagnostic_parser(sub, "trim-command")
|
|
175
|
+
preview.add_argument("project", type=Path)
|
|
176
|
+
_add_trim_detector_options(preview)
|
|
177
|
+
preview.add_argument("--mode", choices=["hybrid", "speed", "cut", "keep"], default=None)
|
|
178
|
+
preview.add_argument("--margin", default=None)
|
|
179
|
+
preview.add_argument("--smooth", default=None)
|
|
180
|
+
preview.add_argument("--silent-speed", type=float, default=None)
|
|
181
|
+
preview.add_argument("--mute-silent-audio", action=argparse.BooleanOptionalAction, default=None)
|
|
182
|
+
_add_speed_indicator_options(preview)
|
|
183
|
+
preview.set_defaults(func=cmd_trim_command)
|
|
184
|
+
|
|
185
|
+
return parser
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
def cmd_init(args: argparse.Namespace) -> int:
|
|
189
|
+
if args.project is not None and args.output_dir is not None:
|
|
190
|
+
raise ValueError("Use either the project argument or --output-dir, not both.")
|
|
191
|
+
|
|
192
|
+
project = args.output_dir or args.project or _default_project_dir(args.source)
|
|
193
|
+
cfg = create_project(
|
|
194
|
+
args.source,
|
|
195
|
+
project,
|
|
196
|
+
frame_interval=args.frame_interval,
|
|
197
|
+
copy_input=not args.symlink,
|
|
198
|
+
overwrite=args.overwrite,
|
|
199
|
+
)
|
|
200
|
+
action = "linked" if args.symlink else "copied"
|
|
201
|
+
print(f"Created project: {project.resolve()}")
|
|
202
|
+
print(f"Source {action} to: {cfg['original_path']}")
|
|
203
|
+
print("Next: uv run vided trim <project>")
|
|
204
|
+
return 0
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
def cmd_trim(args: argparse.Namespace) -> int:
|
|
208
|
+
plan = plan_trim(
|
|
209
|
+
args.project,
|
|
210
|
+
options=_trim_options_from_args(args),
|
|
211
|
+
allow_vad_detection=not args.dry_run,
|
|
212
|
+
)
|
|
213
|
+
result = run_trim_plan(plan, overwrite=args.overwrite, dry_run=args.dry_run)
|
|
214
|
+
print(f"Trimmed video: {result.path}")
|
|
215
|
+
return 0
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
def cmd_vad(args: argparse.Namespace) -> int:
|
|
219
|
+
output = run_vad_detection(
|
|
220
|
+
args.project,
|
|
221
|
+
threshold=args.vad_threshold,
|
|
222
|
+
min_speech_duration_ms=args.vad_min_speech_ms,
|
|
223
|
+
min_silence_duration_ms=args.vad_min_silence_ms,
|
|
224
|
+
speech_pad_ms=args.vad_speech_pad_ms,
|
|
225
|
+
merge_speech_gap_seconds=args.vad_merge_gap,
|
|
226
|
+
)
|
|
227
|
+
print(f"VAD ranges: {output}")
|
|
228
|
+
return 0
|
|
229
|
+
|
|
230
|
+
|
|
231
|
+
def cmd_frames(args: argparse.Namespace) -> int:
|
|
232
|
+
output = generate_frames(
|
|
233
|
+
args.project,
|
|
234
|
+
interval_seconds=args.interval,
|
|
235
|
+
thumbnail_width=args.thumbnail_width,
|
|
236
|
+
overwrite=args.overwrite,
|
|
237
|
+
dry_run=args.dry_run,
|
|
238
|
+
)
|
|
239
|
+
print(f"Frames metadata: {output}")
|
|
240
|
+
return 0
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
def cmd_ui(args: argparse.Namespace) -> int:
|
|
244
|
+
from .ui_server import run_ui
|
|
245
|
+
|
|
246
|
+
run_ui(args.project, host=args.host, port=args.port, open_browser=not args.no_open)
|
|
247
|
+
return 0
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
def cmd_render(args: argparse.Namespace) -> int:
|
|
251
|
+
if args.contact_sheet:
|
|
252
|
+
if args.debug:
|
|
253
|
+
raise ValueError("Use either --debug or --contact-sheet, not both.")
|
|
254
|
+
output = render_contact_sheet(
|
|
255
|
+
args.project,
|
|
256
|
+
final_video=args.final_video,
|
|
257
|
+
output=args.output,
|
|
258
|
+
overwrite=args.overwrite,
|
|
259
|
+
dry_run=args.dry_run,
|
|
260
|
+
)
|
|
261
|
+
print(f"Contact sheet: {output}")
|
|
262
|
+
return 0
|
|
263
|
+
if args.final_video is not None:
|
|
264
|
+
raise ValueError("--final-video can only be used with --contact-sheet.")
|
|
265
|
+
|
|
266
|
+
output = render_project(
|
|
267
|
+
args.project,
|
|
268
|
+
debug=args.debug,
|
|
269
|
+
output=args.output,
|
|
270
|
+
overwrite=args.overwrite,
|
|
271
|
+
dry_run=args.dry_run,
|
|
272
|
+
)
|
|
273
|
+
print(f"Rendered video: {output}")
|
|
274
|
+
return 0
|
|
275
|
+
|
|
276
|
+
|
|
277
|
+
def cmd_validate(args: argparse.Namespace) -> int:
|
|
278
|
+
data = load_redactions(args.project)
|
|
279
|
+
validate_redaction_document(data)
|
|
280
|
+
redactions = render_redactions(data)
|
|
281
|
+
print(
|
|
282
|
+
f"OK: {len(redactions)} redaction(s) valid in {project_paths(args.project).redactions_json}"
|
|
283
|
+
)
|
|
284
|
+
return 0
|
|
285
|
+
|
|
286
|
+
|
|
287
|
+
def cmd_doctor(args: argparse.Namespace) -> int: # noqa: ARG001
|
|
288
|
+
ok = True
|
|
289
|
+
for tool in ["ffmpeg", "ffprobe"]:
|
|
290
|
+
try:
|
|
291
|
+
resolved = ensure_tool(tool)
|
|
292
|
+
print(f"OK: {tool} -> {resolved}")
|
|
293
|
+
except ToolError as exc:
|
|
294
|
+
ok = False
|
|
295
|
+
print(f"MISSING: {exc}")
|
|
296
|
+
return 0 if ok else 1
|
|
297
|
+
|
|
298
|
+
|
|
299
|
+
def cmd_trim_command(args: argparse.Namespace) -> int:
|
|
300
|
+
plan = plan_trim(
|
|
301
|
+
args.project,
|
|
302
|
+
options=_trim_options_from_args(args),
|
|
303
|
+
allow_vad_detection=False,
|
|
304
|
+
)
|
|
305
|
+
cmd = build_trim_command(plan)
|
|
306
|
+
print(" ".join(cmd))
|
|
307
|
+
return 0
|
|
308
|
+
|
|
309
|
+
|
|
310
|
+
def _trim_options_from_args(args: argparse.Namespace) -> TrimOptions:
|
|
311
|
+
return TrimOptions(
|
|
312
|
+
detector=args.detector,
|
|
313
|
+
mode=args.mode,
|
|
314
|
+
margin=args.margin,
|
|
315
|
+
smooth=args.smooth,
|
|
316
|
+
silent_speed=args.silent_speed,
|
|
317
|
+
mute_silent_audio=args.mute_silent_audio,
|
|
318
|
+
vad=VadOptions(
|
|
319
|
+
threshold=args.vad_threshold,
|
|
320
|
+
min_speech_duration_ms=args.vad_min_speech_ms,
|
|
321
|
+
min_silence_duration_ms=args.vad_min_silence_ms,
|
|
322
|
+
speech_pad_ms=args.vad_speech_pad_ms,
|
|
323
|
+
merge_speech_gap_seconds=args.vad_merge_gap,
|
|
324
|
+
),
|
|
325
|
+
speed_indicator=args.speed_indicator,
|
|
326
|
+
speed_indicator_corner=args.speed_indicator_corner,
|
|
327
|
+
speed_indicator_style=args.speed_indicator_style,
|
|
328
|
+
speed_indicator_min_display_seconds=args.speed_indicator_min_seconds,
|
|
329
|
+
)
|
|
330
|
+
|
|
331
|
+
|
|
332
|
+
def main(argv: list[str] | None = None) -> int:
|
|
333
|
+
parser = build_parser()
|
|
334
|
+
args = parser.parse_args(argv)
|
|
335
|
+
if not hasattr(args, "func"):
|
|
336
|
+
parser.print_help()
|
|
337
|
+
return 2
|
|
338
|
+
|
|
339
|
+
try:
|
|
340
|
+
return int(args.func(args))
|
|
341
|
+
except (FileNotFoundError, FileExistsError, VidedError, ValueError, ToolError) as exc:
|
|
342
|
+
print(f"error: {exc}", file=sys.stderr)
|
|
343
|
+
return 1
|
|
344
|
+
|
|
345
|
+
|
|
346
|
+
if __name__ == "__main__":
|
|
347
|
+
raise SystemExit(main())
|
vided/contact_sheet.py
ADDED
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import math
|
|
4
|
+
import shutil
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
from typing import Any
|
|
7
|
+
|
|
8
|
+
from PIL import Image, ImageDraw, ImageFont
|
|
9
|
+
|
|
10
|
+
from .ffmpeg import ensure_tool, probe_media, run_command
|
|
11
|
+
from .project import load_project, project_paths
|
|
12
|
+
from .redactions import Redaction, load_redactions, render_redactions
|
|
13
|
+
|
|
14
|
+
BACKGROUND = "#101218"
|
|
15
|
+
NORMAL_BORDER = "#303746"
|
|
16
|
+
REDACTION_BORDER = "#7db8ff"
|
|
17
|
+
CAPTION_TEXT = "#eef1f5"
|
|
18
|
+
_FONT_CANDIDATES = [
|
|
19
|
+
"Arial Unicode.ttf",
|
|
20
|
+
"DejaVuSans-Bold.ttf",
|
|
21
|
+
"DejaVuSans.ttf",
|
|
22
|
+
"NotoSansSymbols2-Regular.ttf",
|
|
23
|
+
"NotoSansSymbols-Regular.ttf",
|
|
24
|
+
"Segoe UI Symbol.ttf",
|
|
25
|
+
"Apple Symbols.ttf",
|
|
26
|
+
"Arial Bold.ttf",
|
|
27
|
+
"Arial.ttf",
|
|
28
|
+
"Helvetica.ttf",
|
|
29
|
+
]
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def render_contact_sheet(
|
|
33
|
+
project_root: Path,
|
|
34
|
+
*,
|
|
35
|
+
final_video: Path | None = None,
|
|
36
|
+
output: Path | None = None,
|
|
37
|
+
overwrite: bool = False,
|
|
38
|
+
dry_run: bool = False,
|
|
39
|
+
) -> Path:
|
|
40
|
+
cfg = load_project(project_root)
|
|
41
|
+
p = project_paths(project_root, config=cfg)
|
|
42
|
+
|
|
43
|
+
source = _resolve_final_video(p.root, p.output_dir, final_video)
|
|
44
|
+
if not source.exists():
|
|
45
|
+
raise FileNotFoundError(
|
|
46
|
+
f"Final video not found: {source}. Run `vided render {p.root}` first."
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
if output is None:
|
|
50
|
+
output = p.output_dir / "contact-sheet.jpg"
|
|
51
|
+
elif not output.is_absolute():
|
|
52
|
+
output = p.root / output
|
|
53
|
+
|
|
54
|
+
if output.exists() and not overwrite and not dry_run:
|
|
55
|
+
raise FileExistsError(
|
|
56
|
+
f"Contact sheet already exists: {output}. Use --overwrite to replace it."
|
|
57
|
+
)
|
|
58
|
+
|
|
59
|
+
frame_cfg: dict[str, Any] = cfg.get("frames", {})
|
|
60
|
+
interval_seconds = float(frame_cfg.get("interval_seconds", 1.0))
|
|
61
|
+
thumbnail_width = int(frame_cfg.get("thumbnail_width", 640))
|
|
62
|
+
if interval_seconds <= 0:
|
|
63
|
+
raise ValueError("frames.interval_seconds must be greater than 0")
|
|
64
|
+
if thumbnail_width <= 0:
|
|
65
|
+
raise ValueError("frames.thumbnail_width must be greater than 0")
|
|
66
|
+
|
|
67
|
+
ensure_tool("ffmpeg")
|
|
68
|
+
work_dir = p.work_dir / "contact-sheet"
|
|
69
|
+
frame_pattern = work_dir / "frame_%06d.jpg"
|
|
70
|
+
fps = 1.0 / interval_seconds
|
|
71
|
+
cmd = [
|
|
72
|
+
"ffmpeg",
|
|
73
|
+
"-hide_banner",
|
|
74
|
+
"-y",
|
|
75
|
+
"-i",
|
|
76
|
+
str(source),
|
|
77
|
+
"-vf",
|
|
78
|
+
f"fps={fps:.8f},scale={thumbnail_width}:-2",
|
|
79
|
+
"-q:v",
|
|
80
|
+
"2",
|
|
81
|
+
str(frame_pattern),
|
|
82
|
+
]
|
|
83
|
+
|
|
84
|
+
if not dry_run:
|
|
85
|
+
output.parent.mkdir(parents=True, exist_ok=True)
|
|
86
|
+
if work_dir.exists():
|
|
87
|
+
shutil.rmtree(work_dir)
|
|
88
|
+
work_dir.mkdir(parents=True, exist_ok=True)
|
|
89
|
+
|
|
90
|
+
run_command(cmd, dry_run=dry_run)
|
|
91
|
+
if dry_run:
|
|
92
|
+
return output
|
|
93
|
+
|
|
94
|
+
frames = sorted(work_dir.glob("frame_*.jpg"))
|
|
95
|
+
if not frames:
|
|
96
|
+
raise ValueError(f"No frames were extracted from final video: {source}")
|
|
97
|
+
|
|
98
|
+
info = probe_media(source)
|
|
99
|
+
redactions = render_redactions(load_redactions(project_root))
|
|
100
|
+
timestamps = [
|
|
101
|
+
min(index * interval_seconds, max(info.duration, 0.0)) for index in range(len(frames))
|
|
102
|
+
]
|
|
103
|
+
compose_contact_sheet(frames, timestamps=timestamps, redactions=redactions, output=output)
|
|
104
|
+
return output
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def compose_contact_sheet(
|
|
108
|
+
frame_paths: list[Path],
|
|
109
|
+
*,
|
|
110
|
+
timestamps: list[float],
|
|
111
|
+
redactions: list[Redaction],
|
|
112
|
+
output: Path,
|
|
113
|
+
) -> Path:
|
|
114
|
+
if len(frame_paths) != len(timestamps):
|
|
115
|
+
raise ValueError("frame_paths and timestamps must have the same length")
|
|
116
|
+
if not frame_paths:
|
|
117
|
+
raise ValueError("contact sheet needs at least one frame")
|
|
118
|
+
|
|
119
|
+
images = _load_images(frame_paths)
|
|
120
|
+
tile_width = max(image.width for image in images)
|
|
121
|
+
tile_height = max(image.height for image in images)
|
|
122
|
+
columns = min(4, len(images))
|
|
123
|
+
rows = math.ceil(len(images) / columns)
|
|
124
|
+
padding = 16
|
|
125
|
+
gap = 12
|
|
126
|
+
border = 6
|
|
127
|
+
cell_width = tile_width + border * 2
|
|
128
|
+
cell_height = tile_height + border * 2
|
|
129
|
+
width = padding * 2 + columns * cell_width + (columns - 1) * gap
|
|
130
|
+
height = padding * 2 + rows * cell_height + (rows - 1) * gap
|
|
131
|
+
|
|
132
|
+
sheet = Image.new("RGBA", (width, height), BACKGROUND)
|
|
133
|
+
draw = ImageDraw.Draw(sheet)
|
|
134
|
+
font = _load_contact_sheet_font(_timestamp_font_size(tile_width, tile_height))
|
|
135
|
+
|
|
136
|
+
for index, (image, timestamp) in enumerate(zip(images, timestamps, strict=True)):
|
|
137
|
+
row, column = divmod(index, columns)
|
|
138
|
+
x = padding + column * (cell_width + gap)
|
|
139
|
+
y = padding + row * (cell_height + gap)
|
|
140
|
+
image_x = x + border
|
|
141
|
+
image_y = y + border
|
|
142
|
+
redacted = frame_overlaps_redaction(timestamp, redactions)
|
|
143
|
+
border_color = REDACTION_BORDER if redacted else NORMAL_BORDER
|
|
144
|
+
border_width = border if redacted else 2
|
|
145
|
+
|
|
146
|
+
draw.rectangle(
|
|
147
|
+
[x, y, x + tile_width + border * 2 - 1, y + tile_height + border * 2 - 1],
|
|
148
|
+
outline=border_color,
|
|
149
|
+
width=border_width,
|
|
150
|
+
)
|
|
151
|
+
sheet.alpha_composite(image.convert("RGBA"), (image_x, image_y))
|
|
152
|
+
_draw_timestamp_chip(
|
|
153
|
+
sheet, (image_x, image_y), image.size, _format_seconds(timestamp), font
|
|
154
|
+
)
|
|
155
|
+
|
|
156
|
+
image_format = "PNG" if output.suffix.lower() == ".png" else "JPEG"
|
|
157
|
+
if image_format == "JPEG":
|
|
158
|
+
sheet.convert("RGB").save(output, format=image_format, quality=92)
|
|
159
|
+
else:
|
|
160
|
+
sheet.save(output, format=image_format)
|
|
161
|
+
return output
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
def frame_overlaps_redaction(timestamp: float, redactions: list[Redaction]) -> bool:
|
|
165
|
+
return any(redaction.start <= timestamp < redaction.end for redaction in redactions)
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
def _resolve_final_video(project_root: Path, output_dir: Path, final_video: Path | None) -> Path:
|
|
169
|
+
if final_video is None:
|
|
170
|
+
return output_dir / "final.mp4"
|
|
171
|
+
if final_video.is_absolute():
|
|
172
|
+
return final_video
|
|
173
|
+
return project_root / final_video
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
def _load_images(paths: list[Path]) -> list[Image.Image]:
|
|
177
|
+
images: list[Image.Image] = []
|
|
178
|
+
for path in paths:
|
|
179
|
+
with Image.open(path) as image:
|
|
180
|
+
images.append(image.convert("RGB"))
|
|
181
|
+
return images
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
def _load_contact_sheet_font(size: int) -> ImageFont.FreeTypeFont | ImageFont.ImageFont:
|
|
185
|
+
for candidate in _FONT_CANDIDATES:
|
|
186
|
+
try:
|
|
187
|
+
return ImageFont.truetype(candidate, size)
|
|
188
|
+
except OSError:
|
|
189
|
+
continue
|
|
190
|
+
return ImageFont.load_default(size=size)
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
def _timestamp_font_size(tile_width: int, tile_height: int) -> int:
|
|
194
|
+
short_edge = max(1, min(tile_width, tile_height))
|
|
195
|
+
return max(12, min(34, int(round(short_edge * 0.052))))
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
def _draw_timestamp_chip(
|
|
199
|
+
sheet: Image.Image,
|
|
200
|
+
image_origin: tuple[int, int],
|
|
201
|
+
image_size: tuple[int, int],
|
|
202
|
+
label: str,
|
|
203
|
+
font: ImageFont.FreeTypeFont | ImageFont.ImageFont,
|
|
204
|
+
) -> None:
|
|
205
|
+
measure = ImageDraw.Draw(Image.new("RGBA", (1, 1)))
|
|
206
|
+
font_size = max(12, int(getattr(font, "size", 16)))
|
|
207
|
+
stroke_width = max(1, round(font_size * 0.08))
|
|
208
|
+
padding_x = max(8, round(font_size * 0.55))
|
|
209
|
+
padding_y = max(5, round(font_size * 0.35))
|
|
210
|
+
left, top, right, bottom = measure.textbbox(
|
|
211
|
+
(0, 0),
|
|
212
|
+
label,
|
|
213
|
+
font=font,
|
|
214
|
+
stroke_width=stroke_width,
|
|
215
|
+
)
|
|
216
|
+
text_width = int(math.ceil(right - left))
|
|
217
|
+
text_height = int(math.ceil(bottom - top))
|
|
218
|
+
chip_width = text_width + padding_x * 2
|
|
219
|
+
chip_height = text_height + padding_y * 2
|
|
220
|
+
inset = max(8, round(min(image_size) * 0.025))
|
|
221
|
+
chip_x = image_origin[0] + inset
|
|
222
|
+
chip_y = image_origin[1] + image_size[1] - chip_height - inset
|
|
223
|
+
|
|
224
|
+
overlay = Image.new("RGBA", sheet.size, (0, 0, 0, 0))
|
|
225
|
+
draw = ImageDraw.Draw(overlay)
|
|
226
|
+
draw.rounded_rectangle(
|
|
227
|
+
(chip_x, chip_y, chip_x + chip_width - 1, chip_y + chip_height - 1),
|
|
228
|
+
radius=max(6, round(chip_height * 0.22)),
|
|
229
|
+
fill=(0, 0, 0, 166),
|
|
230
|
+
outline=(255, 255, 255, 80),
|
|
231
|
+
width=1,
|
|
232
|
+
)
|
|
233
|
+
text_x = chip_x + padding_x - left
|
|
234
|
+
text_y = chip_y + padding_y - top
|
|
235
|
+
draw.text(
|
|
236
|
+
(text_x, text_y),
|
|
237
|
+
label,
|
|
238
|
+
font=font,
|
|
239
|
+
fill=(255, 255, 255, 255),
|
|
240
|
+
stroke_width=stroke_width,
|
|
241
|
+
stroke_fill=(0, 0, 0, 220),
|
|
242
|
+
)
|
|
243
|
+
sheet.alpha_composite(overlay)
|
|
244
|
+
|
|
245
|
+
|
|
246
|
+
def _format_seconds(seconds: float) -> str:
|
|
247
|
+
seconds = max(0.0, seconds)
|
|
248
|
+
whole = int(seconds)
|
|
249
|
+
tenths = int(round((seconds - whole) * 10))
|
|
250
|
+
if tenths == 10:
|
|
251
|
+
whole += 1
|
|
252
|
+
tenths = 0
|
|
253
|
+
minutes, second = divmod(whole, 60)
|
|
254
|
+
hours, minute = divmod(minutes, 60)
|
|
255
|
+
if hours:
|
|
256
|
+
return f"{hours:d}:{minute:02d}:{second:02d}.{tenths:d}"
|
|
257
|
+
return f"{minute:02d}:{second:02d}.{tenths:d}"
|
vided/data/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""Package data for vided."""
|
|
Binary file
|