File size: 1,385 Bytes
335cfd2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
220874c
 
 
 
 
 
0e3fe63
335cfd2
220874c
 
 
 
 
 
 
 
 
 
 
 
9b27f70
220874c
9b27f70
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
import gradio as gr

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(' -> ')
            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
    return srt_content

def text_to_srt_file(text):
    srt_content = text_to_srt(text)
    file_path = "output.srt"
    with open(file_path, "w") as file:
        file.write(srt_content)
    return file_path

with gr.Blocks() as app:
    gr.Markdown("### Text to SRT Converter")
    text_input = gr.TextArea(label="Enter text in the specified format")
    output = gr.TextArea(label="SRT Output", visible=False)  # Hide the text area output
    download_btn = gr.File(label="Download SRT", visible=False)  # Add download button
    
    def update_output(text):
        srt_content = text_to_srt(text)
        output.update(value=srt_content, visible=True)
        srt_file_path = text_to_srt_file(text)
        download_btn.update(value=srt_file_path, visible=True)
    
    text_input.change(fn=update_output, inputs=text_input, outputs=[output, download_btn])

app.launch()