@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
|
@@ -5,32 +5,53 @@ those stay available for repair tools. People see a version when an AI request
|
|
|
5
5
|
ends (finished, or stopped part way with changes) and after each restore.
|
|
6
6
|
Projects from before review notes may also hold manual edits, still labelled below.
|
|
7
7
|
"""
|
|
8
|
+
|
|
8
9
|
import json
|
|
9
10
|
import sqlite3
|
|
10
11
|
|
|
11
12
|
from clipen.provider_registry import get_provider
|
|
12
13
|
|
|
13
|
-
_STOPS = {
|
|
14
|
+
_STOPS = {"provider_failed", "run_cancelled", "run_interrupted", "budget_exhausted"}
|
|
14
15
|
_STOP_REASONS = {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
16
|
+
"provider_usage_limit": "額度不足時停下",
|
|
17
|
+
"provider_timed_out": "超過時間上限時停下",
|
|
18
|
+
"provider_authentication_required": "登入過期時停下",
|
|
19
|
+
"provider_stopped_early": "AI 提早結束",
|
|
20
|
+
"delivery_blocked": "交付檢查未通過",
|
|
21
|
+
"provider_network_error": "連線中斷時停下",
|
|
18
22
|
}
|
|
19
23
|
# Labels for manual edits: rewording a graphic now, and the edits earlier Clipen versions allowed.
|
|
20
|
-
_MANUAL = {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
24
|
+
_MANUAL = {
|
|
25
|
+
"edit_text": "文字與時間",
|
|
26
|
+
"configure_text_style": "文字外觀",
|
|
27
|
+
"remove_item": "刪除元素",
|
|
28
|
+
"trim_clip": "裁切片段",
|
|
29
|
+
"reorder_clips": "重排片段",
|
|
30
|
+
"split_clip": "分割片段",
|
|
31
|
+
"replace_clip_source": "替換來源",
|
|
32
|
+
"insert_clip": "插入片段",
|
|
33
|
+
"set_source_audio": "原音",
|
|
34
|
+
"set_color_grade": "調色",
|
|
35
|
+
"set_mosaic": "馬賽克",
|
|
36
|
+
"set_transition": "轉場",
|
|
37
|
+
"set_fade": "淡入淡出",
|
|
38
|
+
"remove_clip_effect": "移除效果",
|
|
39
|
+
"place_audio": "音樂與音效",
|
|
40
|
+
"remove_audio": "移除聲音",
|
|
41
|
+
"remove_program_component": "移除節目元件",
|
|
42
|
+
"add_custom_component": "新增自由設計元件",
|
|
43
|
+
"update_custom_component": "修改自由設計元件",
|
|
44
|
+
"set_component_text": "元件文字",
|
|
45
|
+
"edit_component": "修改元件",
|
|
46
|
+
"edit_component_look": "元件外觀",
|
|
47
|
+
}
|
|
27
48
|
|
|
28
49
|
|
|
29
50
|
def _name(provider_id):
|
|
30
51
|
try:
|
|
31
52
|
return get_provider(provider_id).display_name
|
|
32
53
|
except ValueError:
|
|
33
|
-
return
|
|
54
|
+
return "AI"
|
|
34
55
|
|
|
35
56
|
|
|
36
57
|
def versions(database, run_id, provider, styles=None, current=None):
|
|
@@ -39,9 +60,11 @@ def versions(database, run_id, provider, styles=None, current=None):
|
|
|
39
60
|
rows = database.execute(
|
|
40
61
|
"SELECT sequence,event_type,payload_json,created_at FROM edit_agent_events WHERE run_id=? AND event_type IN "
|
|
41
62
|
"('run_created','refinement_requested','revision_created','revision_selected','run_finished','provider_failed','run_cancelled',"
|
|
42
|
-
"'run_interrupted','budget_exhausted','tool_completed','provider_switched','provider_finished') ORDER BY sequence",
|
|
43
|
-
|
|
44
|
-
|
|
63
|
+
"'run_interrupted','budget_exhausted','tool_completed','provider_switched','provider_finished') ORDER BY sequence",
|
|
64
|
+
(run_id,),
|
|
65
|
+
).fetchall()
|
|
66
|
+
switches = [json.loads(r["payload_json"]) for r in rows if r["event_type"] == "provider_switched"]
|
|
67
|
+
current_ai = switches[0]["from"] if switches else provider
|
|
45
68
|
latest = start = None
|
|
46
69
|
request = None
|
|
47
70
|
sent = {}
|
|
@@ -53,81 +76,106 @@ def versions(database, run_id, provider, styles=None, current=None):
|
|
|
53
76
|
|
|
54
77
|
def add(kind, label, created_at, detail=None):
|
|
55
78
|
nonlocal pending, last_ai
|
|
56
|
-
if latest is None or (result and result[-1][
|
|
79
|
+
if latest is None or (result and result[-1]["revision_id"] == latest):
|
|
57
80
|
if latest is not None:
|
|
58
81
|
skipped.append((kind, label, created_at))
|
|
59
82
|
return
|
|
60
|
-
entry = {
|
|
61
|
-
if kind.startswith(
|
|
62
|
-
entry[
|
|
83
|
+
entry = {"revision_id": latest, "kind": kind, "label": label, "detail": detail, "created_at": created_at}
|
|
84
|
+
if kind.startswith("ai_"):
|
|
85
|
+
entry["usage"], pending, last_ai = pending, None, (entry, span)
|
|
63
86
|
result.append(entry)
|
|
64
87
|
|
|
65
88
|
for row in rows:
|
|
66
|
-
kind, payload = row[
|
|
67
|
-
usage = payload.get(
|
|
89
|
+
kind, payload = row["event_type"], json.loads(row["payload_json"])
|
|
90
|
+
usage = payload.get("usage") if kind in ("provider_finished", "provider_failed") else None
|
|
68
91
|
if isinstance(usage, dict):
|
|
69
92
|
# A finished attempt reports after its version is listed; a stopped one before.
|
|
70
|
-
if kind ==
|
|
71
|
-
last_ai[0][
|
|
93
|
+
if kind == "provider_finished" and last_ai is not None and last_ai[1] == span:
|
|
94
|
+
last_ai[0]["usage"] = add_usage(last_ai[0]["usage"], usage)
|
|
72
95
|
else:
|
|
73
96
|
pending = add_usage(pending, usage)
|
|
74
|
-
if kind in (
|
|
97
|
+
if kind in ("run_created", "refinement_requested"):
|
|
75
98
|
# An attempt that changed nothing has no version to show it; it still counts in the film's total.
|
|
76
99
|
span, pending = span + 1, None
|
|
77
|
-
if kind in (
|
|
78
|
-
latest = payload.get(
|
|
100
|
+
if kind in ("revision_created", "revision_selected"):
|
|
101
|
+
latest = payload.get("revision_id", latest)
|
|
79
102
|
if start is None:
|
|
80
103
|
start = latest
|
|
81
|
-
elif kind ==
|
|
104
|
+
elif kind == "run_created":
|
|
82
105
|
request, start, sent = None, latest, {}
|
|
83
|
-
elif kind ==
|
|
84
|
-
request, start = payload.get(
|
|
85
|
-
sent = {
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
106
|
+
elif kind == "refinement_requested":
|
|
107
|
+
request, start = payload.get("instruction"), payload.get("base_revision_id", latest)
|
|
108
|
+
sent = {
|
|
109
|
+
item.get("reference_id"): item
|
|
110
|
+
for item in (payload.get("references") or {}).get("items", [])
|
|
111
|
+
if isinstance(item, dict)
|
|
112
|
+
}
|
|
113
|
+
elif kind == "provider_switched":
|
|
114
|
+
current_ai = payload.get("to", current_ai)
|
|
115
|
+
elif kind == "run_finished":
|
|
116
|
+
add("ai_complete", f"{_name(current_ai)} {'打磨' if request else '製作'}完成", row["created_at"], request)
|
|
117
|
+
if result and result[-1]["revision_id"] == latest:
|
|
118
|
+
result[-1]["report"] = _report(payload, sent)
|
|
92
119
|
start = latest
|
|
93
120
|
elif kind in _STOPS:
|
|
94
121
|
# A stop that changed nothing is not a version.
|
|
95
122
|
if latest != start:
|
|
96
|
-
reason = _STOP_REASONS.get(payload.get(
|
|
97
|
-
add(
|
|
123
|
+
reason = _STOP_REASONS.get(payload.get("error_code"), "製作中斷")
|
|
124
|
+
add("ai_partial", f"{_name(current_ai)} 未完成({reason})", row["created_at"], request)
|
|
98
125
|
start = latest
|
|
99
|
-
elif kind ==
|
|
100
|
-
tool = payload.get(
|
|
101
|
-
add(
|
|
102
|
-
|
|
126
|
+
elif kind == "tool_completed" and payload.get("authority") == "user":
|
|
127
|
+
tool = payload.get("tool")
|
|
128
|
+
add(
|
|
129
|
+
"restore" if tool == "restore_revision" else "manual",
|
|
130
|
+
"還原版本" if tool == "restore_revision" else f"手動修改:{_MANUAL.get(tool, '編輯')}",
|
|
131
|
+
row["created_at"],
|
|
132
|
+
)
|
|
103
133
|
start = latest
|
|
104
134
|
# Older projects did not record a step that went back to an earlier picture; show where the film is now.
|
|
105
|
-
if current and result and result[-1][
|
|
135
|
+
if current and result and result[-1]["revision_id"] != current and skipped:
|
|
106
136
|
kind, label, created_at = skipped[-1]
|
|
107
|
-
result.append({
|
|
137
|
+
result.append({"revision_id": current, "kind": kind, "label": label, "detail": None, "created_at": created_at})
|
|
108
138
|
_describe(database, run_id, result, styles or {})
|
|
109
139
|
for number, entry in enumerate(result, 1):
|
|
110
|
-
entry[
|
|
140
|
+
entry["number"] = number
|
|
111
141
|
return list(reversed(result))
|
|
112
142
|
|
|
113
143
|
|
|
114
144
|
def _report(payload, sent):
|
|
115
145
|
"""What the AI said it changed, with each answer placed next to the note it answers."""
|
|
116
146
|
from .change_report import OUTCOMES
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
147
|
+
|
|
148
|
+
report = payload.get("report") or {}
|
|
149
|
+
notes = [
|
|
150
|
+
{
|
|
151
|
+
**note,
|
|
152
|
+
"outcome_label": OUTCOMES.get(note.get("outcome"), ""),
|
|
153
|
+
"kind": (sent.get(note.get("reference_id")) or {}).get("kind"),
|
|
154
|
+
"note": (sent.get(note.get("reference_id")) or {}).get("note", ""),
|
|
155
|
+
}
|
|
156
|
+
for note in report.get("notes", [])
|
|
157
|
+
]
|
|
120
158
|
from .change_report import REASONS
|
|
159
|
+
|
|
121
160
|
# Reports written before reasons were required hold plain lines.
|
|
122
|
-
left = [
|
|
123
|
-
|
|
161
|
+
left = [
|
|
162
|
+
{"what": item, "reason": None, "reason_label": "", "detail": ""}
|
|
163
|
+
if isinstance(item, str)
|
|
164
|
+
else {**item, "reason_label": REASONS.get(item.get("reason"), "")}
|
|
165
|
+
for item in report.get("not_done", [])
|
|
166
|
+
]
|
|
124
167
|
for note in notes:
|
|
125
|
-
note[
|
|
126
|
-
return {
|
|
168
|
+
note["reason_label"] = REASONS.get(note.get("reason"), "")
|
|
169
|
+
return {
|
|
170
|
+
"summary": payload.get("summary") or "",
|
|
171
|
+
"changes": report.get("changes", []),
|
|
172
|
+
"notes": notes,
|
|
173
|
+
"not_done": left,
|
|
174
|
+
}
|
|
127
175
|
|
|
128
176
|
|
|
129
177
|
def add_usage(total, usage):
|
|
130
|
-
keys = (
|
|
178
|
+
keys = ("input_tokens", "cached_input_tokens", "output_tokens")
|
|
131
179
|
if not isinstance(usage, dict) or not all(isinstance(usage.get(k), int) for k in keys):
|
|
132
180
|
return total
|
|
133
181
|
return {k: (total or {}).get(k, 0) + usage[k] for k in keys}
|
|
@@ -136,26 +184,36 @@ def add_usage(total, usage):
|
|
|
136
184
|
def film_usage(database, run_id):
|
|
137
185
|
"""Every token the AI used on this film, including attempts that changed nothing."""
|
|
138
186
|
total = None
|
|
139
|
-
for row in database.execute(
|
|
140
|
-
|
|
187
|
+
for row in database.execute(
|
|
188
|
+
"SELECT payload_json FROM edit_agent_events WHERE run_id=? AND event_type IN ('provider_finished','provider_failed')",
|
|
189
|
+
(run_id,),
|
|
190
|
+
):
|
|
191
|
+
total = add_usage(total, json.loads(row["payload_json"]).get("usage"))
|
|
141
192
|
return total
|
|
142
193
|
|
|
143
194
|
|
|
144
195
|
def _describe(database, run_id, result, styles):
|
|
145
196
|
"""What each version changed from the one before it, and which moment a restore went back to."""
|
|
146
197
|
from .history_changes import describe, summary
|
|
147
|
-
|
|
198
|
+
|
|
199
|
+
ids = sorted({entry["revision_id"] for entry in result})
|
|
148
200
|
payloads = {}
|
|
149
201
|
try:
|
|
150
202
|
for start in range(0, len(ids), 400):
|
|
151
|
-
chunk = ids[start:start+400]
|
|
152
|
-
for row in database.execute(
|
|
153
|
-
|
|
203
|
+
chunk = ids[start : start + 400]
|
|
204
|
+
for row in database.execute(
|
|
205
|
+
f"SELECT revision_id,payload_json FROM edit_agent_revisions WHERE run_id=? AND revision_id IN ({','.join('?' * len(chunk))})",
|
|
206
|
+
(run_id, *chunk),
|
|
207
|
+
):
|
|
208
|
+
payloads[row["revision_id"]] = json.loads(row["payload_json"])
|
|
154
209
|
except sqlite3.OperationalError:
|
|
155
210
|
pass # Without stored pictures the history still lists every step, only without what changed.
|
|
156
211
|
for index, entry in enumerate(result):
|
|
157
|
-
earlier = next((e for e in reversed(result[:index]) if e[
|
|
158
|
-
if entry[
|
|
159
|
-
entry[
|
|
160
|
-
before, after = (
|
|
161
|
-
|
|
212
|
+
earlier = next((e for e in reversed(result[:index]) if e["revision_id"] == entry["revision_id"]), None)
|
|
213
|
+
if entry["kind"] == "restore" and earlier is not None:
|
|
214
|
+
entry["restored_from"] = earlier["created_at"]
|
|
215
|
+
before, after = (
|
|
216
|
+
(payloads.get(result[index - 1]["revision_id"]) if index else None),
|
|
217
|
+
payloads.get(entry["revision_id"]),
|
|
218
|
+
)
|
|
219
|
+
entry["changes"] = summary(describe(before, after, styles)) if before and after else None
|
|
@@ -2,22 +2,26 @@
|
|
|
2
2
|
|
|
3
3
|
This is a static editing view, not approval of transitions, motion or sound.
|
|
4
4
|
"""
|
|
5
|
+
|
|
5
6
|
import base64
|
|
7
|
+
import math
|
|
8
|
+
import tempfile
|
|
9
|
+
import threading
|
|
6
10
|
from dataclasses import replace
|
|
7
11
|
from fractions import Fraction
|
|
8
12
|
from io import BytesIO
|
|
9
|
-
import math
|
|
10
13
|
from pathlib import Path
|
|
11
|
-
|
|
12
|
-
import threading
|
|
14
|
+
|
|
13
15
|
from PIL import Image, ImageDraw, ImageFont
|
|
14
|
-
|
|
16
|
+
|
|
17
|
+
from clipen.domain import EffectRequest, TrackKind
|
|
18
|
+
from clipen.finishing.audio import run_process
|
|
15
19
|
from clipen.media import MediaPreviewRepository
|
|
16
20
|
from clipen.projects import ProjectError
|
|
17
|
-
from clipen.finishing.audio import run_process
|
|
18
|
-
from clipen.rendering.renderer import FfmpegPreviewRenderer
|
|
19
21
|
from clipen.rendering.production_effects import grade_filters
|
|
22
|
+
from clipen.rendering.renderer import FfmpegPreviewRenderer
|
|
20
23
|
from clipen.rendering.titles import TitleCardRenderer, TitleRenderError
|
|
24
|
+
|
|
21
25
|
from .production_media import load_text_styles
|
|
22
26
|
|
|
23
27
|
|
|
@@ -26,68 +30,160 @@ def frame(repository, project_id, run_id, expected_revision_id, frame_number, co
|
|
|
26
30
|
|
|
27
31
|
component ({element_id, ...unsaved settings}) draws that graphic as the person's changes would
|
|
28
32
|
leave it, and the result says where it sits so the page can let them drag it."""
|
|
29
|
-
revision=repository.current_revision(project_id,run_id)
|
|
30
|
-
|
|
31
|
-
if
|
|
32
|
-
raise ProjectError(
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
33
|
+
revision = repository.current_revision(project_id, run_id)
|
|
34
|
+
timeline = revision.timeline
|
|
35
|
+
if revision.revision_id != expected_revision_id:
|
|
36
|
+
raise ProjectError("revision_conflict", "影片已更新,正在重新讀取編輯畫面。", status_code=409)
|
|
37
|
+
if (
|
|
38
|
+
isinstance(frame_number, bool)
|
|
39
|
+
or not isinstance(frame_number, int)
|
|
40
|
+
or not 0 <= frame_number < timeline.duration_frames
|
|
41
|
+
):
|
|
42
|
+
raise ProjectError("frame_invalid", "預覽時間不在影片內。", status_code=422)
|
|
43
|
+
items = [i for t in timeline.tracks if t.kind is TrackKind.TITLE for i in t.items]
|
|
44
|
+
styles = load_text_styles(repository.projects, project_id)
|
|
45
|
+
draft = profile = None
|
|
36
46
|
if component is not None:
|
|
37
47
|
from .workbench import tools_for
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
48
|
+
|
|
49
|
+
draft, profile, _ = tools_for(repository, project_id, run_id)._component_draft(component)
|
|
50
|
+
styles = {**styles, draft.style_id: profile}
|
|
51
|
+
items = [draft if i.item_id == draft.item_id else i for i in items]
|
|
52
|
+
clip = next(
|
|
53
|
+
(
|
|
54
|
+
i
|
|
55
|
+
for t in timeline.tracks
|
|
56
|
+
if t.kind is TrackKind.VIDEO
|
|
57
|
+
for i in t.items
|
|
58
|
+
if i.start_frame <= frame_number < i.start_frame + i.duration_frames
|
|
59
|
+
),
|
|
60
|
+
None,
|
|
61
|
+
)
|
|
62
|
+
if clip is None:
|
|
63
|
+
raise ProjectError("frame_invalid", "這個時間沒有畫面片段。", status_code=422)
|
|
64
|
+
fps = Fraction(timeline.frame_rate.numerator, timeline.frame_rate.denominator)
|
|
65
|
+
relative = frame_number - clip.start_frame
|
|
66
|
+
span = clip.source.span
|
|
67
|
+
unit = Fraction(span.time_base.numerator, span.time_base.denominator)
|
|
68
|
+
duration = span.duration_pts * unit
|
|
69
|
+
elapsed = Fraction(relative, 1) / fps
|
|
70
|
+
speed = FfmpegPreviewRenderer._speed_profile(clip)
|
|
49
71
|
if speed:
|
|
50
|
-
outside,center=speed
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
72
|
+
outside, center = speed
|
|
73
|
+
first = float(duration) * 0.4 / outside
|
|
74
|
+
middle = float(duration) * 0.2 / center
|
|
75
|
+
t = float(elapsed)
|
|
76
|
+
elapsed = Fraction(
|
|
77
|
+
str(
|
|
78
|
+
t * outside
|
|
79
|
+
if t < first
|
|
80
|
+
else float(duration) * 0.4 + (t - first) * center
|
|
81
|
+
if t < first + middle
|
|
82
|
+
else float(duration) * 0.6 + (t - first - middle) * outside
|
|
83
|
+
)
|
|
84
|
+
)
|
|
85
|
+
seconds = int(span.start_pts) * unit + min(
|
|
86
|
+
max(Fraction(0), elapsed), max(Fraction(0), duration - Fraction(1, 1) / fps)
|
|
87
|
+
)
|
|
88
|
+
media = MediaPreviewRepository(repository.projects)
|
|
89
|
+
source = media.open(project_id, span.version_id)
|
|
54
90
|
try:
|
|
55
|
-
if span.stream_index not in source.video_stream_indices:
|
|
56
|
-
|
|
91
|
+
if span.stream_index not in source.video_stream_indices:
|
|
92
|
+
raise ProjectError("frame_invalid", "來源沒有指定的影像串流。", status_code=422)
|
|
93
|
+
effects = []
|
|
57
94
|
for effect in clip.effects:
|
|
58
|
-
if effect.effect_id==
|
|
59
|
-
p=effect.parameters
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
95
|
+
if effect.effect_id == "video.slow_push":
|
|
96
|
+
p = effect.parameters
|
|
97
|
+
scale = p.get("scale_from", 1) + (p.get("scale_to", 1.08) - p.get("scale_from", 1)) * relative / max(
|
|
98
|
+
1, clip.duration_frames - 1
|
|
99
|
+
)
|
|
100
|
+
effects.append(EffectRequest("video.punch_in", {"scale": scale, "center_x": 0.5, "center_y": 0.5}))
|
|
101
|
+
else:
|
|
102
|
+
effects.append(effect)
|
|
103
|
+
still_clip = replace(clip, effects=tuple(effects))
|
|
104
|
+
filters = f"scale={timeline.width}:{timeline.height}:force_original_aspect_ratio=decrease,pad={timeline.width}:{timeline.height}:(ow-iw)/2:(oh-ih)/2"
|
|
105
|
+
filters += FfmpegPreviewRenderer._video_effect_filters(still_clip, timeline) + grade_filters(clip)
|
|
106
|
+
with tempfile.TemporaryDirectory(prefix="clipen-edit-frame-") as temporary:
|
|
107
|
+
output = Path(temporary) / "source.png"
|
|
108
|
+
run_process(
|
|
109
|
+
[
|
|
110
|
+
"ffmpeg",
|
|
111
|
+
"-v",
|
|
112
|
+
"error",
|
|
113
|
+
"-nostdin",
|
|
114
|
+
"-seek_timestamp",
|
|
115
|
+
"1",
|
|
116
|
+
"-ss",
|
|
117
|
+
f"{float(seconds):.9f}",
|
|
118
|
+
"-i",
|
|
119
|
+
f"/dev/fd/{source.descriptor}",
|
|
120
|
+
"-map",
|
|
121
|
+
f"0:{span.stream_index}",
|
|
122
|
+
"-frames:v",
|
|
123
|
+
"1",
|
|
124
|
+
"-vf",
|
|
125
|
+
filters,
|
|
126
|
+
"-threads",
|
|
127
|
+
"1",
|
|
128
|
+
str(output),
|
|
129
|
+
],
|
|
130
|
+
threading.Event(),
|
|
131
|
+
pass_fds=(source.descriptor,),
|
|
132
|
+
timeout=30,
|
|
133
|
+
)
|
|
134
|
+
with Image.open(output) as pixels:
|
|
135
|
+
image = pixels.convert("RGBA")
|
|
136
|
+
finally:
|
|
137
|
+
source.close()
|
|
70
138
|
# Match the native mosaic geometry for this exact instant.
|
|
71
139
|
for effect in clip.effects:
|
|
72
|
-
if effect.effect_id!=
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
140
|
+
if effect.effect_id != "video.mosaic":
|
|
141
|
+
continue
|
|
142
|
+
for region in effect.parameters["regions"]:
|
|
143
|
+
if not region["start_frame"] <= relative < region["start_frame"] + region["duration_frames"]:
|
|
144
|
+
continue
|
|
145
|
+
width = max(2, min(timeline.width, round(timeline.width * region["width"]) // 2 * 2))
|
|
146
|
+
height = max(2, min(timeline.height, round(timeline.height * region["height"]) // 2 * 2))
|
|
147
|
+
x = min(timeline.width - width, round(timeline.width * region["x"]) // 2 * 2)
|
|
148
|
+
y = min(timeline.height - height, round(timeline.height * region["y"]) // 2 * 2)
|
|
149
|
+
pixels = image.crop((x, y, x + width, y + height))
|
|
150
|
+
pixels = pixels.resize(
|
|
151
|
+
(max(1, math.ceil(width / region["block_size"])), max(1, math.ceil(height / region["block_size"]))),
|
|
152
|
+
Image.Resampling.BOX,
|
|
153
|
+
).resize((width, height), Image.Resampling.NEAREST)
|
|
154
|
+
image.paste(pixels, (x, y))
|
|
155
|
+
font = TitleCardRenderer._font_path()
|
|
156
|
+
if font is None:
|
|
157
|
+
raise ProjectError("preview_title_font_missing", "找不到預覽字型。", status_code=422)
|
|
158
|
+
overlay = Image.new("RGBA", image.size, (0, 0, 0, 0))
|
|
159
|
+
draw = ImageDraw.Draw(overlay)
|
|
160
|
+
renderer = TitleCardRenderer(styles)
|
|
81
161
|
try:
|
|
82
162
|
for item in items:
|
|
83
|
-
if item.start_frame<=frame_number<item.start_frame+item.duration_frames:
|
|
84
|
-
renderer._draw_card(draw,ImageFont,font,timeline,item,frame_number-item.start_frame)
|
|
85
|
-
except TitleRenderError as error:
|
|
86
|
-
|
|
87
|
-
|
|
163
|
+
if item.start_frame <= frame_number < item.start_frame + item.duration_frames:
|
|
164
|
+
renderer._draw_card(draw, ImageFont, font, timeline, item, frame_number - item.start_frame)
|
|
165
|
+
except TitleRenderError as error:
|
|
166
|
+
raise ProjectError(error.code, "文字超出版面或無法顯示,請縮短文字或調整位置。", status_code=422) from error
|
|
167
|
+
box = None
|
|
168
|
+
if draft is not None and draft.start_frame <= frame_number < draft.start_frame + draft.duration_frames:
|
|
88
169
|
from clipen.rendering.program_components import component_bounds
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
170
|
+
|
|
171
|
+
left, top, right, bottom = component_bounds(draw, font, timeline, draft, profile)[0]
|
|
172
|
+
box = {
|
|
173
|
+
"x": left / timeline.width,
|
|
174
|
+
"y": top / timeline.height,
|
|
175
|
+
"width": (right - left) / timeline.width,
|
|
176
|
+
"height": (bottom - top) / timeline.height,
|
|
177
|
+
}
|
|
178
|
+
image = Image.alpha_composite(image, overlay).convert("RGB")
|
|
179
|
+
image.thumbnail((960, 960))
|
|
180
|
+
encoded = BytesIO()
|
|
181
|
+
image.save(encoded, format="JPEG", quality=88)
|
|
182
|
+
return {
|
|
183
|
+
"revision_id": revision.revision_id,
|
|
184
|
+
"frame": frame_number,
|
|
185
|
+
"source_item_id": clip.item_id,
|
|
186
|
+
"image_data_url": "data:image/jpeg;base64," + base64.b64encode(encoded.getvalue()).decode(),
|
|
187
|
+
"scope": "editing_still",
|
|
188
|
+
"component_box": box,
|
|
189
|
+
}
|