Update video2.py
Browse files
video2.py
CHANGED
|
@@ -228,20 +228,18 @@ def audio_func(id, lines, lang):
|
|
| 228 |
return asyncio.run(generate_tts(id, lines, lang))
|
| 229 |
#-----------------------------
|
| 230 |
#---------------------------------
|
| 231 |
-
def video_func(id, lines,
|
| 232 |
-
|
| 233 |
-
listf = lang.split("&&&")
|
| 234 |
-
TEXT = listf[0].strip()
|
| 235 |
-
else:
|
| 236 |
-
TEXT = lines[id]
|
| 237 |
-
duration, audio_path = audio_func(id, lines, lang)
|
| 238 |
if not duration or not audio_path:
|
| 239 |
print("Failed to generate audio.")
|
| 240 |
return None
|
|
|
|
|
|
|
|
|
|
| 241 |
print("-----------------------------------------------------------------------------")
|
| 242 |
print(TEXT)
|
| 243 |
SKIP_SPACES = False
|
| 244 |
-
|
| 245 |
FPS = 30 # Increased for smoother animation
|
| 246 |
ANIMATION_FRAMES_PER_CHAR = 3 # Number of sub-frames for pen movement per character
|
| 247 |
WIDTH, HEIGHT = 1280, 720 # Keep as is
|
|
@@ -264,7 +262,7 @@ def video_func(id, lines, lang):
|
|
| 264 |
PEN_BASE_ANGLE = 45 # Base angle of pen (degrees)
|
| 265 |
PEN_MOVEMENT_AMPLITUDE = 10 # How much the pen moves up/down (pixels)
|
| 266 |
# ===================================
|
| 267 |
-
|
| 268 |
# Helper: wrap text by pixel width using cv2.getTextSize
|
| 269 |
def wrap_text_cv(text, font, font_scale, thickness, max_width):
|
| 270 |
wrapped_lines = []
|
|
@@ -312,8 +310,8 @@ def video_func(id, lines, lang):
|
|
| 312 |
if SKIP_SPACES:
|
| 313 |
visible_indices = [i for i, ch in enumerate(full_text) if (ch != ' ' and ch != '\n' and ch != '\t')]
|
| 314 |
else:
|
| 315 |
-
visible_indices =
|
| 316 |
-
|
| 317 |
total_glyphs = len(visible_indices)
|
| 318 |
print(f"Wrapped lines: {len(wrapped_lines)} lines, total glyphs (counted): {total_glyphs}")
|
| 319 |
if total_glyphs == 0:
|
|
@@ -329,7 +327,7 @@ def video_func(id, lines, lang):
|
|
| 329 |
(w, h), baseline = cv2.getTextSize("Ay", FONT, FONT_SCALE, THICKNESS)
|
| 330 |
else:
|
| 331 |
(w, h), baseline = cv2.getTextSize(line, FONT, FONT_SCALE, THICKNESS)
|
| 332 |
-
line_heights.append(h + LINE_SPACING)
|
| 333 |
y_positions = []
|
| 334 |
y = MARGIN_Y
|
| 335 |
for lh in line_heights:
|
|
@@ -344,7 +342,7 @@ def video_func(id, lines, lang):
|
|
| 344 |
f'{silent_video_path}'
|
| 345 |
)
|
| 346 |
print("FFMPEG CMD:", ffmpeg_cmd)
|
| 347 |
-
|
| 348 |
proc = subprocess.Popen(shlex.split(ffmpeg_cmd), stdin=subprocess.PIPE, bufsize=10**8)
|
| 349 |
# Render function, modified: if pen_x <= 0, no pen
|
| 350 |
def render_frame(visible_text, pen_x, pen_y, anim_offset):
|
|
@@ -354,9 +352,9 @@ def video_func(id, lines, lang):
|
|
| 354 |
x = MARGIN_X
|
| 355 |
y = y_positions[idx]
|
| 356 |
(w, h), baseline = cv2.getTextSize(line, FONT, FONT_SCALE, THICKNESS)
|
| 357 |
-
y_draw = y + h
|
| 358 |
if line != "":
|
| 359 |
-
cv2.putText(img, line, (x,
|
| 360 |
if pen_x > 0: # Only draw pen if pen_x > 0
|
| 361 |
offset_y = int(PEN_MOVEMENT_AMPLITUDE * math.sin(anim_offset * math.pi))
|
| 362 |
pen_tip_y = pen_y + offset_y
|
|
@@ -366,7 +364,7 @@ def video_func(id, lines, lang):
|
|
| 366 |
cv2.line(img, (pen_x, pen_tip_y), (pen_end_x, pen_end_y), PEN_COLOR, PEN_THICKNESS)
|
| 367 |
cv2.circle(img, (pen_x, pen_tip_y), PEN_TIP_RADIUS, PEN_COLOR, -1)
|
| 368 |
return img
|
| 369 |
-
|
| 370 |
t0 = time.time()
|
| 371 |
frames_sent = 0
|
| 372 |
prev_visible_sub = ""
|
|
@@ -416,4 +414,6 @@ def video_func(id, lines, lang):
|
|
| 416 |
# display(Video(final_video_path, embed=True, width=WIDTH, height=HEIGHT))
|
| 417 |
# Clean up silent video if not needed
|
| 418 |
os.remove(silent_video_path)
|
| 419 |
-
return final_video_path
|
|
|
|
|
|
|
|
|
| 228 |
return asyncio.run(generate_tts(id, lines, lang))
|
| 229 |
#-----------------------------
|
| 230 |
#---------------------------------
|
| 231 |
+
def video_func(id, lines,lang):
|
| 232 |
+
duration, audio_path = audio_func(id,lines,lang)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 233 |
if not duration or not audio_path:
|
| 234 |
print("Failed to generate audio.")
|
| 235 |
return None
|
| 236 |
+
#listf = lines.split("&&&")
|
| 237 |
+
#TEXT = listf[0].strip()
|
| 238 |
+
TEXT=lines[id]
|
| 239 |
print("-----------------------------------------------------------------------------")
|
| 240 |
print(TEXT)
|
| 241 |
SKIP_SPACES = False
|
| 242 |
+
|
| 243 |
FPS = 30 # Increased for smoother animation
|
| 244 |
ANIMATION_FRAMES_PER_CHAR = 3 # Number of sub-frames for pen movement per character
|
| 245 |
WIDTH, HEIGHT = 1280, 720 # Keep as is
|
|
|
|
| 262 |
PEN_BASE_ANGLE = 45 # Base angle of pen (degrees)
|
| 263 |
PEN_MOVEMENT_AMPLITUDE = 10 # How much the pen moves up/down (pixels)
|
| 264 |
# ===================================
|
| 265 |
+
|
| 266 |
# Helper: wrap text by pixel width using cv2.getTextSize
|
| 267 |
def wrap_text_cv(text, font, font_scale, thickness, max_width):
|
| 268 |
wrapped_lines = []
|
|
|
|
| 310 |
if SKIP_SPACES:
|
| 311 |
visible_indices = [i for i, ch in enumerate(full_text) if (ch != ' ' and ch != '\n' and ch != '\t')]
|
| 312 |
else:
|
| 313 |
+
visible_indices = list(range(len(full_text)))
|
| 314 |
+
|
| 315 |
total_glyphs = len(visible_indices)
|
| 316 |
print(f"Wrapped lines: {len(wrapped_lines)} lines, total glyphs (counted): {total_glyphs}")
|
| 317 |
if total_glyphs == 0:
|
|
|
|
| 327 |
(w, h), baseline = cv2.getTextSize("Ay", FONT, FONT_SCALE, THICKNESS)
|
| 328 |
else:
|
| 329 |
(w, h), baseline = cv2.getTextSize(line, FONT, FONT_SCALE, THICKNESS)
|
| 330 |
+
line_heights.append(h + baseline + LINE_SPACING)
|
| 331 |
y_positions = []
|
| 332 |
y = MARGIN_Y
|
| 333 |
for lh in line_heights:
|
|
|
|
| 342 |
f'{silent_video_path}'
|
| 343 |
)
|
| 344 |
print("FFMPEG CMD:", ffmpeg_cmd)
|
| 345 |
+
|
| 346 |
proc = subprocess.Popen(shlex.split(ffmpeg_cmd), stdin=subprocess.PIPE, bufsize=10**8)
|
| 347 |
# Render function, modified: if pen_x <= 0, no pen
|
| 348 |
def render_frame(visible_text, pen_x, pen_y, anim_offset):
|
|
|
|
| 352 |
x = MARGIN_X
|
| 353 |
y = y_positions[idx]
|
| 354 |
(w, h), baseline = cv2.getTextSize(line, FONT, FONT_SCALE, THICKNESS)
|
| 355 |
+
y_draw = y + h
|
| 356 |
if line != "":
|
| 357 |
+
cv2.putText(img, line, (x, y_draw), FONT, FONT_SCALE, TEXT_COLOR, THICKNESS, lineType=cv2.LINE_AA)
|
| 358 |
if pen_x > 0: # Only draw pen if pen_x > 0
|
| 359 |
offset_y = int(PEN_MOVEMENT_AMPLITUDE * math.sin(anim_offset * math.pi))
|
| 360 |
pen_tip_y = pen_y + offset_y
|
|
|
|
| 364 |
cv2.line(img, (pen_x, pen_tip_y), (pen_end_x, pen_end_y), PEN_COLOR, PEN_THICKNESS)
|
| 365 |
cv2.circle(img, (pen_x, pen_tip_y), PEN_TIP_RADIUS, PEN_COLOR, -1)
|
| 366 |
return img
|
| 367 |
+
|
| 368 |
t0 = time.time()
|
| 369 |
frames_sent = 0
|
| 370 |
prev_visible_sub = ""
|
|
|
|
| 414 |
# display(Video(final_video_path, embed=True, width=WIDTH, height=HEIGHT))
|
| 415 |
# Clean up silent video if not needed
|
| 416 |
os.remove(silent_video_path)
|
| 417 |
+
return final_video_path
|
| 418 |
+
|
| 419 |
+
#video
|