File size: 1,899 Bytes
335cfd2
b9c3b7b
335cfd2
 
 
 
 
 
 
 
 
 
c63eda6
 
 
 
 
 
 
335cfd2
c63eda6
b9c3b7b
 
caf4087
b9c3b7b
 
335cfd2
 
792c52c
 
 
 
 
 
 
 
 
 
 
 
 
 
caf4087
c63eda6
591cf68
9b27f70
c63eda6
 
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
47
48
49
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)
            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 = '/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
    
    Convert your timestamped text into SRT format easily. Please format your input text as follows (this is format for [Whisper JAX](https://huggingface.co/spaces/sanchit-gandhi/whisper-jax)):
    
    ```
    [00:00:00 -> 00:00:05] Line of dialogue 1
    [00:00:06 -> 00:00:10] Line of dialogue 2
    ```
    
    Each line should include a start and end time, followed by the dialogue, separated by ' -> '. Times should be in HH:MM:SS format, or MM:SS if under an hour.
    
    For web use, please visit [this space](https://huggingface.co/spaces/Lenylvt/Text_to_SRT).
    """)
    text_input = gr.TextArea(label="Enter text")
    output = gr.File(label="Download SRT File", file_count="single")
    text_input.change(fn=text_to_srt, inputs=text_input, outputs=output)

if __name__ == "__main__":
    app.launch()