Use pysrt to provide better content search of srt files.

This commit is contained in:
voussoir 2023-06-24 12:03:55 -07:00
parent 0fe9364d8d
commit fa2610e206

View file

@ -9,6 +9,10 @@ try:
import winshell
except ImportError:
winshell = None
try:
import pysrt
except ImportError:
pysrt = None
from voussoirkit import expressionmatch
from voussoirkit import pathclass
@ -90,6 +94,29 @@ def search_contents_generic(filepath, content_args):
yield from results
yield ''
def _srt_format_line(line):
text = line.text.replace('\n', ' ')
timestamp = f'{line.start.hours:02d}:{line.start.minutes:02d}:{line.start.seconds:02d}:{line.start.milliseconds:03d}'
return f'{timestamp} {text}'
def search_contents_srt(filepath, content_args):
try:
srtlines = pysrt.open(filepath.absolute_path)
except UnicodeDecodeError:
log.warn('%s experienced Unicode Error', filepath.absolute_path)
return
content_args['text'] = '\n'.join(_srt_format_line(line) for line in srtlines)
results = search(**content_args)
results = list(results)
if not results:
return
yield filepath.absolute_path
yield from results
yield ''
def search_contents_windows_lnk(filepath, content_args):
try:
shortcut = winshell.Shortcut(filepath.absolute_path)
@ -227,6 +254,8 @@ def search(
if filepath.extension == 'lnk' and winshell:
yield from search_contents_windows_lnk(filepath, content_args)
if filepath.extension == 'srt' and pysrt:
yield from search_contents_srt(filepath, content_args)
else:
yield from search_contents_generic(filepath, content_args)