cdown/srt

api call equivalent to command

wanghaisheng opened this issue · 1 comments

I can find this promising command from doc

srt lines-matching -m hanzidentifier -f hanzidentifier.has_chinese

after a little dig, still could not find how to write the corresponding code
@cdown

cdown commented

srt-lines-matching is open source (in srt_tools), so for any tool you can consult there to work out how to replicate it in the API.

That said:

In [1]: import srt, hanzidentifier                                                                                                                            

In [2]: with open("e.srt") as f: 
   ...:     s = f.read() 
   ...:                                                                                                                                                       

In [3]: print(s)                                                                                                                                              
1
00:00:00,000 --> 00:00:00,001
Latin script
汉子



In [4]: sub = next(srt.parse(s))                                                                                                                              

In [5]: sub                                                                                                                                                   
Out[5]: Subtitle(index=1, start=datetime.timedelta(0), end=datetime.timedelta(microseconds=1000), content='Latin script\n汉子', proprietary='')

In [6]: sub.content = '\n'.join(line for line in sub.content.splitlines() if hanzidentifier.has_chinese(line))                                                

In [7]: sub                                                                                                                                                   
Out[7]: Subtitle(index=1, start=datetime.timedelta(0), end=datetime.timedelta(microseconds=1000), content='汉子', proprietary='')