@clipen/clipen-cli 1.0.0 → 1.0.2
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/README.md +56 -48
- package/apps/web/dist/assets/{index-D5D2HiB1.js → index-DPHwA954.js} +1 -1
- package/apps/web/dist/index.html +1 -1
- package/bin/clipen.js +1 -1
- package/package.json +7 -6
- package/pyproject.toml +18 -2
- package/requirements/dev.txt +2 -0
- package/src/clipen/__init__.py +1 -1
- package/src/clipen/analysis/__init__.py +7 -7
- package/src/clipen/analysis/artifacts.py +4 -10
- package/src/clipen/analysis/audio.py +10 -8
- package/src/clipen/analysis/evidence_service.py +345 -167
- package/src/clipen/analysis/image_similarity.py +5 -14
- package/src/clipen/analysis/keyframes.py +5 -17
- package/src/clipen/analysis/models.py +5 -7
- package/src/clipen/analysis/qwen_worker.py +19 -54
- package/src/clipen/analysis/resources.py +2 -1
- package/src/clipen/analysis/runtime.py +1 -3
- package/src/clipen/analysis/service.py +16 -48
- package/src/clipen/analysis/setup.py +11 -25
- package/src/clipen/analysis/setup_cli.py +5 -15
- package/src/clipen/analysis/storage.py +96 -278
- package/src/clipen/analysis/transcription.py +40 -82
- package/src/clipen/analysis/visual.py +109 -48
- package/src/clipen/annotations/__init__.py +5 -2
- package/src/clipen/annotations/models.py +1 -1
- package/src/clipen/annotations/storage.py +132 -84
- package/src/clipen/annotations/suggestions.py +3 -8
- package/src/clipen/asr_release.py +4 -11
- package/src/clipen/bootstrap.py +71 -61
- package/src/clipen/{planning/models.py → candidates.py} +29 -47
- package/src/clipen/cli.py +4 -9
- package/src/clipen/controller/app.py +401 -214
- package/src/clipen/controller/server.py +28 -38
- package/src/clipen/domain/__init__.py +1 -1
- package/src/clipen/domain/media_time.py +3 -10
- package/src/clipen/domain/timeline.py +45 -47
- package/src/clipen/domain/validation.py +0 -1
- package/src/clipen/editing/__init__.py +1 -1
- package/src/clipen/editing/agent_models.py +4 -11
- package/src/clipen/editing/agent_repository.py +393 -172
- package/src/clipen/editing/agent_tools.py +180 -99
- package/src/clipen/editing/alignment.py +92 -52
- package/src/clipen/editing/alignment_setup.py +118 -70
- package/src/clipen/editing/alignment_worker.py +11 -9
- package/src/clipen/editing/change_report.py +95 -38
- package/src/clipen/editing/compiler.py +0 -1
- package/src/clipen/editing/component_looks.py +58 -34
- package/src/clipen/editing/component_review.py +34 -38
- package/src/clipen/editing/coordinator.py +213 -92
- package/src/clipen/editing/editorial_review.py +217 -95
- package/src/clipen/editing/evidence_jobs.py +457 -200
- package/src/clipen/editing/history_changes.py +129 -68
- package/src/clipen/editing/music_choices.py +8 -5
- package/src/clipen/editing/production_catalog.py +573 -136
- package/src/clipen/editing/production_limits.py +43 -17
- package/src/clipen/editing/production_media.py +376 -167
- package/src/clipen/editing/production_preview.py +322 -129
- package/src/clipen/editing/production_progress.py +164 -69
- package/src/clipen/editing/production_requirements.py +106 -39
- package/src/clipen/editing/production_tools.py +1064 -462
- package/src/clipen/editing/program_audio.py +211 -81
- package/src/clipen/editing/program_direction.py +59 -25
- package/src/clipen/editing/program_story.py +83 -45
- package/src/clipen/editing/program_subtitles.py +194 -96
- package/src/clipen/editing/programme_review.py +98 -50
- package/src/clipen/editing/prompt_history.py +64 -27
- package/src/clipen/editing/recut.py +65 -25
- package/src/clipen/editing/refinement_references.py +176 -86
- package/src/clipen/editing/resolve_export.py +449 -218
- package/src/clipen/editing/resolve_titles.py +162 -105
- package/src/clipen/editing/skill_tools.py +530 -154
- package/src/clipen/editing/source_dialogue.py +177 -67
- package/src/clipen/editing/speech_coverage.py +42 -22
- package/src/clipen/editing/speech_quality.py +6 -1
- package/src/clipen/editing/tool_receipts.py +78 -32
- package/src/clipen/editing/versions.py +122 -64
- package/src/clipen/editing/visual_preview.py +157 -61
- package/src/clipen/editing/workbench.py +104 -42
- package/src/clipen/finishing/audio.py +69 -30
- package/src/clipen/finishing/catalog.py +176 -93
- package/src/clipen/finishing/models.py +1 -1
- package/src/clipen/finishing/renderer.py +33 -14
- package/src/clipen/mcp_editing/__main__.py +3 -1
- package/src/clipen/mcp_editing/server.py +77 -34
- package/src/clipen/media/__init__.py +3 -3
- package/src/clipen/media/discovery.py +7 -20
- package/src/clipen/media/models.py +7 -18
- package/src/clipen/media/preview.py +14 -24
- package/src/clipen/media/probe.py +16 -44
- package/src/clipen/media/service.py +6 -19
- package/src/clipen/media/storage.py +33 -81
- package/src/clipen/music_vocabulary.py +33 -4
- package/src/clipen/power.py +8 -3
- package/src/clipen/projects/__init__.py +1 -1
- package/src/clipen/projects/deletion.py +19 -32
- package/src/clipen/projects/finishing_schema.py +3 -1
- package/src/clipen/projects/models.py +2 -5
- package/src/clipen/projects/picker.py +13 -37
- package/src/clipen/projects/service.py +8 -24
- package/src/clipen/projects/storage.py +226 -246
- package/src/clipen/provider_registry.py +9 -20
- package/src/clipen/providers/account.py +86 -35
- package/src/clipen/providers/claude.py +59 -25
- package/src/clipen/providers/claude_editing.py +14 -7
- package/src/clipen/providers/codex.py +73 -118
- package/src/clipen/providers/codex_editing.py +2 -2
- package/src/clipen/providers/editing_agent.py +19 -17
- package/src/clipen/providers/recovery.py +7 -8
- package/src/clipen/rendering/cover.py +18 -5
- package/src/clipen/rendering/custom_components.py +167 -79
- package/src/clipen/rendering/custom_render.py +62 -41
- package/src/clipen/rendering/production_effects.py +77 -45
- package/src/clipen/rendering/program_components.py +134 -73
- package/src/clipen/rendering/program_creator.py +432 -277
- package/src/clipen/rendering/program_identities.py +277 -182
- package/src/clipen/rendering/program_motion.py +140 -95
- package/src/clipen/rendering/program_studio.py +175 -116
- package/src/clipen/rendering/program_styles.py +231 -145
- package/src/clipen/rendering/program_subtitles.py +71 -34
- package/src/clipen/rendering/renderer.py +150 -121
- package/src/clipen/rendering/storage.py +9 -22
- package/src/clipen/rendering/titles.py +193 -86
- package/src/clipen/skills/__init__.py +1 -0
- package/src/clipen/skills/catalog.py +151 -55
- package/src/clipen/skills/library.py +13 -7
- package/src/clipen/skills/package.py +46 -23
- package/src/clipen/skills/visual_styles.py +29 -18
- package/src/clipen/workspace.py +15 -44
- package/install +0 -25
- package/src/clipen/planning/__init__.py +0 -9
- package/uninstall +0 -25
|
@@ -2,12 +2,12 @@
|
|
|
2
2
|
|
|
3
3
|
from __future__ import annotations
|
|
4
4
|
|
|
5
|
-
from decimal import Decimal
|
|
6
5
|
import os
|
|
7
|
-
from pathlib import Path
|
|
8
6
|
import subprocess
|
|
9
7
|
import tempfile
|
|
10
8
|
import threading
|
|
9
|
+
from decimal import Decimal
|
|
10
|
+
from pathlib import Path
|
|
11
11
|
from typing import Callable
|
|
12
12
|
|
|
13
13
|
from clipen.domain import EditTimeline, EffectRequest, MediaClip, Track, TrackKind
|
|
@@ -44,17 +44,24 @@ class FfmpegPreviewRenderer:
|
|
|
44
44
|
# Master output feeds another editor: ProRes and PCM instead of a compact preview.
|
|
45
45
|
self.master = master
|
|
46
46
|
|
|
47
|
-
def render(self,project_id,timeline,output,cancel,progress):
|
|
48
|
-
layers=any(
|
|
47
|
+
def render(self, project_id, timeline, output, cancel, progress):
|
|
48
|
+
layers = any(
|
|
49
|
+
e.effect_id == "audio.layers"
|
|
50
|
+
for t in timeline.tracks
|
|
51
|
+
for i in t.items
|
|
52
|
+
if isinstance(i, MediaClip)
|
|
53
|
+
for e in i.effects
|
|
54
|
+
)
|
|
49
55
|
if not layers:
|
|
50
|
-
return self._render_base(project_id,timeline,output,cancel,progress)
|
|
56
|
+
return self._render_base(project_id, timeline, output, cancel, progress)
|
|
51
57
|
from clipen.editing.production_media import mix_audio_layers
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
+
|
|
59
|
+
output.parent.mkdir(mode=0o700, parents=True, exist_ok=True)
|
|
60
|
+
with tempfile.TemporaryDirectory(prefix=".agent-mix-", dir=output.parent) as temp:
|
|
61
|
+
base = Path(temp) / "camera.mp4"
|
|
62
|
+
self._render_base(project_id, timeline, base, cancel, lambda p: progress(p * 0.85))
|
|
63
|
+
mix_audio_layers(self.media, project_id, timeline, base, output, cancel)
|
|
64
|
+
progress(1.0)
|
|
58
65
|
|
|
59
66
|
def _render_base(
|
|
60
67
|
self,
|
|
@@ -64,31 +71,42 @@ class FfmpegPreviewRenderer:
|
|
|
64
71
|
cancel: threading.Event,
|
|
65
72
|
progress: Callable[[float], None],
|
|
66
73
|
) -> None:
|
|
67
|
-
title_track = next(
|
|
68
|
-
(track for track in timeline.tracks if track.kind is TrackKind.TITLE), None
|
|
69
|
-
)
|
|
70
74
|
if not any(t.items for t in timeline.tracks if t.kind is TrackKind.TITLE):
|
|
71
75
|
self._render_media(project_id, timeline, output, cancel, progress)
|
|
72
76
|
return
|
|
73
77
|
output.parent.mkdir(mode=0o700, parents=True, exist_ok=True)
|
|
74
78
|
try:
|
|
75
|
-
with tempfile.TemporaryDirectory(
|
|
76
|
-
prefix=".clipen-title-", dir=output.parent
|
|
77
|
-
) as temporary:
|
|
79
|
+
with tempfile.TemporaryDirectory(prefix=".clipen-title-", dir=output.parent) as temporary:
|
|
78
80
|
directory = Path(temporary)
|
|
79
81
|
media_output = self._media_without_titles(
|
|
80
82
|
project_id, timeline, directory, cancel, lambda value: progress(value * 0.82)
|
|
81
83
|
)
|
|
82
84
|
# 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
|
|
85
|
+
share = 0.0 if media_output.parent != directory else 0.82
|
|
84
86
|
layer = lambda value: progress(share + value * (1 - share))
|
|
85
87
|
from clipen.editing.production_media import load_text_styles
|
|
86
|
-
|
|
87
|
-
|
|
88
|
+
|
|
89
|
+
styles = load_text_styles(getattr(self.media, "projects", None), project_id)
|
|
90
|
+
if sum(len(t.items) for t in timeline.tracks if t.kind is TrackKind.TITLE) > 32 or any(
|
|
91
|
+
styles.get(i.style_id, {}).get("kind") in {"program_subtitle", "program_component"}
|
|
92
|
+
for t in timeline.tracks
|
|
93
|
+
if t.kind is TrackKind.TITLE
|
|
94
|
+
for i in t.items
|
|
95
|
+
):
|
|
88
96
|
from .titles import compose_dense_titles
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
97
|
+
|
|
98
|
+
compose_dense_titles(
|
|
99
|
+
self.executable,
|
|
100
|
+
media_output,
|
|
101
|
+
timeline,
|
|
102
|
+
directory,
|
|
103
|
+
output,
|
|
104
|
+
cancel,
|
|
105
|
+
styles,
|
|
106
|
+
progress=layer,
|
|
107
|
+
hardware=self.hardware_acceleration and not self.master,
|
|
108
|
+
)
|
|
109
|
+
progress(1.0)
|
|
92
110
|
return
|
|
93
111
|
titles = TitleCardRenderer(styles).render(timeline, directory)
|
|
94
112
|
FfmpegTitleCompositor(self.executable, popen=self._popen).compose(
|
|
@@ -114,13 +132,19 @@ class FfmpegPreviewRenderer:
|
|
|
114
132
|
output = directory / "media.mp4"
|
|
115
133
|
self._render_media(project_id, timeline, output, cancel, progress)
|
|
116
134
|
return output
|
|
117
|
-
import json
|
|
118
135
|
import hashlib
|
|
136
|
+
import json
|
|
119
137
|
from dataclasses import replace
|
|
120
|
-
|
|
138
|
+
|
|
139
|
+
picture = replace(
|
|
140
|
+
timeline, tracks=tuple(t for t in timeline.tracks if t.kind is not TrackKind.TITLE), markers=()
|
|
141
|
+
).to_payload()
|
|
121
142
|
picture.pop("timeline_id", None)
|
|
122
|
-
key = hashlib.sha256(
|
|
123
|
-
|
|
143
|
+
key = hashlib.sha256(
|
|
144
|
+
json.dumps(
|
|
145
|
+
{"version": 1, "picture": picture, "master": self.master}, sort_keys=True, ensure_ascii=False
|
|
146
|
+
).encode()
|
|
147
|
+
).hexdigest()[:32]
|
|
124
148
|
folder = projects.root / project_id / "previews" / "media-cache"
|
|
125
149
|
cached = folder / f"{key}.mp4"
|
|
126
150
|
if cached.is_file():
|
|
@@ -132,10 +156,11 @@ class FfmpegPreviewRenderer:
|
|
|
132
156
|
folder.mkdir(mode=0o700, parents=True, exist_ok=True)
|
|
133
157
|
partial = folder / f".{key}.part"
|
|
134
158
|
import shutil
|
|
159
|
+
|
|
135
160
|
shutil.copyfile(output, partial)
|
|
136
161
|
os.replace(partial, cached)
|
|
137
162
|
kept = sorted(folder.glob("*.mp4"), key=lambda path: path.stat().st_mtime, reverse=True)
|
|
138
|
-
for stale in kept[self._MEDIA_CACHE_KEEP:]:
|
|
163
|
+
for stale in kept[self._MEDIA_CACHE_KEEP :]:
|
|
139
164
|
stale.unlink(missing_ok=True)
|
|
140
165
|
except OSError:
|
|
141
166
|
pass # A cache that cannot be written only means the next edit renders in full.
|
|
@@ -143,14 +168,18 @@ class FfmpegPreviewRenderer:
|
|
|
143
168
|
|
|
144
169
|
def _render_media(self, project_id, timeline, output, cancel, progress, *, preserve_audio_gain=False):
|
|
145
170
|
try:
|
|
146
|
-
return self._render_media_once(
|
|
171
|
+
return self._render_media_once(
|
|
172
|
+
project_id, timeline, output, cancel, progress, preserve_audio_gain=preserve_audio_gain
|
|
173
|
+
)
|
|
147
174
|
except PreviewRenderError as error:
|
|
148
|
-
if not self.hardware_acceleration or error.code !=
|
|
175
|
+
if not self.hardware_acceleration or error.code != "ffmpeg_render_failed" or cancel.is_set():
|
|
149
176
|
raise
|
|
150
177
|
# Unsupported input/encoder on this machine: retry with the original
|
|
151
178
|
# software path, retaining all filters, source ranges and timing.
|
|
152
179
|
self.hardware_acceleration = False
|
|
153
|
-
return self._render_media_once(
|
|
180
|
+
return self._render_media_once(
|
|
181
|
+
project_id, timeline, output, cancel, progress, preserve_audio_gain=preserve_audio_gain
|
|
182
|
+
)
|
|
154
183
|
|
|
155
184
|
def _render_media_once(
|
|
156
185
|
self,
|
|
@@ -159,28 +188,33 @@ class FfmpegPreviewRenderer:
|
|
|
159
188
|
output: Path,
|
|
160
189
|
cancel: threading.Event,
|
|
161
190
|
progress: Callable[[float], None],
|
|
162
|
-
*,
|
|
191
|
+
*,
|
|
192
|
+
preserve_audio_gain: bool = False,
|
|
163
193
|
) -> None:
|
|
164
194
|
if os.name == "nt":
|
|
165
195
|
raise PreviewRenderError("preview_platform_unsupported")
|
|
166
|
-
video = next(
|
|
167
|
-
|
|
168
|
-
)
|
|
169
|
-
audio = next(
|
|
170
|
-
(track for track in timeline.tracks if track.kind is TrackKind.AUDIO and track.items), None
|
|
171
|
-
)
|
|
196
|
+
video = next((track for track in timeline.tracks if track.kind is TrackKind.VIDEO), None)
|
|
197
|
+
audio = next((track for track in timeline.tracks if track.kind is TrackKind.AUDIO and track.items), None)
|
|
172
198
|
if video is None or not video.items:
|
|
173
199
|
raise PreviewRenderError("preview_video_required")
|
|
174
200
|
if cancel.is_set():
|
|
175
201
|
raise PreviewRenderError("cancelled")
|
|
176
202
|
output.parent.mkdir(mode=0o700, parents=True, exist_ok=True)
|
|
177
|
-
controlled_audio =
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
203
|
+
controlled_audio = (
|
|
204
|
+
preserve_audio_gain
|
|
205
|
+
or timeline.production_session_id is not None
|
|
206
|
+
or any(
|
|
207
|
+
effect.effect_id in {"audio.source", "audio.layers"}
|
|
208
|
+
for track in timeline.tracks
|
|
209
|
+
for item in track.items
|
|
210
|
+
if isinstance(item, MediaClip)
|
|
211
|
+
for effect in item.effects
|
|
212
|
+
)
|
|
181
213
|
)
|
|
182
214
|
if audio is None and len(video.items) > self._BATCH_SIZE:
|
|
183
|
-
self._render_batched(
|
|
215
|
+
self._render_batched(
|
|
216
|
+
project_id, timeline, video, output, cancel, progress, preserve_audio_gain=controlled_audio
|
|
217
|
+
)
|
|
184
218
|
return
|
|
185
219
|
|
|
186
220
|
opened = []
|
|
@@ -199,11 +233,9 @@ class FfmpegPreviewRenderer:
|
|
|
199
233
|
audio_labels: list[str] = []
|
|
200
234
|
video_inputs = []
|
|
201
235
|
|
|
202
|
-
for clip_index,item in enumerate(video.items):
|
|
236
|
+
for clip_index, item in enumerate(video.items):
|
|
203
237
|
assert isinstance(item, MediaClip)
|
|
204
|
-
input_index, duration, source = self._add_input(
|
|
205
|
-
command, opened, project_id, item
|
|
206
|
-
)
|
|
238
|
+
input_index, duration, source = self._add_input(command, opened, project_id, item)
|
|
207
239
|
video_inputs.append((input_index, duration, source, item))
|
|
208
240
|
stream = self._stream_index(
|
|
209
241
|
item.source.span.stream_index,
|
|
@@ -220,31 +252,30 @@ class FfmpegPreviewRenderer:
|
|
|
220
252
|
f"fps={timeline.frame_rate.numerator}/"
|
|
221
253
|
f"{timeline.frame_rate.denominator}[{base_label}]"
|
|
222
254
|
)
|
|
223
|
-
current_label = self._retime_video(
|
|
224
|
-
filters, base_label, item, float(duration), len(video_labels)
|
|
225
|
-
)
|
|
255
|
+
current_label = self._retime_video(filters, base_label, item, float(duration), len(video_labels))
|
|
226
256
|
effect_filters = self._video_effect_filters(item, timeline)
|
|
227
257
|
chain = f"[{current_label}]"
|
|
228
258
|
if effect_filters:
|
|
229
259
|
chain += effect_filters.removeprefix(",") + ","
|
|
230
260
|
from .production_effects import grade_filters, mosaic_filters, transition_filters
|
|
231
|
-
|
|
232
|
-
|
|
261
|
+
|
|
262
|
+
chain += "null" + grade_filters(item) + ",format=yuv420p"
|
|
263
|
+
styled_label = f"vstyled{clip_index}"
|
|
233
264
|
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
|
|
265
|
+
masked = mosaic_filters(filters, styled_label, item, timeline, clip_index)
|
|
266
|
+
following = video.items[clip_index + 1] if clip_index + 1 < len(video.items) else None
|
|
236
267
|
# Source PTS and fps rounding can otherwise add a frame at a
|
|
237
268
|
# 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(
|
|
269
|
+
frame_pad = Decimal(2) * timeline.frame_rate.denominator / timeline.frame_rate.numerator
|
|
270
|
+
filters.append(
|
|
271
|
+
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}]"
|
|
272
|
+
)
|
|
240
273
|
video_labels.append(f"[{label}]")
|
|
241
274
|
|
|
242
275
|
if audio is not None:
|
|
243
276
|
for item in audio.items:
|
|
244
277
|
assert isinstance(item, MediaClip)
|
|
245
|
-
input_index, duration, source = self._add_input(
|
|
246
|
-
command, opened, project_id, item
|
|
247
|
-
)
|
|
278
|
+
input_index, duration, source = self._add_input(command, opened, project_id, item)
|
|
248
279
|
stream = self._stream_index(
|
|
249
280
|
item.source.span.stream_index,
|
|
250
281
|
source.audio_stream_indices,
|
|
@@ -254,7 +285,7 @@ class FfmpegPreviewRenderer:
|
|
|
254
285
|
filters.append(
|
|
255
286
|
f"[{input_index}:{stream}]atrim=duration={duration},"
|
|
256
287
|
"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}]"
|
|
288
|
+
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
289
|
)
|
|
259
290
|
audio_labels.append(f"[{label}]")
|
|
260
291
|
else:
|
|
@@ -275,32 +306,27 @@ class FfmpegPreviewRenderer:
|
|
|
275
306
|
filters, base_label, item, float(duration), len(audio_labels)
|
|
276
307
|
)
|
|
277
308
|
from .production_effects import camera_audio_filters
|
|
278
|
-
|
|
309
|
+
|
|
310
|
+
filters.append(
|
|
311
|
+
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}]"
|
|
312
|
+
)
|
|
279
313
|
else:
|
|
280
314
|
filters.append(
|
|
281
315
|
"anullsrc=r=48000:cl=stereo,"
|
|
282
|
-
f"atrim=duration={Decimal(item.duration_frames)*timeline.frame_rate.denominator/timeline.frame_rate.numerator:.6f}[{label}]"
|
|
316
|
+
f"atrim=duration={Decimal(item.duration_frames) * timeline.frame_rate.denominator / timeline.frame_rate.numerator:.6f}[{label}]"
|
|
283
317
|
)
|
|
284
318
|
audio_labels.append(f"[{label}]")
|
|
285
319
|
|
|
286
|
-
filters.append(
|
|
287
|
-
f"{''.join(video_labels)}concat=n={len(video_labels)}:v=1:a=0[vout]"
|
|
288
|
-
)
|
|
320
|
+
filters.append(f"{''.join(video_labels)}concat=n={len(video_labels)}:v=1:a=0[vout]")
|
|
289
321
|
timeline_duration = self._timeline_seconds(timeline)
|
|
290
322
|
if audio_labels:
|
|
291
|
-
filters.append(
|
|
292
|
-
f"{''.join(audio_labels)}concat=n={len(audio_labels)}:v=0:a=1[asequence]"
|
|
293
|
-
)
|
|
323
|
+
filters.append(f"{''.join(audio_labels)}concat=n={len(audio_labels)}:v=0:a=1[asequence]")
|
|
294
324
|
# Programme camera sound is a stem: automatic loudness makeup
|
|
295
325
|
# here would undo explicit attenuation before the music mix.
|
|
296
|
-
finishing =
|
|
297
|
-
filters.append(
|
|
298
|
-
f"[asequence]apad,atrim=duration={timeline_duration},{finishing}[aout]"
|
|
299
|
-
)
|
|
326
|
+
finishing = "alimiter=limit=0.841395:level=0" if controlled_audio else "loudnorm=I=-16:LRA=11:TP=-1.5"
|
|
327
|
+
filters.append(f"[asequence]apad,atrim=duration={timeline_duration},{finishing}[aout]")
|
|
300
328
|
else:
|
|
301
|
-
filters.append(
|
|
302
|
-
f"anullsrc=r=48000:cl=stereo,atrim=duration={timeline_duration}[aout]"
|
|
303
|
-
)
|
|
329
|
+
filters.append(f"anullsrc=r=48000:cl=stereo,atrim=duration={timeline_duration}[aout]")
|
|
304
330
|
|
|
305
331
|
command.extend(
|
|
306
332
|
[
|
|
@@ -333,17 +359,27 @@ class FfmpegPreviewRenderer:
|
|
|
333
359
|
]
|
|
334
360
|
)
|
|
335
361
|
if self.master:
|
|
336
|
-
position = command.index(
|
|
337
|
-
end = command.index(
|
|
338
|
-
command[position:end] = [
|
|
339
|
-
|
|
362
|
+
position = command.index("libx264") - 1
|
|
363
|
+
end = command.index("+faststart") + 1
|
|
364
|
+
command[position:end] = [
|
|
365
|
+
"-c:v",
|
|
366
|
+
"prores_ks",
|
|
367
|
+
"-profile:v",
|
|
368
|
+
"3",
|
|
369
|
+
"-pix_fmt",
|
|
370
|
+
"yuv422p10le",
|
|
371
|
+
"-c:a",
|
|
372
|
+
"pcm_s16le",
|
|
373
|
+
"-ar",
|
|
374
|
+
"48000",
|
|
375
|
+
]
|
|
340
376
|
elif self.hardware_acceleration:
|
|
341
|
-
position = command.index(
|
|
342
|
-
command[position] =
|
|
343
|
-
for option in (
|
|
377
|
+
position = command.index("libx264")
|
|
378
|
+
command[position] = "h264_videotoolbox"
|
|
379
|
+
for option in ("-preset", "-crf"):
|
|
344
380
|
index = command.index(option)
|
|
345
|
-
del command[index:index + 2]
|
|
346
|
-
command[-1:-1] = [
|
|
381
|
+
del command[index : index + 2]
|
|
382
|
+
command[-1:-1] = ["-b:v", "8000k"]
|
|
347
383
|
with tempfile.TemporaryFile() as errors:
|
|
348
384
|
try:
|
|
349
385
|
process = self._popen(
|
|
@@ -396,14 +432,13 @@ class FfmpegPreviewRenderer:
|
|
|
396
432
|
raise PreviewRenderError(error.code) from error
|
|
397
433
|
opened.append(source)
|
|
398
434
|
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
|
-
)
|
|
435
|
+
duration = self._seconds(str(item.source.span.duration_pts), item.source.span.time_base)
|
|
402
436
|
if self.hardware_acceleration:
|
|
403
|
-
command.extend([
|
|
437
|
+
command.extend(["-hwaccel", "videotoolbox"])
|
|
404
438
|
command.extend(
|
|
405
439
|
[
|
|
406
|
-
"-seek_timestamp",
|
|
440
|
+
"-seek_timestamp",
|
|
441
|
+
"1",
|
|
407
442
|
"-ss",
|
|
408
443
|
start,
|
|
409
444
|
"-t",
|
|
@@ -424,32 +459,31 @@ class FfmpegPreviewRenderer:
|
|
|
424
459
|
output: Path,
|
|
425
460
|
cancel: threading.Event,
|
|
426
461
|
progress: Callable[[float], None],
|
|
427
|
-
*,
|
|
462
|
+
*,
|
|
463
|
+
preserve_audio_gain: bool = False,
|
|
428
464
|
) -> None:
|
|
429
465
|
total_frames = sum(item.duration_frames for item in video.items)
|
|
430
466
|
completed_frames = 0
|
|
431
|
-
with tempfile.TemporaryDirectory(
|
|
432
|
-
prefix=".clipen-preview-", dir=output.parent
|
|
433
|
-
) as temporary:
|
|
467
|
+
with tempfile.TemporaryDirectory(prefix=".clipen-preview-", dir=output.parent) as temporary:
|
|
434
468
|
directory = Path(temporary)
|
|
435
469
|
segments = []
|
|
436
470
|
segment_durations = []
|
|
437
|
-
for batch_index, offset in enumerate(
|
|
438
|
-
range(0, len(video.items), self._BATCH_SIZE)
|
|
439
|
-
):
|
|
471
|
+
for batch_index, offset in enumerate(range(0, len(video.items), self._BATCH_SIZE)):
|
|
440
472
|
if cancel.is_set():
|
|
441
473
|
raise PreviewRenderError("cancelled")
|
|
442
474
|
source_items = video.items[offset : offset + self._BATCH_SIZE]
|
|
443
475
|
cursor = 0
|
|
444
476
|
normalized_items = []
|
|
445
|
-
for local_index,item in enumerate(source_items):
|
|
477
|
+
for local_index, item in enumerate(source_items):
|
|
446
478
|
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(
|
|
479
|
+
effects = item.effects
|
|
480
|
+
position = offset + local_index
|
|
481
|
+
if local_index == len(source_items) - 1 and position + 1 < len(video.items):
|
|
482
|
+
incoming = next(
|
|
483
|
+
(e for e in video.items[position + 1].effects if e.effect_id == "video.transition"), None
|
|
484
|
+
)
|
|
451
485
|
if incoming:
|
|
452
|
-
effects+=(EffectRequest(
|
|
486
|
+
effects += (EffectRequest("video.transition_out", incoming.parameters),)
|
|
453
487
|
normalized_items.append(
|
|
454
488
|
MediaClip(
|
|
455
489
|
item.item_id,
|
|
@@ -494,7 +528,10 @@ class FfmpegPreviewRenderer:
|
|
|
494
528
|
progress(1.0)
|
|
495
529
|
|
|
496
530
|
def _concat_segments(
|
|
497
|
-
self,
|
|
531
|
+
self,
|
|
532
|
+
segments: list[Path],
|
|
533
|
+
output: Path,
|
|
534
|
+
cancel: threading.Event,
|
|
498
535
|
durations=None,
|
|
499
536
|
) -> None:
|
|
500
537
|
manifest = segments[0].parent / "segments.txt"
|
|
@@ -502,7 +539,10 @@ class FfmpegPreviewRenderer:
|
|
|
502
539
|
# AAC encoder padding may make a segment's container longer than
|
|
503
540
|
# its video. Letting concat infer that duration inserts a timestamp
|
|
504
541
|
# gap at each batch, changing avg_frame_rate despite correct frames.
|
|
505
|
-
"".join(
|
|
542
|
+
"".join(
|
|
543
|
+
f"file '{segment.name}'\n" + (f"duration {durations[n]}\n" if durations is not None else "")
|
|
544
|
+
for n, segment in enumerate(segments)
|
|
545
|
+
),
|
|
506
546
|
encoding="utf-8",
|
|
507
547
|
)
|
|
508
548
|
command = [
|
|
@@ -585,8 +625,7 @@ class FfmpegPreviewRenderer:
|
|
|
585
625
|
x = min(1.0, max(0.0, float(parameters.get("x", 0.5))))
|
|
586
626
|
y = min(1.0, max(0.0, float(parameters.get("y", 0.5))))
|
|
587
627
|
filters += (
|
|
588
|
-
",scale=iw*1.08:ih*1.08,"
|
|
589
|
-
f"crop={timeline.width}:{timeline.height}:(iw-ow)*{x:.4f}:(ih-oh)*{y:.4f}"
|
|
628
|
+
f",scale=iw*1.08:ih*1.08,crop={timeline.width}:{timeline.height}:(iw-ow)*{x:.4f}:(ih-oh)*{y:.4f}"
|
|
590
629
|
)
|
|
591
630
|
return filters
|
|
592
631
|
|
|
@@ -604,13 +643,10 @@ class FfmpegPreviewRenderer:
|
|
|
604
643
|
return source_label
|
|
605
644
|
outside_rate, center_rate = profile
|
|
606
645
|
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
|
-
)
|
|
646
|
+
filters.append(f"[{source_label}]split=3[vr{index}a][vr{index}b][vr{index}c]")
|
|
610
647
|
filters.extend(
|
|
611
648
|
(
|
|
612
|
-
f"[vr{index}a]trim=start=0:end={first_end:.6f},"
|
|
613
|
-
f"setpts=(PTS-STARTPTS)/{outside_rate:.6f}[vr{index}ao]",
|
|
649
|
+
f"[vr{index}a]trim=start=0:end={first_end:.6f},setpts=(PTS-STARTPTS)/{outside_rate:.6f}[vr{index}ao]",
|
|
614
650
|
f"[vr{index}b]trim=start={first_end:.6f}:end={center_end:.6f},"
|
|
615
651
|
f"setpts=(PTS-STARTPTS)/{center_rate:.6f}[vr{index}bo]",
|
|
616
652
|
f"[vr{index}c]trim=start={center_end:.6f}:end={duration:.6f},"
|
|
@@ -636,13 +672,10 @@ class FfmpegPreviewRenderer:
|
|
|
636
672
|
first_end, center_end = duration * 0.4, duration * 0.6
|
|
637
673
|
outside_tempo = cls._atempo(outside_rate)
|
|
638
674
|
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
|
-
)
|
|
675
|
+
filters.append(f"[{source_label}]asplit=3[ar{index}a][ar{index}b][ar{index}c]")
|
|
642
676
|
filters.extend(
|
|
643
677
|
(
|
|
644
|
-
f"[ar{index}a]atrim=start=0:end={first_end:.6f},"
|
|
645
|
-
f"asetpts=PTS-STARTPTS,{outside_tempo}[ar{index}ao]",
|
|
678
|
+
f"[ar{index}a]atrim=start=0:end={first_end:.6f},asetpts=PTS-STARTPTS,{outside_tempo}[ar{index}ao]",
|
|
646
679
|
f"[ar{index}b]atrim=start={first_end:.6f}:end={center_end:.6f},"
|
|
647
680
|
f"asetpts=PTS-STARTPTS,{center_tempo}[ar{index}bo]",
|
|
648
681
|
f"[ar{index}c]atrim=start={center_end:.6f}:end={duration:.6f},"
|
|
@@ -700,9 +733,5 @@ class FfmpegPreviewRenderer:
|
|
|
700
733
|
|
|
701
734
|
@staticmethod
|
|
702
735
|
def _seconds(pts: str, time_base) -> str:
|
|
703
|
-
value = (
|
|
704
|
-
Decimal(pts)
|
|
705
|
-
* Decimal(time_base.numerator)
|
|
706
|
-
/ Decimal(time_base.denominator)
|
|
707
|
-
)
|
|
736
|
+
value = Decimal(pts) * Decimal(time_base.numerator) / Decimal(time_base.denominator)
|
|
708
737
|
return format(value.quantize(Decimal("0.000001")), "f")
|
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
from __future__ import annotations
|
|
4
4
|
|
|
5
|
-
from pathlib import Path
|
|
6
5
|
import secrets
|
|
7
6
|
import sqlite3
|
|
7
|
+
from pathlib import Path
|
|
8
8
|
|
|
9
9
|
from clipen.filesystem import restrict_permissions
|
|
10
10
|
from clipen.projects import ProjectError, ProjectRepository
|
|
@@ -16,18 +16,14 @@ class PreviewRenderRepository:
|
|
|
16
16
|
def __init__(self, projects: ProjectRepository) -> None:
|
|
17
17
|
self.projects = projects
|
|
18
18
|
|
|
19
|
-
def create(
|
|
20
|
-
self, project_id: str, request_id: object, timeline_id: str
|
|
21
|
-
) -> dict[str, object]:
|
|
19
|
+
def create(self, project_id: str, request_id: object, timeline_id: str) -> dict[str, object]:
|
|
22
20
|
validate_project_id(project_id)
|
|
23
21
|
request = validate_request_id(request_id)
|
|
24
22
|
now = utc_now()
|
|
25
23
|
try:
|
|
26
24
|
with self.projects.connect(project_id) as database:
|
|
27
25
|
database.execute("BEGIN IMMEDIATE")
|
|
28
|
-
existing = database.execute(
|
|
29
|
-
"SELECT * FROM preview_renders WHERE request_id=?", (request,)
|
|
30
|
-
).fetchone()
|
|
26
|
+
existing = database.execute("SELECT * FROM preview_renders WHERE request_id=?", (request,)).fetchone()
|
|
31
27
|
if existing is not None:
|
|
32
28
|
if existing["timeline_id"] != timeline_id:
|
|
33
29
|
database.rollback()
|
|
@@ -70,7 +66,6 @@ class PreviewRenderRepository:
|
|
|
70
66
|
).fetchone()
|
|
71
67
|
return None if row is None else self._public(row)
|
|
72
68
|
|
|
73
|
-
|
|
74
69
|
def get(self, project_id: str, render_id: str) -> dict[str, object]:
|
|
75
70
|
validate_project_id(project_id)
|
|
76
71
|
with self.projects.connect(project_id) as database:
|
|
@@ -110,8 +105,7 @@ class PreviewRenderRepository:
|
|
|
110
105
|
values.extend([render_id, project_id])
|
|
111
106
|
with self.projects.connect(project_id) as database:
|
|
112
107
|
database.execute(
|
|
113
|
-
f"UPDATE preview_renders SET {','.join(fields)} "
|
|
114
|
-
"WHERE render_id=? AND project_id=?",
|
|
108
|
+
f"UPDATE preview_renders SET {','.join(fields)} WHERE render_id=? AND project_id=?",
|
|
115
109
|
values,
|
|
116
110
|
)
|
|
117
111
|
return self.get(project_id, render_id)
|
|
@@ -127,14 +121,11 @@ class PreviewRenderRepository:
|
|
|
127
121
|
)
|
|
128
122
|
return self.get(project_id, render_id)
|
|
129
123
|
|
|
130
|
-
def update_progress(
|
|
131
|
-
self, project_id: str, render_id: str, progress: float
|
|
132
|
-
) -> dict[str, object]:
|
|
124
|
+
def update_progress(self, project_id: str, render_id: str, progress: float) -> dict[str, object]:
|
|
133
125
|
normalized = max(0.0, min(1.0, progress))
|
|
134
126
|
with self.projects.connect(project_id) as database:
|
|
135
127
|
database.execute(
|
|
136
|
-
"UPDATE preview_renders SET progress=? "
|
|
137
|
-
"WHERE render_id=? AND project_id=? AND state='running'",
|
|
128
|
+
"UPDATE preview_renders SET progress=? WHERE render_id=? AND project_id=? AND state='running'",
|
|
138
129
|
(normalized, render_id, project_id),
|
|
139
130
|
)
|
|
140
131
|
return self.get(project_id, render_id)
|
|
@@ -189,12 +180,11 @@ class PreviewRenderRepository:
|
|
|
189
180
|
cursor = database.execute(
|
|
190
181
|
"UPDATE preview_renders SET state='interrupted',error_code='attempt_ended',completed_at=? "
|
|
191
182
|
"WHERE state IN ('queued','running','cancelling') AND request_id LIKE ? ESCAPE '\\'",
|
|
192
|
-
(utc_now(),
|
|
183
|
+
(utc_now(), "agent-preview-" + run_id.replace("_", "\\_") + "-%"),
|
|
193
184
|
)
|
|
194
185
|
database.commit()
|
|
195
186
|
return cursor.rowcount
|
|
196
187
|
|
|
197
|
-
|
|
198
188
|
def output_path(
|
|
199
189
|
self,
|
|
200
190
|
project_id: str,
|
|
@@ -214,9 +204,7 @@ class PreviewRenderRepository:
|
|
|
214
204
|
name = f".{render_id}.partial.mp4" if staging else f"{render_id}.mp4"
|
|
215
205
|
return directory / name
|
|
216
206
|
if directory.is_symlink() or not directory.is_dir():
|
|
217
|
-
raise ProjectError(
|
|
218
|
-
"storage_corrupt", "Clipen 無法安全建立初剪資料夾。", status_code=500
|
|
219
|
-
)
|
|
207
|
+
raise ProjectError("storage_corrupt", "Clipen 無法安全建立初剪資料夾。", status_code=500)
|
|
220
208
|
if prepare:
|
|
221
209
|
restrict_permissions(directory, 0o700)
|
|
222
210
|
name = f".{render_id}.partial.mp4" if staging else f"{render_id}.mp4"
|
|
@@ -235,8 +223,7 @@ class PreviewRenderRepository:
|
|
|
235
223
|
"started_at": row["started_at"],
|
|
236
224
|
"completed_at": row["completed_at"],
|
|
237
225
|
"playback_url": (
|
|
238
|
-
f"/api/projects/{row['project_id']}/preview/"
|
|
239
|
-
f"{row['render_id']}/media"
|
|
226
|
+
f"/api/projects/{row['project_id']}/preview/{row['render_id']}/media"
|
|
240
227
|
if row["state"] == "succeeded" and row["output_relative_path"]
|
|
241
228
|
else None
|
|
242
229
|
),
|