File size: 1,725 Bytes
b0bd796
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54e6ff7
1802deb
54e6ff7
1802deb
b0bd796
 
 
1802deb
b0bd796
1802deb
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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(' -> ')
            # Check if the timestamp includes hours; if not, prepend "00:"
            if start.count(":") == 1:
                start = "00:" + start
            if end.count(":") == 1:
                end = "00:" + end
            # Replace '.' with ',' in timestamps for SRT format
            srt_content += f"{i+1}\n{start.replace('.', ',')} --> {end.replace('.', ',')}\n{content.strip()}\n\n"
        except ValueError:
            continue  # Skip lines that don't match the expected format
    # Save SRT content to a temporary file
    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

# Streamlit app
st.title("Text to SRT Converter")
st.markdown("For API use, please visit [this space](https://huggingface.co/spaces/Lenylvt/Whisper).")

text = st.text_area("🗯️ Enter text from [Whisper](https://huggingface.co/spaces/sanchit-gandhi/whisper-jax)", height=300)
if st.button("📁 Convert to SRT"):
    if text:
        srt_path = text_to_srt(text)
        with open(srt_path, "r") as file:
            st.download_button(label="⬇️ Download SRT File", data=file, file_name="output.srt", mime="text/plain")
    else:
        st.warning("🔴 Please enter some text.")