Skip to content

Commit c0ed0e1

Browse files
authored
Merge pull request #13 from video-db/ankit/add-text-asset
Ankit/add text asset
2 parents e41cca0 + 20b9c67 commit c0ed0e1

File tree

4 files changed

+82
-12
lines changed

4 files changed

+82
-12
lines changed

videodb/__init__.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
SubtitleAlignment,
1313
SubtitleBorderStyle,
1414
SubtitleStyle,
15+
TextStyle,
1516
)
1617
from videodb.client import Connection
1718
from videodb.exceptions import (
@@ -23,7 +24,7 @@
2324

2425
logger: logging.Logger = logging.getLogger("videodb")
2526

26-
__version__ = "0.0.5"
27+
__version__ = "0.1.0"
2728
__author__ = "videodb"
2829

2930
__all__ = [
@@ -37,6 +38,7 @@
3738
"SubtitleAlignment",
3839
"SubtitleBorderStyle",
3940
"SubtitleStyle",
41+
"TextStyle",
4042
]
4143

4244

videodb/_constants.py

+30-1
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"
@@ -106,3 +106,32 @@ class SubtitleStyle:
106106
margin_l: int = 10
107107
margin_r: int = 10
108108
margin_v: int = 10
109+
110+
111+
@dataclass
112+
class TextStyle:
113+
fontsize: int = 24
114+
fontcolor: str = "black"
115+
fontcolor_expr: str = ""
116+
alpha: float = 1.0
117+
font: str = "Sans"
118+
box: bool = True
119+
boxcolor: str = "white"
120+
boxborderw: str = "10"
121+
boxw: int = 0
122+
boxh: int = 0
123+
line_spacing: int = 0
124+
text_align: str = "T"
125+
y_align: str = "text"
126+
borderw: int = 0
127+
bordercolor: str = "black"
128+
expansion: str = "normal"
129+
basetime: int = 0
130+
fix_bounds: bool = False
131+
text_shaping: bool = True
132+
shadowcolor: str = "black"
133+
shadowx: int = 0
134+
shadowy: int = 0
135+
tabsize: int = 4
136+
x: Union[str, int] = "(main_w-text_w)/2"
137+
y: Union[str, int] = "(main_h-text_h)/2"

videodb/asset.py

+36-5
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

@@ -32,8 +33,8 @@ class VideoAsset(MediaAsset):
3233
def __init__(
3334
self,
3435
asset_id: str,
35-
start: Optional[int] = 0,
36-
end: Optional[Union[int, None]] = None,
36+
start: Optional[float] = 0,
37+
end: Optional[float] = None,
3738
) -> None:
3839
super().__init__(asset_id)
3940
self.start: int = start
@@ -55,8 +56,8 @@ class AudioAsset(MediaAsset):
5556
def __init__(
5657
self,
5758
asset_id: str,
58-
start: Optional[int] = 0,
59-
end: Optional[Union[int, None]] = None,
59+
start: Optional[float] = 0,
60+
end: Optional[float] = None,
6061
disable_other_tracks: Optional[bool] = True,
6162
fade_in_duration: Optional[Union[int, float]] = 0,
6263
fade_out_duration: Optional[Union[int, float]] = 0,
@@ -117,3 +118,33 @@ 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+
text: str,
127+
duration: Optional[int] = None,
128+
style: TextStyle = TextStyle(),
129+
) -> None:
130+
super().__init__(f"txt-{str(uuid.uuid4())}")
131+
self.text = text
132+
self.duration = duration
133+
self.style: TextStyle = style
134+
135+
def to_json(self) -> dict:
136+
return {
137+
"text": copy.deepcopy(self.text),
138+
"asset_id": copy.deepcopy(self.asset_id),
139+
"duration": copy.deepcopy(self.duration),
140+
"style": copy.deepcopy(self.style.__dict__),
141+
}
142+
143+
def __repr__(self) -> str:
144+
return (
145+
f"TextAsset("
146+
f"text={self.text}, "
147+
f"asset_id={self.asset_id}, "
148+
f"duration={self.duration}, "
149+
f"style={self.style})"
150+
)

videodb/timeline.py

+13-5
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)