habulaj commited on
Commit
4febcad
·
verified ·
1 Parent(s): 4760ff9

Update srt_utils.py

Browse files
Files changed (1) hide show
  1. srt_utils.py +23 -8
srt_utils.py CHANGED
@@ -38,11 +38,14 @@ def format_text_lines(text, max_chars=42):
38
  if not words:
39
  return ""
40
 
41
- # If fits in one line
42
- if len(text) <= max_chars:
 
 
 
43
  return text
44
 
45
- # Needs splitting
46
  # Simple split strategy: find middle space
47
  best_split_idx = -1
48
  best_balance = float('inf')
@@ -57,24 +60,36 @@ def format_text_lines(text, max_chars=42):
57
  len2 = len(line2)
58
 
59
  # Valid split? Only if both fit max_chars
60
- # (Or if single line is impossible, pick best fit)
61
  if len1 <= max_chars and len2 <= max_chars:
62
  balance = abs(len2 - len1)
63
- # Bonus for bottom heavy (line2 >= line1)
64
  if len2 >= len1:
65
  balance -= 5
66
 
67
  if balance < best_balance:
68
  best_balance = balance
69
  best_split_idx = i
70
-
 
71
  if best_split_idx != -1:
 
 
 
 
 
 
 
 
 
72
  line1 = " ".join(words[:best_split_idx])
73
  line2 = " ".join(words[best_split_idx:])
74
  return f"{line1}\n{line2}"
75
 
76
- # Fallback: if no valid split found (e.g. one word is super long or total > 84)
77
- # Just try to split in half by words regardless of limit (player will wrap or clip)
 
 
 
78
  mid = len(words) // 2
79
  return " ".join(words[:mid]) + "\n" + " ".join(words[mid:])
80
 
 
38
  if not words:
39
  return ""
40
 
41
+ # If fits in one line, but we might WANT to split if it's long (> 30 chars) for better reading (pyramid shape)
42
+ # The user complained about 42 chars being too long for one line.
43
+ FORCE_SPLIT_THRESHOLD = 30
44
+
45
+ if len(text) <= max_chars and len(text) <= FORCE_SPLIT_THRESHOLD:
46
  return text
47
 
48
+ # Needs splitting (or we want to try splitting)
49
  # Simple split strategy: find middle space
50
  best_split_idx = -1
51
  best_balance = float('inf')
 
60
  len2 = len(line2)
61
 
62
  # Valid split? Only if both fit max_chars
 
63
  if len1 <= max_chars and len2 <= max_chars:
64
  balance = abs(len2 - len1)
65
+ # Bonus for bottom heavy (line2 >= line1) which looks better often (pyramid)
66
  if len2 >= len1:
67
  balance -= 5
68
 
69
  if balance < best_balance:
70
  best_balance = balance
71
  best_split_idx = i
72
+
73
+ # If we found a valid split
74
  if best_split_idx != -1:
75
+ # If the original text fit in one line (< max_chars), only use the split if it's reasonably balanced.
76
+ # If the split results in a tiny orphan like "I\nam going", stick to 1 line if possible.
77
+ if len(text) <= max_chars:
78
+ line1 = " ".join(words[:best_split_idx])
79
+ line2 = " ".join(words[best_split_idx:])
80
+ # If one line is very short relative to the other, maybe don't split?
81
+ # But user wants "Netflix style", usually balanced.
82
+ pass
83
+
84
  line1 = " ".join(words[:best_split_idx])
85
  line2 = " ".join(words[best_split_idx:])
86
  return f"{line1}\n{line2}"
87
 
88
+ # Fallback: if no valid split found (e.g. words too long), but whole text fits in max_chars
89
+ if len(text) <= max_chars:
90
+ return text
91
+
92
+ # Fallback 2: Really long text, just split in middle
93
  mid = len(words) // 2
94
  return " ".join(words[:mid]) + "\n" + " ".join(words[mid:])
95