Lenylvt commited on
Commit
b0bd796
·
verified ·
1 Parent(s): 4708105

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -0
app.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import os
3
+
4
+ def text_to_srt(text):
5
+ lines = text.split('\n')
6
+ srt_content = ""
7
+ for i, line in enumerate(lines):
8
+ if line.strip() == "":
9
+ continue
10
+ try:
11
+ times, content = line.split(']', 1)
12
+ start, end = times[1:].split(' -> ')
13
+ # Check if the timestamp includes hours; if not, prepend "00:"
14
+ if start.count(":") == 1:
15
+ start = "00:" + start
16
+ if end.count(":") == 1:
17
+ end = "00:" + end
18
+ # Replace '.' with ',' in timestamps for SRT format
19
+ srt_content += f"{i+1}\n{start.replace('.', ',')} --> {end.replace('.', ',')}\n{content.strip()}\n\n"
20
+ except ValueError:
21
+ continue # Skip lines that don't match the expected format
22
+ # Save SRT content to a temporary file
23
+ temp_file_path = 'output.srt'
24
+ with open(temp_file_path, 'w', encoding='utf-8') as file:
25
+ file.write(srt_content)
26
+ return temp_file_path
27
+
28
+ def save_temp_file(content, filename="output.srt"):
29
+ with open(filename, "w") as f:
30
+ f.write(content)
31
+ return filename
32
+
33
+ # Streamlit app
34
+ st.title("Text to SRT Converter")
35
+ text = st.text_area("Enter text", height=300)
36
+ if st.button("Convert to SRT"):
37
+ if text:
38
+ srt_path = text_to_srt(text)
39
+ with open(srt_path, "r") as file:
40
+ st.download_button(label="Download SRT File", data=file, file_name="output.srt", mime="text/plain")
41
+ else:
42
+ st.warning("Please enter some text.")