How to open *.srt subtitle file?
I need to get the same object as when opening file by pysrt.
Welcome to the community!
Please share a code example or your public github repo.
What have you tried so far? What does not work?
- I read file *.srt
Blockquote
st.title(‘H_E_R_H_I’)
uploaded_file = st.file_uploader(“Выберите файл субтитров *.srt”,
type=['srt'],
label_visibility="visible"
)
a = uploaded_file.read()
a
Blockquote
I get the same text:
b'1\n00:03:19,349 --> 00:03:20,599\nHello.\n\n2\n00:03:21,559 --> 00:03:24,228\nMy name\'s Forrest. Forrest Gump.\n\n3\n00:03:29,776 --> 00:03:31,819\nDo you want a chocolate?\n\n4\n00:03:34,405 --> 00:03:37,825\nI could eat about\na million and a half of these.\n\n5\n00:03:38,993 --> 00:03:44,206\nMy mama always said\nlife was like a box of chocolates.\n\n6\n00:03:46,209 -->
Here all, text of sublitles, time subtitles. But when I read this file *.srt with Pysrt library, I get list all sentenses and list all time sentenses’s
Blockquote
patch = ‘./English_level/Forrest_Gump(1994).srt’
sub = pysrt.open(patch, encoding=‘iso-8859-1’)
print(sub[10].text) # Print 10th sentense
print(sub[10].start.to_time()) # Print 10th start time of sentense
print(sub[10].end.to_time()) # Print 10th end time of sentense
Blockquote
I need to read srt file as pysrt object, because in my programm I use same function end processing.
Thanks
(sorry, I bad know English)
Then use pysrt
to read it. What is the issue?
This is a very simple working example:
import pysrt
import streamlit as st
st.title('pysrt Test for Streamlit')
uploaded_file = st.file_uploader("Choose a .srt file", type="srt")
if uploaded_file is not None:
sub = pysrt.open(uploaded_file.name, encoding='utf-8')
texts = sub.text
st.write(texts)
Franky1 thank, but it’s not works.
PyCharm gives it:
File "/home/dron/PycharmProjects/pythonProject2/main.py", line 9, in <module>
sub = pysrt.open(uploaded_file.name(), encoding='iso-8859-1')
TypeError: 'str' object is not callable
uploaded_file.name
not uploaded_file.name()
No, so that doesn’t work
2023-02-21 15:40:14.820 Uncaught app exception
Traceback (most recent call last):
File "/usr/local/lib/python3.9/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 565, in _run_script
exec(code, module.__dict__)
File "/home/dron/PycharmProjects/pythonProject2/main.py", line 9, in <module>
sub = pysrt.open(uploaded_file.name, encoding='iso-8859-1')
File "/usr/local/lib/python3.9/site-packages/pysrt/srtfile.py", line 151, in open
source_file, encoding = cls._open_unicode_file(path, claimed_encoding=encoding)
File "/usr/local/lib/python3.9/site-packages/pysrt/srtfile.py", line 293, in _open_unicode_file
source_file = codecs.open(path, 'r', encoding=encoding)
File "/usr/local/lib/python3.9/codecs.py", line 905, in open
file = builtins.open(filename, mode, buffering)
FileNotFoundError: [Errno 2] No such file or directory: 'An_American_tail(1986).srt'
one srt file
Zum Beispiel. This file open for you?
This below should work.
from tempfile import NamedTemporaryFile
import pysrt
import streamlit as st
st.title('pysrt Test for Streamlit')
uploaded_file = st.file_uploader("Choose a .srt file", type="srt")
if uploaded_file is not None:
with NamedTemporaryFile(suffix='.srt', delete=False) as tempfile:
st.info(tempfile.name)
tempfile.write(uploaded_file.getbuffer())
sub = pysrt.open(tempfile.name, encoding='utf-8')
texts = sub.text
st.markdown(texts, unsafe_allow_html=True)
It’s difficult for me, but this code really works !
Franky1 Thank you for your hard work!
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.