Coverage for src/blob_dict/blob/video.py: 0%
34 statements
« prev ^ index » next coverage.py v7.8.1, created at 2025-05-31 14:35 -0700
« prev ^ index » next coverage.py v7.8.1, created at 2025-05-31 14:35 -0700
1from __future__ import annotations
3from pathlib import Path
4from typing import Self, override
6from moviepy.editor import VideoClip, VideoFileClip
8from . import BytesBlob
9from .audio_video import read_from_clip
12class VideoBlob(BytesBlob):
13 def __init__(
14 self,
15 blob: bytes | VideoClip,
16 ) -> None:
17 if isinstance(blob, VideoClip):
18 blob = read_from_clip(
19 blob,
20 ".mp4",
21 delete_temp_clip_file=delete_temp_clip_file,
22 )
24 super().__init__(blob)
26 def as_video(self, filename: str) -> VideoFileClip:
27 Path(filename).write_bytes(self._blob_bytes)
29 return VideoFileClip(filename)
31 @override
32 def __repr__(self) -> str:
33 return f"{self.__class__.__name__}(...)"
35 @classmethod
36 @override
37 def load(cls: type[Self], f: Path | str) -> Self:
38 f = Path(f).expanduser()
40 if f.suffix.lower() == ".mp4":
41 return cls(f.read_bytes())
43 clip = VideoFileClip(str(f))
44 blob = cls(clip)
45 clip.close()
47 return blob
49 @override
50 def dump(self, f: Path | str) -> None:
51 f = Path(f).expanduser()
52 if f.suffix.lower() != ".mp4":
53 msg = "Only MP4 file is supported."
54 raise ValueError(msg)
56 f.write_bytes(self.as_bytes())