Is it possible to add the function of playing subtitle in the terminal?
Closed this issue · 13 comments
that is, the function of subtitle player.
I'm not sure what you're asking. What would "playing" mean here?
@cdown printing the content of the subtitle in the terminal (or somewhere) at the corresponding time of the subtitle.
I think the new srt play
utility I just added should do basically what you want: 1de5f02
Could you kindly give an example how to use this utility with an existing .srt
file? That is, an example like
import srt
test = open("test.srt")
srt.play(test)
After installing using setup.py
or pip
, srt play -i test.srt
should work. If the srt file is not in your default system encoding, you may need to use the -e
flag to set the encoding, by doing something like srt play -e gb2312 -i test.srt
, where gb2312
is the source encoding.
srt play -i test.srt
gives an error that
... srt_tools/utils.py", line 119, in set_basic_args with open(stream, encoding=args.encoding) as input_f:
TypeError: 'encoding' is an invalid keyword argument for this function
Hm, it works in Python 3, but not in Python 2. When I fix issue #28, that shouldn't happen any more :-)
The reason is because we use the new open()
API in utils which is not compatible with Python 2. Let me go fix that.
You can try with the latest changes on develop
. It works for me with Python 2:
% srt play -i ~/Downloads/test.srt -e gb2312
他能徒手抓住子弹 拦住疾速的火车
轻轻一跳就能飞跃高楼
-在天上 -是鸟 还是飞机
白天只是一名邮差 晚上却化身超级英雄
保卫纽约市民免受邪恶滋扰
Here is my test file (I have renamed it to test.txt to upload, the original name is test.srt)
test.txt
The error says that
srt.py", line 325, in _raise_if_not_contiguous
raise SRTParseError(expected_start, actual_start, unmatched_content)
srt.SRTParseError: Expected contiguous start of match or end of input at char 0, but started at char 1 (unmatched content: u'\ufeff')
Your file is encoded as utf-8-sig
, not utf-8
, so you need to explicitly specify this:
% srt play -i /tmp/test.txt -e utf-8-sig
(Graznidos)
(Relinchos)
Thanks! It works! One more question, how to find the encoding of the file?
And I think it will be not difficult to add the beginning time as args
to the function play
. That is,
srt play -i [args:beginning time] /tmp/test.txt -e utf-8-sig
will print from the first content of the subtitle whose time is after [args:beginning time]
.
I try to adhere to the Unix philosophy when designing my tools -- ie. one tool should do one thing, and one thing only. To that effect, you want to use fixed-timeshift
like so (you need to use an intermediate file because stdin and stdout cannot have their encoding changed in a reasonable way, although it's possible to soak to an intermediate file transparently, see #33):
srt fixed-timeshift -i /tmp/test.txt --seconds -60 -e utf-8-sig -o /tmp/test2.txt
srt play -i /tmp/test2.txt -e utf-8-sig