Skip to main content

protect_video()

Detect and encrypt license plates across all frames of a video file.

Signature

safelicensing.protect_video(
video_path: str,
seed: float = 0.5,
output_path: str | None = None,
model = None,
model_path: str | None = None,
progress_callback: Callable[[float], None] | None = None,
) -> ProtectVideoResult

Parameters

ParameterTypeDefaultDescription
video_pathstrrequiredPath to the input video file. Supported formats: mp4, avi, mov (OpenCV-readable).
seedfloat0.5Chaotic encryption seed in (0.0, 1.0) exclusive.
output_pathstrNoneDestination path for the encrypted video. When None, the file is written as <stem>_encrypted.mp4 in the same directory as the source.
modelYOLO instanceNonePre-loaded YOLO model. Loads bundled best.pt when None.
model_pathstrNonePath to custom YOLOv8 .pt weights. Ignored when model is given.
progress_callbackcallableNoneOptional callable receiving a float in [0.0, 1.0] after each processed frame.

Returns: ProtectVideoResult

FieldTypeDescription
output_pathstrAbsolute path to the encrypted output video file
frame_countintTotal number of frames processed
fpsfloatFrame rate of the output video
elapsedfloatWall-clock processing time in seconds

Raises

ExceptionCondition
FileNotFoundErrorvideo_path does not exist
ValueErrorseed is not strictly inside (0.0, 1.0)

Examples

Basic usage

import safelicensing as sl

model = sl.load_model()
result = sl.protect_video("dashcam.mp4", seed=0.42, model=model)

print(result.output_path) # /abs/path/to/dashcam_encrypted.mp4
print(result.frame_count) # 1800
print(result.fps) # 30.0
print(result.elapsed) # 94.3

Custom output path

result = sl.protect_video(
"footage/recording.mp4",
seed=0.5,
output_path="protected/recording_safe.mp4",
model=model,
)

Progress tracking

from tqdm import tqdm

with tqdm(total=100, desc="Encrypting") as bar:
prev = 0.0

def on_progress(p: float):
nonlocal prev
delta = int(p * 100) - int(prev * 100)
bar.update(delta)
prev = p

result = sl.protect_video("dashcam.mp4", seed=0.5, progress_callback=on_progress)

Simple console progress

result = sl.protect_video(
"dashcam.mp4",
seed=0.42,
progress_callback=lambda p: print(f"\r{p:.0%}", end="", flush=True),
)
print() # newline after progress

Notes

  • The original audio track is preserved in the output video.
  • Output is always encoded as MP4 (H.264 via OpenCV / FFmpeg).
  • Large videos may require significant RAM; all frames are processed in memory.

See also: protect_image() → | Low-level video API →