Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Octo 10316 expose the first timestamp from input #325

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions pycaption/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,19 @@ def detect(self, content):
def read(self, content):
return CaptionSet({DEFAULT_LANGUAGE_CODE: []})

def get_initial_timestamp(self, content):
"""
return the start time of first caption in xx:xx:xx.xxx format
invocation: reader.get_initial_timestamp(content)
:param content: caption file content
"""
try:
caps = self.read(content)
lang = list(caps._captions.keys())[0]
return caps._captions[lang][0].format_start()
except (AttributeError, IndexError):
return None


class BaseWriter:
def __init__(self, relativize=True, video_width=None, video_height=None,
Expand Down
4 changes: 2 additions & 2 deletions pycaption/scc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
from copy import deepcopy

from pycaption.base import (
BaseReader, BaseWriter, CaptionSet, CaptionNode,
BaseReader, BaseWriter, CaptionSet
)
from pycaption.exceptions import CaptionReadNoCaptions, InvalidInputError, \
CaptionReadTimingError, CaptionLineLengthError
Expand All @@ -94,7 +94,7 @@
MICROSECONDS_PER_CODEWORD, CHARACTER_TO_CODE,
SPECIAL_OR_EXTENDED_CHAR_TO_CODE, PAC_BYTES_TO_POSITIONING_MAP,
PAC_HIGH_BYTE_BY_ROW, PAC_LOW_BYTE_BY_ROW_RESTRICTED,
PAC_TAB_OFFSET_COMMANDS,
PAC_TAB_OFFSET_COMMANDS
)
from .specialized_collections import ( # noqa: F401
TimingCorrectingCaptionList, NotifyingDict, CaptionCreator,
Expand Down
15 changes: 15 additions & 0 deletions tests/test_base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import pytest

from pycaption.base import CaptionList, Caption
from pycaption import (
DFXPReader, SAMIReader, SCCReader
)


class TestCaption:
Expand Down Expand Up @@ -56,3 +59,15 @@ def test_add_two_caption_lists(self):

with pytest.raises(ValueError):
newcaps = self.caps + CaptionList([4], layout_info="Other Layout")


class TestBaseReader:

def test_get_initial_timestamp_dfxp(self, sample_dfxp):
assert DFXPReader().get_initial_timestamp(sample_dfxp) == "00:00:09.209"

def test_get_initial_timestamp_sami(self, sample_sami):
assert SAMIReader().get_initial_timestamp(sample_sami) == "00:00:09.209"

def test_get_initial_timestamp_scc(self, sample_scc_pop_on):
assert SCCReader().get_initial_timestamp(sample_scc_pop_on) == "00:00:09.743"
Loading