youtube_ai_studio / utils.py
PLXR's picture
Create utils.py
8f86c8e verified
raw
history blame contribute delete
877 Bytes
import re
import math
def group_sentences(text, target_sec=10):
raw_sentences = re.split(r'(?<=[.?!])\s+', text.strip())
scenes = []
curr_text, curr_time = "", 0
for s in raw_sentences:
if not s: continue
s_time = max(5, math.ceil(len(s)/35)*5)
if curr_time + s_time > target_sec and curr_text:
scenes.append({"text": curr_text, "time": curr_time})
curr_text, curr_time = s, s_time
else:
curr_text = (curr_text + " " + s).strip()
curr_time += s_time
if curr_text: scenes.append({"text": curr_text, "time": curr_time})
return scenes
def get_smart_filename(index, text):
clean_text = re.sub(r'[\\/*?:"<>|]', "", text).strip()
name_part = clean_text[:12] if len(clean_text) <= 12 else f"{clean_text[:6]}~{clean_text[-4:]}"
return f"{index+1:02d}_{name_part}.png"