Skip to content

Commit e71d9d3

Browse files
committed
Text Asset added
1 parent 3afb70d commit e71d9d3

File tree

4 files changed

+75
-5
lines changed

4 files changed

+75
-5
lines changed

videodb/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
SubtitleAlignment,
1515
SubtitleBorderStyle,
1616
SubtitleStyle,
17+
TextStyle,
1718
)
1819
from videodb.client import Connection
1920
from videodb.exceptions import (
@@ -41,6 +42,7 @@
4142
"SubtitleBorderStyle",
4243
"SubtitleStyle",
4344
"SceneModels",
45+
"TextStyle"
4446
]
4547

4648

videodb/_constants.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Constants used in the videodb package."""
22

3+
from typing import Union
34
from dataclasses import dataclass
45

56
VIDEO_DB_API: str = "https://api.videodb.io"
@@ -21,11 +22,13 @@ class IndexType:
2122
semantic = "semantic"
2223
scene = "scene"
2324

25+
2426
class ExtractionType:
2527
scene_based = "scene"
2628
compression_based = "compression"
2729
time_based = "time"
2830

31+
2932
class SceneModels:
3033
gemini_vision: str = "gemini-vision"
3134
gpt4_vision: str = "gpt4-v"
@@ -119,3 +122,31 @@ class SubtitleStyle:
119122
margin_l: int = 10
120123
margin_r: int = 10
121124
margin_v: int = 10
125+
126+
@dataclass
127+
class TextStyle:
128+
fontsize: int = 24
129+
fontcolor: str = "black"
130+
fontcolor_expr: str = ""
131+
alpha: float = 1.0
132+
font: str = "Sans"
133+
box: bool = True
134+
boxcolor: str = "white"
135+
boxborderw: str = "10"
136+
boxw: int = 0
137+
boxh: int = 0
138+
line_spacing: int = 0
139+
text_align: str = "T"
140+
y_align: str = "text"
141+
borderw: int = 0
142+
bordercolor: str = "black"
143+
expansion: str = "normal"
144+
basetime: int = 0
145+
fix_bounds: bool = False
146+
text_shaping: bool = True
147+
shadowcolor: str = "black"
148+
shadowx: int = 0
149+
shadowy: int = 0
150+
tabsize: int = 4
151+
x: Union[str, int] = "(main_w-text_w)/2"
152+
y: Union[str, int] = "(main_h-text_h)/2"

videodb/asset.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from typing import Optional, Union
55

6-
from videodb._constants import MaxSupported
6+
from videodb._constants import MaxSupported, TextStyle
77

88
logger = logging.getLogger(__name__)
99

@@ -117,3 +117,32 @@ def __repr__(self) -> str:
117117
f"y={self.y}, "
118118
f"duration={self.duration})"
119119
)
120+
121+
class TextAsset(MediaAsset):
122+
def __init__(
123+
self,
124+
text: str,
125+
duration: Optional[int] = None,
126+
style: TextStyle = TextStyle(),
127+
) -> None:
128+
super().__init__(f"txt-{str(uuid.uuid4())}")
129+
self.text = text
130+
self.duration = duration
131+
self.style: TextStyle = style
132+
133+
def to_json(self) -> dict:
134+
return {
135+
"text": copy.deepcopy(self.text),
136+
"asset_id": copy.deepcopy(self.asset_id),
137+
"duration": copy.deepcopy(self.duration),
138+
"style": copy.deepcopy(self.style.__dict__),
139+
}
140+
141+
def __repr__(self) -> str:
142+
return (
143+
f"TextAsset("
144+
f"text={self.text}, "
145+
f"asset_id={self.asset_id}, "
146+
f"duration={self.duration}, "
147+
f"style={self.style})"
148+
)

videodb/timeline.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from typing import Union
22

33
from videodb._constants import ApiPath
4-
from videodb.asset import VideoAsset, AudioAsset, ImageAsset
4+
from videodb.asset import VideoAsset, AudioAsset, ImageAsset, TextAsset
55

66

77
class Timeline(object):
@@ -28,9 +28,17 @@ def add_inline(self, asset: Union[VideoAsset]) -> None:
2828
raise ValueError("asset must be of type VideoAsset")
2929
self._timeline.append(asset)
3030

31-
def add_overlay(self, start: int, asset: Union[AudioAsset, ImageAsset]) -> None:
32-
if not isinstance(asset, AudioAsset) and not isinstance(asset, ImageAsset):
33-
raise ValueError("asset must be of type AudioAsset or ImageAsset")
31+
def add_overlay(
32+
self, start: int, asset: Union[AudioAsset, ImageAsset, TextAsset]
33+
) -> None:
34+
if (
35+
not isinstance(asset, AudioAsset)
36+
and not isinstance(asset, ImageAsset)
37+
and not isinstance(asset, TextAsset)
38+
):
39+
raise ValueError(
40+
"asset must be of type AudioAsset, ImageAsset or TextAsset"
41+
)
3442
self._timeline.append((start, asset))
3543

3644
def generate_stream(self) -> str:

0 commit comments

Comments
 (0)