diff options
author | Louis Vézina <[email protected]> | 2019-09-13 15:14:31 -0400 |
---|---|---|
committer | Louis Vézina <[email protected]> | 2019-09-13 15:14:31 -0400 |
commit | c5fa0f56e41ee03fd7b02f6474cbb4fd1bdf08e6 (patch) | |
tree | ed41edcfd2c977a989216a69fb5887d70ebb1098 /libs/subliminal | |
parent | 645952c61aba3cccb5ca919be966a7ba02d853fa (diff) | |
download | bazarr-c5fa0f56e41ee03fd7b02f6474cbb4fd1bdf08e6.tar.gz bazarr-c5fa0f56e41ee03fd7b02f6474cbb4fd1bdf08e6.zip |
WIP
Diffstat (limited to 'libs/subliminal')
-rw-r--r-- | libs/subliminal/subtitles/__init__.py | 88 | ||||
-rw-r--r-- | libs/subliminal/subtitles/subrip.py | 82 |
2 files changed, 170 insertions, 0 deletions
diff --git a/libs/subliminal/subtitles/__init__.py b/libs/subliminal/subtitles/__init__.py new file mode 100644 index 000000000..2bddfe0f0 --- /dev/null +++ b/libs/subliminal/subtitles/__init__.py @@ -0,0 +1,88 @@ +# -*- coding: utf-8 -*- +from datetime import time + + +class Component(object): + """Base class for cue text. + + :param list components: sub-components of this one. + + """ + tag_name = 'Component' + + def __init__(self, components=None): + if components is None: + self.components = [] + elif isinstance(components, list): + self.components = components + else: + self.components = [components] + + def __iter__(self): + return iter(self.components) + + def __len__(self): + return len(self.components) + + def __str__(self): + return ''.join(str(c) for c in self.components) + + def __repr__(self): + return '<{name}>{components}</{name}>'.format(name=self.tag_name, + components=''.join(repr(c) for c in self.components)) + + +class Bold(Component): + """Bold :class:`Component`.""" + tag_name = 'b' + + +class Italic(Component): + """Italic :class:`Component`.""" + tag_name = 'i' + + +class Underline(Component): + """Underline :class:`Component`.""" + tag_name = 'u' + + +class Strikethrough(Component): + """Strikethrough :class:`Component`.""" + tag_name = 's' + + +class Font(Component): + """Font :class:`Component`.""" + tag_name = 'font' + + def __init__(self, color, *args, **kwargs): + super(Font, self).__init__(*args, **kwargs) + self.color = color + + def __repr__(self): + return '<{name} "{color}">{components}</{name}>'.format(name=self.tag_name, color=self.color, + components=''.join(repr(c) for c in self.components)) + + +class Cue(object): + """A single subtitle cue with timings and components. + + :param datetime.time start_time: start time. + :param datetime.time end_time: end time. + :param list components: cue components. + + """ + def __init__(self, start_time, end_time, components): + self.start_time = start_time + self.end_time = end_time + self.components = components + + def __repr__(self): + return '<Cue [{start_time}->{end_time}] "{text}">'.format(start_time=self.start_time, end_time=self.end_time, + text=''.join(repr(c) for c in self.components)) + + +if __name__ == '__main__': + cue = Cue(time(), time(1), [Bold('Hello')]) + print repr(cue) diff --git a/libs/subliminal/subtitles/subrip.py b/libs/subliminal/subtitles/subrip.py new file mode 100644 index 000000000..47e5b477a --- /dev/null +++ b/libs/subliminal/subtitles/subrip.py @@ -0,0 +1,82 @@ +# -*- coding: utf-8 -*- +import re +from datetime import time + +from subliminal.subtitles import Cue + +index_re = re.compile(r'(?P<index>\d+)') +timing_re = re.compile(r'(?P<hour>\d{2}):(?P<minute>\d{2}):(?P<second>\d{2}),(?P<milliseconds>\d{3})') + + +class SubripReadError(Exception): + pass + + +class SubripReadIndexError(SubripReadError): + pass + + +class SubripReader(object): + INDEX = 1 + TIMINGS = 2 + TEXT = 3 + + def __init__(self): + self.state = self.INDEX + + def read(self, content): + pass + + def read_line(self, line): + if self.state == self.INDEX: + if index_re.match(line): + raise SubripReadIndexError + + +def read_cue(stream): + """Attempt to parse a complete Cue from the stream""" + # skip blank lines + line = '' + while not line: + line = stream.readline() + + # parse index + if not index_re.match(line): + raise SubripReadIndexError + + # parse timings + line = stream.readline() + if '-->' not in line: + raise SubripReadError + timings = line.split('-->') + if not len(timings): + raise SubripReadError + + # parse start time + match = timing_re.match(timings[0].strip()) + if not match: + raise SubripReadError + start_time = time(**match.groupdict()) + + # parse end time + match = timing_re.match(timings[0].strip()) + if not match: + raise SubripReadError + end_time = time(**match.groupdict()) + + + + +class SubripSubtitle(object): + def __init__(self): + self.cues = [] + + +if __name__ == '__main__': + print read_cue('toto') + i = 0 + for x in read_cue('toto'): + print x + if i > 10: + break + i += 1 |