diff --git a/app/main.py b/app/main.py index 89dda52..1c87360 100644 --- a/app/main.py +++ b/app/main.py @@ -224,13 +224,43 @@ def generate_srt_from_segments(segments, clip_start, clip_end, output_path): s_start = float(seg["start"]) s_end = float(seg["end"]) text = str(seg["text"]).strip() + words = seg.get("words", []) or [] # Filter v range if s_end <= clip_start or s_start >= clip_end: continue - # Klipni - s_start = max(s_start, clip_start) - s_end = min(s_end, clip_end) + + # Če segment delno štrli iz clip range-a IN imamo word-level timestampe, + # uporabi samo tiste besede ki dejansko padejo v clip range + # (sicer subtitle vsebuje besedilo iz prejšnjega/naslednjega refrena/verza) + if words and (s_start < clip_start or s_end > clip_end): + words_in_clip = [] + for w in words: + w_start = float(w.get("start", 0)) + w_end = float(w.get("end", 0)) + w_text = w.get("text", "").strip() + if not w_text: + continue + # Beseda padeva v clip če se prekriva (ne mora biti popolnoma znotraj) + if w_end > clip_start and w_start < clip_end: + words_in_clip.append({ + "start": max(w_start, clip_start), + "end": min(w_end, clip_end), + "text": w_text, + }) + + if not words_in_clip: + continue + + # Reconstruiraj segment z dejanskim word-level timing-om + text = " ".join(w["text"] for w in words_in_clip) + s_start = words_in_clip[0]["start"] + s_end = words_in_clip[-1]["end"] + else: + # Klipni segment začetek/konec na clip range + s_start = max(s_start, clip_start) + s_end = min(s_end, clip_end) + if s_end - s_start < 0.2: continue