Skip to content

Commit

Permalink
Merge pull request #314 from pbs/OCTO-10864-the-last-PAC-command-sent
Browse files Browse the repository at this point in the history
ignore first pac command if there are two consecutive pac commands
  • Loading branch information
OlteanuRares authored Feb 20, 2024
2 parents 50b9aa1 + 1b8f6be commit ed81ae9
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 33 deletions.
4 changes: 2 additions & 2 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@
# built documents.
#
# The short X.Y version.
version = '2.2.4'
version = '2.2.5'
# The full version, including alpha/beta/rc tags.
release = '2.2.4'
release = '2.2.5'

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
Expand Down
52 changes: 33 additions & 19 deletions pycaption/scc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,21 +301,27 @@ def _translate_line(self, line):
self.time_translator.start_at(parts[0][0])

# loop through each word
for word in parts[0][2].split(' '):
# ignore empty results or invalid commands
word = word.strip()
if len(word) == 4:
self._translate_word(word)

def _translate_word(self, word):
if self._handle_double_command(word):
words = [word.strip() for word in parts[0][2].split(' ') if len(word) == 4]

for idx, word in enumerate(words):
self._translate_word(word, words, idx)

@staticmethod
def get_command(commands, idx):
try:
return commands[idx]
except IndexError:
return None

def _translate_word(self, word, words, idx):
if self._skip_double_command(word, words, idx):
# count frames for timing
self.time_translator.increment_frames()
return
# first check if word is a command
# TODO - check that all the positioning commands are here, or use
# some other strategy to determine if the word is a command.
if word in COMMANDS or _is_pac_command(word):
if word in COMMANDS or _is_pac_command(word) or word in PAC_TAB_OFFSET_COMMANDS:
self._translate_command(word)

# second, check if word is a special character
Expand All @@ -332,31 +338,39 @@ def _translate_word(self, word):
# count frames for timing only after processing a command
self.time_translator.increment_frames()

def _handle_double_command(self, word):
def _skip_double_command(self, word, words, idx):
# If the caption is to be broadcast, each of the commands are doubled
# up for redundancy in case the signal is garbled in transmission.
# The decoder is programmed to ignore a second command when it is the
# same as the first.
# Also like codes, Special Characters are always doubled up,
# with only one member of each pair being displayed.
next_command = self.get_command(words, idx + 1)
second_next = self.get_command(words, idx + 2)

if word in COMMANDS or _is_pac_command(word) or word in SPECIAL_CHARS or word in EXTENDED_CHARS:
if word == self.last_command:
# skip duplicates, execute the last occurrence if not a positioning command
if word == self.last_command and not _is_pac_command(word):
self.last_command = ''
return True
# Fix for the <position> <tab offset> <position> <tab offset>
# repetition
elif _is_pac_command(word) and word in self.last_command:
# skip consecutive positioning commands, execute the last one
elif _is_pac_command(word) and _is_pac_command(next_command):
self.last_command = ''
return True
# Fix for the <position> <tab offset> <position> <tab offset> repetition
# execute the last positioning command
elif _is_pac_command(word) and next_command in PAC_TAB_OFFSET_COMMANDS and _is_pac_command(second_next):
self.last_command = ''
return True
# execute offset commands only if previous command is PAC and next is not PAC
elif word in PAC_TAB_OFFSET_COMMANDS:
if _is_pac_command(self.last_command):
self.last_command += f" {word}"
if _is_pac_command(self.last_command) and not _is_pac_command(next_command):
self.last_command = word
return False
else:
return True

self.last_command = word
return False
self.last_command = word
return False

def _translate_special_char(self, word):
self.buffer.add_chars(SPECIAL_CHARS[word])
Expand Down
2 changes: 2 additions & 0 deletions pycaption/scc/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -987,6 +987,8 @@ def _restructure_bytes_to_position_map(byte_to_pos_map):

HEADER = 'Scenarist_SCC V1.0'

UNHANDLED_COMMANDS = ["9120", "91ae", "912f", "91a1"]

# taken from
# http://www.theneitherworld.com/mcpoodle/SCC_TOOLS/DOCS/CC_CHARS.HTML
INCONVERTIBLE_TO_ASCII_EXTENDED_CHARS_ASSOCIATION = {
Expand Down
8 changes: 4 additions & 4 deletions pycaption/scc/specialized_collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
)
from .constants import (
PAC_BYTES_TO_POSITIONING_MAP, COMMANDS, PAC_TAB_OFFSET_COMMANDS,
MICROSECONDS_PER_CODEWORD, INCONVERTIBLE_TO_ASCII_EXTENDED_CHARS_ASSOCIATION
MICROSECONDS_PER_CODEWORD, UNHANDLED_COMMANDS,
INCONVERTIBLE_TO_ASCII_EXTENDED_CHARS_ASSOCIATION
)

