@clipen/clipen-cli 1.0.0 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +56 -48
- package/apps/web/dist/assets/{index-D5D2HiB1.js → index-DPHwA954.js} +1 -1
- package/apps/web/dist/index.html +1 -1
- package/bin/clipen.js +1 -1
- package/package.json +7 -6
- package/pyproject.toml +18 -2
- package/requirements/dev.txt +2 -0
- package/src/clipen/__init__.py +1 -1
- package/src/clipen/analysis/__init__.py +7 -7
- package/src/clipen/analysis/artifacts.py +4 -10
- package/src/clipen/analysis/audio.py +10 -8
- package/src/clipen/analysis/evidence_service.py +345 -167
- package/src/clipen/analysis/image_similarity.py +5 -14
- package/src/clipen/analysis/keyframes.py +5 -17
- package/src/clipen/analysis/models.py +5 -7
- package/src/clipen/analysis/qwen_worker.py +19 -54
- package/src/clipen/analysis/resources.py +2 -1
- package/src/clipen/analysis/runtime.py +1 -3
- package/src/clipen/analysis/service.py +16 -48
- package/src/clipen/analysis/setup.py +11 -25
- package/src/clipen/analysis/setup_cli.py +5 -15
- package/src/clipen/analysis/storage.py +96 -278
- package/src/clipen/analysis/transcription.py +40 -82
- package/src/clipen/analysis/visual.py +109 -48
- package/src/clipen/annotations/__init__.py +5 -2
- package/src/clipen/annotations/models.py +1 -1
- package/src/clipen/annotations/storage.py +132 -84
- package/src/clipen/annotations/suggestions.py +3 -8
- package/src/clipen/asr_release.py +4 -11
- package/src/clipen/bootstrap.py +71 -61
- package/src/clipen/{planning/models.py → candidates.py} +29 -47
- package/src/clipen/cli.py +4 -9
- package/src/clipen/controller/app.py +401 -214
- package/src/clipen/controller/server.py +28 -38
- package/src/clipen/domain/__init__.py +1 -1
- package/src/clipen/domain/media_time.py +3 -10
- package/src/clipen/domain/timeline.py +45 -47
- package/src/clipen/domain/validation.py +0 -1
- package/src/clipen/editing/__init__.py +1 -1
- package/src/clipen/editing/agent_models.py +4 -11
- package/src/clipen/editing/agent_repository.py +393 -172
- package/src/clipen/editing/agent_tools.py +180 -99
- package/src/clipen/editing/alignment.py +92 -52
- package/src/clipen/editing/alignment_setup.py +118 -70
- package/src/clipen/editing/alignment_worker.py +11 -9
- package/src/clipen/editing/change_report.py +95 -38
- package/src/clipen/editing/compiler.py +0 -1
- package/src/clipen/editing/component_looks.py +58 -34
- package/src/clipen/editing/component_review.py +34 -38
- package/src/clipen/editing/coordinator.py +213 -92
- package/src/clipen/editing/editorial_review.py +217 -95
- package/src/clipen/editing/evidence_jobs.py +457 -200
- package/src/clipen/editing/history_changes.py +129 -68
- package/src/clipen/editing/music_choices.py +8 -5
- package/src/clipen/editing/production_catalog.py +573 -136
- package/src/clipen/editing/production_limits.py +43 -17
- package/src/clipen/editing/production_media.py +376 -167
- package/src/clipen/editing/production_preview.py +322 -129
- package/src/clipen/editing/production_progress.py +164 -69
- package/src/clipen/editing/production_requirements.py +106 -39
- package/src/clipen/editing/production_tools.py +1064 -462
- package/src/clipen/editing/program_audio.py +211 -81
- package/src/clipen/editing/program_direction.py +59 -25
- package/src/clipen/editing/program_story.py +83 -45
- package/src/clipen/editing/program_subtitles.py +194 -96
- package/src/clipen/editing/programme_review.py +98 -50
- package/src/clipen/editing/prompt_history.py +64 -27
- package/src/clipen/editing/recut.py +65 -25
- package/src/clipen/editing/refinement_references.py +176 -86
- package/src/clipen/editing/resolve_export.py +449 -218
- package/src/clipen/editing/resolve_titles.py +162 -105
- package/src/clipen/editing/skill_tools.py +530 -154
- package/src/clipen/editing/source_dialogue.py +177 -67
- package/src/clipen/editing/speech_coverage.py +42 -22
- package/src/clipen/editing/speech_quality.py +6 -1
- package/src/clipen/editing/tool_receipts.py +78 -32
- package/src/clipen/editing/versions.py +122 -64
- package/src/clipen/editing/visual_preview.py +157 -61
- package/src/clipen/editing/workbench.py +104 -42
- package/src/clipen/finishing/audio.py +69 -30
- package/src/clipen/finishing/catalog.py +176 -93
- package/src/clipen/finishing/models.py +1 -1
- package/src/clipen/finishing/renderer.py +33 -14
- package/src/clipen/mcp_editing/__main__.py +3 -1
- package/src/clipen/mcp_editing/server.py +77 -34
- package/src/clipen/media/__init__.py +3 -3
- package/src/clipen/media/discovery.py +7 -20
- package/src/clipen/media/models.py +7 -18
- package/src/clipen/media/preview.py +14 -24
- package/src/clipen/media/probe.py +16 -44
- package/src/clipen/media/service.py +6 -19
- package/src/clipen/media/storage.py +33 -81
- package/src/clipen/music_vocabulary.py +33 -4
- package/src/clipen/power.py +8 -3
- package/src/clipen/projects/__init__.py +1 -1
- package/src/clipen/projects/deletion.py +19 -32
- package/src/clipen/projects/finishing_schema.py +3 -1
- package/src/clipen/projects/models.py +2 -5
- package/src/clipen/projects/picker.py +13 -37
- package/src/clipen/projects/service.py +8 -24
- package/src/clipen/projects/storage.py +226 -246
- package/src/clipen/provider_registry.py +9 -20
- package/src/clipen/providers/account.py +86 -35
- package/src/clipen/providers/claude.py +59 -25
- package/src/clipen/providers/claude_editing.py +14 -7
- package/src/clipen/providers/codex.py +73 -118
- package/src/clipen/providers/codex_editing.py +2 -2
- package/src/clipen/providers/editing_agent.py +19 -17
- package/src/clipen/providers/recovery.py +7 -8
- package/src/clipen/rendering/cover.py +18 -5
- package/src/clipen/rendering/custom_components.py +167 -79
- package/src/clipen/rendering/custom_render.py +62 -41
- package/src/clipen/rendering/production_effects.py +77 -45
- package/src/clipen/rendering/program_components.py +134 -73
- package/src/clipen/rendering/program_creator.py +432 -277
- package/src/clipen/rendering/program_identities.py +277 -182
- package/src/clipen/rendering/program_motion.py +140 -95
- package/src/clipen/rendering/program_studio.py +175 -116
- package/src/clipen/rendering/program_styles.py +231 -145
- package/src/clipen/rendering/program_subtitles.py +71 -34
- package/src/clipen/rendering/renderer.py +150 -121
- package/src/clipen/rendering/storage.py +9 -22
- package/src/clipen/rendering/titles.py +193 -86
- package/src/clipen/skills/__init__.py +1 -0
- package/src/clipen/skills/catalog.py +151 -55
- package/src/clipen/skills/library.py +13 -7
- package/src/clipen/skills/package.py +46 -23
- package/src/clipen/skills/visual_styles.py +29 -18
- package/src/clipen/workspace.py +15 -44
- package/install +0 -25
- package/src/clipen/planning/__init__.py +0 -9
- package/uninstall +0 -25
|
@@ -2,12 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
from __future__ import annotations
|
|
4
4
|
|
|
5
|
-
from dataclasses import dataclass
|
|
6
|
-
from decimal import Decimal, InvalidOperation, ROUND_CEILING, ROUND_FLOOR
|
|
7
5
|
import hashlib
|
|
8
6
|
import json
|
|
9
7
|
import os
|
|
10
|
-
from pathlib import Path
|
|
11
8
|
import queue
|
|
12
9
|
import re
|
|
13
10
|
import signal
|
|
@@ -17,11 +14,13 @@ import tempfile
|
|
|
17
14
|
import threading
|
|
18
15
|
import time
|
|
19
16
|
import unicodedata
|
|
17
|
+
from dataclasses import dataclass
|
|
18
|
+
from decimal import ROUND_CEILING, ROUND_FLOOR, Decimal, InvalidOperation
|
|
19
|
+
from pathlib import Path
|
|
20
20
|
|
|
21
21
|
from .audio import PreparedAudio
|
|
22
22
|
from .resources import local_analysis_slot
|
|
23
23
|
|
|
24
|
-
|
|
25
24
|
_MODEL_ID = re.compile(r"[a-z][a-z0-9_.-]{0,127}\Z")
|
|
26
25
|
_SHA256 = re.compile(r"[0-9a-f]{64}\Z")
|
|
27
26
|
_LANGUAGE = re.compile(r"[a-z]{2,3}(?:-[A-Z]{2})?\Z")
|
|
@@ -49,18 +48,11 @@ class LocalModelSnapshot:
|
|
|
49
48
|
def __post_init__(self) -> None:
|
|
50
49
|
if _MODEL_ID.fullmatch(self.model_id) is None:
|
|
51
50
|
raise ValueError("ASR model ID is invalid")
|
|
52
|
-
if not self.revision or len(self.revision) > 128 or any(
|
|
53
|
-
ord(character) < 32 for character in self.revision
|
|
54
|
-
):
|
|
51
|
+
if not self.revision or len(self.revision) > 128 or any(ord(character) < 32 for character in self.revision):
|
|
55
52
|
raise ValueError("ASR model revision is invalid")
|
|
56
53
|
if _SHA256.fullmatch(self.manifest_sha256) is None:
|
|
57
54
|
raise ValueError("ASR model manifest digest is invalid")
|
|
58
|
-
if any(
|
|
59
|
-
not filename
|
|
60
|
-
or Path(filename).name != filename
|
|
61
|
-
or size < 1
|
|
62
|
-
for filename, size in self.expected_files
|
|
63
|
-
):
|
|
55
|
+
if any(not filename or Path(filename).name != filename or size < 1 for filename, size in self.expected_files):
|
|
64
56
|
raise ValueError("ASR model file contract is invalid")
|
|
65
57
|
|
|
66
58
|
|
|
@@ -113,11 +105,7 @@ class TranscriptSegment:
|
|
|
113
105
|
|
|
114
106
|
@property
|
|
115
107
|
def word_confidence(self) -> str | None:
|
|
116
|
-
probabilities = [
|
|
117
|
-
Decimal(word.probability)
|
|
118
|
-
for word in self.words
|
|
119
|
-
if word.probability is not None
|
|
120
|
-
]
|
|
108
|
+
probabilities = [Decimal(word.probability) for word in self.words if word.probability is not None]
|
|
121
109
|
if not probabilities:
|
|
122
110
|
return None
|
|
123
111
|
return _canonical_probability(sum(probabilities) / len(probabilities))
|
|
@@ -160,9 +148,7 @@ class TranscriptionResult:
|
|
|
160
148
|
# results always carry a phrase-level confidence value.
|
|
161
149
|
return any(segment.text.strip() == "[音訊不清楚]" for segment in self.segments)
|
|
162
150
|
mean = sum(probabilities) / len(probabilities)
|
|
163
|
-
low_ratio = sum(
|
|
164
|
-
probability < Decimal("0.5") for probability in probabilities
|
|
165
|
-
) / len(probabilities)
|
|
151
|
+
low_ratio = sum(probability < Decimal("0.5") for probability in probabilities) / len(probabilities)
|
|
166
152
|
return mean < Decimal("0.65") or low_ratio > 0.3
|
|
167
153
|
|
|
168
154
|
def to_storage(self) -> dict[str, object]:
|
|
@@ -322,20 +308,21 @@ class _PersistentASRWorkerAdapter:
|
|
|
322
308
|
output_path = _verify_empty_output(output)
|
|
323
309
|
model_path = self._verify_model()
|
|
324
310
|
try:
|
|
325
|
-
request =
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
)
|
|
311
|
+
request = (
|
|
312
|
+
json.dumps(
|
|
313
|
+
{
|
|
314
|
+
"audio": str(audio_path),
|
|
315
|
+
"initial_prompt": initial_prompt,
|
|
316
|
+
"language": language,
|
|
317
|
+
"output": str(output_path),
|
|
318
|
+
},
|
|
319
|
+
ensure_ascii=False,
|
|
320
|
+
sort_keys=True,
|
|
321
|
+
separators=(",", ":"),
|
|
322
|
+
).encode("utf-8")
|
|
323
|
+
+ b"\n"
|
|
324
|
+
)
|
|
325
|
+
with self._worker_lock, local_analysis_slot(cancel, lambda: TranscriptionError("cancelled")):
|
|
339
326
|
process = self._ensure_server(model_path, cancel)
|
|
340
327
|
try:
|
|
341
328
|
assert process.stdin is not None
|
|
@@ -348,9 +335,7 @@ class _PersistentASRWorkerAdapter:
|
|
|
348
335
|
if response.get("type") != "result" or response.get("ok") is not True:
|
|
349
336
|
code = response.get("code")
|
|
350
337
|
self._stop_server()
|
|
351
|
-
raise TranscriptionError(
|
|
352
|
-
code if code in {"asr_failed", "asr_request_invalid"} else "asr_failed"
|
|
353
|
-
)
|
|
338
|
+
raise TranscriptionError(code if code in {"asr_failed", "asr_request_invalid"} else "asr_failed")
|
|
354
339
|
return _parse_result(output_path, audio.frame_count, audio.sample_rate)
|
|
355
340
|
except Exception:
|
|
356
341
|
output_path.unlink(missing_ok=True)
|
|
@@ -360,9 +345,7 @@ class _PersistentASRWorkerAdapter:
|
|
|
360
345
|
with self._worker_lock:
|
|
361
346
|
self._stop_server()
|
|
362
347
|
|
|
363
|
-
def _ensure_server(
|
|
364
|
-
self, model_path: Path, cancel: threading.Event | None
|
|
365
|
-
) -> subprocess.Popen[bytes]:
|
|
348
|
+
def _ensure_server(self, model_path: Path, cancel: threading.Event | None) -> subprocess.Popen[bytes]:
|
|
366
349
|
if self._process is not None and self._process.poll() is None:
|
|
367
350
|
return self._process
|
|
368
351
|
self._stop_server()
|
|
@@ -392,11 +375,7 @@ class _PersistentASRWorkerAdapter:
|
|
|
392
375
|
).start()
|
|
393
376
|
ready = self._wait_for_response(process, cancel)
|
|
394
377
|
version = ready.get(self._readiness_key)
|
|
395
|
-
if (
|
|
396
|
-
ready.get("type") != "ready"
|
|
397
|
-
or not isinstance(version, str)
|
|
398
|
-
or not 1 <= len(version) <= 80
|
|
399
|
-
):
|
|
378
|
+
if ready.get("type") != "ready" or not isinstance(version, str) or not 1 <= len(version) <= 80:
|
|
400
379
|
self._stop_server()
|
|
401
380
|
raise TranscriptionError("asr_runtime_unsupported")
|
|
402
381
|
return process
|
|
@@ -512,7 +491,11 @@ class _PersistentASRWorkerAdapter:
|
|
|
512
491
|
observed_output: Path | None = None,
|
|
513
492
|
) -> tuple[bytes, bytes, int]:
|
|
514
493
|
environment = self._environment()
|
|
515
|
-
with
|
|
494
|
+
with (
|
|
495
|
+
local_analysis_slot(cancel, lambda: TranscriptionError("cancelled")),
|
|
496
|
+
tempfile.TemporaryFile() as stdout_file,
|
|
497
|
+
tempfile.TemporaryFile() as stderr_file,
|
|
498
|
+
):
|
|
516
499
|
deadline = time.monotonic() + self.timeout_seconds
|
|
517
500
|
try:
|
|
518
501
|
process = subprocess.Popen(
|
|
@@ -646,11 +629,7 @@ def _model_directory_signature(path: Path) -> str:
|
|
|
646
629
|
relative = candidate.relative_to(resolved).as_posix()
|
|
647
630
|
digest.update(relative.encode("utf-8"))
|
|
648
631
|
digest.update(b"\0")
|
|
649
|
-
digest.update(
|
|
650
|
-
f"{info.st_dev}:{info.st_ino}:{info.st_size}:{info.st_mtime_ns}".encode(
|
|
651
|
-
"ascii"
|
|
652
|
-
)
|
|
653
|
-
)
|
|
632
|
+
digest.update(f"{info.st_dev}:{info.st_ino}:{info.st_size}:{info.st_mtime_ns}".encode("ascii"))
|
|
654
633
|
digest.update(b"\0")
|
|
655
634
|
if files < 1:
|
|
656
635
|
raise TranscriptionError("asr_model_unavailable")
|
|
@@ -669,9 +648,7 @@ def _verify_model(model: LocalModelSnapshot) -> Path:
|
|
|
669
648
|
return path.resolve(strict=True)
|
|
670
649
|
|
|
671
650
|
|
|
672
|
-
def _verify_expected_model_files(
|
|
673
|
-
path: Path, expected_files: tuple[tuple[str, int], ...]
|
|
674
|
-
) -> Path:
|
|
651
|
+
def _verify_expected_model_files(path: Path, expected_files: tuple[tuple[str, int], ...]) -> Path:
|
|
675
652
|
if path.is_symlink() or not path.is_dir():
|
|
676
653
|
raise TranscriptionError("asr_model_unavailable")
|
|
677
654
|
resolved = path.resolve(strict=True)
|
|
@@ -774,12 +751,8 @@ def _parse_result(path: Path, frame_count: int, sample_rate: int) -> Transcripti
|
|
|
774
751
|
)
|
|
775
752
|
)
|
|
776
753
|
previous_word_end = word_end
|
|
777
|
-
no_speech_probability = _optional_probability(
|
|
778
|
-
|
|
779
|
-
)
|
|
780
|
-
if _low_quality_segment(
|
|
781
|
-
raw["text"], no_speech_probability, probabilities
|
|
782
|
-
):
|
|
754
|
+
no_speech_probability = _optional_probability(raw["no_speech_probability"])
|
|
755
|
+
if _low_quality_segment(raw["text"], no_speech_probability, probabilities):
|
|
783
756
|
continue
|
|
784
757
|
segments.append(
|
|
785
758
|
TranscriptSegment(
|
|
@@ -791,9 +764,7 @@ def _parse_result(path: Path, frame_count: int, sample_rate: int) -> Transcripti
|
|
|
791
764
|
)
|
|
792
765
|
)
|
|
793
766
|
segments = _drop_repetition_loops(segments)
|
|
794
|
-
accepted_text = "\n".join(
|
|
795
|
-
segment.text.strip() for segment in segments if segment.text.strip()
|
|
796
|
-
)
|
|
767
|
+
accepted_text = "\n".join(segment.text.strip() for segment in segments if segment.text.strip())
|
|
797
768
|
return TranscriptionResult(language, accepted_text, tuple(segments))
|
|
798
769
|
except (KeyError, TypeError, ValueError, InvalidOperation) as error:
|
|
799
770
|
raise TranscriptionError("asr_result_invalid") from error
|
|
@@ -830,19 +801,12 @@ def _low_quality_segment(
|
|
|
830
801
|
return True
|
|
831
802
|
if not any(unicodedata.category(character)[0] in {"L", "N"} for character in compact):
|
|
832
803
|
return True
|
|
833
|
-
if (
|
|
834
|
-
no_speech_probability is not None
|
|
835
|
-
and Decimal(no_speech_probability) >= Decimal("0.6")
|
|
836
|
-
):
|
|
804
|
+
if no_speech_probability is not None and Decimal(no_speech_probability) >= Decimal("0.6"):
|
|
837
805
|
return True
|
|
838
806
|
if word_probabilities:
|
|
839
807
|
mean = sum(word_probabilities) / len(word_probabilities)
|
|
840
|
-
low_ratio = sum(
|
|
841
|
-
|
|
842
|
-
) / len(word_probabilities)
|
|
843
|
-
if mean < Decimal("0.2") or (
|
|
844
|
-
mean < Decimal("0.35") and low_ratio >= 0.75
|
|
845
|
-
):
|
|
808
|
+
low_ratio = sum(probability < Decimal("0.5") for probability in word_probabilities) / len(word_probabilities)
|
|
809
|
+
if mean < Decimal("0.2") or (mean < Decimal("0.35") and low_ratio >= 0.75):
|
|
846
810
|
return True
|
|
847
811
|
if len(compact) >= 12 and len(set(compact)) / len(compact) < 0.15:
|
|
848
812
|
return True
|
|
@@ -860,9 +824,7 @@ def _drop_repetition_loops(
|
|
|
860
824
|
consecutive = 0
|
|
861
825
|
for segment in segments:
|
|
862
826
|
normalized = "".join(
|
|
863
|
-
character.casefold()
|
|
864
|
-
for character in segment.text
|
|
865
|
-
if unicodedata.category(character)[0] in {"L", "N"}
|
|
827
|
+
character.casefold() for character in segment.text if unicodedata.category(character)[0] in {"L", "N"}
|
|
866
828
|
)
|
|
867
829
|
if not normalized:
|
|
868
830
|
continue
|
|
@@ -899,11 +861,7 @@ def _decimal(value: object, *, minimum: Decimal, maximum: Decimal) -> Decimal:
|
|
|
899
861
|
|
|
900
862
|
|
|
901
863
|
def _validate_text(value: object, maximum_bytes: int, label: str) -> None:
|
|
902
|
-
if (
|
|
903
|
-
not isinstance(value, str)
|
|
904
|
-
or "\x00" in value
|
|
905
|
-
or len(value.encode("utf-8")) > maximum_bytes
|
|
906
|
-
):
|
|
864
|
+
if not isinstance(value, str) or "\x00" in value or len(value.encode("utf-8")) > maximum_bytes:
|
|
907
865
|
raise ValueError(f"{label} text is invalid")
|
|
908
866
|
|
|
909
867
|
|
|
@@ -1,21 +1,23 @@
|
|
|
1
1
|
"""Bounded local evidence extraction, using decoded PTS rather than average FPS."""
|
|
2
|
+
|
|
2
3
|
from __future__ import annotations
|
|
3
4
|
|
|
4
|
-
from array import array
|
|
5
|
-
from contextlib import contextmanager
|
|
6
|
-
from fractions import Fraction
|
|
7
5
|
import math
|
|
8
6
|
import os
|
|
9
|
-
from pathlib import Path
|
|
10
7
|
import re
|
|
11
8
|
import shutil
|
|
12
9
|
import sys
|
|
13
10
|
import threading
|
|
14
11
|
import wave
|
|
12
|
+
from array import array
|
|
13
|
+
from contextlib import contextmanager
|
|
14
|
+
from fractions import Fraction
|
|
15
|
+
from pathlib import Path
|
|
15
16
|
|
|
16
17
|
from clipen.media import bounded_file_fingerprint
|
|
17
18
|
from clipen.media.discovery import open_file_beneath
|
|
18
|
-
|
|
19
|
+
|
|
20
|
+
from .audio import AudioPreparationError, AudioPreparationInput, FFmpegAudioAdapter
|
|
19
21
|
from .keyframes import AdaptiveKeyframeSelector
|
|
20
22
|
|
|
21
23
|
|
|
@@ -23,9 +25,11 @@ from .keyframes import AdaptiveKeyframeSelector
|
|
|
23
25
|
def verified_source(source: AudioPreparationInput):
|
|
24
26
|
descriptor = open_file_beneath(source.root, source.relative_path)
|
|
25
27
|
try:
|
|
28
|
+
|
|
26
29
|
def identity():
|
|
27
30
|
info = os.fstat(descriptor)
|
|
28
31
|
return info.st_size, info.st_mtime_ns, bounded_file_fingerprint(descriptor, info.st_size)
|
|
32
|
+
|
|
29
33
|
expected = source.size_bytes, source.mtime_ns, source.fingerprint
|
|
30
34
|
if identity() != expected:
|
|
31
35
|
raise AudioPreparationError("source_changed")
|
|
@@ -46,15 +50,14 @@ def verified_source(source: AudioPreparationInput):
|
|
|
46
50
|
|
|
47
51
|
class LocalEvidenceExtractor(FFmpegAudioAdapter):
|
|
48
52
|
"""Bounded sparse video evidence plus a 120-second audio window per stream."""
|
|
53
|
+
|
|
49
54
|
MAX_FRAMES = AdaptiveKeyframeSelector.MAXIMUM_BUDGET
|
|
50
55
|
WINDOW_SECONDS = 120
|
|
51
56
|
|
|
52
57
|
def __init__(self, executable: str | None = None, probe_executable: str | None = None):
|
|
53
58
|
super().__init__(executable, timeout_seconds=120, maximum_output_bytes=8 * 1024**2)
|
|
54
59
|
sibling = Path(self.executable).with_name("ffprobe")
|
|
55
|
-
self.probe_executable = probe_executable or (
|
|
56
|
-
str(sibling) if sibling.is_file() else shutil.which("ffprobe")
|
|
57
|
-
)
|
|
60
|
+
self.probe_executable = probe_executable or (str(sibling) if sibling.is_file() else shutil.which("ffprobe"))
|
|
58
61
|
|
|
59
62
|
def frame(self, source, offset: Fraction, output: Path, cancel: threading.Event):
|
|
60
63
|
output = self._validate_empty_output(output)
|
|
@@ -67,13 +70,41 @@ class LocalEvidenceExtractor(FFmpegAudioAdapter):
|
|
|
67
70
|
raise AudioPreparationError("frame_time_mapping_invalid")
|
|
68
71
|
absolute_time = Fraction(int(span.start_pts)) * Fraction(tb.numerator, tb.denominator) + offset
|
|
69
72
|
with verified_source(source) as descriptor:
|
|
70
|
-
_, stderr, code = self._run(
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
73
|
+
_, stderr, code = self._run(
|
|
74
|
+
(
|
|
75
|
+
self.executable,
|
|
76
|
+
"-nostdin",
|
|
77
|
+
"-hide_banner",
|
|
78
|
+
"-loglevel",
|
|
79
|
+
"info",
|
|
80
|
+
"-copyts",
|
|
81
|
+
"-noaccurate_seek",
|
|
82
|
+
"-seek_timestamp",
|
|
83
|
+
"1",
|
|
84
|
+
"-ss",
|
|
85
|
+
f"{float(absolute_time):.9f}",
|
|
86
|
+
"-i",
|
|
87
|
+
f"/dev/fd/{descriptor}",
|
|
88
|
+
"-map",
|
|
89
|
+
f"0:{source.stream_index}",
|
|
90
|
+
"-an",
|
|
91
|
+
"-frames:v",
|
|
92
|
+
"1",
|
|
93
|
+
"-vf",
|
|
94
|
+
f"scale=512:512:force_original_aspect_ratio=decrease,format=rgb24,settb={tb.numerator}/{tb.denominator},select=gte(pts\\,{math.ceil(absolute_time / Fraction(tb.numerator, tb.denominator))}),showinfo",
|
|
95
|
+
"-threads",
|
|
96
|
+
"1",
|
|
97
|
+
"-c:v",
|
|
98
|
+
"png",
|
|
99
|
+
"-f",
|
|
100
|
+
"image2",
|
|
101
|
+
"-y",
|
|
102
|
+
str(output),
|
|
103
|
+
),
|
|
104
|
+
(descriptor,),
|
|
105
|
+
cancel,
|
|
106
|
+
observed_output=output,
|
|
107
|
+
)
|
|
77
108
|
matches = re.findall(rb"\bn:\s*0\s+pts:\s*(-?\d+)", stderr)
|
|
78
109
|
if code or len(matches) != 1 or not output.stat().st_size:
|
|
79
110
|
raise AudioPreparationError("frame_decode_failed")
|
|
@@ -87,20 +118,28 @@ class LocalEvidenceExtractor(FFmpegAudioAdapter):
|
|
|
87
118
|
raise AudioPreparationError("ffprobe_not_found")
|
|
88
119
|
span = source.source_span
|
|
89
120
|
time_base = Fraction(span.time_base.numerator, span.time_base.denominator)
|
|
90
|
-
anchors = AdaptiveKeyframeSelector.uniform(
|
|
91
|
-
|
|
92
|
-
)
|
|
93
|
-
intervals = ",".join(
|
|
94
|
-
f"{float(Fraction(candidate.pts) * time_base):.9f}%+0.02"
|
|
95
|
-
for candidate in anchors
|
|
96
|
-
)
|
|
121
|
+
anchors = AdaptiveKeyframeSelector.uniform(int(span.start_pts), int(span.end_pts), 32)
|
|
122
|
+
intervals = ",".join(f"{float(Fraction(candidate.pts) * time_base):.9f}%+0.02" for candidate in anchors)
|
|
97
123
|
with verified_source(source) as descriptor:
|
|
98
|
-
stdout, _, code = self._run(
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
124
|
+
stdout, _, code = self._run(
|
|
125
|
+
(
|
|
126
|
+
self.probe_executable,
|
|
127
|
+
"-v",
|
|
128
|
+
"error",
|
|
129
|
+
"-read_intervals",
|
|
130
|
+
intervals,
|
|
131
|
+
"-select_streams",
|
|
132
|
+
str(source.stream_index),
|
|
133
|
+
"-show_packets",
|
|
134
|
+
"-show_entries",
|
|
135
|
+
"packet=pts,size,flags",
|
|
136
|
+
"-of",
|
|
137
|
+
"csv=p=0",
|
|
138
|
+
f"/dev/fd/{descriptor}",
|
|
139
|
+
),
|
|
140
|
+
(descriptor,),
|
|
141
|
+
cancel,
|
|
142
|
+
)
|
|
104
143
|
if code:
|
|
105
144
|
raise AudioPreparationError("keyframe_packet_probe_failed")
|
|
106
145
|
packets = {}
|
|
@@ -117,18 +156,13 @@ class LocalEvidenceExtractor(FFmpegAudioAdapter):
|
|
|
117
156
|
ordered = sorted(packets.items())
|
|
118
157
|
limited = len(ordered) > 256
|
|
119
158
|
if limited:
|
|
120
|
-
ordered = [
|
|
121
|
-
ordered[index * len(ordered) // 256]
|
|
122
|
-
for index in range(256)
|
|
123
|
-
]
|
|
159
|
+
ordered = [ordered[index * len(ordered) // 256] for index in range(256)]
|
|
124
160
|
sizes = [size for _, size in ordered]
|
|
125
161
|
minimum, maximum = (min(sizes), max(sizes)) if sizes else (0, 0)
|
|
126
162
|
candidates = [
|
|
127
163
|
{
|
|
128
164
|
"pts": str(point),
|
|
129
|
-
"score": round((size - minimum) / (maximum - minimum), 6)
|
|
130
|
-
if maximum > minimum
|
|
131
|
-
else 0.0,
|
|
165
|
+
"score": round((size - minimum) / (maximum - minimum), 6) if maximum > minimum else 0.0,
|
|
132
166
|
}
|
|
133
167
|
for point, size in ordered
|
|
134
168
|
]
|
|
@@ -145,12 +179,35 @@ class LocalEvidenceExtractor(FFmpegAudioAdapter):
|
|
|
145
179
|
def audio_features(self, source, output: Path, cancel: threading.Event):
|
|
146
180
|
output = self._validate_empty_output(output)
|
|
147
181
|
with verified_source(source) as descriptor:
|
|
148
|
-
_, _, code = self._run(
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
182
|
+
_, _, code = self._run(
|
|
183
|
+
(
|
|
184
|
+
self.executable,
|
|
185
|
+
"-nostdin",
|
|
186
|
+
"-hide_banner",
|
|
187
|
+
"-loglevel",
|
|
188
|
+
"error",
|
|
189
|
+
"-i",
|
|
190
|
+
f"/dev/fd/{descriptor}",
|
|
191
|
+
"-map",
|
|
192
|
+
f"0:{source.stream_index}",
|
|
193
|
+
"-t",
|
|
194
|
+
str(self.WINDOW_SECONDS),
|
|
195
|
+
"-vn",
|
|
196
|
+
"-ac",
|
|
197
|
+
"1",
|
|
198
|
+
"-ar",
|
|
199
|
+
"16000",
|
|
200
|
+
"-c:a",
|
|
201
|
+
"pcm_s16le",
|
|
202
|
+
"-f",
|
|
203
|
+
"wav",
|
|
204
|
+
"-y",
|
|
205
|
+
str(output),
|
|
206
|
+
),
|
|
207
|
+
(descriptor,),
|
|
208
|
+
cancel,
|
|
209
|
+
observed_output=output,
|
|
210
|
+
)
|
|
154
211
|
if code:
|
|
155
212
|
raise AudioPreparationError("audio_decode_failed")
|
|
156
213
|
windows = []
|
|
@@ -164,13 +221,17 @@ class LocalEvidenceExtractor(FFmpegAudioAdapter):
|
|
|
164
221
|
values = array("h", data)
|
|
165
222
|
if sys.byteorder != "little":
|
|
166
223
|
values.byteswap()
|
|
167
|
-
rms = math.sqrt(sum(v*v for v in values) / len(values)) / 32768
|
|
168
|
-
windows.append(
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
224
|
+
rms = math.sqrt(sum(v * v for v in values) / len(values)) / 32768
|
|
225
|
+
windows.append(
|
|
226
|
+
{
|
|
227
|
+
"start_sample": start,
|
|
228
|
+
"end_sample": start + len(values),
|
|
229
|
+
"rms_dbfs": round(20 * math.log10(max(rms, 1e-6)), 2),
|
|
230
|
+
"peak": round(max(abs(v) for v in values) / 32768, 6),
|
|
231
|
+
"low_energy": rms < 0.01,
|
|
232
|
+
}
|
|
233
|
+
)
|
|
172
234
|
start += len(values)
|
|
173
235
|
if not windows:
|
|
174
236
|
raise AudioPreparationError("normalized_audio_invalid")
|
|
175
|
-
return {"sample_rate": 16000, "windows": windows,
|
|
176
|
-
"method": "signal_energy_not_music_or_speech_classification"}
|
|
237
|
+
return {"sample_rate": 16000, "windows": windows, "method": "signal_energy_not_music_or_speech_classification"}
|
|
@@ -5,6 +5,9 @@ from .storage import AnnotationRepository
|
|
|
5
5
|
from .suggestions import annotation_suggestion, annotation_suggestions
|
|
6
6
|
|
|
7
7
|
__all__ = [
|
|
8
|
-
"AnnotationRepository",
|
|
9
|
-
"
|
|
8
|
+
"AnnotationRepository",
|
|
9
|
+
"DayPart",
|
|
10
|
+
"MaterialUsage",
|
|
11
|
+
"annotation_suggestion",
|
|
12
|
+
"annotation_suggestions",
|
|
10
13
|
]
|