@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,234 @@
|
|
|
1
|
+
"""Turn Clipen's components into DaVinci Resolve titles whose words stay editable.
|
|
2
|
+
|
|
3
|
+
Resolve keeps Fusion titles only in its own timeline format (.drt), which it
|
|
4
|
+
writes but never reads from FCPXML. So the hand-off goes through Resolve: the
|
|
5
|
+
FCPXML places each component as a transparent PNG, the person exports that
|
|
6
|
+
timeline from Resolve as .drt, and convert() swaps every component PNG on it
|
|
7
|
+
for a Fusion title. The title draws the component's rows with Text+ (font,
|
|
8
|
+
size, tracking, colour and shadow as Clipen set them) over a picture of
|
|
9
|
+
everything else (boxes, rules, icons), and publishes each row's words to the
|
|
10
|
+
Edit page Inspector.
|
|
11
|
+
|
|
12
|
+
A .drt is a zip of XML. A clip's Fusion composition is a qCompress'd Qt
|
|
13
|
+
QVariantMap whose "0_data" is the composition header, a NUL, a little-endian
|
|
14
|
+
length and the zlib'd tool list; DaVinci Resolve 21 writes and reads that.
|
|
15
|
+
"""
|
|
16
|
+
from __future__ import annotations
|
|
17
|
+
|
|
18
|
+
import html
|
|
19
|
+
import json
|
|
20
|
+
import re
|
|
21
|
+
import struct
|
|
22
|
+
import unicodedata
|
|
23
|
+
import uuid
|
|
24
|
+
import zipfile
|
|
25
|
+
import zlib
|
|
26
|
+
from pathlib import Path
|
|
27
|
+
|
|
28
|
+
from clipen.projects import ProjectError
|
|
29
|
+
|
|
30
|
+
MANIFEST = '.clipen 元件.json'
|
|
31
|
+
# Text+ Size is a fraction of the frame width, and a glyph's em is 1/1.76 of it; Text+ tracking adds
|
|
32
|
+
# (CharacterSpacing - 1) x Size x width between letters. Measured in Resolve 21 on PingFang TC and Songti TC.
|
|
33
|
+
_SIZE = 1.76
|
|
34
|
+
_ITEM = re.compile(r'<Sm2TiVideoClip DbId="[^"]+">.*?</Sm2TiVideoClip>', re.S)
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def _lua(value: str) -> str:
|
|
38
|
+
return '"' + value.replace('\\', '\\\\').replace('"', '\\"').replace('\n', '\\n') + '"'
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def _number(value: float) -> str:
|
|
42
|
+
return f'{value:.6g}'
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def tools(rows: list[dict], picture: Path | None, width: int, height: int, frames: int) -> str:
|
|
46
|
+
"""The Fusion tool list for one component: rows of Text+ merged over its picture."""
|
|
47
|
+
last = frames - 1
|
|
48
|
+
frame = f'GlobalOut = Input {{ Value = {last}, }}, Width = Input {{ Value = {width}, }}, Height = Input {{ Value = {height}, }}, UseFrameFormatSettings = Input {{ Value = 1, }}, '
|
|
49
|
+
nodes, inputs = [], []
|
|
50
|
+
if picture:
|
|
51
|
+
nodes.append(f'Picture = Loader {{ Clips = {{ Clip {{ ID = "Clip1", Filename = {_lua(str(picture))}, FormatID = "PNGFormat", '
|
|
52
|
+
f'StartFrame = -1, LengthSetManually = true, TrimIn = 0, TrimOut = 0, ExtendFirst = 0, ExtendLast = {last}, '
|
|
53
|
+
f'Loop = 0, AspectMode = 0, Depth = 0, TimeCode = 0, GlobalStart = 0, GlobalEnd = {last} }} }}, '
|
|
54
|
+
'ViewInfo = OperatorInfo { Pos = { 0, 0 } }, }')
|
|
55
|
+
# Each row merges over what lies below it: the picture, or the first row when there is none.
|
|
56
|
+
below = 'Picture' if picture else None
|
|
57
|
+
for number, row in enumerate(rows, 1):
|
|
58
|
+
name, label = f'Row{number}', '' if len(rows) == 1 else f'第 {number} 行'
|
|
59
|
+
red, green, blue, alpha = (v / 255 for v in row['fill'])
|
|
60
|
+
shading = ''
|
|
61
|
+
if row['shadow']:
|
|
62
|
+
sr, sg, sb, sa = (v / 255 for v in row['shadow']['colour'])
|
|
63
|
+
offset = row['shadow']['offset']
|
|
64
|
+
shading = (f'Enabled2 = Input {{ Value = 1, }}, Name2 = Input {{ Value = "Shadow", }}, ElementShape2 = Input {{ Value = 0, }}, '
|
|
65
|
+
f'Red2 = Input {{ Value = {_number(sr)}, }}, Green2 = Input {{ Value = {_number(sg)}, }}, Blue2 = Input {{ Value = {_number(sb)}, }}, '
|
|
66
|
+
f'Alpha2 = Input {{ Value = {_number(sa)}, }}, Offset2 = Input {{ Value = {{ {_number(offset / width)}, {_number(-offset / height)} }}, }}, '
|
|
67
|
+
f'Softness2 = Input {{ Value = 1, }}, SoftnessX2 = Input {{ Value = {row["shadow"]["blur"]}, }}, SoftnessY2 = Input {{ Value = {row["shadow"]["blur"]}, }}, ')
|
|
68
|
+
nodes.append(
|
|
69
|
+
f'{name} = TextPlus {{ Inputs = {{ {frame}'
|
|
70
|
+
f'Center = Input {{ Value = {{ {_number(row["x"] / width)}, {_number(1 - row["baseline"] / height)} }}, }}, '
|
|
71
|
+
'CenterOnBaseOfFirstLine = Input { Value = 1, }, HorizontalLeftCenterRight = Input { Value = -1, }, '
|
|
72
|
+
'VerticalJustificationNew = Input { Value = 3, }, HorizontalJustificationNew = Input { Value = 3, }, '
|
|
73
|
+
f'StyledText = Input {{ Value = {_lua(row["text"])}, }}, Font = Input {{ Value = {_lua(row["family"])}, }}, '
|
|
74
|
+
f'Style = Input {{ Value = {_lua(row["style"])}, }}, Size = Input {{ Value = {_number(row["size"] * _SIZE / width)}, }}, '
|
|
75
|
+
f'CharacterSpacing = Input {{ Value = {_number(1 + row["track"] / _SIZE)}, }}, '
|
|
76
|
+
f'Red1 = Input {{ Value = {_number(red)}, }}, Green1 = Input {{ Value = {_number(green)}, }}, Blue1 = Input {{ Value = {_number(blue)}, }}, '
|
|
77
|
+
f'Alpha1 = Input {{ Value = {_number(alpha)}, }}, {shading}}}, ViewInfo = OperatorInfo {{ Pos = {{ 110, {60 * number} }} }}, }}')
|
|
78
|
+
if below:
|
|
79
|
+
nodes.append(f'Merge{number} = Merge {{ Inputs = {{ Background = Input {{ SourceOp = "{below}", Source = "Output", }}, '
|
|
80
|
+
f'Foreground = Input {{ SourceOp = "{name}", Source = "Output", }}, PerformDepthMerge = Input {{ Value = 0, }}, }}, '
|
|
81
|
+
f'ViewInfo = OperatorInfo {{ Pos = {{ 220, {60 * number} }} }}, }}')
|
|
82
|
+
below = f'Merge{number}' if below else name
|
|
83
|
+
group = 10 * number
|
|
84
|
+
inputs += [f'{name}Text = InstanceInput {{ SourceOp = "{name}", Source = "StyledText", Name = {_lua((label + " 文字").strip())}, }}',
|
|
85
|
+
f'{name}Font = InstanceInput {{ SourceOp = "{name}", Source = "Font", Name = {_lua((label + " 字型").strip())}, ControlGroup = {group}, }}',
|
|
86
|
+
f'{name}Style = InstanceInput {{ SourceOp = "{name}", Source = "Style", ControlGroup = {group}, }}',
|
|
87
|
+
f'{name}Size = InstanceInput {{ SourceOp = "{name}", Source = "Size", Name = {_lua((label + " 大小").strip())}, }}',
|
|
88
|
+
f'{name}Red = InstanceInput {{ SourceOp = "{name}", Source = "Red1Clone", Name = {_lua((label + " 顏色").strip())}, ControlGroup = {group + 1}, }}',
|
|
89
|
+
f'{name}Green = InstanceInput {{ SourceOp = "{name}", Source = "Green1Clone", ControlGroup = {group + 1}, }}',
|
|
90
|
+
f'{name}Blue = InstanceInput {{ SourceOp = "{name}", Source = "Blue1Clone", ControlGroup = {group + 1}, }}',
|
|
91
|
+
f'{name}Alpha = InstanceInput {{ SourceOp = "{name}", Source = "Alpha1Clone", ControlGroup = {group + 1}, }}']
|
|
92
|
+
# Resolve shows a Text+ title's controls from a tool called Template, and MediaOut reads the tool inside the group.
|
|
93
|
+
return ('{ Template = GroupOperator { CtrlWZoom = false, Inputs = ordered() { ' + ', '.join(inputs) + ' }, '
|
|
94
|
+
f'Outputs = {{ MainOutput1 = InstanceOutput {{ SourceOp = "{below}", Source = "Output", }} }}, '
|
|
95
|
+
'ViewInfo = GroupInfo { Pos = { 0, 0 } }, Tools = ordered() { ' + ', '.join(nodes) + ' }, }, '
|
|
96
|
+
f'MediaOut1 = MediaOut {{ Inputs = {{ Index = Input {{ Value = "0", }}, Input = Input {{ SourceOp = "{below}", Source = "Output", }} }}, '
|
|
97
|
+
'ViewInfo = OperatorInfo { Pos = { 330, 0 } }, } }')
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def _qstring(value: str) -> bytes:
|
|
101
|
+
data = value.encode('utf-16-be')
|
|
102
|
+
return struct.pack('>I', len(data)) + data
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def composition(tool_list: str, width: int, height: int, frames: int) -> str:
|
|
106
|
+
"""A clip's CompositionBA: the tool list wrapped the way Resolve stores a Text+ title's comp."""
|
|
107
|
+
last = frames - 1
|
|
108
|
+
header = (f'Composition {{ CurrentTime = 0, RenderRange = {{ 0, {last} }}, GlobalRange = {{ 0, {last} }}, CurrentID = 1, HiQ = true, '
|
|
109
|
+
'PlaybackUpdateMode = 0, StereoMode = false, Version = "DaVinci Resolve 21.1.2.0010", SavedOutputs = 1, HeldTools = 0, '
|
|
110
|
+
'DisabledTools = 0, LockedTools = 0, AudioOffset = 0, Resumable = true, OutputClips = { }, CustomData = { TEMPLATE_ID = "Text+" }, '
|
|
111
|
+
f'Prefs = {{ Comp = {{ FrameFormat = {{ Width = {width}, Height = {height}, GuideRatio = {_number(width / height)}, '
|
|
112
|
+
f'DepthFull = 3, DepthPreview = 3, DepthInteractive = 3, }}, Unsorted = {{ GlobalEnd = {last} }}, }} }}, Compressed = true, }}')
|
|
113
|
+
inner = tool_list.encode('utf-8')
|
|
114
|
+
data = header.encode('utf-8') + b'\x00' + struct.pack('<I', len(inner)) + zlib.compress(inner, 9)
|
|
115
|
+
variant = struct.pack('>II', 1, 4)
|
|
116
|
+
variant += _qstring('count') + struct.pack('>IBi', 2, 0, 1)
|
|
117
|
+
variant += _qstring('0_name') + struct.pack('>IB', 10, 0) + _qstring('Composition 1')
|
|
118
|
+
variant += _qstring('0_mediaIdList') + struct.pack('>IBI', 11, 0, 0)
|
|
119
|
+
variant += _qstring('0_data') + struct.pack('>IBI', 12, 0, len(data)) + data
|
|
120
|
+
return (struct.pack('>I', len(variant)) + zlib.compress(variant, 9)).hex()
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
def _varint(value: int) -> bytes:
|
|
124
|
+
out = bytearray()
|
|
125
|
+
while True:
|
|
126
|
+
byte, value = value & 0x7f, value >> 7
|
|
127
|
+
out.append(byte | (0x80 if value else 0))
|
|
128
|
+
if not value:
|
|
129
|
+
return bytes(out)
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
def _fields(start: int) -> str:
|
|
133
|
+
"""A Text+ Fusion Title clip's FieldsBlob: its template name and the frame it starts on."""
|
|
134
|
+
name = b'Text+'
|
|
135
|
+
title = b'\x22' + bytes([len(name)]) + name + b'\x4a' + bytes([len(name)]) + name + b'\xa8\x01' + _varint(start)
|
|
136
|
+
clip = b'\x0a' + _varint(len(title)) + title + b'\x20\x01'
|
|
137
|
+
body = b'\x0a' + _varint(len(clip)) + clip + b'\x12\x10' + bytes.fromhex('0000000000000006ffffffffffffffff')
|
|
138
|
+
return (struct.pack('>II', 2, len(body) + 1) + b'\x80' + body).hex()
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
def title_item(start: int, duration: int, frame_rate: str, comp: str, indent: str) -> str:
|
|
142
|
+
"""A Fusion Title clip in a .drt sequence, shaped like the ones Resolve 21 writes."""
|
|
143
|
+
lines = [
|
|
144
|
+
f'<Sm2TiVideoClip DbId="{uuid.uuid4()}">', f' <FieldsBlob>{_fields(start)}</FieldsBlob>',
|
|
145
|
+
' <PrettyType>Fusion Title</PrettyType>', ' <Name>Fusion Title</Name>',
|
|
146
|
+
f' <Start>{start}</Start>', f' <Duration>{duration}</Duration>',
|
|
147
|
+
' <LinkedItemSync/>', ' <WasDisbanded>false</WasDisbanded>', ' <MarkersBA/>', ' <UiMemento>0</UiMemento>',
|
|
148
|
+
' <Flags>0</Flags>', ' <PriorityIndex>0</PriorityIndex>', ' <EffectFiltersBA/>', ' <ImportExportMetadataBA/>',
|
|
149
|
+
' <RenderTextEnabled>true</RenderTextEnabled>', ' <RenderTextGanged>true</RenderTextGanged>',
|
|
150
|
+
' <RenderTextPrefixed>true</RenderTextPrefixed>', ' <CompositionTable>',
|
|
151
|
+
f' <Sm2TiCompositionTable DbId="{uuid.uuid4()}">',
|
|
152
|
+
' <FieldsBlob>000000010000000200000016004c006100730074004d006f006400540069006d0065000000040000000000b5a8a2230000002c00460075007300'
|
|
153
|
+
'69006f006e00530074006100720074004600720061006d0065004f00660066007300650074000000020000000000</FieldsBlob>',
|
|
154
|
+
' <DbSavedTime>0</DbSavedTime>', f' <CompositionBA>{comp}</CompositionBA>',
|
|
155
|
+
' <ActiveCompositionIdx>0</ActiveCompositionIdx>', ' </Sm2TiCompositionTable>', ' </CompositionTable>',
|
|
156
|
+
' <In/>', ' <MixedFrameRateAlignment>0</MixedFrameRateAlignment>', ' <MediaStartTime>0</MediaStartTime>',
|
|
157
|
+
' <MediaFilePath/>', ' <MediaReelNumber/>', f' <MediaFrameRate>{frame_rate}</MediaFrameRate>',
|
|
158
|
+
' <MediaTimemapBA>024013ddddddddddde</MediaTimemapBA>', ' <LastChangedTime>0</LastChangedTime>',
|
|
159
|
+
' <LastRenderedTime>0</LastRenderedTime>', ' <IsMarkedForCaching>false</IsMarkedForCaching>',
|
|
160
|
+
' <IsForceConformed>true</IsForceConformed>', ' <MatchConflictState>0</MatchConflictState>',
|
|
161
|
+
' <UseOppositeSrcForLeftEye>false</UseOppositeSrcForLeftEye>', ' <UseOppositeSrcForRightEye>false</UseOppositeSrcForRightEye>',
|
|
162
|
+
' <RenderCacheBA/>', ' <CurrentSelectorIdx>1</CurrentSelectorIdx>', ' <IsPreConformed>false</IsPreConformed>',
|
|
163
|
+
' <PreConformMediaExtents>00000100000030c20000010000003042</PreConformMediaExtents>', ' <MediaMetadata/>',
|
|
164
|
+
' <Thumbnail>', f' <BtThumnail DbId="{uuid.uuid4()}">', ' <FieldsBlob/>', ' <ImgWidth>-1</ImgWidth>',
|
|
165
|
+
' <ImgHeight>-1</ImgHeight>', ' <Buffer/>', ' </BtThumnail>', ' </Thumbnail>',
|
|
166
|
+
' <ThumbnailDirtyFlag>true</ThumbnailDirtyFlag>', '</Sm2TiVideoClip>']
|
|
167
|
+
return ('\n' + indent).join(lines)
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
def _tag(item: str, name: str) -> str | None:
|
|
171
|
+
found = re.search(f'<{name}>([^<]*)</{name}>', item)
|
|
172
|
+
return html.unescape(found.group(1)) if found else None
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
def _sequences(archive: zipfile.ZipFile):
|
|
176
|
+
return [info for info in archive.infolist() if info.filename.startswith('SeqContainer/') and info.filename.endswith('.xml')]
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
def uses(source: Path, components: Path) -> bool:
|
|
180
|
+
"""Whether a .drt has clips that point at the component PNGs in components."""
|
|
181
|
+
marker = '<MediaFilePath>' + unicodedata.normalize('NFC', html.escape(str(components), quote=False)) + '/'
|
|
182
|
+
try:
|
|
183
|
+
with zipfile.ZipFile(source) as archive:
|
|
184
|
+
return any(marker in unicodedata.normalize('NFC', archive.read(info).decode('utf-8', 'replace'))
|
|
185
|
+
for info in _sequences(archive))
|
|
186
|
+
except (OSError, zipfile.BadZipFile):
|
|
187
|
+
return False
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
def convert(source: Path, target: Path) -> int:
|
|
191
|
+
"""Write target: the Resolve timeline at source with every Clipen component PNG made a Fusion title.
|
|
192
|
+
|
|
193
|
+
The components' type lives in the manifest beside their PNGs, found through each clip's file
|
|
194
|
+
path. Returns how many clips were replaced."""
|
|
195
|
+
manifests: dict[Path, dict] = {}
|
|
196
|
+
|
|
197
|
+
def entry(path: Path):
|
|
198
|
+
folder = path.parent
|
|
199
|
+
if folder not in manifests:
|
|
200
|
+
try:
|
|
201
|
+
manifests[folder] = json.loads((folder / MANIFEST).read_text(encoding='utf-8'))
|
|
202
|
+
except (OSError, ValueError):
|
|
203
|
+
manifests[folder] = {}
|
|
204
|
+
return manifests[folder].get(path.name)
|
|
205
|
+
|
|
206
|
+
replaced = 0
|
|
207
|
+
|
|
208
|
+
def swap(match: re.Match) -> str:
|
|
209
|
+
nonlocal replaced
|
|
210
|
+
item = match.group(0)
|
|
211
|
+
path = _tag(item, 'MediaFilePath')
|
|
212
|
+
component = entry(Path(path)) if path else None
|
|
213
|
+
if not component:
|
|
214
|
+
return item
|
|
215
|
+
start, duration = int(_tag(item, 'Start')), int(_tag(item, 'Duration'))
|
|
216
|
+
picture = Path(path).parent / component['picture'] if component['picture'] else None
|
|
217
|
+
width, height = component['width'], component['height']
|
|
218
|
+
comp = composition(tools(component['rows'], picture, width, height, duration), width, height, duration)
|
|
219
|
+
indent = match.string[match.string.rfind('\n', 0, match.start()) + 1:match.start()]
|
|
220
|
+
replaced += 1
|
|
221
|
+
return title_item(start, duration, _tag(item, 'MediaFrameRate') or '0000000000003e400000000000000000', comp, indent)
|
|
222
|
+
|
|
223
|
+
try:
|
|
224
|
+
archive = zipfile.ZipFile(source)
|
|
225
|
+
except (OSError, zipfile.BadZipFile) as error:
|
|
226
|
+
raise ProjectError('resolve_timeline_unreadable', '這個檔案不是 DaVinci Resolve 匯出的 .drt 時間線。', status_code=409) from error
|
|
227
|
+
with archive, zipfile.ZipFile(target, 'w', zipfile.ZIP_DEFLATED) as out:
|
|
228
|
+
sequences = {info.filename for info in _sequences(archive)}
|
|
229
|
+
for info in archive.infolist():
|
|
230
|
+
data = archive.read(info)
|
|
231
|
+
if info.filename in sequences:
|
|
232
|
+
data = _ITEM.sub(swap, data.decode('utf-8')).encode('utf-8')
|
|
233
|
+
out.writestr(info, data)
|
|
234
|
+
return replaced
|