Check srt file content validity
Closed this issue · 2 comments
Hello,
I would like to use your library to check an srt file content validity. Is there a way like parsing file content and check if an exception is raised ?
Thanks
Hey!
We raise SRTParseError
when it wasn't possible to parse an SRT, so this should work:
import srt
try:
with open('somefile') as f:
list(srt.parse(f.read()))
except srt.SRTParseError:
# invalid srt
However, bear in mind that since there is no real formal standard for SRT, there are a lot of subtly different SRT file formats, and some players support some format deviations but not others. We try and tolerate most "reasonable" deviations, but when we output an SRT file, we're careful to make it as compliant as possible. Therefore, if you want some sort of strict mode, this might work:
import srt
try:
with open('somefile') as f:
raw = f.read()
subs = list(srt.parse())
except srt.SRTParseError:
# invalid srt
if raw != srt.compose(subs):
# parsable, but not strictly compliant
Would this work for you? What kind of things are you thinking of checking when you think about "validity"? :-)
10 days without response, so closing for now. Feel free to comment again if you have a reply to the above.