srt / app.py
Lenylvt's picture
Create app.py
0699d0f verified
import streamlit as st
import os
def text_to_srt(text):
lines = text.split('\n')
srt_content = ""
for i, line in enumerate(lines):
if line.strip() == "":
continue
try:
times, content = line.split(']', 1)
start, end = times[1:].split(' -> ')
# Vérifier si l'horodatage inclut les heures; sinon, préfixer par "00:"
if start.count(":") == 1:
start = "00:" + start
if end.count(":") == 1:
end = "00:" + end
# Remplacer '.' par ',' dans les horodatages pour le format SRT
srt_content += f"{i+1}\n{start.replace('.', ',')} --> {end.replace('.', ',')}\n{content.strip()}\n\n"
except ValueError:
continue # Ignorer les lignes qui ne correspondent pas au format attendu
# Enregistrer le contenu SRT dans un fichier temporaire
temp_file_path = 'output.srt'
with open(temp_file_path, 'w', encoding='utf-8') as file:
file.write(srt_content)
return temp_file_path
def save_temp_file(content, filename="output.srt"):
with open(filename, "w") as f:
f.write(content)
return filename
# Application Streamlit
st.title("Convertisseur de Texte en SRT")
text = st.text_area("🗯️ Entrez le texte de [Whisper](https://huggingface.co/spaces/sanchit-gandhi/whisper-jax)", height=300)
if st.button("📁 Convertir en SRT"):
if text:
srt_path = text_to_srt(text)
with open(srt_path, "r") as file:
st.download_button(label="⬇️ Télécharger le fichier SRT", data=file, file_name="output.srt", mime="text/plain")
else:
st.warning("🔴 Veuillez entrer du texte.")