@clipen/clipen-cli 0.0.0-stage → 1.0.1
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 +89 -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 +55 -4
- package/pyproject.toml +44 -0
- package/requirements/asr-runtime.txt +13 -0
- package/requirements/dev.txt +2 -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 +410 -0
- package/src/clipen/analysis/audio.py +394 -0
- package/src/clipen/analysis/evidence.py +102 -0
- package/src/clipen/analysis/evidence_service.py +929 -0
- package/src/clipen/analysis/image_similarity.py +134 -0
- package/src/clipen/analysis/keyframes.py +159 -0
- package/src/clipen/analysis/models.py +228 -0
- package/src/clipen/analysis/qwen_worker.py +594 -0
- package/src/clipen/analysis/resources.py +22 -0
- package/src/clipen/analysis/runtime.py +62 -0
- package/src/clipen/analysis/service.py +594 -0
- package/src/clipen/analysis/setup.py +921 -0
- package/src/clipen/analysis/setup_cli.py +107 -0
- package/src/clipen/analysis/storage.py +1864 -0
- package/src/clipen/analysis/transcription.py +906 -0
- package/src/clipen/analysis/visual.py +237 -0
- package/src/clipen/annotations/__init__.py +13 -0
- package/src/clipen/annotations/models.py +22 -0
- package/src/clipen/annotations/storage.py +428 -0
- package/src/clipen/annotations/suggestions.py +115 -0
- package/src/clipen/asr_release.py +132 -0
- package/src/clipen/bootstrap.py +978 -0
- package/src/clipen/candidates.py +222 -0
- package/src/clipen/cli.py +84 -0
- package/src/clipen/controller/__init__.py +17 -0
- package/src/clipen/controller/app.py +1657 -0
- package/src/clipen/controller/server.py +533 -0
- package/src/clipen/domain/__init__.py +30 -0
- package/src/clipen/domain/errors.py +5 -0
- package/src/clipen/domain/media_time.py +110 -0
- package/src/clipen/domain/timeline.py +582 -0
- package/src/clipen/domain/validation.py +14 -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 +127 -0
- package/src/clipen/editing/agent_repository.py +978 -0
- package/src/clipen/editing/agent_tools.py +613 -0
- package/src/clipen/editing/alignment.py +150 -0
- package/src/clipen/editing/alignment_setup.py +156 -0
- package/src/clipen/editing/alignment_worker.py +24 -0
- package/src/clipen/editing/change_report.py +128 -0
- package/src/clipen/editing/compiler.py +40 -0
- package/src/clipen/editing/component_looks.py +131 -0
- package/src/clipen/editing/component_review.py +74 -0
- package/src/clipen/editing/coordinator.py +497 -0
- package/src/clipen/editing/editorial_review.py +309 -0
- package/src/clipen/editing/evidence_jobs.py +552 -0
- package/src/clipen/editing/history_changes.py +192 -0
- package/src/clipen/editing/music_choices.py +18 -0
- package/src/clipen/editing/production_catalog.py +629 -0
- package/src/clipen/editing/production_limits.py +85 -0
- package/src/clipen/editing/production_media.py +435 -0
- package/src/clipen/editing/production_preview.py +373 -0
- package/src/clipen/editing/production_progress.py +236 -0
- package/src/clipen/editing/production_requirements.py +117 -0
- package/src/clipen/editing/production_tools.py +1198 -0
- package/src/clipen/editing/program_audio.py +248 -0
- package/src/clipen/editing/program_direction.py +83 -0
- package/src/clipen/editing/program_story.py +91 -0
- package/src/clipen/editing/program_subtitles.py +232 -0
- package/src/clipen/editing/programme_review.py +121 -0
- package/src/clipen/editing/prompt_history.py +84 -0
- package/src/clipen/editing/recut.py +167 -0
- package/src/clipen/editing/refinement_references.py +278 -0
- package/src/clipen/editing/resolve_export.py +847 -0
- package/src/clipen/editing/resolve_titles.py +291 -0
- package/src/clipen/editing/skill_tools.py +951 -0
- package/src/clipen/editing/source_dialogue.py +192 -0
- package/src/clipen/editing/speech_coverage.py +50 -0
- package/src/clipen/editing/speech_quality.py +11 -0
- package/src/clipen/editing/tool_receipts.py +98 -0
- package/src/clipen/editing/versions.py +219 -0
- package/src/clipen/editing/visual_preview.py +189 -0
- package/src/clipen/editing/workbench.py +136 -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 +188 -0
- package/src/clipen/finishing/catalog.py +264 -0
- package/src/clipen/finishing/models.py +14 -0
- package/src/clipen/finishing/renderer.py +45 -0
- package/src/clipen/mcp_editing/__init__.py +5 -0
- package/src/clipen/mcp_editing/__main__.py +42 -0
- package/src/clipen/mcp_editing/server.py +396 -0
- package/src/clipen/media/__init__.py +50 -0
- package/src/clipen/media/discovery.py +253 -0
- package/src/clipen/media/models.py +349 -0
- package/src/clipen/media/preview.py +137 -0
- package/src/clipen/media/probe.py +418 -0
- package/src/clipen/media/service.py +304 -0
- package/src/clipen/media/storage.py +694 -0
- package/src/clipen/music_vocabulary.py +34 -0
- package/src/clipen/power.py +28 -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 +256 -0
- package/src/clipen/projects/evidence_schema.py +52 -0
- package/src/clipen/projects/finishing_schema.py +36 -0
- package/src/clipen/projects/models.py +189 -0
- package/src/clipen/projects/picker.py +232 -0
- package/src/clipen/projects/preview_schema.py +29 -0
- package/src/clipen/projects/service.py +159 -0
- package/src/clipen/projects/storage.py +1458 -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 +77 -0
- package/src/clipen/providers/__init__.py +43 -0
- package/src/clipen/providers/account.py +245 -0
- package/src/clipen/providers/claude.py +409 -0
- package/src/clipen/providers/claude_editing.py +118 -0
- package/src/clipen/providers/codex.py +991 -0
- package/src/clipen/providers/codex_editing.py +91 -0
- package/src/clipen/providers/editing_agent.py +567 -0
- package/src/clipen/providers/errors.py +51 -0
- package/src/clipen/providers/recovery.py +269 -0
- package/src/clipen/py.typed +1 -0
- package/src/clipen/rendering/__init__.py +12 -0
- package/src/clipen/rendering/cover.py +139 -0
- package/src/clipen/rendering/custom_components.py +328 -0
- package/src/clipen/rendering/custom_render.py +218 -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 +96 -0
- package/src/clipen/rendering/program_components.py +254 -0
- package/src/clipen/rendering/program_creator.py +804 -0
- package/src/clipen/rendering/program_identities.py +350 -0
- package/src/clipen/rendering/program_motion.py +186 -0
- package/src/clipen/rendering/program_studio.py +227 -0
- package/src/clipen/rendering/program_styles.py +321 -0
- package/src/clipen/rendering/program_subtitles.py +89 -0
- package/src/clipen/rendering/renderer.py +737 -0
- package/src/clipen/rendering/storage.py +230 -0
- package/src/clipen/rendering/titles.py +467 -0
- package/src/clipen/skills/__init__.py +5 -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 +314 -0
- package/src/clipen/skills/library.py +137 -0
- package/src/clipen/skills/package.py +254 -0
- package/src/clipen/skills/visual_styles.py +55 -0
- package/src/clipen/workspace.py +181 -0
- package/uninstall +25 -0
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
"""Bounded public music discovery with retained source and license evidence."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import ipaddress
|
|
6
|
+
import json
|
|
7
|
+
import socket
|
|
8
|
+
import time
|
|
9
|
+
from urllib.error import HTTPError, URLError
|
|
10
|
+
from urllib.parse import urlencode, urljoin, urlsplit
|
|
11
|
+
from urllib.request import HTTPRedirectHandler, Request, build_opener
|
|
12
|
+
|
|
13
|
+
from clipen.music_vocabulary import MUSIC_TAGS
|
|
14
|
+
from clipen.projects import DirectoryReference, DirectoryRole, ProjectError
|
|
15
|
+
from clipen.projects.storage import utc_now
|
|
16
|
+
|
|
17
|
+
from .audio import inspect_library
|
|
18
|
+
from .models import digest, encoded
|
|
19
|
+
|
|
20
|
+
_HOSTS = {
|
|
21
|
+
"api.openverse.org",
|
|
22
|
+
"prod-1.storage.jamendo.com",
|
|
23
|
+
"prod-2.storage.jamendo.com",
|
|
24
|
+
"cdn.freesound.org",
|
|
25
|
+
"freesound.org",
|
|
26
|
+
"www.jamendo.com",
|
|
27
|
+
"ccmixter.org",
|
|
28
|
+
"dig.ccmixter.org",
|
|
29
|
+
"upload.wikimedia.org",
|
|
30
|
+
"commons.wikimedia.org",
|
|
31
|
+
}
|
|
32
|
+
_USER_AGENT = "Clipen/0.0 (+https://github.com/IMGMANISME/clipen)"
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
class NoRedirect(HTTPRedirectHandler):
|
|
36
|
+
def redirect_request(self, req, fp, code, msg, headers, newurl):
|
|
37
|
+
return None
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def checked_url(url):
|
|
41
|
+
if not isinstance(url, str) or len(url) > 2048:
|
|
42
|
+
raise ValueError("invalid catalog URL")
|
|
43
|
+
parsed = urlsplit(url)
|
|
44
|
+
if (
|
|
45
|
+
parsed.scheme != "https"
|
|
46
|
+
or parsed.hostname not in _HOSTS
|
|
47
|
+
or parsed.username
|
|
48
|
+
or parsed.password
|
|
49
|
+
or parsed.port not in (None, 443)
|
|
50
|
+
):
|
|
51
|
+
raise ValueError("unapproved catalog destination")
|
|
52
|
+
addresses = socket.getaddrinfo(parsed.hostname, 443, type=socket.SOCK_STREAM)
|
|
53
|
+
if not addresses or any(not ipaddress.ip_address(a[4][0]).is_global for a in addresses):
|
|
54
|
+
raise ValueError("non-public catalog address")
|
|
55
|
+
return url
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
class PublicMusicCatalog:
|
|
59
|
+
"""Only musical vocabulary is searched; private project text is never queried."""
|
|
60
|
+
|
|
61
|
+
def fetch(self, url, cancel, *, limit=2 * 1024 * 1024, destination=None):
|
|
62
|
+
opener = build_opener(NoRedirect)
|
|
63
|
+
deadline = time.monotonic() + 60
|
|
64
|
+
for _ in range(4):
|
|
65
|
+
if cancel.is_set():
|
|
66
|
+
raise ProjectError("user_cancelled", "已取消自動配樂。", status_code=409)
|
|
67
|
+
checked_url(url)
|
|
68
|
+
try:
|
|
69
|
+
response = opener.open(
|
|
70
|
+
Request(
|
|
71
|
+
url,
|
|
72
|
+
headers={
|
|
73
|
+
"User-Agent": _USER_AGENT,
|
|
74
|
+
"Accept": "application/json, audio/*;q=0.9, text/html;q=0.5",
|
|
75
|
+
},
|
|
76
|
+
),
|
|
77
|
+
timeout=15,
|
|
78
|
+
)
|
|
79
|
+
except HTTPError as error:
|
|
80
|
+
if error.code in {301, 302, 303, 307, 308} and error.headers.get("Location"):
|
|
81
|
+
url = urljoin(url, error.headers["Location"])
|
|
82
|
+
error.close()
|
|
83
|
+
continue
|
|
84
|
+
raise
|
|
85
|
+
with response:
|
|
86
|
+
length = response.headers.get("Content-Length")
|
|
87
|
+
if length and int(length) > limit:
|
|
88
|
+
raise ValueError("catalog response too large")
|
|
89
|
+
data = bytearray()
|
|
90
|
+
output = None if destination is None else destination.open("xb")
|
|
91
|
+
size = 0
|
|
92
|
+
try:
|
|
93
|
+
while True:
|
|
94
|
+
if cancel.is_set():
|
|
95
|
+
raise ProjectError("user_cancelled", "已取消自動配樂。", status_code=409)
|
|
96
|
+
if time.monotonic() > deadline:
|
|
97
|
+
raise TimeoutError("music transfer exceeded its time bound")
|
|
98
|
+
block = response.read(65536)
|
|
99
|
+
if not block:
|
|
100
|
+
break
|
|
101
|
+
size += len(block)
|
|
102
|
+
if size > limit:
|
|
103
|
+
raise ValueError("catalog response too large")
|
|
104
|
+
if output is None:
|
|
105
|
+
data.extend(block)
|
|
106
|
+
else:
|
|
107
|
+
output.write(block)
|
|
108
|
+
finally:
|
|
109
|
+
if output is not None:
|
|
110
|
+
output.close()
|
|
111
|
+
return bytes(data)
|
|
112
|
+
raise ValueError("too many catalog redirects")
|
|
113
|
+
|
|
114
|
+
def search_and_prepare(self, brief, directory, cancel, progress):
|
|
115
|
+
tags = brief["music_tags"]
|
|
116
|
+
if not tags or any(t not in MUSIC_TAGS for t in tags):
|
|
117
|
+
raise ProjectError("finishing_music_query_invalid", "AI 配樂方向無效。", status_code=422)
|
|
118
|
+
directory.mkdir(mode=0o700, exist_ok=False)
|
|
119
|
+
metadata = {}
|
|
120
|
+
issues = []
|
|
121
|
+
# Two bounded searches; choose music tagged instrumental, never field recordings.
|
|
122
|
+
queries = [tags[0] + " instrumental"]
|
|
123
|
+
if len(tags) > 1:
|
|
124
|
+
queries.append(tags[1] + " instrumental")
|
|
125
|
+
for query in dict.fromkeys(queries):
|
|
126
|
+
try:
|
|
127
|
+
url = "https://api.openverse.org/v1/audio/?" + urlencode(
|
|
128
|
+
{"q": query, "license": "cc0,by", "page_size": 8}
|
|
129
|
+
)
|
|
130
|
+
result = json.loads(self.fetch(url, cancel))
|
|
131
|
+
for raw in result.get("results", [])[:8]:
|
|
132
|
+
candidate = normalize_candidate(raw)
|
|
133
|
+
if candidate is not None:
|
|
134
|
+
metadata.setdefault(candidate["catalog_id"], candidate)
|
|
135
|
+
except (HTTPError, URLError, TimeoutError, OSError, ValueError):
|
|
136
|
+
issues.append({"code": "search_unavailable", "query": query})
|
|
137
|
+
if not metadata:
|
|
138
|
+
raise ProjectError("finishing_catalog_unavailable", "目前搜尋不到可用配樂,請稍後重試。", status_code=409)
|
|
139
|
+
# Metadata tags are catalog evidence, not a claim that Codex heard the music.
|
|
140
|
+
ranked = sorted(metadata.values(), key=lambda c: sum(t in c["tags"] for t in tags), reverse=True)
|
|
141
|
+
downloaded = {}
|
|
142
|
+
for candidate in ranked[:8]:
|
|
143
|
+
if len(downloaded) >= 3:
|
|
144
|
+
break
|
|
145
|
+
path = None
|
|
146
|
+
try:
|
|
147
|
+
# Refresh the exact candidate immediately before acquisition.
|
|
148
|
+
raw = json.loads(
|
|
149
|
+
self.fetch("https://api.openverse.org/v1/audio/" + candidate["catalog_id"] + "/", cancel)
|
|
150
|
+
)
|
|
151
|
+
current = normalize_candidate(raw)
|
|
152
|
+
if current is None:
|
|
153
|
+
continue
|
|
154
|
+
name = "music-" + digest(current["catalog_id"])[:24] + ".mp3"
|
|
155
|
+
path = directory / name
|
|
156
|
+
self.fetch(current["download_url"], cancel, limit=24 * 1024 * 1024, destination=path)
|
|
157
|
+
downloaded[name] = current
|
|
158
|
+
progress(0.25 + len(downloaded) * 0.1)
|
|
159
|
+
except Exception:
|
|
160
|
+
if cancel.is_set():
|
|
161
|
+
raise
|
|
162
|
+
if path is not None:
|
|
163
|
+
path.unlink(missing_ok=True)
|
|
164
|
+
issues.append({"code": "candidate_unavailable", "title": candidate["title"]})
|
|
165
|
+
if not downloaded:
|
|
166
|
+
raise ProjectError(
|
|
167
|
+
"finishing_music_unavailable",
|
|
168
|
+
"找不到可下載且有完整授權資料的配樂,請調整影片方向或稍後重試。",
|
|
169
|
+
status_code=409,
|
|
170
|
+
)
|
|
171
|
+
info = directory.stat()
|
|
172
|
+
reference = DirectoryReference(
|
|
173
|
+
DirectoryRole.SOURCE,
|
|
174
|
+
str(directory.resolve()),
|
|
175
|
+
directory.name,
|
|
176
|
+
directory.name,
|
|
177
|
+
info.st_dev,
|
|
178
|
+
info.st_ino,
|
|
179
|
+
utc_now(),
|
|
180
|
+
).to_storage()
|
|
181
|
+
assets, audio_issues = inspect_library("music", reference, cancel, lambda value: progress(0.55 + value * 0.4))
|
|
182
|
+
prepared = []
|
|
183
|
+
for asset in assets:
|
|
184
|
+
item = downloaded.get(asset["name"])
|
|
185
|
+
if item is None or asset["duration_seconds"] < 30:
|
|
186
|
+
continue
|
|
187
|
+
asset["name"] = item["title"]
|
|
188
|
+
asset["tags"] = item["tags"]
|
|
189
|
+
asset["genres"] = item["genres"]
|
|
190
|
+
asset["provenance"] = {k: v for k, v in item.items() if k != "download_url"} | {
|
|
191
|
+
"retrieved_at": utc_now(),
|
|
192
|
+
"verification": "catalog_metadata",
|
|
193
|
+
"changes": "Clipen 將本曲截取、調整音量並混入影片。",
|
|
194
|
+
}
|
|
195
|
+
prepared.append(asset)
|
|
196
|
+
if not prepared:
|
|
197
|
+
raise ProjectError("finishing_music_unusable", "候選配樂無法解碼或長度不足,請重新搜尋。", status_code=409)
|
|
198
|
+
# Keep the exact metadata consulted; future index updates do not rewrite this record.
|
|
199
|
+
(directory / "sources.json").write_text(
|
|
200
|
+
encoded(
|
|
201
|
+
{"queries": queries, "sources": [a["provenance"] for a in prepared], "issues": issues + audio_issues}
|
|
202
|
+
)
|
|
203
|
+
)
|
|
204
|
+
return prepared, reference
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
def normalize_candidate(raw):
|
|
208
|
+
"""Exclude restrictive/unknown license metadata rather than inventing rights."""
|
|
209
|
+
if not isinstance(raw, dict):
|
|
210
|
+
return None
|
|
211
|
+
license_id = raw.get("license")
|
|
212
|
+
version = str(raw.get("license_version") or "")
|
|
213
|
+
expected = (
|
|
214
|
+
"https://creativecommons.org/publicdomain/zero/1.0/"
|
|
215
|
+
if license_id == "cc0"
|
|
216
|
+
else f"https://creativecommons.org/licenses/by/{version}/"
|
|
217
|
+
)
|
|
218
|
+
license_url = str(raw.get("license_url") or "").replace("http://", "https://")
|
|
219
|
+
if (
|
|
220
|
+
license_id not in {"cc0", "by"}
|
|
221
|
+
or license_url != expected
|
|
222
|
+
or (license_id == "by" and version not in {"1.0", "2.0", "2.5", "3.0", "4.0"})
|
|
223
|
+
):
|
|
224
|
+
return None
|
|
225
|
+
from uuid import UUID
|
|
226
|
+
|
|
227
|
+
try:
|
|
228
|
+
identifier = str(UUID(raw["id"]))
|
|
229
|
+
duration = float(raw.get("duration", 0)) / 1000
|
|
230
|
+
download = urlsplit(raw["url"])
|
|
231
|
+
landing = urlsplit(raw["foreign_landing_url"])
|
|
232
|
+
tags = [
|
|
233
|
+
t["name"].lower() for t in (raw.get("tags") or []) if isinstance(t, dict) and isinstance(t.get("name"), str)
|
|
234
|
+
][:80]
|
|
235
|
+
if not 30 <= duration <= 3600 or "instrumental" not in tags:
|
|
236
|
+
return None
|
|
237
|
+
if (
|
|
238
|
+
download.scheme != "https"
|
|
239
|
+
or download.hostname not in _HOSTS
|
|
240
|
+
or landing.scheme not in {"http", "https"}
|
|
241
|
+
or landing.hostname not in _HOSTS
|
|
242
|
+
):
|
|
243
|
+
return None
|
|
244
|
+
if any(part.username or part.password or part.port not in (None, 443) for part in (download, landing)):
|
|
245
|
+
return None
|
|
246
|
+
title, creator = raw.get("title"), raw.get("creator")
|
|
247
|
+
if not isinstance(title, str) or not title.strip() or not isinstance(creator, str) or not creator.strip():
|
|
248
|
+
return None
|
|
249
|
+
return {
|
|
250
|
+
"catalog_id": identifier,
|
|
251
|
+
"title": title[:255],
|
|
252
|
+
"creator": creator[:255],
|
|
253
|
+
"source_url": raw["foreign_landing_url"].replace("http://", "https://"),
|
|
254
|
+
"download_url": raw["url"],
|
|
255
|
+
"license": license_id,
|
|
256
|
+
"license_version": version,
|
|
257
|
+
"license_url": expected,
|
|
258
|
+
"attribution": f"{title[:255]} — {creator[:255]}. {expected} {raw['foreign_landing_url']}",
|
|
259
|
+
"tags": tags,
|
|
260
|
+
"genres": [str(g)[:80] for g in (raw.get("genres") or [])[:12]],
|
|
261
|
+
"catalog_url": "https://api.openverse.org/v1/audio/" + identifier + "/",
|
|
262
|
+
}
|
|
263
|
+
except (KeyError, TypeError, ValueError):
|
|
264
|
+
return None
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"""Stable encodings shared by Clipen's music and production records."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import hashlib
|
|
6
|
+
import json
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def encoded(value):
|
|
10
|
+
return json.dumps(value, ensure_ascii=False, sort_keys=True, separators=(",", ":"), allow_nan=False)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def digest(value):
|
|
14
|
+
return hashlib.sha256(encoded(value).encode()).hexdigest()
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"""Check a rendered programme against its timeline before Clipen hands it over."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import json
|
|
6
|
+
|
|
7
|
+
from clipen.projects import ProjectError
|
|
8
|
+
|
|
9
|
+
from .audio import run_process
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def verify_output(path, timeline, cancel):
|
|
13
|
+
raw = run_process(
|
|
14
|
+
["ffprobe", "-v", "error", "-show_streams", "-show_format", "-of", "json", str(path)], cancel, timeout=20
|
|
15
|
+
)
|
|
16
|
+
value = json.loads(raw)
|
|
17
|
+
videos = [s for s in value["streams"] if s["codec_type"] == "video"]
|
|
18
|
+
audios = [s for s in value["streams"] if s["codec_type"] == "audio"]
|
|
19
|
+
fps = timeline.frame_rate.numerator / timeline.frame_rate.denominator
|
|
20
|
+
duration = timeline.duration_frames / fps
|
|
21
|
+
if (
|
|
22
|
+
len(videos) != 1
|
|
23
|
+
or len(audios) != 1
|
|
24
|
+
or (videos[0]["width"], videos[0]["height"]) != (timeline.width, timeline.height)
|
|
25
|
+
):
|
|
26
|
+
raise ProjectError("finishing_verification_failed", "成片的影像或聲音規格不完整。", status_code=409)
|
|
27
|
+
from fractions import Fraction
|
|
28
|
+
|
|
29
|
+
if (
|
|
30
|
+
videos[0]["codec_name"] != "h264"
|
|
31
|
+
or audios[0]["codec_name"] != "aac"
|
|
32
|
+
or Fraction(videos[0]["avg_frame_rate"])
|
|
33
|
+
!= Fraction(timeline.frame_rate.numerator, timeline.frame_rate.denominator)
|
|
34
|
+
or abs(int(videos[0].get("nb_frames", 0)) - timeline.duration_frames) > 1
|
|
35
|
+
):
|
|
36
|
+
raise ProjectError(
|
|
37
|
+
"finishing_verification_failed",
|
|
38
|
+
f"成片的編碼、格率或格數與時間軸不一致:{videos[0]['codec_name']}/{audios[0]['codec_name']},格率 {videos[0]['avg_frame_rate']},格數 {videos[0].get('nb_frames', 0)}(預期 {timeline.duration_frames})。",
|
|
39
|
+
status_code=409,
|
|
40
|
+
)
|
|
41
|
+
if any(abs(float(s.get("duration", 0)) - duration) > max(0.1, 2 / fps) for s in (videos[0], audios[0])):
|
|
42
|
+
raise ProjectError("finishing_verification_failed", "成片的影音長度與時間軸不一致。", status_code=409)
|
|
43
|
+
run_process(
|
|
44
|
+
["ffmpeg", "-v", "error", "-xerror", "-nostdin", "-i", str(path), "-f", "null", "-"], cancel, timeout=90
|
|
45
|
+
)
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"""Run the project-scoped Clipen editing MCP server."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import argparse
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
|
|
8
|
+
from clipen.editing import EditAgentRepository, EditAgentTools
|
|
9
|
+
from clipen.projects import ProjectRepository
|
|
10
|
+
|
|
11
|
+
from .server import EditingMcpServer, run_stdio
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def main() -> int:
|
|
15
|
+
parser = argparse.ArgumentParser(add_help=False)
|
|
16
|
+
parser.add_argument("--projects-root", required=True)
|
|
17
|
+
parser.add_argument("--project-id", required=True)
|
|
18
|
+
parser.add_argument("--run-id", required=True)
|
|
19
|
+
parser.add_argument("--data-root")
|
|
20
|
+
parser.add_argument("--asr-requirements")
|
|
21
|
+
arguments = parser.parse_args()
|
|
22
|
+
projects = ProjectRepository(Path(arguments.projects_root))
|
|
23
|
+
repository = EditAgentRepository(projects)
|
|
24
|
+
tools = EditAgentTools(repository, arguments.project_id, arguments.run_id)
|
|
25
|
+
setup = None
|
|
26
|
+
if arguments.data_root and arguments.asr_requirements:
|
|
27
|
+
from clipen.analysis.setup import ASRSetupManager
|
|
28
|
+
|
|
29
|
+
setup = ASRSetupManager(Path(arguments.data_root), Path(arguments.asr_requirements))
|
|
30
|
+
tools.local_data_root = Path(arguments.data_root)
|
|
31
|
+
tools.transcriber_binding = setup.runtime_binding
|
|
32
|
+
tools.asr_setup_manager = setup
|
|
33
|
+
try:
|
|
34
|
+
return run_stdio(EditingMcpServer(tools))
|
|
35
|
+
finally:
|
|
36
|
+
tools.close_production()
|
|
37
|
+
if setup:
|
|
38
|
+
setup.close()
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
if __name__ == "__main__":
|
|
42
|
+
raise SystemExit(main())
|