@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,85 @@
|
|
|
1
|
+
"""Session-scoped production bounds and the timeline counts they are checked against."""
|
|
2
|
+
|
|
3
|
+
from dataclasses import dataclass
|
|
4
|
+
from fractions import Fraction
|
|
5
|
+
|
|
6
|
+
from clipen.domain import EditTimeline, MediaClip, TrackKind
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
@dataclass(frozen=True, slots=True)
|
|
10
|
+
class TimelineStats:
|
|
11
|
+
duration_seconds: float
|
|
12
|
+
clip_count: int
|
|
13
|
+
longest_clip_seconds: float
|
|
14
|
+
title_count: int
|
|
15
|
+
effect_count: int
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def timeline_stats(timeline: EditTimeline) -> TimelineStats:
|
|
19
|
+
frame_rate = Fraction(timeline.frame_rate.numerator, timeline.frame_rate.denominator)
|
|
20
|
+
video = next((track for track in timeline.tracks if track.kind is TrackKind.VIDEO), None)
|
|
21
|
+
clips = () if video is None else video.items
|
|
22
|
+
clip_seconds = [Fraction(item.duration_frames, 1) / frame_rate for item in clips]
|
|
23
|
+
return TimelineStats(
|
|
24
|
+
float(Fraction(timeline.duration_frames, 1) / frame_rate),
|
|
25
|
+
len(clips),
|
|
26
|
+
float(max(clip_seconds, default=Fraction(0, 1))),
|
|
27
|
+
sum(len(track.items) for track in timeline.tracks if track.kind is TrackKind.TITLE),
|
|
28
|
+
sum(len(item.effects) for track in timeline.tracks for item in track.items if isinstance(item, MediaClip)),
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
@dataclass(frozen=True)
|
|
33
|
+
class ProductionLimits:
|
|
34
|
+
duration_seconds: int = 75
|
|
35
|
+
clips: int = 30
|
|
36
|
+
titles: int = 96
|
|
37
|
+
components: int = 48
|
|
38
|
+
effects: int = 256
|
|
39
|
+
delivery_min_seconds: int = 30
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def limits_for(tools):
|
|
43
|
+
target = tools._session_value()["intent"].get("target_duration_seconds", 60)
|
|
44
|
+
return (
|
|
45
|
+
ProductionLimits(max(75, round(target * 1.1)), 180, 960, 128, 1024, round(target * 0.9))
|
|
46
|
+
if target > 75
|
|
47
|
+
else ProductionLimits()
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def production_limit_issues(tools, timeline, *, complete=False):
|
|
52
|
+
limits = limits_for(tools)
|
|
53
|
+
stats = timeline_stats(timeline)
|
|
54
|
+
bounds = (
|
|
55
|
+
(
|
|
56
|
+
"duration_seconds",
|
|
57
|
+
"片長(秒)",
|
|
58
|
+
stats.duration_seconds,
|
|
59
|
+
limits.delivery_min_seconds if complete else 0.75,
|
|
60
|
+
limits.duration_seconds,
|
|
61
|
+
),
|
|
62
|
+
("clips", "鏡頭數", stats.clip_count, 1, limits.clips),
|
|
63
|
+
("longest_clip_seconds", "單鏡最長秒數", stats.longest_clip_seconds, 0, 30),
|
|
64
|
+
("titles", "文字數", stats.title_count, 0, limits.titles),
|
|
65
|
+
("effects", "效果數", stats.effect_count, 0, limits.effects),
|
|
66
|
+
)
|
|
67
|
+
issues = []
|
|
68
|
+
for field, label, actual, minimum, maximum in bounds:
|
|
69
|
+
if minimum <= actual <= maximum:
|
|
70
|
+
continue
|
|
71
|
+
below_target = field == "duration_seconds" and complete and actual < minimum
|
|
72
|
+
message = f"{label}為 {actual:g},本次允許範圍為 {minimum:g}–{maximum:g}。"
|
|
73
|
+
if below_target:
|
|
74
|
+
message += " 若使用者已更改片長要求,請以 set_target_duration 保存新目標後重新驗證。"
|
|
75
|
+
issues.append(
|
|
76
|
+
{
|
|
77
|
+
"code": "production_duration_below_target" if below_target else "production_limits_exceeded",
|
|
78
|
+
"field": field,
|
|
79
|
+
"actual": actual,
|
|
80
|
+
"minimum": minimum,
|
|
81
|
+
"maximum": maximum,
|
|
82
|
+
"message": message,
|
|
83
|
+
}
|
|
84
|
+
)
|
|
85
|
+
return issues
|
|
@@ -0,0 +1,435 @@
|
|
|
1
|
+
"""Project-owned audio evidence, exact source sampling and local mix execution."""
|
|
2
|
+
|
|
3
|
+
import json
|
|
4
|
+
import math
|
|
5
|
+
import os
|
|
6
|
+
import secrets
|
|
7
|
+
from fractions import Fraction
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
|
|
10
|
+
from clipen.domain import MediaClip, TrackKind
|
|
11
|
+
from clipen.finishing.audio import open_asset, run_process
|
|
12
|
+
from clipen.finishing.models import digest, encoded
|
|
13
|
+
from clipen.projects import ProjectError
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def atomic_json(path, value):
|
|
17
|
+
if path.is_symlink():
|
|
18
|
+
raise ProjectError("storage_unsafe", "製作檔案不安全。", status_code=409)
|
|
19
|
+
temporary = path.parent / (".write-" + secrets.token_hex(16))
|
|
20
|
+
try:
|
|
21
|
+
fd = os.open(temporary, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600)
|
|
22
|
+
with os.fdopen(fd, "w") as out:
|
|
23
|
+
out.write(encoded(value))
|
|
24
|
+
out.flush()
|
|
25
|
+
os.fsync(out.fileno())
|
|
26
|
+
os.replace(temporary, path)
|
|
27
|
+
finally:
|
|
28
|
+
temporary.unlink(missing_ok=True)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class AgentAudioStore:
|
|
32
|
+
def __init__(self, projects, project_id):
|
|
33
|
+
self.directory = projects.root / project_id / "agent-audio"
|
|
34
|
+
if self.directory.is_symlink():
|
|
35
|
+
raise ProjectError("storage_unsafe", "音訊目錄不安全。", status_code=409)
|
|
36
|
+
self.directory.mkdir(mode=0o700, exist_ok=True)
|
|
37
|
+
self.manifest = self.directory / "assets.json"
|
|
38
|
+
|
|
39
|
+
def records(self):
|
|
40
|
+
if self.manifest.is_symlink():
|
|
41
|
+
raise ProjectError("storage_unsafe", "音訊索引不安全。", status_code=409)
|
|
42
|
+
if not self.manifest.exists():
|
|
43
|
+
return []
|
|
44
|
+
if self.manifest.stat().st_size > 8 * 1024 * 1024:
|
|
45
|
+
raise ProjectError("storage_corrupt", "音訊索引超過上限。", status_code=409)
|
|
46
|
+
return json.loads(self.manifest.read_text())
|
|
47
|
+
|
|
48
|
+
def add(self, assets, reference):
|
|
49
|
+
records = self.records()
|
|
50
|
+
known = {r["asset"]["asset_id"] for r in records}
|
|
51
|
+
records.extend({"asset": a, "reference": reference} for a in assets if a["asset_id"] not in known)
|
|
52
|
+
if len(records) > 48:
|
|
53
|
+
raise ProjectError("agent_audio_limit", "這個作品已達 48 個音訊候選上限。", status_code=409)
|
|
54
|
+
atomic_json(self.manifest, records)
|
|
55
|
+
|
|
56
|
+
def public_assets(self):
|
|
57
|
+
return [
|
|
58
|
+
{k: v for k, v in r["asset"].items() if k not in {"relative_path", "fingerprint", "mtime_ns", "size_bytes"}}
|
|
59
|
+
for r in self.records()
|
|
60
|
+
]
|
|
61
|
+
|
|
62
|
+
def context(self, asset_id):
|
|
63
|
+
record = next((r for r in self.records() if r["asset"]["asset_id"] == asset_id), None)
|
|
64
|
+
if record is None:
|
|
65
|
+
raise ProjectError("agent_audio_missing", "找不到這個作品已取得的音訊。", status_code=404)
|
|
66
|
+
asset = record["asset"]
|
|
67
|
+
reference = record["reference"]
|
|
68
|
+
path = Path(reference["canonical_path"])
|
|
69
|
+
if path.parent != self.directory or path.is_symlink():
|
|
70
|
+
raise ProjectError("storage_unsafe", "音訊來源不在本作品的管理範圍。", status_code=409)
|
|
71
|
+
return {"assets": [asset], "directories": {asset["role"]: reference}}
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def sample_frames(media, project_id, span, offsets, directory, cancel):
|
|
75
|
+
source = media.open(project_id, span.version_id)
|
|
76
|
+
paths = []
|
|
77
|
+
manifest = []
|
|
78
|
+
try:
|
|
79
|
+
if span.stream_index not in source.video_stream_indices:
|
|
80
|
+
raise ProjectError("agent_video_missing", "指定來源沒有這個影像串流。", status_code=409)
|
|
81
|
+
unit = Fraction(span.time_base.numerator, span.time_base.denominator)
|
|
82
|
+
for offset in offsets:
|
|
83
|
+
seconds = int(span.start_pts) * unit + Fraction(str(offset))
|
|
84
|
+
path = directory / ("frame-" + secrets.token_hex(12) + ".png")
|
|
85
|
+
run_process(
|
|
86
|
+
[
|
|
87
|
+
"ffmpeg",
|
|
88
|
+
"-v",
|
|
89
|
+
"error",
|
|
90
|
+
"-nostdin",
|
|
91
|
+
"-seek_timestamp",
|
|
92
|
+
"1",
|
|
93
|
+
"-ss",
|
|
94
|
+
f"{float(seconds):.9f}",
|
|
95
|
+
"-i",
|
|
96
|
+
f"/dev/fd/{source.descriptor}",
|
|
97
|
+
"-map",
|
|
98
|
+
f"0:{span.stream_index}",
|
|
99
|
+
"-frames:v",
|
|
100
|
+
"1",
|
|
101
|
+
"-vf",
|
|
102
|
+
"scale=640:-2",
|
|
103
|
+
"-threads",
|
|
104
|
+
"1",
|
|
105
|
+
str(path),
|
|
106
|
+
],
|
|
107
|
+
cancel,
|
|
108
|
+
pass_fds=(source.descriptor,),
|
|
109
|
+
timeout=30,
|
|
110
|
+
)
|
|
111
|
+
if not path.is_file() or not path.stat().st_size:
|
|
112
|
+
raise ProjectError("agent_sample_missing", "取樣時間沒有可解碼畫面。", status_code=409)
|
|
113
|
+
paths.append(path)
|
|
114
|
+
manifest.append({"offset_seconds": offset, "source_seconds": float(seconds), "version_id": span.version_id})
|
|
115
|
+
finally:
|
|
116
|
+
source.close()
|
|
117
|
+
return tuple(paths), manifest
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
def transcript_cues(projects, project_id, timeline, *, include_drafts=False):
|
|
121
|
+
from .speech_quality import speech_flags
|
|
122
|
+
|
|
123
|
+
cues = []
|
|
124
|
+
limitations = []
|
|
125
|
+
fps = Fraction(timeline.frame_rate.numerator, timeline.frame_rate.denominator)
|
|
126
|
+
with projects.connect(project_id) as db:
|
|
127
|
+
for track in timeline.tracks:
|
|
128
|
+
if track.kind is not TrackKind.VIDEO:
|
|
129
|
+
continue
|
|
130
|
+
for clip in track.items:
|
|
131
|
+
source_mode = next(
|
|
132
|
+
(e.parameters["mode"] for e in clip.effects if e.effect_id == "audio.source"), "keep"
|
|
133
|
+
)
|
|
134
|
+
if source_mode == "mute":
|
|
135
|
+
continue
|
|
136
|
+
if any(e.effect_id == "video.speed_ramp" for e in clip.effects):
|
|
137
|
+
limitations.append({"item_id": clip.item_id, "reason": "speed_ramp_requires_new_transcription"})
|
|
138
|
+
continue
|
|
139
|
+
span = clip.source.span
|
|
140
|
+
rows = db.execute(
|
|
141
|
+
"SELECT tr.*,pi.review_required FROM transcription_revisions tr JOIN preparation_items pi ON pi.transcript_revision_id=tr.revision_id JOIN current_preparation cp ON cp.run_id=pi.run_id WHERE tr.version_id=? AND tr.status IN ('transcribed','needs_review') AND tr.stream_index=(SELECT MIN(stream_index) FROM media_streams WHERE version_id=tr.version_id AND kind='audio') ORDER BY tr.stream_index",
|
|
142
|
+
(span.version_id,),
|
|
143
|
+
).fetchall()
|
|
144
|
+
if not rows:
|
|
145
|
+
continue
|
|
146
|
+
row = rows[0]
|
|
147
|
+
annotation = db.execute(
|
|
148
|
+
"SELECT * FROM transcript_annotations WHERE version_id=? AND stream_index=? AND source_revision_id=?",
|
|
149
|
+
(span.version_id, row["stream_index"], row["revision_id"]),
|
|
150
|
+
).fetchone()
|
|
151
|
+
if annotation and annotation["discarded"]:
|
|
152
|
+
continue
|
|
153
|
+
needs_review = bool(
|
|
154
|
+
(row["review_required"] or row["status"] == "needs_review")
|
|
155
|
+
and not (annotation and annotation["confirmed"])
|
|
156
|
+
)
|
|
157
|
+
corrections = (
|
|
158
|
+
{}
|
|
159
|
+
if annotation is None
|
|
160
|
+
else {str(c["ordinal"]): c["text"] for c in json.loads(annotation["corrections_json"])}
|
|
161
|
+
)
|
|
162
|
+
src = json.loads(row["source_span_json"])
|
|
163
|
+
tb = Fraction(src["time_base"]["num"], src["time_base"]["den"])
|
|
164
|
+
unit = Fraction(span.time_base.numerator, span.time_base.denominator)
|
|
165
|
+
start, end = int(span.start_pts) * unit, int(span.end_pts) * unit
|
|
166
|
+
for seg in db.execute(
|
|
167
|
+
"SELECT * FROM transcript_segments WHERE revision_id=? ORDER BY ordinal", (row["revision_id"],)
|
|
168
|
+
):
|
|
169
|
+
left, right = int(seg["start_pts"]) * tb, int(seg["end_pts"]) * tb
|
|
170
|
+
if right <= start or left >= end:
|
|
171
|
+
continue
|
|
172
|
+
complete_span = {
|
|
173
|
+
"version_id": span.version_id,
|
|
174
|
+
"stream_index": span.stream_index,
|
|
175
|
+
"start_pts": str(math.floor(left / unit)),
|
|
176
|
+
"end_pts": str(math.ceil(right / unit)),
|
|
177
|
+
"time_base": span.time_base.to_storage(),
|
|
178
|
+
}
|
|
179
|
+
# Video cuts are quantized to frames while ASR uses audio
|
|
180
|
+
# samples. A trailing AAC/sample remainder below one frame
|
|
181
|
+
# is not a cut-off utterance; clamp displayed timing only.
|
|
182
|
+
tolerance = 1 / fps
|
|
183
|
+
partial = left < start - tolerance or right > end + tolerance
|
|
184
|
+
if needs_review:
|
|
185
|
+
limitations.append(
|
|
186
|
+
{
|
|
187
|
+
"item_id": clip.item_id,
|
|
188
|
+
"reason": "transcript_needs_review",
|
|
189
|
+
"segment_id": seg["segment_id"],
|
|
190
|
+
"provisional_text": corrections.get(str(seg["ordinal"]), seg["text"]),
|
|
191
|
+
"source_revision_id": row["revision_id"],
|
|
192
|
+
"source_start_seconds": float(left),
|
|
193
|
+
"source_end_seconds": float(right),
|
|
194
|
+
"complete_source_span": complete_span,
|
|
195
|
+
"partial": partial,
|
|
196
|
+
"policy": "Low-confidence ASR. Explicit include_drafts/allow_asr_draft can produce a labelled draft; never claim human confirmation.",
|
|
197
|
+
}
|
|
198
|
+
)
|
|
199
|
+
if not include_drafts or partial:
|
|
200
|
+
continue
|
|
201
|
+
if partial:
|
|
202
|
+
limitations.append(
|
|
203
|
+
{
|
|
204
|
+
"item_id": clip.item_id,
|
|
205
|
+
"reason": "partial_utterance",
|
|
206
|
+
"segment_id": seg["segment_id"],
|
|
207
|
+
"text": corrections.get(str(seg["ordinal"]), seg["text"]),
|
|
208
|
+
"complete_source_span": {
|
|
209
|
+
"version_id": span.version_id,
|
|
210
|
+
"stream_index": span.stream_index,
|
|
211
|
+
"start_pts": str(math.floor(left / unit)),
|
|
212
|
+
"end_pts": str(math.ceil(right / unit)),
|
|
213
|
+
"time_base": span.time_base.to_storage(),
|
|
214
|
+
},
|
|
215
|
+
"repair": "Use replace_clip_source with this complete_source_span; inspect the expanded shot, then rebuild subtitles. Alternatively remove/mute this speech for an explicit editorial reason.",
|
|
216
|
+
}
|
|
217
|
+
)
|
|
218
|
+
continue
|
|
219
|
+
begin = max(clip.start_frame, clip.start_frame + math.floor((left - start) * fps))
|
|
220
|
+
finish = min(
|
|
221
|
+
clip.start_frame + clip.duration_frames, clip.start_frame + math.ceil((right - start) * fps)
|
|
222
|
+
)
|
|
223
|
+
cues.append(
|
|
224
|
+
{
|
|
225
|
+
"cue_id": digest([clip.item_id, row["revision_id"], seg["segment_id"]])[:24],
|
|
226
|
+
"item_id": clip.item_id,
|
|
227
|
+
"start_frame": begin,
|
|
228
|
+
"duration_frames": finish - begin,
|
|
229
|
+
"text": corrections.get(str(seg["ordinal"]), seg["text"]),
|
|
230
|
+
"segment_id": seg["segment_id"],
|
|
231
|
+
"source_revision_id": row["revision_id"],
|
|
232
|
+
"verification": "asr_draft" if needs_review else "reviewed_source",
|
|
233
|
+
"confidence": seg["confidence"],
|
|
234
|
+
"quality_flags": speech_flags(corrections.get(str(seg["ordinal"]), seg["text"]))
|
|
235
|
+
if needs_review
|
|
236
|
+
else [],
|
|
237
|
+
"source": complete_span,
|
|
238
|
+
"boundary_adjustment_seconds": float(max(start - left, 0) + max(right - end, 0)),
|
|
239
|
+
}
|
|
240
|
+
)
|
|
241
|
+
return sorted(cues, key=lambda c: c["start_frame"]), limitations
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
PRODUCTION_EFFECTS = {
|
|
245
|
+
"video.punch_in",
|
|
246
|
+
"video.slow_push",
|
|
247
|
+
"video.reframe",
|
|
248
|
+
"video.speed_ramp",
|
|
249
|
+
"video.grade",
|
|
250
|
+
"video.mosaic",
|
|
251
|
+
"video.transition",
|
|
252
|
+
"video.fade",
|
|
253
|
+
"audio.source",
|
|
254
|
+
"audio.layers",
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
def validate_production_timeline(timeline, *, max_texts=96):
|
|
259
|
+
issues = []
|
|
260
|
+
cursor = 0
|
|
261
|
+
video = next((t for t in timeline.tracks if t.kind is TrackKind.VIDEO), None)
|
|
262
|
+
if video is None:
|
|
263
|
+
return [{"code": "video_required", "message": "至少需要一個畫面軌。"}]
|
|
264
|
+
if not video.items:
|
|
265
|
+
return [{"code": "video_required", "message": "空白草稿尚無可渲染畫面;請先加入來源片段。"}]
|
|
266
|
+
if len([t for t in timeline.tracks if t.kind is TrackKind.VIDEO]) != 1:
|
|
267
|
+
issues.append({"code": "multiple_video_tracks_unsupported"})
|
|
268
|
+
if any(t.items for t in timeline.tracks if t.kind is TrackKind.GRAPHIC):
|
|
269
|
+
issues.append({"code": "graphics_delivery_unsupported"})
|
|
270
|
+
for i in video.items:
|
|
271
|
+
if i.start_frame != cursor:
|
|
272
|
+
issues.append({"code": "video_gap_or_overlap", "item_id": i.item_id})
|
|
273
|
+
cursor = i.start_frame + i.duration_frames
|
|
274
|
+
if cursor != timeline.duration_frames:
|
|
275
|
+
issues.append({"code": "video_end_mismatch"})
|
|
276
|
+
texts = []
|
|
277
|
+
for t in timeline.tracks:
|
|
278
|
+
for i in t.items:
|
|
279
|
+
if i.start_frame + i.duration_frames > timeline.duration_frames:
|
|
280
|
+
issues.append({"code": "item_out_of_bounds", "item_id": i.item_id})
|
|
281
|
+
if t.kind is TrackKind.TITLE:
|
|
282
|
+
texts.append(i)
|
|
283
|
+
if isinstance(i, MediaClip):
|
|
284
|
+
for e in i.effects:
|
|
285
|
+
if e.effect_id not in PRODUCTION_EFFECTS:
|
|
286
|
+
issues.append({"code": "unsupported_effect", "item_id": i.item_id, "effect_id": e.effect_id})
|
|
287
|
+
for n, a in enumerate(texts):
|
|
288
|
+
for b in texts[n + 1 :]:
|
|
289
|
+
if (
|
|
290
|
+
a.style_id == b.style_id
|
|
291
|
+
and a.start_frame < b.start_frame + b.duration_frames
|
|
292
|
+
and b.start_frame < a.start_frame + a.duration_frames
|
|
293
|
+
):
|
|
294
|
+
issues.append(
|
|
295
|
+
{
|
|
296
|
+
"code": "text_layout_collision",
|
|
297
|
+
"items": [a.item_id, b.item_id],
|
|
298
|
+
"message": "相同位置文字同時顯示;請調整時間或樣式。",
|
|
299
|
+
}
|
|
300
|
+
)
|
|
301
|
+
if len(texts) > max_texts:
|
|
302
|
+
issues.append({"code": "text_limit_96" if max_texts == 96 else "production_text_limit"})
|
|
303
|
+
return issues
|
|
304
|
+
|
|
305
|
+
|
|
306
|
+
def mix_audio_layers(media, project_id, timeline, base, output, cancel):
|
|
307
|
+
if timeline.production_session_id is not None:
|
|
308
|
+
from .program_audio import mix_program_audio
|
|
309
|
+
|
|
310
|
+
return mix_program_audio(media, project_id, timeline, base, output, cancel)
|
|
311
|
+
store = AgentAudioStore(media.projects, project_id)
|
|
312
|
+
fps = timeline.frame_rate.numerator / timeline.frame_rate.denominator
|
|
313
|
+
seconds = timeline.duration_frames / fps
|
|
314
|
+
layers = [
|
|
315
|
+
(i, l)
|
|
316
|
+
for t in timeline.tracks
|
|
317
|
+
for i in t.items
|
|
318
|
+
if isinstance(i, MediaClip)
|
|
319
|
+
for e in i.effects
|
|
320
|
+
if e.effect_id == "audio.layers"
|
|
321
|
+
for l in e.parameters["layers"]
|
|
322
|
+
]
|
|
323
|
+
if len(layers) > 24:
|
|
324
|
+
raise ProjectError("agent_audio_limit", "單次輸出最多 24 個混音片段。", status_code=409)
|
|
325
|
+
command = ["ffmpeg", "-v", "error", "-nostdin", "-y", "-filter_complex_threads", "2", "-i", str(base)]
|
|
326
|
+
opened = []
|
|
327
|
+
filters = ["[0:a]anull[original]"]
|
|
328
|
+
labels = ["[original]"]
|
|
329
|
+
try:
|
|
330
|
+
for index, (clip, layer) in enumerate(layers, 1):
|
|
331
|
+
context = store.context(layer["asset_id"])
|
|
332
|
+
fd, asset = open_asset(context, layer["asset_id"])
|
|
333
|
+
opened.append(fd)
|
|
334
|
+
duration = layer["duration_frames"] / fps
|
|
335
|
+
start = (clip.start_frame + layer["start_frame"]) / fps
|
|
336
|
+
if start + duration > seconds or layer["source_offset_seconds"] + duration > asset["duration_seconds"]:
|
|
337
|
+
raise ProjectError("agent_audio_out_of_bounds", "混音片段超出影片或來源長度。", status_code=409)
|
|
338
|
+
command += ["-ss", str(layer["source_offset_seconds"]), "-t", str(duration), "-i", f"/dev/fd/{fd}"]
|
|
339
|
+
fade = min(layer["fade_seconds"], duration / 2)
|
|
340
|
+
chain = f"[{index}:{asset['stream_index']}]asetpts=PTS-STARTPTS,aresample=48000,aformat=channel_layouts=stereo,volume={layer['gain_db']:.4f}dB"
|
|
341
|
+
if fade:
|
|
342
|
+
chain += f",afade=t=in:d={fade:.6f},afade=t=out:st={duration - fade:.6f}:d={fade:.6f}"
|
|
343
|
+
chain += f",adelay={round(start * 1000)}|{round(start * 1000)}[layer{index}]"
|
|
344
|
+
filters.append(chain)
|
|
345
|
+
if layer["duck"]:
|
|
346
|
+
filters.append(f"[0:a]anull[side{index}]")
|
|
347
|
+
filters.append(
|
|
348
|
+
f"[layer{index}][side{index}]sidechaincompress=threshold=0.025:ratio=8:attack=20:release=350[duck{index}]"
|
|
349
|
+
)
|
|
350
|
+
labels.append(f"[duck{index}]")
|
|
351
|
+
else:
|
|
352
|
+
labels.append(f"[layer{index}]")
|
|
353
|
+
filters.append(
|
|
354
|
+
"".join(labels)
|
|
355
|
+
+ f"amix=inputs={len(labels)}:duration=first:normalize=0,alimiter=limit=0.841395:level=0[aout]"
|
|
356
|
+
)
|
|
357
|
+
command += [
|
|
358
|
+
"-filter_complex",
|
|
359
|
+
";".join(filters),
|
|
360
|
+
"-map",
|
|
361
|
+
"0:v:0",
|
|
362
|
+
"-map",
|
|
363
|
+
"[aout]",
|
|
364
|
+
"-c:v",
|
|
365
|
+
"copy",
|
|
366
|
+
"-c:a",
|
|
367
|
+
"aac",
|
|
368
|
+
"-t",
|
|
369
|
+
str(seconds),
|
|
370
|
+
"-movflags",
|
|
371
|
+
"+faststart",
|
|
372
|
+
str(output),
|
|
373
|
+
]
|
|
374
|
+
run_process(command, cancel, pass_fds=tuple(opened), timeout=300)
|
|
375
|
+
finally:
|
|
376
|
+
for fd in opened:
|
|
377
|
+
os.close(fd)
|
|
378
|
+
|
|
379
|
+
|
|
380
|
+
def text_style_path(projects, project_id):
|
|
381
|
+
return projects.root / project_id / "agent-text-styles.json"
|
|
382
|
+
|
|
383
|
+
|
|
384
|
+
def load_text_styles(projects, project_id):
|
|
385
|
+
if projects is None:
|
|
386
|
+
return {}
|
|
387
|
+
path = text_style_path(projects, project_id)
|
|
388
|
+
if path.is_symlink():
|
|
389
|
+
raise ProjectError("storage_unsafe", "文字樣式檔案不安全。", status_code=409)
|
|
390
|
+
if not path.exists():
|
|
391
|
+
return {}
|
|
392
|
+
if path.stat().st_size > 16 * 1024 * 1024:
|
|
393
|
+
raise ProjectError("storage_corrupt", "文字樣式超出上限。", status_code=409)
|
|
394
|
+
return json.loads(path.read_text())
|
|
395
|
+
|
|
396
|
+
|
|
397
|
+
def source_rows(projects, project_id):
|
|
398
|
+
"""The live inventory is the authority; prior planning windows are not a source boundary."""
|
|
399
|
+
with projects.connect(project_id) as db:
|
|
400
|
+
rows = db.execute(
|
|
401
|
+
"SELECT i.version_id,i.display_name,s.stream_index,s.kind,s.details_json,v.probe_json FROM current_inventory c JOIN inventory_runs r ON r.run_id=c.run_id JOIN inventory_items i ON i.run_id=r.run_id JOIN assets a ON a.asset_id=i.asset_id JOIN asset_versions v ON v.version_id=i.version_id JOIN media_streams s ON s.version_id=i.version_id WHERE c.slot=1 AND r.state='succeeded' AND a.role='source' AND i.status='usable' AND s.kind IN ('video','audio') ORDER BY i.display_name,i.version_id,s.stream_index LIMIT 10001"
|
|
402
|
+
).fetchall()
|
|
403
|
+
if len(rows) > 10000:
|
|
404
|
+
raise ProjectError("agent_source_limit", "素材串流超過上限,請拆分作品。", status_code=409)
|
|
405
|
+
result = []
|
|
406
|
+
for row in rows:
|
|
407
|
+
details = json.loads(row["details_json"])
|
|
408
|
+
probe = json.loads(row["probe_json"] or "{}")
|
|
409
|
+
if details.get("attached_pic") or details.get("timed_thumbnails"):
|
|
410
|
+
continue
|
|
411
|
+
try:
|
|
412
|
+
base = Fraction(details["time_base"])
|
|
413
|
+
start = int(details.get("start_pts", 0))
|
|
414
|
+
duration = (
|
|
415
|
+
int(details["duration_ts"])
|
|
416
|
+
if "duration_ts" in details
|
|
417
|
+
else int(Fraction(str(probe["duration_seconds"])) / base)
|
|
418
|
+
)
|
|
419
|
+
if duration <= 0 or base <= 0:
|
|
420
|
+
continue
|
|
421
|
+
except (KeyError, ValueError, TypeError, ZeroDivisionError):
|
|
422
|
+
continue
|
|
423
|
+
result.append(
|
|
424
|
+
{
|
|
425
|
+
"version_id": row["version_id"],
|
|
426
|
+
"display_name": row["display_name"],
|
|
427
|
+
"stream_index": row["stream_index"],
|
|
428
|
+
"kind": row["kind"],
|
|
429
|
+
"start_pts": str(start),
|
|
430
|
+
"end_pts": str(start + duration),
|
|
431
|
+
"time_base": {"num": base.numerator, "den": base.denominator},
|
|
432
|
+
"duration_seconds": float(duration * base),
|
|
433
|
+
}
|
|
434
|
+
)
|
|
435
|
+
return result
|