qqwjq1981 commited on
Commit
e2e15a2
·
verified ·
1 Parent(s): 2d95de4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -9
app.py CHANGED
@@ -367,16 +367,14 @@ def create_subtitle_clip_pil(text, start_time, end_time, video_width, video_heig
367
  try:
368
  subtitle_width = int(video_width * 0.8)
369
  aspect_ratio = video_height / video_width
370
- if aspect_ratio > 1.2: # Portrait video
371
- subtitle_font_size = int(video_width // 22)
372
- else: # Landscape video
373
- subtitle_font_size = int(video_height // 24)
374
 
375
  font = ImageFont.truetype(font_path, subtitle_font_size)
376
 
377
  dummy_img = Image.new("RGBA", (subtitle_width, 1), (0, 0, 0, 0))
378
  draw = ImageDraw.Draw(dummy_img)
379
 
 
380
  lines = []
381
  line = ""
382
  for word in text.split():
@@ -392,21 +390,34 @@ def create_subtitle_clip_pil(text, start_time, end_time, video_width, video_heig
392
 
393
  line_heights = [draw.textbbox((0, 0), l, font=font)[3] - draw.textbbox((0, 0), l, font=font)[1] for l in lines]
394
  total_height = sum(line_heights) + (len(lines) - 1) * 5
 
395
  img = Image.new("RGBA", (subtitle_width, total_height), (0, 0, 0, 0))
396
  draw = ImageDraw.Draw(img)
397
 
 
 
 
 
 
 
 
 
 
 
398
  y = 0
399
  for idx, line in enumerate(lines):
400
  bbox = draw.textbbox((0, 0), line, font=font)
401
  w = bbox[2] - bbox[0]
402
- draw.text(((subtitle_width - w) // 2, y), line, font=font, fill="yellow")
 
403
  y += line_heights[idx] + 5
404
-
405
- img_np = np.array(img) # <- ✅ Fix: convert to NumPy
406
- txt_clip = ImageClip(img_np).set_start(start_time).set_duration(end_time - start_time).set_position("bottom").set_opacity(0.8)
407
  return txt_clip
 
408
  except Exception as e:
409
- logger.error(f"\u274c Failed to create subtitle clip: {e}")
410
  return None
411
 
412
  def solve_optimal_alignment(original_segments, generated_durations, total_duration):
 
367
  try:
368
  subtitle_width = int(video_width * 0.8)
369
  aspect_ratio = video_height / video_width
370
+ subtitle_font_size = int(video_width // 22 if aspect_ratio > 1.2 else video_height // 24)
 
 
 
371
 
372
  font = ImageFont.truetype(font_path, subtitle_font_size)
373
 
374
  dummy_img = Image.new("RGBA", (subtitle_width, 1), (0, 0, 0, 0))
375
  draw = ImageDraw.Draw(dummy_img)
376
 
377
+ # Word wrapping
378
  lines = []
379
  line = ""
380
  for word in text.split():
 
390
 
391
  line_heights = [draw.textbbox((0, 0), l, font=font)[3] - draw.textbbox((0, 0), l, font=font)[1] for l in lines]
392
  total_height = sum(line_heights) + (len(lines) - 1) * 5
393
+
394
  img = Image.new("RGBA", (subtitle_width, total_height), (0, 0, 0, 0))
395
  draw = ImageDraw.Draw(img)
396
 
397
+ def draw_text_with_outline(draw, pos, text, font, fill="white", outline="black", outline_width=2):
398
+ x, y = pos
399
+ # Draw outline
400
+ for dx in range(-outline_width, outline_width + 1):
401
+ for dy in range(-outline_width, outline_width + 1):
402
+ if dx != 0 or dy != 0:
403
+ draw.text((x + dx, y + dy), text, font=font, fill=outline)
404
+ # Draw main text
405
+ draw.text((x, y), text, font=font, fill=fill)
406
+
407
  y = 0
408
  for idx, line in enumerate(lines):
409
  bbox = draw.textbbox((0, 0), line, font=font)
410
  w = bbox[2] - bbox[0]
411
+ x = (subtitle_width - w) // 2
412
+ draw_text_with_outline(draw, (x, y), line, font)
413
  y += line_heights[idx] + 5
414
+
415
+ img_np = np.array(img)
416
+ txt_clip = ImageClip(img_np).set_start(start_time).set_duration(end_time - start_time).set_position(("center", video_height * 0.85)).set_opacity(0.9)
417
  return txt_clip
418
+
419
  except Exception as e:
420
+ logger.error(f" Failed to create subtitle clip: {e}")
421
  return None
422
 
423
  def solve_optimal_alignment(original_segments, generated_durations, total_duration):