Spaces:
Sleeping
Sleeping
Update src/video_utils.py
Browse files- src/video_utils.py +22 -5
src/video_utils.py
CHANGED
|
@@ -48,24 +48,41 @@ def prepare_video_presentateur(video_path, audio_duration, position, plein_ecran
|
|
| 48 |
print(f"[Video] Traceback: {traceback.format_exc()}")
|
| 49 |
return None
|
| 50 |
|
| 51 |
-
def write_srt(text, duration):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
parts = re.split(r'(?<=[\.!?])\s+', text.strip())
|
| 53 |
parts = [p for p in parts if p]
|
| 54 |
total = len("".join(parts)) or 1
|
| 55 |
cur = 0.0
|
| 56 |
srt = []
|
| 57 |
for i, p in enumerate(parts, 1):
|
| 58 |
-
prop = len(p)/total
|
| 59 |
start = cur
|
| 60 |
-
end = min(duration, cur + duration*prop)
|
| 61 |
cur = end
|
|
|
|
| 62 |
def ts(t):
|
| 63 |
m, s = divmod(t, 60)
|
| 64 |
h, m = divmod(m, 60)
|
| 65 |
return f"{int(h):02}:{int(m):02}:{int(s):02},000"
|
|
|
|
| 66 |
srt += [f"{i}", f"{ts(start)} --> {ts(end)}", p, ""]
|
| 67 |
-
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
return path
|
| 70 |
|
| 71 |
def write_video_with_fallback(final_clip, out_path_base, fps=25):
|
|
|
|
| 48 |
print(f"[Video] Traceback: {traceback.format_exc()}")
|
| 49 |
return None
|
| 50 |
|
| 51 |
+
def write_srt(text, duration, base_name=None):
|
| 52 |
+
"""
|
| 53 |
+
Crée un fichier SRT synchronisé avec la durée audio.
|
| 54 |
+
Si base_name est fourni, le fichier SRT porte ce nom fixe.
|
| 55 |
+
"""
|
| 56 |
parts = re.split(r'(?<=[\.!?])\s+', text.strip())
|
| 57 |
parts = [p for p in parts if p]
|
| 58 |
total = len("".join(parts)) or 1
|
| 59 |
cur = 0.0
|
| 60 |
srt = []
|
| 61 |
for i, p in enumerate(parts, 1):
|
| 62 |
+
prop = len(p) / total
|
| 63 |
start = cur
|
| 64 |
+
end = min(duration, cur + duration * prop)
|
| 65 |
cur = end
|
| 66 |
+
|
| 67 |
def ts(t):
|
| 68 |
m, s = divmod(t, 60)
|
| 69 |
h, m = divmod(m, 60)
|
| 70 |
return f"{int(h):02}:{int(m):02}:{int(s):02},000"
|
| 71 |
+
|
| 72 |
srt += [f"{i}", f"{ts(start)} --> {ts(end)}", p, ""]
|
| 73 |
+
|
| 74 |
+
# Nom du fichier SRT
|
| 75 |
+
if base_name:
|
| 76 |
+
safe_base = re.sub(r'[^\w\-]+', '_', base_name)[:40]
|
| 77 |
+
path = os.path.join(OUT_DIR, f"{safe_base}.srt")
|
| 78 |
+
else:
|
| 79 |
+
path = os.path.join(OUT_DIR, f"srt_default.srt")
|
| 80 |
+
|
| 81 |
+
# Écriture du fichier
|
| 82 |
+
with open(path, "w", encoding="utf-8") as f:
|
| 83 |
+
f.write("\n".join(srt))
|
| 84 |
+
|
| 85 |
+
print(f"[SRT] ✅ Fichier SRT généré : {path}")
|
| 86 |
return path
|
| 87 |
|
| 88 |
def write_video_with_fallback(final_clip, out_path_base, fps=25):
|