Skip to content

Commit 5d6cc0d

Browse files
committed
feat: add SubtitleStyle
1 parent aae1b06 commit 5d6cc0d

File tree

6 files changed

+41
-71
lines changed

6 files changed

+41
-71
lines changed

videodb/__init__.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,13 @@
55

66
from typing import Optional
77
from videodb._utils._video import play_stream
8-
from videodb._constants import VIDEO_DB_API, MediaType
8+
from videodb._constants import (
9+
VIDEO_DB_API,
10+
MediaType,
11+
SubtitleAlignment,
12+
SubtitleBorderStyle,
13+
SubtitleStyle,
14+
)
915
from videodb.client import Connection
1016
from videodb.exceptions import (
1117
VideodbError,
@@ -26,6 +32,9 @@
2632
"SearchError",
2733
"play_stream",
2834
"MediaType",
35+
"SubtitleAlignment",
36+
"SubtitleBorderStyle",
37+
"SubtitleStyle",
2938
]
3039

3140

videodb/_constants.py

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

3+
from dataclasses import dataclass
34

45
VIDEO_DB_API: str = "https://api.videodb.io"
56

@@ -78,7 +79,8 @@ class SubtitleAlignment:
7879
top_right = 9
7980

8081

81-
class SubtitleStyleDefaultValues:
82+
@dataclass
83+
class SubtitleStyle:
8284
font_name: str = "Arial"
8385
font_size: float = 18
8486
primary_colour: str = "&H00FFFFFF" # white

videodb/asset.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,16 +93,16 @@ def __init__(
9393
asset_id: str,
9494
width: Optional[int] = 100,
9595
height: Optional[int] = 100,
96-
position_x: Optional[int] = 80,
97-
position_y: Optional[int] = 20,
98-
end: Optional[Union[int, None]] = None,
96+
x: Optional[int] = 80,
97+
y: Optional[int] = 20,
98+
duration: Optional[Union[int, None]] = None,
9999
) -> None:
100100
super().__init__(asset_id)
101101
self.width = width
102102
self.height = height
103-
self.position_x = position_x
104-
self.position_y = position_y
105-
self.end = end
103+
self.x = x
104+
self.y = y
105+
self.duration = duration
106106

107107
def to_json(self) -> dict:
108108
return copy.deepcopy(self.__dict__)
@@ -113,7 +113,7 @@ def __repr__(self) -> str:
113113
f"asset_id={self.asset_id}, "
114114
f"width={self.width}, "
115115
f"height={self.height}, "
116-
f"position_x={self.position_x}, "
117-
f"position_y={self.position_y}, "
118-
f"end={self.end})"
116+
f"x={self.x}, "
117+
f"y={self.y}, "
118+
f"duration={self.duration})"
119119
)

videodb/client.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,10 @@ def upload(
5757
description,
5858
callback_url,
5959
)
60-
if upload_data.get("id", "").startswith("m-"):
61-
return Video(self, **upload_data) if upload_data else None
62-
elif upload_data.get("id", "").startswith("a-"):
63-
return Audio(self, **upload_data) if upload_data else None
64-
elif upload_data.get("id", "").startswith("i-"):
65-
return Image(self, **upload_data) if upload_data else None
60+
media_id = upload_data.get("id", "")
61+
if media_id.startswith("m-"):
62+
return Video(self, **upload_data)
63+
elif media_id.startswith("a-"):
64+
return Audio(self, **upload_data)
65+
elif media_id.startswith("img-"):
66+
return Image(self, **upload_data)

videodb/collection.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,10 @@ def upload(
101101
description,
102102
callback_url,
103103
)
104-
if upload_data.get("id", "").startswith("m-"):
105-
return Video(self._connection, **upload_data) if upload_data else None
106-
elif upload_data.get("id", "").startswith("a-"):
107-
return Audio(self._connection, **upload_data) if upload_data else None
108-
elif upload_data.get("id", "").startswith("i-"):
109-
return Image(self._connection, **upload_data) if upload_data else None
104+
media_id = upload_data.get("id", "")
105+
if media_id.startswith("m-"):
106+
return Video(self, **upload_data)
107+
elif media_id.startswith("a-"):
108+
return Audio(self, **upload_data)
109+
elif media_id.startswith("img-"):
110+
return Image(self, **upload_data)

videodb/video.py

Lines changed: 5 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
SearchType,
66
IndexType,
77
Workflows,
8-
SubtitleStyleDefaultValues,
8+
SubtitleStyle,
99
)
1010
from videodb.search import SearchFactory, SearchResult
1111
from videodb.shot import Shot
@@ -130,57 +130,14 @@ def index_spoken_words(self) -> None:
130130
},
131131
)
132132

133-
def add_subtitle(
134-
self,
135-
font_name: str = SubtitleStyleDefaultValues.font_name,
136-
font_size: float = SubtitleStyleDefaultValues.font_size,
137-
primary_colour: str = SubtitleStyleDefaultValues.primary_colour,
138-
secondary_colour: str = SubtitleStyleDefaultValues.secondary_colour,
139-
outline_colour: str = SubtitleStyleDefaultValues.outline_colour,
140-
back_colour: str = SubtitleStyleDefaultValues.back_colour,
141-
bold: bool = SubtitleStyleDefaultValues.bold,
142-
italic: bool = SubtitleStyleDefaultValues.italic,
143-
underline: bool = SubtitleStyleDefaultValues.underline,
144-
strike_out: bool = SubtitleStyleDefaultValues.strike_out,
145-
scale_x: float = SubtitleStyleDefaultValues.scale_x,
146-
scale_y: float = SubtitleStyleDefaultValues.scale_x,
147-
spacing: float = SubtitleStyleDefaultValues.spacing,
148-
angle: float = SubtitleStyleDefaultValues.angle,
149-
border_style: int = SubtitleStyleDefaultValues.border_style,
150-
outline: float = SubtitleStyleDefaultValues.outline,
151-
shadow: float = SubtitleStyleDefaultValues.shadow,
152-
alignment: int = SubtitleStyleDefaultValues.alignment,
153-
margin_l: int = SubtitleStyleDefaultValues.margin_l,
154-
margin_r: int = SubtitleStyleDefaultValues.margin_r,
155-
margin_v: int = SubtitleStyleDefaultValues.margin_v,
156-
) -> str:
133+
def add_subtitle(self, style: SubtitleStyle = SubtitleStyle()) -> str:
134+
if not isinstance(style, SubtitleStyle):
135+
raise ValueError("style must be of type SubtitleStyle")
157136
subtitle_data = self._connection.post(
158137
path=f"{ApiPath.video}/{self.id}/{ApiPath.workflow}",
159138
data={
160139
"type": Workflows.add_subtitles,
161-
"subtitle_style": {
162-
"font_name": font_name,
163-
"font_size": font_size,
164-
"primary_colour": primary_colour,
165-
"secondary_colour": secondary_colour,
166-
"outline_colour": outline_colour,
167-
"back_colour": back_colour,
168-
"bold": bold,
169-
"italic": italic,
170-
"underline": underline,
171-
"strike_out": strike_out,
172-
"scale_x": scale_x,
173-
"scale_y": scale_y,
174-
"spacing": spacing,
175-
"angle": angle,
176-
"border_style": border_style,
177-
"outline": outline,
178-
"shadow": shadow,
179-
"alignment": alignment,
180-
"margin_l": margin_l,
181-
"margin_r": margin_r,
182-
"margin_v": margin_v,
183-
},
140+
"subtitle_style": style.__dict__,
184141
},
185142
)
186143
return subtitle_data.get("stream_url", None)

0 commit comments

Comments
 (0)