visualkit 0.0.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,130 @@
1
+ import random
2
+ from pathlib import Path
3
+
4
+ from visualkit.media_element import MediaElement
5
+ from visualkit.transitions import TransitionElement
6
+
7
+
8
+ class ProjectCreator:
9
+ @staticmethod
10
+ def create_project_from_media_and_transcript(
11
+ media: dict,
12
+ transcript: dict,
13
+ media_dir: str = "",
14
+ resolution: tuple[int, int] = (720, 1280),
15
+ fps: int = 30,
16
+ codec: str = "mp4v",
17
+ transition_duration: float = 1.0,
18
+ resize_method: str = "adaptive", # Options: smart, adaptive, gradual, aspect_ratio
19
+ ):
20
+ """
21
+ Creates a project configuration from media and transcript data.
22
+ Args:
23
+ media (dict): Media assets to include in the project. `media['media']` list of dicts with keys:
24
+ - file_path (str): Path to the media file.
25
+ - start_time (float): Start time of the media in seconds.
26
+ - end_time (float): End time of the media in seconds.
27
+ - type (str): Type of media, e.g., "image" or "video". [optional, default is "image"]
28
+
29
+ transcript (dict): Transcript data for subtitles. `transcript['segments']` list of dicts with keys:
30
+ - start (float): Start time of the subtitle in seconds.
31
+ - end (float): End time of the subtitle in seconds.
32
+ - text (str): Text of the subtitle.
33
+
34
+ media_dir (str): Directory containing media files.
35
+ resolution (tuple): Video resolution as (width, height).
36
+ fps (int): Frames per second for the video.
37
+ codec (str): Codec to use for video encoding.
38
+ transition_duration (float): Duration of transitions between media elements.
39
+ resize_method (str): Method to use for resizing images. [adaptive (default)] Options: smart, adaptive, gradual, aspect_ratio.
40
+ Returns:
41
+ dict: Project configuration dictionary.
42
+ """ # noqa: E501
43
+
44
+ project = {
45
+ "config": {
46
+ "width": resolution[0],
47
+ "height": resolution[1],
48
+ "fps": fps,
49
+ "codec": codec,
50
+ "resize_method": resize_method,
51
+ },
52
+ "assets": {},
53
+ "main_layer": [],
54
+ "subtitle_layers": [],
55
+ }
56
+ total_media = len(media["media"])
57
+ # load media assets
58
+ for i, entry in enumerate(media["media"]):
59
+ media_id = f"media_{i + 1:02d}"
60
+ media_path: Path = Path(media_dir) / entry["file_path"]
61
+ media_type = entry.get("type", "image")
62
+ project["assets"][media_id] = {
63
+ "path": str(media_path),
64
+ "type": media_type,
65
+ }
66
+ # if not first index, add a transition
67
+ duration = float(entry["end_time"]) - float(entry["start_time"])
68
+ taken = False
69
+
70
+ if i > 0 and duration > 1:
71
+ project["main_layer"].append(
72
+ {
73
+ "type": "transition",
74
+ "duration": transition_duration,
75
+ "transition_type": random.choice(
76
+ TransitionElement.get_available_transitions()
77
+ ),
78
+ }
79
+ )
80
+ taken = True
81
+
82
+ # calculate transition overlapping duration for media element
83
+ sub_duration = 0
84
+ if not taken or total_media == 1: # Only one media element
85
+ sub_duration = 0
86
+ elif i == 0 or i == total_media - 1: # first or last media
87
+ sub_duration = transition_duration / 2
88
+ else: # middle media
89
+ sub_duration = transition_duration
90
+
91
+ if media_type == "image":
92
+ project["main_layer"].append(
93
+ {
94
+ "type": "media",
95
+ "media": media_id,
96
+ "duration": round(duration - sub_duration, 3),
97
+ "effect": entry.get(
98
+ "effect", random.choice(MediaElement.get_moving_effects())
99
+ ),
100
+ }
101
+ )
102
+ elif media_type == "video":
103
+ # todo: handle video asset properly
104
+ project["main_layer"].append(
105
+ {
106
+ "type": "media",
107
+ "media": media_id,
108
+ "duration": entry.get("duration", 5.0),
109
+ }
110
+ )
111
+
112
+ # transcript elements for subtitle layers
113
+ subtitle_layer = {
114
+ "name": "main_subtitles",
115
+ "elements": [],
116
+ }
117
+
118
+ for seg in transcript["segments"]:
119
+ subtitle_layer["elements"].append(
120
+ {
121
+ "start_time": seg["start"],
122
+ "end_time": seg["end"],
123
+ "text": seg["text"],
124
+ "animation_type": "typewriter",
125
+ }
126
+ )
127
+
128
+ project["subtitle_layers"].append(subtitle_layer)
129
+
130
+ return project
@@ -0,0 +1,277 @@
1
+ from typing import Any, Dict, List, Optional, Tuple
2
+
3
+ import cv2
4
+ import numpy as np
5
+
6
+ from visualkit.config import VideoConfig
7
+ from visualkit.enums import AnimationType
8
+
9
+
10
+ class SubtitleElement:
11
+ def __init__(
12
+ self,
13
+ start_time: float,
14
+ end_time: float,
15
+ text: str,
16
+ animation_type: AnimationType = AnimationType.TYPEWRITER,
17
+ typewriter_speed: float = 20.0,
18
+ ):
19
+ if start_time < 0 or end_time < 0:
20
+ raise ValueError("Times must be non-negative")
21
+ if start_time >= end_time:
22
+ raise ValueError("Start time must be less than end time")
23
+ if typewriter_speed <= 0:
24
+ raise ValueError("Typewriter speed must be positive")
25
+
26
+ self.start_time = start_time
27
+ self.end_time = end_time
28
+ self.text = text
29
+ self.animation_type = animation_type
30
+ self.typewriter_speed = typewriter_speed
31
+ self._wrapped_text_cache = None
32
+ self._cache_max_width = None
33
+
34
+ def is_active(self, timestamp: float) -> bool:
35
+ return self.start_time <= timestamp <= self.end_time
36
+
37
+ def get_typewriter_progress(self, timestamp: float) -> float:
38
+ if timestamp < self.start_time or len(self.text) == 0:
39
+ return 0.0
40
+ elapsed = timestamp - self.start_time
41
+ chars_revealed = elapsed * self.typewriter_speed
42
+ return min(chars_revealed / len(self.text), 1.0)
43
+
44
+ def get_fade_progress(self, timestamp: float) -> float:
45
+ if not self.is_active(timestamp):
46
+ return 0.0
47
+ duration = self.end_time - self.start_time
48
+ if duration <= 0:
49
+ return 1.0
50
+ return (timestamp - self.start_time) / duration
51
+
52
+
53
+ class TextRenderer:
54
+ def __init__(self, config: VideoConfig):
55
+ self.config = config
56
+ self.font = cv2.FONT_HERSHEY_SIMPLEX
57
+ self.font_scale = 1.0
58
+ self.thickness = 2
59
+ self.outline_thickness = 4
60
+ self.background_color = (0, 0, 0)
61
+ self.background_opacity = 0.85
62
+ self.complete_text_color = (180, 180, 180)
63
+ self.active_text_color = (255, 255, 0)
64
+ self.outline_color = (0, 0, 0)
65
+ self.margin = 60
66
+ self.line_spacing = 8
67
+ self.corner_radius = 15
68
+ self.background_padding = 20
69
+
70
+ def _wrap_text(self, text: str, max_width: int) -> List[str]:
71
+ if not text:
72
+ return []
73
+ words = text.split()
74
+ lines = []
75
+ current_line = ""
76
+ for word in words:
77
+ test_line = current_line + (" " if current_line else "") + word
78
+ (text_width, _), _ = cv2.getTextSize(
79
+ test_line, self.font, self.font_scale, self.thickness
80
+ )
81
+ if text_width <= max_width:
82
+ current_line = test_line
83
+ else:
84
+ if current_line:
85
+ lines.append(current_line)
86
+ current_line = word
87
+ if current_line:
88
+ lines.append(current_line)
89
+ return lines
90
+
91
+ def _get_text_dimensions(self, lines: List[str]) -> Tuple[int, int]:
92
+ if not lines:
93
+ return 0, 0
94
+ max_width = 0
95
+ total_height = 0
96
+ for i, line in enumerate(lines):
97
+ (text_width, text_height), baseline = cv2.getTextSize(
98
+ line, self.font, self.font_scale, self.thickness
99
+ )
100
+ max_width = max(max_width, text_width)
101
+ total_height += text_height + (self.line_spacing if i > 0 else 0)
102
+ return max_width, total_height
103
+
104
+ def _get_text_position(
105
+ self,
106
+ text: str,
107
+ line_index: int = 0,
108
+ total_lines: int = 1,
109
+ text_y_start: int = 0,
110
+ ) -> Tuple[int, int]:
111
+ (text_width, text_height), baseline = cv2.getTextSize(
112
+ text, self.font, self.font_scale, self.thickness
113
+ )
114
+ x = (self.config.width - text_width) // 2
115
+ line_height = text_height + self.line_spacing
116
+ y = text_y_start + text_height + (line_index * line_height)
117
+ return x, y
118
+
119
+ def _draw_rounded_rectangle(
120
+ self,
121
+ img: np.ndarray,
122
+ pt1: Tuple[int, int],
123
+ pt2: Tuple[int, int],
124
+ color: Tuple[int, int, int],
125
+ radius: int = 10,
126
+ ):
127
+ x1, y1 = pt1
128
+ x2, y2 = pt2
129
+ if x1 > x2:
130
+ x1, x2 = x2, x1
131
+ if y1 > y2:
132
+ y1, y2 = y2, y1
133
+ radius = min(radius, (x2 - x1) // 2, (y2 - y1) // 2)
134
+ if radius <= 0:
135
+ cv2.rectangle(img, (x1, y1), (x2, y2), color, -1)
136
+ return
137
+ cv2.rectangle(img, (x1, y1 + radius), (x2, y2 - radius), color, -1)
138
+ cv2.rectangle(img, (x1 + radius, y1), (x2 - radius, y2), color, -1)
139
+ cv2.circle(img, (x1 + radius, y1 + radius), radius, color, -1)
140
+ cv2.circle(img, (x2 - radius, y1 + radius), radius, color, -1)
141
+ cv2.circle(img, (x1 + radius, y2 - radius), radius, color, -1)
142
+ cv2.circle(img, (x2 - radius, y2 - radius), radius, color, -1)
143
+
144
+ def _draw_background(self, frame: np.ndarray, lines: List[str]):
145
+ if not lines:
146
+ return
147
+ text_width, text_height = self._get_text_dimensions(lines)
148
+ x_center = self.config.width // 2
149
+ y_bottom = self.config.height - self.margin
150
+ x1 = max(0, x_center - text_width // 2 - self.background_padding)
151
+ x2 = min(
152
+ self.config.width, x_center + text_width // 2 + self.background_padding
153
+ )
154
+ y1 = max(0, y_bottom - text_height - self.background_padding)
155
+ y2 = min(self.config.height, y_bottom + self.background_padding)
156
+ overlay = frame.copy()
157
+ self._draw_rounded_rectangle(
158
+ overlay, (x1, y1), (x2, y2), self.background_color, self.corner_radius
159
+ )
160
+ cv2.addWeighted(
161
+ overlay,
162
+ self.background_opacity,
163
+ frame,
164
+ 1 - self.background_opacity,
165
+ 0,
166
+ frame,
167
+ )
168
+
169
+ def _draw_text_with_outline(
170
+ self,
171
+ frame: np.ndarray,
172
+ text: str,
173
+ position: Tuple[int, int],
174
+ color: Tuple[int, int, int],
175
+ outline_color: Tuple[int, int, int],
176
+ ):
177
+ x, y = position
178
+ cv2.putText(
179
+ frame,
180
+ text,
181
+ (x, y),
182
+ self.font,
183
+ self.font_scale,
184
+ outline_color,
185
+ self.outline_thickness,
186
+ cv2.LINE_AA,
187
+ )
188
+ cv2.putText(
189
+ frame,
190
+ text,
191
+ (x, y),
192
+ self.font,
193
+ self.font_scale,
194
+ color,
195
+ self.thickness,
196
+ cv2.LINE_AA,
197
+ )
198
+
199
+ def render_subtitle(
200
+ self, frame: np.ndarray, subtitle: SubtitleElement, timestamp: float
201
+ ) -> np.ndarray:
202
+ if not subtitle.is_active(timestamp):
203
+ return frame
204
+ max_width = self.config.width - (2 * self.background_padding) - 100
205
+ if (
206
+ subtitle._wrapped_text_cache is None
207
+ or subtitle._cache_max_width != max_width
208
+ ):
209
+ subtitle._wrapped_text_cache = self._wrap_text(subtitle.text, max_width)
210
+ subtitle._cache_max_width = max_width
211
+ lines = subtitle._wrapped_text_cache
212
+ if not lines:
213
+ return frame
214
+ self._draw_background(frame, lines) # Uncomment if background is needed
215
+ text_width, text_height = self._get_text_dimensions(lines)
216
+ text_y_start = self.config.height - self.margin - text_height
217
+ total_chars = sum(len(line) for line in lines)
218
+ if subtitle.animation_type == AnimationType.TYPEWRITER:
219
+ progress = subtitle.get_typewriter_progress(timestamp)
220
+ chars_to_show = int(total_chars * progress)
221
+ else:
222
+ chars_to_show = total_chars
223
+ char_offset = 0
224
+ for line_idx, line in enumerate(lines):
225
+ position = self._get_text_position(line, line_idx, len(lines), text_y_start)
226
+ if subtitle.animation_type == AnimationType.TYPEWRITER:
227
+ self._render_typewriter_line(
228
+ frame, line, position, chars_to_show, char_offset
229
+ )
230
+ elif subtitle.animation_type == AnimationType.FADE_IN:
231
+ fade_progress = subtitle.get_fade_progress(timestamp)
232
+ self._render_fade_line(frame, line, position, fade_progress)
233
+ else:
234
+ self._render_simple_line(frame, line, position)
235
+ char_offset += len(line)
236
+ return frame
237
+
238
+ def _render_typewriter_line(
239
+ self,
240
+ frame: np.ndarray,
241
+ line: str,
242
+ position: Tuple[int, int],
243
+ chars_to_show: int,
244
+ char_offset: int,
245
+ ):
246
+ self._draw_text_with_outline(
247
+ frame, line, position, self.complete_text_color, self.outline_color
248
+ )
249
+ line_start = char_offset
250
+ if chars_to_show > line_start:
251
+ active_chars_in_line = min(chars_to_show - line_start, len(line))
252
+ if active_chars_in_line > 0:
253
+ active_text = line[:active_chars_in_line]
254
+ self._draw_text_with_outline(
255
+ frame,
256
+ active_text,
257
+ position,
258
+ self.active_text_color,
259
+ self.outline_color,
260
+ )
261
+
262
+ def _render_fade_line(
263
+ self, frame: np.ndarray, line: str, position: Tuple[int, int], progress: float
264
+ ):
265
+ opacity = min(1.0, progress * 2)
266
+ overlay = frame.copy()
267
+ self._draw_text_with_outline(
268
+ overlay, line, position, self.active_text_color, self.outline_color
269
+ )
270
+ cv2.addWeighted(overlay, opacity, frame, 1 - opacity, 0, frame)
271
+
272
+ def _render_simple_line(
273
+ self, frame: np.ndarray, line: str, position: Tuple[int, int]
274
+ ):
275
+ self._draw_text_with_outline(
276
+ frame, line, position, self.active_text_color, self.outline_color
277
+ )
visualkit/timeline.py ADDED
@@ -0,0 +1,47 @@
1
+ from visualkit.media_element import MediaElement
2
+
3
+
4
+ class Timeline:
5
+ def __init__(self):
6
+ self.elements = []
7
+
8
+ def add_element(self, element, start_time: float, end_time: float):
9
+ if start_time >= end_time:
10
+ raise ValueError("Start time must be less than end time")
11
+ self.elements.append(
12
+ {"element": element, "start_time": start_time, "end_time": end_time}
13
+ )
14
+ self.elements.sort(key=lambda x: x["start_time"])
15
+
16
+ def get_active_element(self, timestamp: float):
17
+ for elem_info in self.elements:
18
+ if elem_info["start_time"] <= timestamp <= elem_info["end_time"]:
19
+ return elem_info
20
+ return None
21
+
22
+ def get_duration(self) -> float:
23
+ if not self.elements:
24
+ return 0.0
25
+ return max(elem["end_time"] for elem in self.elements)
26
+
27
+ def get_previous_media_element(self, current_start_time: float):
28
+ candidates = []
29
+ for elem_info in self.elements:
30
+ if elem_info["end_time"] <= current_start_time and isinstance(
31
+ elem_info["element"], MediaElement
32
+ ):
33
+ candidates.append(elem_info)
34
+ if candidates:
35
+ return max(candidates, key=lambda x: x["end_time"])
36
+ return None
37
+
38
+ def get_next_media_element(self, current_end_time: float):
39
+ candidates = []
40
+ for elem_info in self.elements:
41
+ if elem_info["start_time"] >= current_end_time and isinstance(
42
+ elem_info["element"], MediaElement
43
+ ):
44
+ candidates.append(elem_info)
45
+ if candidates:
46
+ return min(candidates, key=lambda x: x["start_time"])
47
+ return None