Skip to content

Commit 1c903d9

Browse files
committed
feat: add text asset
1 parent df45aea commit 1c903d9

File tree

3 files changed

+72
-7
lines changed

3 files changed

+72
-7
lines changed

videodb/_constants.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""Constants used in the videodb package."""
2-
2+
from typing import Union
33
from dataclasses import dataclass
44

55
VIDEO_DB_API: str = "https://api.videodb.io"
@@ -103,3 +103,33 @@ class SubtitleStyle:
103103
margin_l: int = 10
104104
margin_r: int = 10
105105
margin_v: int = 10
106+
107+
108+
@dataclass
109+
class TextStyle:
110+
fontsize: int = 24
111+
fontcolor: str = "black"
112+
fontcolor_expr: str = ""
113+
alpha: float = 1.0
114+
font: str = "Sans"
115+
ft_load_flags: str = "default"
116+
box: bool = True
117+
boxcolor: str = "white"
118+
boxborderw: str = "10"
119+
boxw: int = 0
120+
boxh: int = 0
121+
line_spacing: int = 0
122+
text_align: str = "T"
123+
y_align: str = "text"
124+
borderw: int = 0
125+
bordercolor: str = "black"
126+
expansion: str = "normal"
127+
basetime: int = 0
128+
fix_bounds: bool = False
129+
text_shaping: bool = True
130+
shadowcolor: str = "black"
131+
shadowx: int = 0
132+
shadowy: int = 0
133+
tabsize: int = 4
134+
x: Union[str, int] = "(main_w-text_w)/2"
135+
y: Union[str, int] = "(main_h-text_h)/2"

videodb/asset.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import copy
22
import logging
3+
import uuid
34

45
from typing import Optional, Union
56

6-
from videodb._constants import MaxSupported
7+
from videodb._constants import MaxSupported, TextStyle
78

89
logger = logging.getLogger(__name__)
910

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

videodb/timeline.py

Lines changed: 13 additions & 5 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):
@@ -23,14 +23,22 @@ def to_json(self) -> dict:
2323
timeline_json.append(asset.to_json())
2424
return {"timeline": timeline_json}
2525

26-
def add_inline(self, asset: Union[VideoAsset]) -> None:
26+
def add_inline(self, asset: VideoAsset) -> None:
2727
if not isinstance(asset, VideoAsset):
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)