@clipen/clipen-cli 0.0.0-stage → 1.0.0
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/LICENSE +21 -0
- package/README.md +79 -2
- package/apps/web/dist/assets/index-D5D2HiB1.js +14 -0
- package/apps/web/dist/assets/index-DIMuOXSu.css +1 -0
- package/apps/web/dist/favicon.svg +1 -0
- package/apps/web/dist/index.html +16 -0
- package/bin/clipen.js +157 -0
- package/clipen +18 -0
- package/install +25 -0
- package/package.json +52 -4
- package/pyproject.toml +28 -0
- package/requirements/asr-runtime.txt +13 -0
- package/requirements/runtime.txt +20 -0
- package/src/clipen/__init__.py +3 -0
- package/src/clipen/analysis/__init__.py +89 -0
- package/src/clipen/analysis/artifacts.py +416 -0
- package/src/clipen/analysis/audio.py +392 -0
- package/src/clipen/analysis/evidence.py +102 -0
- package/src/clipen/analysis/evidence_service.py +751 -0
- package/src/clipen/analysis/image_similarity.py +143 -0
- package/src/clipen/analysis/keyframes.py +171 -0
- package/src/clipen/analysis/models.py +230 -0
- package/src/clipen/analysis/qwen_worker.py +629 -0
- package/src/clipen/analysis/resources.py +21 -0
- package/src/clipen/analysis/runtime.py +64 -0
- package/src/clipen/analysis/service.py +626 -0
- package/src/clipen/analysis/setup.py +935 -0
- package/src/clipen/analysis/setup_cli.py +117 -0
- package/src/clipen/analysis/storage.py +2046 -0
- package/src/clipen/analysis/transcription.py +948 -0
- package/src/clipen/analysis/visual.py +176 -0
- package/src/clipen/annotations/__init__.py +10 -0
- package/src/clipen/annotations/models.py +22 -0
- package/src/clipen/annotations/storage.py +380 -0
- package/src/clipen/annotations/suggestions.py +120 -0
- package/src/clipen/asr_release.py +139 -0
- package/src/clipen/bootstrap.py +967 -0
- package/src/clipen/cli.py +84 -0
- package/src/clipen/controller/__init__.py +17 -0
- package/src/clipen/controller/app.py +1470 -0
- package/src/clipen/controller/server.py +543 -0
- package/src/clipen/domain/__init__.py +30 -0
- package/src/clipen/domain/errors.py +5 -0
- package/src/clipen/domain/media_time.py +117 -0
- package/src/clipen/domain/timeline.py +584 -0
- package/src/clipen/domain/validation.py +15 -0
- package/src/clipen/editing/EDITORIAL_REGRESSIONS.md +38 -0
- package/src/clipen/editing/PROGRAM_FORMAT.md +102 -0
- package/src/clipen/editing/README.md +115 -0
- package/src/clipen/editing/REFACTOR_DESIGN.md +241 -0
- package/src/clipen/editing/REFACTOR_HANDOFF.md +73 -0
- package/src/clipen/editing/__init__.py +15 -0
- package/src/clipen/editing/agent.py +13 -0
- package/src/clipen/editing/agent_models.py +134 -0
- package/src/clipen/editing/agent_repository.py +757 -0
- package/src/clipen/editing/agent_tools.py +532 -0
- package/src/clipen/editing/alignment.py +110 -0
- package/src/clipen/editing/alignment_setup.py +108 -0
- package/src/clipen/editing/alignment_worker.py +22 -0
- package/src/clipen/editing/change_report.py +71 -0
- package/src/clipen/editing/compiler.py +41 -0
- package/src/clipen/editing/component_looks.py +107 -0
- package/src/clipen/editing/component_review.py +78 -0
- package/src/clipen/editing/coordinator.py +374 -0
- package/src/clipen/editing/editorial_review.py +187 -0
- package/src/clipen/editing/evidence_jobs.py +295 -0
- package/src/clipen/editing/history_changes.py +131 -0
- package/src/clipen/editing/music_choices.py +15 -0
- package/src/clipen/editing/production_catalog.py +192 -0
- package/src/clipen/editing/production_limits.py +59 -0
- package/src/clipen/editing/production_media.py +226 -0
- package/src/clipen/editing/production_preview.py +180 -0
- package/src/clipen/editing/production_progress.py +141 -0
- package/src/clipen/editing/production_requirements.py +50 -0
- package/src/clipen/editing/production_tools.py +596 -0
- package/src/clipen/editing/program_audio.py +118 -0
- package/src/clipen/editing/program_direction.py +49 -0
- package/src/clipen/editing/program_story.py +53 -0
- package/src/clipen/editing/program_subtitles.py +134 -0
- package/src/clipen/editing/programme_review.py +73 -0
- package/src/clipen/editing/prompt_history.py +47 -0
- package/src/clipen/editing/recut.py +127 -0
- package/src/clipen/editing/refinement_references.py +188 -0
- package/src/clipen/editing/resolve_export.py +616 -0
- package/src/clipen/editing/resolve_titles.py +234 -0
- package/src/clipen/editing/skill_tools.py +575 -0
- package/src/clipen/editing/source_dialogue.py +82 -0
- package/src/clipen/editing/speech_coverage.py +30 -0
- package/src/clipen/editing/speech_quality.py +6 -0
- package/src/clipen/editing/tool_receipts.py +52 -0
- package/src/clipen/editing/versions.py +161 -0
- package/src/clipen/editing/visual_preview.py +93 -0
- package/src/clipen/editing/workbench.py +74 -0
- package/src/clipen/filesystem.py +25 -0
- package/src/clipen/finishing/README.md +103 -0
- package/src/clipen/finishing/__init__.py +1 -0
- package/src/clipen/finishing/audio.py +149 -0
- package/src/clipen/finishing/catalog.py +181 -0
- package/src/clipen/finishing/models.py +14 -0
- package/src/clipen/finishing/renderer.py +26 -0
- package/src/clipen/mcp_editing/__init__.py +5 -0
- package/src/clipen/mcp_editing/__main__.py +40 -0
- package/src/clipen/mcp_editing/server.py +353 -0
- package/src/clipen/media/__init__.py +50 -0
- package/src/clipen/media/discovery.py +266 -0
- package/src/clipen/media/models.py +360 -0
- package/src/clipen/media/preview.py +147 -0
- package/src/clipen/media/probe.py +446 -0
- package/src/clipen/media/service.py +317 -0
- package/src/clipen/media/storage.py +742 -0
- package/src/clipen/music_vocabulary.py +5 -0
- package/src/clipen/planning/__init__.py +9 -0
- package/src/clipen/planning/models.py +240 -0
- package/src/clipen/power.py +23 -0
- package/src/clipen/projects/__init__.py +26 -0
- package/src/clipen/projects/agent_schema.py +94 -0
- package/src/clipen/projects/annotation_schema.py +40 -0
- package/src/clipen/projects/deletion.py +269 -0
- package/src/clipen/projects/evidence_schema.py +52 -0
- package/src/clipen/projects/finishing_schema.py +34 -0
- package/src/clipen/projects/models.py +192 -0
- package/src/clipen/projects/picker.py +256 -0
- package/src/clipen/projects/preview_schema.py +29 -0
- package/src/clipen/projects/service.py +175 -0
- package/src/clipen/projects/storage.py +1478 -0
- package/src/clipen/projects/timeline_schema.py +32 -0
- package/src/clipen/projects/validation_schema.py +28 -0
- package/src/clipen/provider_registry.py +75 -0
- package/src/clipen/providers/__init__.py +43 -0
- package/src/clipen/providers/account.py +194 -0
- package/src/clipen/providers/claude.py +375 -0
- package/src/clipen/providers/claude_editing.py +111 -0
- package/src/clipen/providers/codex.py +1036 -0
- package/src/clipen/providers/codex_editing.py +91 -0
- package/src/clipen/providers/editing_agent.py +565 -0
- package/src/clipen/providers/errors.py +51 -0
- package/src/clipen/providers/recovery.py +270 -0
- package/src/clipen/py.typed +1 -0
- package/src/clipen/rendering/__init__.py +12 -0
- package/src/clipen/rendering/cover.py +126 -0
- package/src/clipen/rendering/custom_components.py +240 -0
- package/src/clipen/rendering/custom_render.py +197 -0
- package/src/clipen/rendering/fonts/anton-OFL.txt +93 -0
- package/src/clipen/rendering/fonts/bebasneue-OFL.txt +93 -0
- package/src/clipen/rendering/fonts/caveat-OFL.txt +93 -0
- package/src/clipen/rendering/fonts/chenyu-OFL.txt +93 -0
- package/src/clipen/rendering/fonts/delagothicone-OFL.txt +93 -0
- package/src/clipen/rendering/fonts/hachimarupop-OFL.txt +93 -0
- package/src/clipen/rendering/fonts/iansui-OFL.txt +93 -0
- package/src/clipen/rendering/fonts/manifest.json +145 -0
- package/src/clipen/rendering/fonts/playfairdisplay-OFL.txt +93 -0
- package/src/clipen/rendering/fonts/rocknrollone-OFL.txt +93 -0
- package/src/clipen/rendering/fonts/yomogi-OFL.txt +94 -0
- package/src/clipen/rendering/fonts/yuseimagic-OFL.txt +93 -0
- package/src/clipen/rendering/fonts/zenkurenaido-OFL.txt +93 -0
- package/src/clipen/rendering/fonts/zenmarugothic-OFL.txt +93 -0
- package/src/clipen/rendering/production_effects.py +64 -0
- package/src/clipen/rendering/program_components.py +193 -0
- package/src/clipen/rendering/program_creator.py +649 -0
- package/src/clipen/rendering/program_identities.py +255 -0
- package/src/clipen/rendering/program_motion.py +141 -0
- package/src/clipen/rendering/program_studio.py +168 -0
- package/src/clipen/rendering/program_styles.py +235 -0
- package/src/clipen/rendering/program_subtitles.py +52 -0
- package/src/clipen/rendering/renderer.py +708 -0
- package/src/clipen/rendering/storage.py +243 -0
- package/src/clipen/rendering/titles.py +360 -0
- package/src/clipen/skills/__init__.py +4 -0
- package/src/clipen/skills/builtin/SKILL.md +72 -0
- package/src/clipen/skills/builtin/assets/sample-daily-vlog.mp4 +0 -0
- package/src/clipen/skills/builtin/assets/sample-event-recap.mp4 +0 -0
- package/src/clipen/skills/builtin/assets/sample-food-review.mp4 +0 -0
- package/src/clipen/skills/builtin/assets/sample-product-demo.mp4 +0 -0
- package/src/clipen/skills/builtin/assets/sample-product-review.mp4 +0 -0
- package/src/clipen/skills/builtin/assets/sample-product-unboxing.mp4 +0 -0
- package/src/clipen/skills/builtin/assets/sample-shorts-highlight.mp4 +0 -0
- package/src/clipen/skills/builtin/assets/sample-travel-highlight.mp4 +0 -0
- package/src/clipen/skills/builtin/assets/sample-travel-vlog.mp4 +0 -0
- package/src/clipen/skills/builtin/assets/style-cinema.png +0 -0
- package/src/clipen/skills/builtin/assets/style-editorial.png +0 -0
- package/src/clipen/skills/builtin/assets/style-tpl-daily-vlog.png +0 -0
- package/src/clipen/skills/builtin/assets/style-tpl-event-recap.png +0 -0
- package/src/clipen/skills/builtin/assets/style-tpl-food-review.png +0 -0
- package/src/clipen/skills/builtin/assets/style-tpl-product-demo.png +0 -0
- package/src/clipen/skills/builtin/assets/style-tpl-product-review.png +0 -0
- package/src/clipen/skills/builtin/assets/style-tpl-product-unboxing.png +0 -0
- package/src/clipen/skills/builtin/assets/style-tpl-shorts-highlight.png +0 -0
- package/src/clipen/skills/builtin/assets/style-tpl-travel-highlight.png +0 -0
- package/src/clipen/skills/builtin/assets/style-tpl-travel-vlog.png +0 -0
- package/src/clipen/skills/builtin/assets/style-variety.png +0 -0
- package/src/clipen/skills/builtin/components.json +133 -0
- package/src/clipen/skills/builtin/font_sources.json +20 -0
- package/src/clipen/skills/builtin/font_styles.json +329 -0
- package/src/clipen/skills/builtin/manifest.json +32 -0
- package/src/clipen/skills/builtin/presets.json +8 -0
- package/src/clipen/skills/builtin/references/captions.md +14 -0
- package/src/clipen/skills/builtin/references/channel-design.md +37 -0
- package/src/clipen/skills/builtin/references/editing.md +30 -0
- package/src/clipen/skills/builtin/references/font-chenyu-OFL.txt +93 -0
- package/src/clipen/skills/builtin/references/font-iansui-OFL.txt +93 -0
- package/src/clipen/skills/builtin/references/graphics.md +59 -0
- package/src/clipen/skills/builtin/references/review.md +16 -0
- package/src/clipen/skills/builtin/references/sound.md +8 -0
- package/src/clipen/skills/builtin/references/story.md +7 -0
- package/src/clipen/skills/builtin/skills/travel-program/SKILL.md +27 -0
- package/src/clipen/skills/builtin/skills/travel-program/manifest.json +6 -0
- package/src/clipen/skills/builtin/template-skills/clipen-daily-vlog/SKILL.md +33 -0
- package/src/clipen/skills/builtin/template-skills/clipen-daily-vlog/manifest.json +8 -0
- package/src/clipen/skills/builtin/template-skills/clipen-event-recap/SKILL.md +34 -0
- package/src/clipen/skills/builtin/template-skills/clipen-event-recap/manifest.json +8 -0
- package/src/clipen/skills/builtin/template-skills/clipen-food-review/SKILL.md +33 -0
- package/src/clipen/skills/builtin/template-skills/clipen-food-review/manifest.json +8 -0
- package/src/clipen/skills/builtin/template-skills/clipen-product-demo/SKILL.md +35 -0
- package/src/clipen/skills/builtin/template-skills/clipen-product-demo/manifest.json +8 -0
- package/src/clipen/skills/builtin/template-skills/clipen-product-review/SKILL.md +36 -0
- package/src/clipen/skills/builtin/template-skills/clipen-product-review/manifest.json +8 -0
- package/src/clipen/skills/builtin/template-skills/clipen-product-unboxing/SKILL.md +39 -0
- package/src/clipen/skills/builtin/template-skills/clipen-product-unboxing/manifest.json +8 -0
- package/src/clipen/skills/builtin/template-skills/clipen-shorts-highlight/SKILL.md +38 -0
- package/src/clipen/skills/builtin/template-skills/clipen-shorts-highlight/manifest.json +8 -0
- package/src/clipen/skills/builtin/template-skills/clipen-travel-highlight/SKILL.md +34 -0
- package/src/clipen/skills/builtin/template-skills/clipen-travel-highlight/manifest.json +8 -0
- package/src/clipen/skills/builtin/template-skills/clipen-travel-vlog/SKILL.md +37 -0
- package/src/clipen/skills/builtin/template-skills/clipen-travel-vlog/manifest.json +8 -0
- package/src/clipen/skills/builtin/templates.json +284 -0
- package/src/clipen/skills/builtin/visual_styles.json +1074 -0
- package/src/clipen/skills/catalog.py +218 -0
- package/src/clipen/skills/library.py +131 -0
- package/src/clipen/skills/package.py +231 -0
- package/src/clipen/skills/visual_styles.py +44 -0
- package/src/clipen/workspace.py +210 -0
- package/uninstall +25 -0
|
@@ -0,0 +1,708 @@
|
|
|
1
|
+
"""FFmpeg renderer for sequential rough-cut preview timelines."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from decimal import Decimal
|
|
6
|
+
import os
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
import subprocess
|
|
9
|
+
import tempfile
|
|
10
|
+
import threading
|
|
11
|
+
from typing import Callable
|
|
12
|
+
|
|
13
|
+
from clipen.domain import EditTimeline, EffectRequest, MediaClip, Track, TrackKind
|
|
14
|
+
from clipen.media import MediaPreviewRepository
|
|
15
|
+
from clipen.projects import ProjectError
|
|
16
|
+
|
|
17
|
+
from .titles import FfmpegTitleCompositor, TitleCardRenderer, TitleRenderError
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class PreviewRenderError(RuntimeError):
|
|
21
|
+
def __init__(self, code: str) -> None:
|
|
22
|
+
super().__init__(code)
|
|
23
|
+
self.code = code
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class FfmpegPreviewRenderer:
|
|
27
|
+
"""Render immutable media plus trusted, bounded presentation effects."""
|
|
28
|
+
|
|
29
|
+
_BATCH_SIZE = 8
|
|
30
|
+
|
|
31
|
+
def __init__(
|
|
32
|
+
self,
|
|
33
|
+
media: MediaPreviewRepository,
|
|
34
|
+
executable: str = "ffmpeg",
|
|
35
|
+
*,
|
|
36
|
+
popen: Callable[..., subprocess.Popen[str]] = subprocess.Popen,
|
|
37
|
+
hardware_acceleration: bool = False,
|
|
38
|
+
master: bool = False,
|
|
39
|
+
) -> None:
|
|
40
|
+
self.media = media
|
|
41
|
+
self.executable = executable
|
|
42
|
+
self._popen = popen
|
|
43
|
+
self.hardware_acceleration = hardware_acceleration
|
|
44
|
+
# Master output feeds another editor: ProRes and PCM instead of a compact preview.
|
|
45
|
+
self.master = master
|
|
46
|
+
|
|
47
|
+
def render(self,project_id,timeline,output,cancel,progress):
|
|
48
|
+
layers=any(e.effect_id=='audio.layers' for t in timeline.tracks for i in t.items if isinstance(i,MediaClip) for e in i.effects)
|
|
49
|
+
if not layers:
|
|
50
|
+
return self._render_base(project_id,timeline,output,cancel,progress)
|
|
51
|
+
from clipen.editing.production_media import mix_audio_layers
|
|
52
|
+
output.parent.mkdir(mode=0o700,parents=True,exist_ok=True)
|
|
53
|
+
with tempfile.TemporaryDirectory(prefix='.agent-mix-',dir=output.parent) as temp:
|
|
54
|
+
base=Path(temp)/'camera.mp4'
|
|
55
|
+
self._render_base(project_id,timeline,base,cancel,lambda p:progress(p*.85))
|
|
56
|
+
mix_audio_layers(self.media,project_id,timeline,base,output,cancel)
|
|
57
|
+
progress(1.)
|
|
58
|
+
|
|
59
|
+
def _render_base(
|
|
60
|
+
self,
|
|
61
|
+
project_id: str,
|
|
62
|
+
timeline: EditTimeline,
|
|
63
|
+
output: Path,
|
|
64
|
+
cancel: threading.Event,
|
|
65
|
+
progress: Callable[[float], None],
|
|
66
|
+
) -> None:
|
|
67
|
+
title_track = next(
|
|
68
|
+
(track for track in timeline.tracks if track.kind is TrackKind.TITLE), None
|
|
69
|
+
)
|
|
70
|
+
if not any(t.items for t in timeline.tracks if t.kind is TrackKind.TITLE):
|
|
71
|
+
self._render_media(project_id, timeline, output, cancel, progress)
|
|
72
|
+
return
|
|
73
|
+
output.parent.mkdir(mode=0o700, parents=True, exist_ok=True)
|
|
74
|
+
try:
|
|
75
|
+
with tempfile.TemporaryDirectory(
|
|
76
|
+
prefix=".clipen-title-", dir=output.parent
|
|
77
|
+
) as temporary:
|
|
78
|
+
directory = Path(temporary)
|
|
79
|
+
media_output = self._media_without_titles(
|
|
80
|
+
project_id, timeline, directory, cancel, lambda value: progress(value * 0.82)
|
|
81
|
+
)
|
|
82
|
+
# A reused cut leaves only the text layer, so that layer is the whole of the progress.
|
|
83
|
+
share = 0. if media_output.parent != directory else 0.82
|
|
84
|
+
layer = lambda value: progress(share + value * (1 - share))
|
|
85
|
+
from clipen.editing.production_media import load_text_styles
|
|
86
|
+
styles=load_text_styles(getattr(self.media,'projects',None),project_id)
|
|
87
|
+
if sum(len(t.items) for t in timeline.tracks if t.kind is TrackKind.TITLE)>32 or any(styles.get(i.style_id,{}).get('kind') in {'program_subtitle','program_component'} for t in timeline.tracks if t.kind is TrackKind.TITLE for i in t.items):
|
|
88
|
+
from .titles import compose_dense_titles
|
|
89
|
+
compose_dense_titles(self.executable,media_output,timeline,directory,output,cancel,styles,
|
|
90
|
+
progress=layer,hardware=self.hardware_acceleration and not self.master)
|
|
91
|
+
progress(1.)
|
|
92
|
+
return
|
|
93
|
+
titles = TitleCardRenderer(styles).render(timeline, directory)
|
|
94
|
+
FfmpegTitleCompositor(self.executable, popen=self._popen).compose(
|
|
95
|
+
media_output,
|
|
96
|
+
titles,
|
|
97
|
+
output,
|
|
98
|
+
cancel,
|
|
99
|
+
float(self._timeline_seconds(timeline)),
|
|
100
|
+
layer,
|
|
101
|
+
)
|
|
102
|
+
except TitleRenderError as error:
|
|
103
|
+
raise PreviewRenderError(error.code) from error
|
|
104
|
+
|
|
105
|
+
_MEDIA_CACHE_KEEP = 2
|
|
106
|
+
|
|
107
|
+
def _media_without_titles(self, project_id, timeline, directory, cancel, progress) -> Path:
|
|
108
|
+
"""The cut with its effects and sound, before any text is laid over it.
|
|
109
|
+
|
|
110
|
+
Rewording, moving or restyling graphics and subtitles leaves this unchanged, so the
|
|
111
|
+
last few are kept and such an edit only redoes the text layer."""
|
|
112
|
+
projects = getattr(self.media, "projects", None)
|
|
113
|
+
if projects is None:
|
|
114
|
+
output = directory / "media.mp4"
|
|
115
|
+
self._render_media(project_id, timeline, output, cancel, progress)
|
|
116
|
+
return output
|
|
117
|
+
import json
|
|
118
|
+
import hashlib
|
|
119
|
+
from dataclasses import replace
|
|
120
|
+
picture = replace(timeline, tracks=tuple(t for t in timeline.tracks if t.kind is not TrackKind.TITLE), markers=()).to_payload()
|
|
121
|
+
picture.pop("timeline_id", None)
|
|
122
|
+
key = hashlib.sha256(json.dumps({"version": 1, "picture": picture, "master": self.master},
|
|
123
|
+
sort_keys=True, ensure_ascii=False).encode()).hexdigest()[:32]
|
|
124
|
+
folder = projects.root / project_id / "previews" / "media-cache"
|
|
125
|
+
cached = folder / f"{key}.mp4"
|
|
126
|
+
if cached.is_file():
|
|
127
|
+
os.utime(cached)
|
|
128
|
+
return cached
|
|
129
|
+
output = directory / "media.mp4"
|
|
130
|
+
self._render_media(project_id, timeline, output, cancel, progress)
|
|
131
|
+
try:
|
|
132
|
+
folder.mkdir(mode=0o700, parents=True, exist_ok=True)
|
|
133
|
+
partial = folder / f".{key}.part"
|
|
134
|
+
import shutil
|
|
135
|
+
shutil.copyfile(output, partial)
|
|
136
|
+
os.replace(partial, cached)
|
|
137
|
+
kept = sorted(folder.glob("*.mp4"), key=lambda path: path.stat().st_mtime, reverse=True)
|
|
138
|
+
for stale in kept[self._MEDIA_CACHE_KEEP:]:
|
|
139
|
+
stale.unlink(missing_ok=True)
|
|
140
|
+
except OSError:
|
|
141
|
+
pass # A cache that cannot be written only means the next edit renders in full.
|
|
142
|
+
return output
|
|
143
|
+
|
|
144
|
+
def _render_media(self, project_id, timeline, output, cancel, progress, *, preserve_audio_gain=False):
|
|
145
|
+
try:
|
|
146
|
+
return self._render_media_once(project_id, timeline, output, cancel, progress, preserve_audio_gain=preserve_audio_gain)
|
|
147
|
+
except PreviewRenderError as error:
|
|
148
|
+
if not self.hardware_acceleration or error.code != 'ffmpeg_render_failed' or cancel.is_set():
|
|
149
|
+
raise
|
|
150
|
+
# Unsupported input/encoder on this machine: retry with the original
|
|
151
|
+
# software path, retaining all filters, source ranges and timing.
|
|
152
|
+
self.hardware_acceleration = False
|
|
153
|
+
return self._render_media_once(project_id, timeline, output, cancel, progress, preserve_audio_gain=preserve_audio_gain)
|
|
154
|
+
|
|
155
|
+
def _render_media_once(
|
|
156
|
+
self,
|
|
157
|
+
project_id: str,
|
|
158
|
+
timeline: EditTimeline,
|
|
159
|
+
output: Path,
|
|
160
|
+
cancel: threading.Event,
|
|
161
|
+
progress: Callable[[float], None],
|
|
162
|
+
*, preserve_audio_gain: bool = False,
|
|
163
|
+
) -> None:
|
|
164
|
+
if os.name == "nt":
|
|
165
|
+
raise PreviewRenderError("preview_platform_unsupported")
|
|
166
|
+
video = next(
|
|
167
|
+
(track for track in timeline.tracks if track.kind is TrackKind.VIDEO), None
|
|
168
|
+
)
|
|
169
|
+
audio = next(
|
|
170
|
+
(track for track in timeline.tracks if track.kind is TrackKind.AUDIO and track.items), None
|
|
171
|
+
)
|
|
172
|
+
if video is None or not video.items:
|
|
173
|
+
raise PreviewRenderError("preview_video_required")
|
|
174
|
+
if cancel.is_set():
|
|
175
|
+
raise PreviewRenderError("cancelled")
|
|
176
|
+
output.parent.mkdir(mode=0o700, parents=True, exist_ok=True)
|
|
177
|
+
controlled_audio = preserve_audio_gain or timeline.production_session_id is not None or any(
|
|
178
|
+
effect.effect_id in {'audio.source', 'audio.layers'}
|
|
179
|
+
for track in timeline.tracks for item in track.items
|
|
180
|
+
if isinstance(item, MediaClip) for effect in item.effects
|
|
181
|
+
)
|
|
182
|
+
if audio is None and len(video.items) > self._BATCH_SIZE:
|
|
183
|
+
self._render_batched(project_id, timeline, video, output, cancel, progress, preserve_audio_gain=controlled_audio)
|
|
184
|
+
return
|
|
185
|
+
|
|
186
|
+
opened = []
|
|
187
|
+
try:
|
|
188
|
+
command = [
|
|
189
|
+
self.executable,
|
|
190
|
+
"-hide_banner",
|
|
191
|
+
"-loglevel",
|
|
192
|
+
"error",
|
|
193
|
+
"-filter_complex_threads",
|
|
194
|
+
"2",
|
|
195
|
+
"-y",
|
|
196
|
+
]
|
|
197
|
+
filters: list[str] = []
|
|
198
|
+
video_labels: list[str] = []
|
|
199
|
+
audio_labels: list[str] = []
|
|
200
|
+
video_inputs = []
|
|
201
|
+
|
|
202
|
+
for clip_index,item in enumerate(video.items):
|
|
203
|
+
assert isinstance(item, MediaClip)
|
|
204
|
+
input_index, duration, source = self._add_input(
|
|
205
|
+
command, opened, project_id, item
|
|
206
|
+
)
|
|
207
|
+
video_inputs.append((input_index, duration, source, item))
|
|
208
|
+
stream = self._stream_index(
|
|
209
|
+
item.source.span.stream_index,
|
|
210
|
+
source.video_stream_indices,
|
|
211
|
+
"preview_video_stream_missing",
|
|
212
|
+
)
|
|
213
|
+
label = f"v{len(video_labels)}"
|
|
214
|
+
base_label = f"vbase{len(video_labels)}"
|
|
215
|
+
filters.append(
|
|
216
|
+
f"[{input_index}:{stream}]trim=duration={duration},setpts=PTS-STARTPTS,"
|
|
217
|
+
f"scale={timeline.width}:{timeline.height}:"
|
|
218
|
+
"force_original_aspect_ratio=decrease,"
|
|
219
|
+
f"pad={timeline.width}:{timeline.height}:(ow-iw)/2:(oh-ih)/2,"
|
|
220
|
+
f"fps={timeline.frame_rate.numerator}/"
|
|
221
|
+
f"{timeline.frame_rate.denominator}[{base_label}]"
|
|
222
|
+
)
|
|
223
|
+
current_label = self._retime_video(
|
|
224
|
+
filters, base_label, item, float(duration), len(video_labels)
|
|
225
|
+
)
|
|
226
|
+
effect_filters = self._video_effect_filters(item, timeline)
|
|
227
|
+
chain = f"[{current_label}]"
|
|
228
|
+
if effect_filters:
|
|
229
|
+
chain += effect_filters.removeprefix(",") + ","
|
|
230
|
+
from .production_effects import grade_filters, mosaic_filters, transition_filters
|
|
231
|
+
chain += 'null'+grade_filters(item)+",format=yuv420p"
|
|
232
|
+
styled_label=f'vstyled{clip_index}'
|
|
233
|
+
filters.append(f"{chain}[{styled_label}]")
|
|
234
|
+
masked=mosaic_filters(filters,styled_label,item,timeline,clip_index)
|
|
235
|
+
following=video.items[clip_index+1] if clip_index+1<len(video.items) else None
|
|
236
|
+
# Source PTS and fps rounding can otherwise add a frame at a
|
|
237
|
+
# cut and show the previous shot behind the next caption.
|
|
238
|
+
frame_pad=Decimal(2)*timeline.frame_rate.denominator/timeline.frame_rate.numerator
|
|
239
|
+
filters.append(f"[{masked}]null{transition_filters(item,following,timeline)},tpad=stop_mode=clone:stop_duration={frame_pad},trim=end_frame={item.duration_frames},setpts=N*{timeline.frame_rate.denominator}/({timeline.frame_rate.numerator}*TB)[{label}]")
|
|
240
|
+
video_labels.append(f"[{label}]")
|
|
241
|
+
|
|
242
|
+
if audio is not None:
|
|
243
|
+
for item in audio.items:
|
|
244
|
+
assert isinstance(item, MediaClip)
|
|
245
|
+
input_index, duration, source = self._add_input(
|
|
246
|
+
command, opened, project_id, item
|
|
247
|
+
)
|
|
248
|
+
stream = self._stream_index(
|
|
249
|
+
item.source.span.stream_index,
|
|
250
|
+
source.audio_stream_indices,
|
|
251
|
+
"preview_audio_stream_missing",
|
|
252
|
+
)
|
|
253
|
+
label = f"a{len(audio_labels)}"
|
|
254
|
+
filters.append(
|
|
255
|
+
f"[{input_index}:{stream}]atrim=duration={duration},"
|
|
256
|
+
"asetpts=PTS-STARTPTS,aresample=48000,"
|
|
257
|
+
f"aformat=sample_rates=48000:channel_layouts=stereo,apad,atrim=duration={Decimal(item.duration_frames)*timeline.frame_rate.denominator/timeline.frame_rate.numerator}[{label}]"
|
|
258
|
+
)
|
|
259
|
+
audio_labels.append(f"[{label}]")
|
|
260
|
+
else:
|
|
261
|
+
# A compiled rough cut may not yet have an editable audio track.
|
|
262
|
+
# Keep the camera audio aligned with every video clip so the user
|
|
263
|
+
# can still judge the cut. Silent source clips get matching silence.
|
|
264
|
+
for input_index, duration, source, item in video_inputs:
|
|
265
|
+
label = f"a{len(audio_labels)}"
|
|
266
|
+
if source.audio_stream_indices:
|
|
267
|
+
stream = source.audio_stream_indices[0]
|
|
268
|
+
base_label = f"abase{len(audio_labels)}"
|
|
269
|
+
filters.append(
|
|
270
|
+
f"[{input_index}:{stream}]atrim=duration={duration},"
|
|
271
|
+
"asetpts=PTS-STARTPTS,aresample=48000,"
|
|
272
|
+
f"aformat=sample_rates=48000:channel_layouts=stereo[{base_label}]"
|
|
273
|
+
)
|
|
274
|
+
current_label = self._retime_audio(
|
|
275
|
+
filters, base_label, item, float(duration), len(audio_labels)
|
|
276
|
+
)
|
|
277
|
+
from .production_effects import camera_audio_filters
|
|
278
|
+
filters.append(f"[{current_label}]anull{camera_audio_filters(item)},apad,atrim=duration={Decimal(item.duration_frames)*timeline.frame_rate.denominator/timeline.frame_rate.numerator},asetpts=PTS-STARTPTS[{label}]")
|
|
279
|
+
else:
|
|
280
|
+
filters.append(
|
|
281
|
+
"anullsrc=r=48000:cl=stereo,"
|
|
282
|
+
f"atrim=duration={Decimal(item.duration_frames)*timeline.frame_rate.denominator/timeline.frame_rate.numerator:.6f}[{label}]"
|
|
283
|
+
)
|
|
284
|
+
audio_labels.append(f"[{label}]")
|
|
285
|
+
|
|
286
|
+
filters.append(
|
|
287
|
+
f"{''.join(video_labels)}concat=n={len(video_labels)}:v=1:a=0[vout]"
|
|
288
|
+
)
|
|
289
|
+
timeline_duration = self._timeline_seconds(timeline)
|
|
290
|
+
if audio_labels:
|
|
291
|
+
filters.append(
|
|
292
|
+
f"{''.join(audio_labels)}concat=n={len(audio_labels)}:v=0:a=1[asequence]"
|
|
293
|
+
)
|
|
294
|
+
# Programme camera sound is a stem: automatic loudness makeup
|
|
295
|
+
# here would undo explicit attenuation before the music mix.
|
|
296
|
+
finishing = 'alimiter=limit=0.841395:level=0' if controlled_audio else 'loudnorm=I=-16:LRA=11:TP=-1.5'
|
|
297
|
+
filters.append(
|
|
298
|
+
f"[asequence]apad,atrim=duration={timeline_duration},{finishing}[aout]"
|
|
299
|
+
)
|
|
300
|
+
else:
|
|
301
|
+
filters.append(
|
|
302
|
+
f"anullsrc=r=48000:cl=stereo,atrim=duration={timeline_duration}[aout]"
|
|
303
|
+
)
|
|
304
|
+
|
|
305
|
+
command.extend(
|
|
306
|
+
[
|
|
307
|
+
"-filter_complex",
|
|
308
|
+
";".join(filters),
|
|
309
|
+
"-map",
|
|
310
|
+
"[vout]",
|
|
311
|
+
"-map",
|
|
312
|
+
"[aout]",
|
|
313
|
+
"-c:v",
|
|
314
|
+
"libx264",
|
|
315
|
+
"-preset",
|
|
316
|
+
"veryfast",
|
|
317
|
+
"-threads",
|
|
318
|
+
"4",
|
|
319
|
+
"-crf",
|
|
320
|
+
"24",
|
|
321
|
+
"-c:a",
|
|
322
|
+
"aac",
|
|
323
|
+
"-ar",
|
|
324
|
+
"48000",
|
|
325
|
+
"-b:a",
|
|
326
|
+
"160k",
|
|
327
|
+
"-movflags",
|
|
328
|
+
"+faststart",
|
|
329
|
+
"-progress",
|
|
330
|
+
"pipe:1",
|
|
331
|
+
"-nostats",
|
|
332
|
+
str(output),
|
|
333
|
+
]
|
|
334
|
+
)
|
|
335
|
+
if self.master:
|
|
336
|
+
position = command.index('libx264') - 1
|
|
337
|
+
end = command.index('+faststart') + 1
|
|
338
|
+
command[position:end] = ['-c:v', 'prores_ks', '-profile:v', '3', '-pix_fmt', 'yuv422p10le',
|
|
339
|
+
'-c:a', 'pcm_s16le', '-ar', '48000']
|
|
340
|
+
elif self.hardware_acceleration:
|
|
341
|
+
position = command.index('libx264')
|
|
342
|
+
command[position] = 'h264_videotoolbox'
|
|
343
|
+
for option in ('-preset', '-crf'):
|
|
344
|
+
index = command.index(option)
|
|
345
|
+
del command[index:index + 2]
|
|
346
|
+
command[-1:-1] = ['-b:v', '8000k']
|
|
347
|
+
with tempfile.TemporaryFile() as errors:
|
|
348
|
+
try:
|
|
349
|
+
process = self._popen(
|
|
350
|
+
command,
|
|
351
|
+
stdout=subprocess.PIPE,
|
|
352
|
+
stderr=errors,
|
|
353
|
+
text=True,
|
|
354
|
+
pass_fds=tuple(item.descriptor for item in opened),
|
|
355
|
+
)
|
|
356
|
+
except FileNotFoundError as error:
|
|
357
|
+
raise PreviewRenderError("ffmpeg_unavailable") from error
|
|
358
|
+
except OSError as error:
|
|
359
|
+
raise PreviewRenderError("ffmpeg_start_failed") from error
|
|
360
|
+
assert process.stdout is not None
|
|
361
|
+
terminated = False
|
|
362
|
+
try:
|
|
363
|
+
for line in process.stdout:
|
|
364
|
+
if cancel.is_set() and not terminated:
|
|
365
|
+
process.terminate()
|
|
366
|
+
terminated = True
|
|
367
|
+
if line.startswith("out_time_ms="):
|
|
368
|
+
raw_micros = line.split("=", 1)[1].strip()
|
|
369
|
+
if not raw_micros.isdigit():
|
|
370
|
+
continue
|
|
371
|
+
micros = int(raw_micros)
|
|
372
|
+
progress(
|
|
373
|
+
min(
|
|
374
|
+
0.99,
|
|
375
|
+
micros / 1_000_000 / float(timeline_duration),
|
|
376
|
+
)
|
|
377
|
+
)
|
|
378
|
+
finally:
|
|
379
|
+
close_stdout = getattr(process.stdout, "close", None)
|
|
380
|
+
if close_stdout is not None:
|
|
381
|
+
close_stdout()
|
|
382
|
+
code = process.wait()
|
|
383
|
+
if cancel.is_set():
|
|
384
|
+
raise PreviewRenderError("cancelled")
|
|
385
|
+
if code != 0 or not output.is_file() or output.stat().st_size < 1:
|
|
386
|
+
raise PreviewRenderError("ffmpeg_render_failed")
|
|
387
|
+
progress(1.0)
|
|
388
|
+
finally:
|
|
389
|
+
for item in opened:
|
|
390
|
+
item.close()
|
|
391
|
+
|
|
392
|
+
def _add_input(self, command, opened, project_id: str, item: MediaClip):
|
|
393
|
+
try:
|
|
394
|
+
source = self.media.open(project_id, item.source.span.version_id)
|
|
395
|
+
except ProjectError as error:
|
|
396
|
+
raise PreviewRenderError(error.code) from error
|
|
397
|
+
opened.append(source)
|
|
398
|
+
start = self._seconds(item.source.span.start_pts, item.source.span.time_base)
|
|
399
|
+
duration = self._seconds(
|
|
400
|
+
str(item.source.span.duration_pts), item.source.span.time_base
|
|
401
|
+
)
|
|
402
|
+
if self.hardware_acceleration:
|
|
403
|
+
command.extend(['-hwaccel', 'videotoolbox'])
|
|
404
|
+
command.extend(
|
|
405
|
+
[
|
|
406
|
+
"-seek_timestamp", "1",
|
|
407
|
+
"-ss",
|
|
408
|
+
start,
|
|
409
|
+
"-t",
|
|
410
|
+
duration,
|
|
411
|
+
"-threads",
|
|
412
|
+
"1",
|
|
413
|
+
"-i",
|
|
414
|
+
f"/dev/fd/{source.descriptor}",
|
|
415
|
+
]
|
|
416
|
+
)
|
|
417
|
+
return len(opened) - 1, duration, source
|
|
418
|
+
|
|
419
|
+
def _render_batched(
|
|
420
|
+
self,
|
|
421
|
+
project_id: str,
|
|
422
|
+
timeline: EditTimeline,
|
|
423
|
+
video: Track,
|
|
424
|
+
output: Path,
|
|
425
|
+
cancel: threading.Event,
|
|
426
|
+
progress: Callable[[float], None],
|
|
427
|
+
*, preserve_audio_gain: bool = False,
|
|
428
|
+
) -> None:
|
|
429
|
+
total_frames = sum(item.duration_frames for item in video.items)
|
|
430
|
+
completed_frames = 0
|
|
431
|
+
with tempfile.TemporaryDirectory(
|
|
432
|
+
prefix=".clipen-preview-", dir=output.parent
|
|
433
|
+
) as temporary:
|
|
434
|
+
directory = Path(temporary)
|
|
435
|
+
segments = []
|
|
436
|
+
segment_durations = []
|
|
437
|
+
for batch_index, offset in enumerate(
|
|
438
|
+
range(0, len(video.items), self._BATCH_SIZE)
|
|
439
|
+
):
|
|
440
|
+
if cancel.is_set():
|
|
441
|
+
raise PreviewRenderError("cancelled")
|
|
442
|
+
source_items = video.items[offset : offset + self._BATCH_SIZE]
|
|
443
|
+
cursor = 0
|
|
444
|
+
normalized_items = []
|
|
445
|
+
for local_index,item in enumerate(source_items):
|
|
446
|
+
assert isinstance(item, MediaClip)
|
|
447
|
+
effects=item.effects
|
|
448
|
+
position=offset+local_index
|
|
449
|
+
if local_index==len(source_items)-1 and position+1<len(video.items):
|
|
450
|
+
incoming=next((e for e in video.items[position+1].effects if e.effect_id=='video.transition'),None)
|
|
451
|
+
if incoming:
|
|
452
|
+
effects+=(EffectRequest('video.transition_out',incoming.parameters),)
|
|
453
|
+
normalized_items.append(
|
|
454
|
+
MediaClip(
|
|
455
|
+
item.item_id,
|
|
456
|
+
cursor,
|
|
457
|
+
item.duration_frames,
|
|
458
|
+
item.source,
|
|
459
|
+
effects,
|
|
460
|
+
)
|
|
461
|
+
)
|
|
462
|
+
cursor += item.duration_frames
|
|
463
|
+
batch = EditTimeline(
|
|
464
|
+
timeline.timeline_id,
|
|
465
|
+
timeline.project_id,
|
|
466
|
+
timeline.plan_id,
|
|
467
|
+
timeline.plan_revision,
|
|
468
|
+
timeline.plan_content_hash,
|
|
469
|
+
timeline.frame_rate,
|
|
470
|
+
timeline.width,
|
|
471
|
+
timeline.height,
|
|
472
|
+
cursor,
|
|
473
|
+
(Track("video-primary", TrackKind.VIDEO, tuple(normalized_items)),),
|
|
474
|
+
production_session_id=timeline.production_session_id,
|
|
475
|
+
)
|
|
476
|
+
segment = directory / f"segment-{batch_index:04d}.mp4"
|
|
477
|
+
base = completed_frames
|
|
478
|
+
|
|
479
|
+
def batch_progress(value: float, *, base=base, size=cursor) -> None:
|
|
480
|
+
progress(0.98 * (base + value * size) / total_frames)
|
|
481
|
+
|
|
482
|
+
self._render_media(
|
|
483
|
+
project_id,
|
|
484
|
+
batch,
|
|
485
|
+
segment,
|
|
486
|
+
cancel,
|
|
487
|
+
batch_progress,
|
|
488
|
+
preserve_audio_gain=preserve_audio_gain,
|
|
489
|
+
)
|
|
490
|
+
segments.append(segment)
|
|
491
|
+
segment_durations.append(self._timeline_seconds(batch))
|
|
492
|
+
completed_frames += cursor
|
|
493
|
+
self._concat_segments(segments, output, cancel, segment_durations)
|
|
494
|
+
progress(1.0)
|
|
495
|
+
|
|
496
|
+
def _concat_segments(
|
|
497
|
+
self, segments: list[Path], output: Path, cancel: threading.Event,
|
|
498
|
+
durations=None,
|
|
499
|
+
) -> None:
|
|
500
|
+
manifest = segments[0].parent / "segments.txt"
|
|
501
|
+
manifest.write_text(
|
|
502
|
+
# AAC encoder padding may make a segment's container longer than
|
|
503
|
+
# its video. Letting concat infer that duration inserts a timestamp
|
|
504
|
+
# gap at each batch, changing avg_frame_rate despite correct frames.
|
|
505
|
+
"".join(f"file '{segment.name}'\n"+(f'duration {durations[n]}\n' if durations is not None else '') for n,segment in enumerate(segments)),
|
|
506
|
+
encoding="utf-8",
|
|
507
|
+
)
|
|
508
|
+
command = [
|
|
509
|
+
self.executable,
|
|
510
|
+
"-hide_banner",
|
|
511
|
+
"-loglevel",
|
|
512
|
+
"error",
|
|
513
|
+
"-y",
|
|
514
|
+
"-f",
|
|
515
|
+
"concat",
|
|
516
|
+
"-safe",
|
|
517
|
+
"0",
|
|
518
|
+
"-i",
|
|
519
|
+
str(manifest),
|
|
520
|
+
"-c",
|
|
521
|
+
"copy",
|
|
522
|
+
"-movflags",
|
|
523
|
+
"+faststart",
|
|
524
|
+
"-progress",
|
|
525
|
+
"pipe:1",
|
|
526
|
+
"-nostats",
|
|
527
|
+
str(output),
|
|
528
|
+
]
|
|
529
|
+
with tempfile.TemporaryFile() as errors:
|
|
530
|
+
try:
|
|
531
|
+
process = self._popen(
|
|
532
|
+
command,
|
|
533
|
+
stdout=subprocess.PIPE,
|
|
534
|
+
stderr=errors,
|
|
535
|
+
text=True,
|
|
536
|
+
)
|
|
537
|
+
except FileNotFoundError as error:
|
|
538
|
+
raise PreviewRenderError("ffmpeg_unavailable") from error
|
|
539
|
+
except OSError as error:
|
|
540
|
+
raise PreviewRenderError("ffmpeg_start_failed") from error
|
|
541
|
+
assert process.stdout is not None
|
|
542
|
+
terminated = False
|
|
543
|
+
try:
|
|
544
|
+
for _line in process.stdout:
|
|
545
|
+
if cancel.is_set() and not terminated:
|
|
546
|
+
process.terminate()
|
|
547
|
+
terminated = True
|
|
548
|
+
finally:
|
|
549
|
+
close_stdout = getattr(process.stdout, "close", None)
|
|
550
|
+
if close_stdout is not None:
|
|
551
|
+
close_stdout()
|
|
552
|
+
code = process.wait()
|
|
553
|
+
if cancel.is_set():
|
|
554
|
+
raise PreviewRenderError("cancelled")
|
|
555
|
+
if code != 0 or not output.is_file() or output.stat().st_size < 1:
|
|
556
|
+
raise PreviewRenderError("ffmpeg_render_failed")
|
|
557
|
+
|
|
558
|
+
@staticmethod
|
|
559
|
+
def _video_effect_filters(item: MediaClip, timeline: EditTimeline) -> str:
|
|
560
|
+
"""Map allowlisted semantic effects to bounded FFmpeg video filters."""
|
|
561
|
+
|
|
562
|
+
filters = ""
|
|
563
|
+
for effect in item.effects:
|
|
564
|
+
parameters = effect.parameters
|
|
565
|
+
if effect.effect_id == "video.punch_in":
|
|
566
|
+
scale = min(2.0, max(1.01, float(parameters.get("scale", 1.08))))
|
|
567
|
+
center_x = min(1.0, max(0.0, float(parameters.get("center_x", 0.5))))
|
|
568
|
+
center_y = min(1.0, max(0.0, float(parameters.get("center_y", 0.5))))
|
|
569
|
+
filters += (
|
|
570
|
+
f",scale=iw*{scale:.4f}:ih*{scale:.4f},"
|
|
571
|
+
f"crop={timeline.width}:{timeline.height}:"
|
|
572
|
+
f"(iw-ow)*{center_x:.4f}:(ih-oh)*{center_y:.4f}"
|
|
573
|
+
)
|
|
574
|
+
elif effect.effect_id == "video.slow_push":
|
|
575
|
+
start = min(2.0, max(1.0, float(parameters.get("scale_from", 1.0))))
|
|
576
|
+
end = min(2.0, max(1.0, float(parameters.get("scale_to", 1.08))))
|
|
577
|
+
frames = max(1, item.duration_frames - 1)
|
|
578
|
+
filters += (
|
|
579
|
+
f",zoompan=z='{start:.4f}+({end:.4f}-{start:.4f})*on/{frames}':"
|
|
580
|
+
"x='iw/2-(iw/zoom/2)':y='ih/2-(ih/zoom/2)':d=1:"
|
|
581
|
+
f"s={timeline.width}x{timeline.height}:"
|
|
582
|
+
f"fps={timeline.frame_rate.numerator}/{timeline.frame_rate.denominator}"
|
|
583
|
+
)
|
|
584
|
+
elif effect.effect_id == "video.reframe":
|
|
585
|
+
x = min(1.0, max(0.0, float(parameters.get("x", 0.5))))
|
|
586
|
+
y = min(1.0, max(0.0, float(parameters.get("y", 0.5))))
|
|
587
|
+
filters += (
|
|
588
|
+
",scale=iw*1.08:ih*1.08,"
|
|
589
|
+
f"crop={timeline.width}:{timeline.height}:(iw-ow)*{x:.4f}:(ih-oh)*{y:.4f}"
|
|
590
|
+
)
|
|
591
|
+
return filters
|
|
592
|
+
|
|
593
|
+
@classmethod
|
|
594
|
+
def _retime_video(
|
|
595
|
+
cls,
|
|
596
|
+
filters: list[str],
|
|
597
|
+
source_label: str,
|
|
598
|
+
item: MediaClip,
|
|
599
|
+
duration: float,
|
|
600
|
+
index: int,
|
|
601
|
+
) -> str:
|
|
602
|
+
profile = cls._speed_profile(item)
|
|
603
|
+
if profile is None:
|
|
604
|
+
return source_label
|
|
605
|
+
outside_rate, center_rate = profile
|
|
606
|
+
first_end, center_end = duration * 0.4, duration * 0.6
|
|
607
|
+
filters.append(
|
|
608
|
+
f"[{source_label}]split=3[vr{index}a][vr{index}b][vr{index}c]"
|
|
609
|
+
)
|
|
610
|
+
filters.extend(
|
|
611
|
+
(
|
|
612
|
+
f"[vr{index}a]trim=start=0:end={first_end:.6f},"
|
|
613
|
+
f"setpts=(PTS-STARTPTS)/{outside_rate:.6f}[vr{index}ao]",
|
|
614
|
+
f"[vr{index}b]trim=start={first_end:.6f}:end={center_end:.6f},"
|
|
615
|
+
f"setpts=(PTS-STARTPTS)/{center_rate:.6f}[vr{index}bo]",
|
|
616
|
+
f"[vr{index}c]trim=start={center_end:.6f}:end={duration:.6f},"
|
|
617
|
+
f"setpts=(PTS-STARTPTS)/{outside_rate:.6f}[vr{index}co]",
|
|
618
|
+
f"[vr{index}ao][vr{index}bo][vr{index}co]concat=n=3:v=1:a=0[vr{index}out]",
|
|
619
|
+
)
|
|
620
|
+
)
|
|
621
|
+
return f"vr{index}out"
|
|
622
|
+
|
|
623
|
+
@classmethod
|
|
624
|
+
def _retime_audio(
|
|
625
|
+
cls,
|
|
626
|
+
filters: list[str],
|
|
627
|
+
source_label: str,
|
|
628
|
+
item: MediaClip,
|
|
629
|
+
duration: float,
|
|
630
|
+
index: int,
|
|
631
|
+
) -> str:
|
|
632
|
+
profile = cls._speed_profile(item)
|
|
633
|
+
if profile is None:
|
|
634
|
+
return source_label
|
|
635
|
+
outside_rate, center_rate = profile
|
|
636
|
+
first_end, center_end = duration * 0.4, duration * 0.6
|
|
637
|
+
outside_tempo = cls._atempo(outside_rate)
|
|
638
|
+
center_tempo = cls._atempo(center_rate)
|
|
639
|
+
filters.append(
|
|
640
|
+
f"[{source_label}]asplit=3[ar{index}a][ar{index}b][ar{index}c]"
|
|
641
|
+
)
|
|
642
|
+
filters.extend(
|
|
643
|
+
(
|
|
644
|
+
f"[ar{index}a]atrim=start=0:end={first_end:.6f},"
|
|
645
|
+
f"asetpts=PTS-STARTPTS,{outside_tempo}[ar{index}ao]",
|
|
646
|
+
f"[ar{index}b]atrim=start={first_end:.6f}:end={center_end:.6f},"
|
|
647
|
+
f"asetpts=PTS-STARTPTS,{center_tempo}[ar{index}bo]",
|
|
648
|
+
f"[ar{index}c]atrim=start={center_end:.6f}:end={duration:.6f},"
|
|
649
|
+
f"asetpts=PTS-STARTPTS,{outside_tempo}[ar{index}co]",
|
|
650
|
+
f"[ar{index}ao][ar{index}bo][ar{index}co]concat=n=3:v=0:a=1[ar{index}out]",
|
|
651
|
+
)
|
|
652
|
+
)
|
|
653
|
+
return f"ar{index}out"
|
|
654
|
+
|
|
655
|
+
@staticmethod
|
|
656
|
+
def _speed_profile(item: MediaClip) -> tuple[float, float] | None:
|
|
657
|
+
effect = next(
|
|
658
|
+
(value for value in item.effects if value.effect_id == "video.speed_ramp"),
|
|
659
|
+
None,
|
|
660
|
+
)
|
|
661
|
+
if effect is None:
|
|
662
|
+
return None
|
|
663
|
+
center_rate = min(4.0, max(0.25, float(effect.parameters.get("rate", 1.25))))
|
|
664
|
+
if abs(center_rate - 1.0) < 0.001:
|
|
665
|
+
return None
|
|
666
|
+
# The center 20% receives the requested rate. The two outer 40%
|
|
667
|
+
# sections compensate evenly so the clip keeps its exact timeline
|
|
668
|
+
# duration and remains aligned with every following edit.
|
|
669
|
+
outside_rate = 0.8 / (1.0 - 0.2 / center_rate)
|
|
670
|
+
return outside_rate, center_rate
|
|
671
|
+
|
|
672
|
+
@staticmethod
|
|
673
|
+
def _atempo(rate: float) -> str:
|
|
674
|
+
values = []
|
|
675
|
+
remaining = rate
|
|
676
|
+
while remaining < 0.5:
|
|
677
|
+
values.append(0.5)
|
|
678
|
+
remaining /= 0.5
|
|
679
|
+
while remaining > 2.0:
|
|
680
|
+
values.append(2.0)
|
|
681
|
+
remaining /= 2.0
|
|
682
|
+
values.append(remaining)
|
|
683
|
+
return ",".join(f"atempo={value:.6f}" for value in values)
|
|
684
|
+
|
|
685
|
+
@staticmethod
|
|
686
|
+
def _stream_index(requested: int, available: tuple[int, ...], error: str) -> int:
|
|
687
|
+
if requested in available:
|
|
688
|
+
return requested
|
|
689
|
+
if available:
|
|
690
|
+
return available[0]
|
|
691
|
+
raise PreviewRenderError(error)
|
|
692
|
+
|
|
693
|
+
@staticmethod
|
|
694
|
+
def _timeline_seconds(timeline: EditTimeline) -> Decimal:
|
|
695
|
+
return (
|
|
696
|
+
Decimal(timeline.duration_frames)
|
|
697
|
+
* Decimal(timeline.frame_rate.denominator)
|
|
698
|
+
/ Decimal(timeline.frame_rate.numerator)
|
|
699
|
+
)
|
|
700
|
+
|
|
701
|
+
@staticmethod
|
|
702
|
+
def _seconds(pts: str, time_base) -> str:
|
|
703
|
+
value = (
|
|
704
|
+
Decimal(pts)
|
|
705
|
+
* Decimal(time_base.numerator)
|
|
706
|
+
/ Decimal(time_base.denominator)
|
|
707
|
+
)
|
|
708
|
+
return format(value.quantize(Decimal("0.000001")), "f")
|