File size: 1,358 Bytes
335cfd2
b9c3b7b
335cfd2
 
 
 
 
 
 
 
 
caf4087
335cfd2
caf4087
 
 
 
 
335cfd2
caf4087
 
b9c3b7b
 
caf4087
b9c3b7b
 
335cfd2
 
caf4087
 
b9c3b7b
591cf68
9b27f70
e0e1808
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
import gradio as gr
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)
            # Splitting times by ' -> ' to get start and end times
            start, end = times[1:].split(' -> ')
            # Replacing '.' with ',' for milliseconds and ensuring the time format is correct
            start_formatted = start.replace('.', ',')
            end_formatted = end.replace('.', ',')
            # Adding the subtitle entry to the SRT content
            srt_content += f"{i+1}\n{start_formatted} --> {end_formatted}\n{content.strip()}\n\n"
        except ValueError:
            # Skip lines that don't match the expected format
            continue
    # Save SRT content to a temporary file
    temp_file_path = '/tmp/output.srt'
    with open(temp_file_path, 'w', encoding='utf-8') as file:
        file.write(srt_content)
    return temp_file_path

with gr.Blocks() as app:
    gr.Markdown("# Text to SRT Converter")
    text_input = gr.TextArea(label="Enter text")
    output = gr.File(label="Download SRT File", file_count="single")  # Change output to a File component
    text_input.change(fn=text_to_srt, inputs=text_input, outputs=output)

app.launch(share=True)