PopOnCue = collections.namedtuple("PopOnCue", "buffer, start, end")
Expand Down Expand Up @@ -342,10 +343,9 @@ def interpret_command(self, command):
:type command: str
"""
self._update_positioning(command)

if command not in UNHANDLED_COMMANDS:
self._update_positioning(command)
text = COMMANDS.get(command, '')

if 'italic' in text:
if 'end' not in text:
self._collection.append(
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

setup(
name='pycaption',
version='2.2.4',
version='2.2.5.dev',
description='Closed caption converter',
long_description=open(README_PATH).read(),
author='Joe Norton',
Expand Down
2 changes: 2 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@
sample_scc_duplicate_tab_offset, sample_scc_duplicate_special_characters,
sample_scc_tab_offset, sample_scc_with_unknown_commands,
sample_scc_special_and_extended_characters,
sample_scc_with_consecutive_pac_commands,
sample_scc_special_and_extended_characters,
sample_scc_with_line_too_long
)
from tests.fixtures.srt import ( # noqa: F401
Expand Down
4 changes: 2 additions & 2 deletions tests/fixtures/dfxp.py
Original file line number Diff line number Diff line change
Expand Up @@ -920,10 +920,10 @@ def sample_dfxp_from_scc_output():
<region tts:displayAlign="before" tts:origin="40% 53%" tts:textAlign="left" xml:id="r5"/>
<region tts:displayAlign="before" tts:origin="70% 17%" tts:textAlign="left" xml:id="r6"/>
<region tts:displayAlign="before" tts:origin="20% 35%" tts:textAlign="left" xml:id="r7"/>
<region tts:displayAlign="before" tts:origin="20% 83%" tts:textAlign="left" xml:id="r8"/>
<region tts:displayAlign="before" tts:origin="25% 83%" tts:textAlign="left" xml:id="r8"/>
<region tts:displayAlign="before" tts:origin="70% 11%" tts:textAlign="left" xml:id="r9"/>
<region tts:displayAlign="before" tts:origin="40% 41%" tts:textAlign="left" xml:id="r10"/>
<region tts:displayAlign="before" tts:origin="20% 71%" tts:textAlign="left" xml:id="r11"/>
<region tts:displayAlign="before" tts:origin="25% 71%" tts:textAlign="left" xml:id="r11"/>
</layout>
</head>
<body>
Expand Down
29 changes: 28 additions & 1 deletion tests/fixtures/scc.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,33 @@ def sample_scc_created_dfxp_with_wrongly_closing_spans():
"""


@pytest.fixture(scope="session")
def sample_scc_with_consecutive_pac_commands():
return """\
Scenarist_SCC V1.0
00:00:00;15 942c
00:11:45;10 9420 94d0 94ce 5b20 cec1 5252 c154 4f52 205d 9470 946e cd4f cecb 45d9 d320 4c4f d645 2054 c849 cec7 d320 54c8 c154 2046 4cd9 ae80 942c 8080 8080 942f
00:11:47;28 9420 9454 9723 d9c1 d9a1 94f4 9723 5b20 c84f 4f54 49ce c720 5d80 942c 8080 8080 942f
00:11:50;08 9420 94d0 94ce 45d3 d045 4349 c14c 4cd9 2049 4620 54c8 45d9 a752 4520 54c8 4520 4fce 45d3 9470 946e 57c8 4f20 c745 5420 544f 2046 4cd9 2054 c845 cdae 942c 8080 8080 942f
00:11:54;06 942c
00:23:00;13 9420 1370 136e 5b20 43c8 494c c420 5d80 94d0 94ce c745 4f52 c745 20cd c1c4 4520 c120 cdc1 43c8 49ce 4580 9470 946e 464f 5220 c84f 5749 4520 544f 942c 8080 8080 942f
00:23:02;04 9420 91d0 91ce 544f 2046 49ce c420 43d5 5249 4fd5 d320 c745 4f52 c745 9170 916e c1ce c420 c849 d320 4652 4945 cec4 d380 92d0 92ce 45d6 4552 d920 c4c1 d920 4fce 4c49 ce45 2c80 942c 8080 8080 942f
00:23:05;00 9420 9152 91ae d357 49ce c720 c2d9 20d0 c2d3 cb49 c4d3 ae4f 52c7 9170 916e 544f 20d0 4cc1 d920 46d5 ce20 c7c1 cd45 d320 c1ce c420 57c1 5443 c880 92d0 9723 91ae d94f d552 2046 c1d6 4f52 4954 4520 d649 c445 4fd3 ae80 942c 8080 8080 942f
00:23:05;00 9420 9152 91ae d357 49ce c720 c2d9 20d0 c2d3 cb49 c4d3 ae4f 52c7 9170 916e 544f 20d0 4cc1 d920 46d5 ce20 c7c1 cd45 d320 c1ce c420 57c1 5443 c880 92d0 9723 91ae d94f d552 2046 c1d6 4f52 4954 4520 d649 c445 4fd3 ae80 942c 8080 8080 942f
"""


