Skip to content

Commit

Permalink
Added support for WebVTT cue settings
Browse files Browse the repository at this point in the history
We don't use them for anything, but we should still parse the
subtitles.

Added a test for this and also for voice spans.
  • Loading branch information
Ben Dean-Kawamura committed Sep 30, 2015
1 parent 8b54135 commit 5340643
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 0 deletions.
1 change: 1 addition & 0 deletions babelsubs/parsers/webvtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def __init__(self, input_string, language_code, eager_parse=True):
pattern = r'(?P<s_hour>\d{2}):(?P<s_min>\d{2}):(?P<s_sec>\d{2})(.(?P<s_secfr>\d*))?'
pattern += r' --> '
pattern += r'(?P<e_hour>\d{2}):(?P<e_min>\d{2}):(?P<e_sec>\d{2})(.(?P<e_secfr>\d*))?'
pattern += r'([ \t]+(?P<cue_settings>[^\r\n]+))?'
pattern += r'\n(\n|(?P<text>.+?)\n\n)'
input_string = input_string.replace('\r\n', '\n').replace('\r', '\n')+'\n\n'
super(WEBVTTParser, self).__init__(input_string, pattern, language=language_code,
Expand Down
21 changes: 21 additions & 0 deletions babelsubs/tests/data/cue-settings.vtt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
WEBVTT
Region: id=fred width=40% lines=3 regionanchor=0%,100% viewportanchor=10%,90% scroll=up
Region: id=bill width=40% lines=3 regionanchor=100%,100% viewportanchor=90%,90% scroll=up
00:00:00.000 --> 00:00:20.000 region:fred align:left
Hi, my name is Fred

00:00:02.500 --> 00:00:22.500 region:bill align:right
Hi, I'm Bill

00:00:05.000 --> 00:00:25.000 region:fred align:left
Would you like to get a coffee?

00:00:07.500 --> 00:00:27.500 region:bill align:right
Sure! I've only had one today.

00:00:10.000 --> 00:00:30.000 region:fred align:left
This is my fourth!

00:00:12.500 --> 00:00:32.500 region:fred align:left
OK, let's go.
21 changes: 21 additions & 0 deletions babelsubs/tests/data/voice-span.vtt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
WEBVTT
Region: id=fred width=40% lines=3 regionanchor=0%,100% viewportanchor=10%,90% scroll=up
Region: id=bill width=40% lines=3 regionanchor=100%,100% viewportanchor=90%,90% scroll=up
00:00:00.000 --> 00:00:20.000
<v Fred>Hi, my name is Fred

00:00:02.500 --> 00:00:22.500
<v Bill>Hi, I'm Bill

00:00:05.000 --> 00:00:25.000
<v Fred>Would you like to get a coffee?

00:00:07.500 --> 00:00:27.500
<v Bill>Sure! I've only had one today.

00:00:10.000 --> 00:00:30.000
<v Fred>This is my fourth!

00:00:12.500 --> 00:00:32.500
<v Fred>OK, let's go.
11 changes: 11 additions & 0 deletions babelsubs/tests/test_webvtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,17 @@ def test_mixed_newlines(self):
self.assertIn('<p begin="99:59:59.000" end="99:59:59.000">I\'m gutted. <br/>Absolutely gutted.</p>',
parsed.to_xml())

def test_cue_settings(self):
subs = utils.get_subs("cue-settings.vtt")
self.assertEquals(len(subs), 6)
self.assertEquals(subs.to_internal().subtitle_items()[0].text,
'Hi, my name is Fred')

def test_voice_span(self):
subs = utils.get_subs("voice-span.vtt")
self.assertEquals(len(subs), 6)
self.assertEquals(subs.to_internal().subtitle_items()[0].text,
'Hi, my name is Fred')

class WEBVTTGeneratorTest(TestCase):

Expand Down

0 comments on commit 5340643

Please sign in to comment.