@pytest.fixture(scope="session")
def scc_that_generates_webvtt_with_proper_newlines():
return """\
Expand Down Expand Up @@ -435,7 +462,7 @@ def sample_scc_with_line_too_long():
00:00:00;03 942c
00:00:01;45 9420 91f4 cb45 4c4c d920 4ac1 cd45 d3ba 20c8 eff7 9254 f468 e520 7368 eff7 2073 f461 f2f4 e564 942c 8080 8080 942f
00:00:01;45 9420 91f4 cb45 4c4c d920 4ac1 cd45 d3ba 20c8 eff7 9254 f468 e520 7368 eff7d3ba 20c8 eff7 9254 f468 e520 7368 eff7 eff7 eff7 eff7 eff7 eff7 eff7 eff7 eff7 eff7 eff7 eff7 eff7 eff7 eff7 eff7 eff7 eff7 eff7 eff7 eff7 eff7 eff7 eff7 eff7 eff7 eff7 2073 f461 f2f4 e564 942c 8080 8080 942f
00:00:02;55 9420 91e0 9723 f761 7320 4361 ec20 ec20 ec20 ec20 ec20 ec20 ec20 ec20 ec20 ec20 ec20 ec20 ec20 ec20 ec20 ec20 ec20 ec20 ec20 ec20 ec20 ec20 ec20 ec20 ec20 ec20 ec20 ec20 ec20 c4e5 6ee9 73ef 6e2c 2061 20e6 f2e9 e56e 6480 9240 9723 efe6 20ef 75f2 732c 20f7 6173 2064 efe9 6e67 206d 7920 43c4 73ae 942c 8080 8080 942f
Expand Down
39 changes: 35 additions & 4 deletions tests/test_scc.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ def test_positioning(self, sample_scc_multiple_positioning):
((40.0, UnitEnum.PERCENT), (53.0, UnitEnum.PERCENT)),
((70.0, UnitEnum.PERCENT), (17.0, UnitEnum.PERCENT)),
((20.0, UnitEnum.PERCENT), (35.0, UnitEnum.PERCENT)),
((20.0, UnitEnum.PERCENT), (83.0, UnitEnum.PERCENT)),
((25.0, UnitEnum.PERCENT), (83.0, UnitEnum.PERCENT)),
((70.0, UnitEnum.PERCENT), (11.0, UnitEnum.PERCENT)),
((40.0, UnitEnum.PERCENT), (41.0, UnitEnum.PERCENT)),
((20.0, UnitEnum.PERCENT), (71.0, UnitEnum.PERCENT))
((25.0, UnitEnum.PERCENT), (71.0, UnitEnum.PERCENT))
]

actual_positioning = [
Expand Down Expand Up @@ -244,6 +244,38 @@ def test_flashing_cue(self, sample_scc_flashing_cue):
assert exc_info.value.args[0].startswith(
"Unsupported cue duration around 00:00:20.433")

def test_skip_first_pac_command(self, sample_scc_with_consecutive_pac_commands):
caption_set = SCCReader().read(sample_scc_with_consecutive_pac_commands)
caption = caption_set.get_captions('en-US')
actual_lines = [
node.content
for cap_ in caption
for node in cap_.nodes
if node.type_ == CaptionNode.TEXT
]
expected_lines = [
'[ NARRATOR ]',
'MONKEYS LOVE THINGS THAT FLY.',
'YAY!',
'[ HOOTING ]',
"ESPECIALLY IF THEY'RE THE ONES",
'WHO GET TO FLY THEM.',
'[ CHILD ]',
'GEORGE MADE A MACHINE',
'FOR HOWIE TO',
'TO FIND CURIOUS GEORGE',
'AND HIS FRIENDS',
'EVERY DAY ONLINE,',
'SWING BY PBSKIDS.ORG',
'TO PLAY FUN GAMES AND WATCH',
'YOUR FAVORITE VIDEOS.',
'SWING BY PBSKIDS.ORG',
'TO PLAY FUN GAMES AND WATCH',
'YOUR FAVORITE VIDEOS.'
]
# is not breaking the lines
assert expected_lines == actual_lines

def test_line_too_long(self, sample_scc_with_line_too_long):
with pytest.raises(CaptionLineLengthError) as exc_info:
SCCReader().read(sample_scc_with_line_too_long)
Expand Down Expand Up @@ -345,7 +377,6 @@ def test_freeze_semicolon_spec_time(self, sample_scc_roll_up_ru2):
]

actual_timings = [(c_.start, c_.end) for c_ in captions]

assert expected_timings == actual_timings

def test_freeze_colon_spec_time(self, sample_scc_pop_on):
Expand Down Expand Up @@ -584,4 +615,4 @@ def test_eoc_first_command(self, sample_scc_eoc_first_command):
# just one caption, first EOC disappears
num_captions = len(caption_set.get_captions('en-US'))

assert num_captions == 2
assert num_captions == 1

0 comments on commit ed81ae9

Please sign in to comment.