Skip to content

Commit

Permalink
srt_tools: simplify srt_remove
Browse files Browse the repository at this point in the history
Simplifies srt_remove by maintainer standards.
  • Loading branch information
switchupcb committed May 20, 2021
1 parent a800a1d commit e6f37aa
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 75 deletions.
6 changes: 3 additions & 3 deletions srt_tools/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ Utilities
you can naively strip some basic HTML-like markup with ``srt process -m re -f
'lambda sub: re.sub("<[^<]+?>", "", sub)'``. HTML-like syntax is especially
prevalant in `SSA/ASS`_ subtitles that have been directly converted to SRT.
- *remove* allows removal by index/timestamp in sequential or non-sequential
order. By placing indexes/timestamps non-sequentially (i.e 10, 5), you specify
to remove all captions past 10 and before 5.
- *remove* allows removal by timestamp in sequential or non-sequential
order. By placing timestamps non-sequentially (i.e :08, :05), you specify
to remove all captions past :08 and before :05.

.. _mux: https://en.wikipedia.org/wiki/Multiplexing
.. _`SSA/ASS`: https://en.wikipedia.org/wiki/SubStation_Alpha
Expand Down
48 changes: 13 additions & 35 deletions srt_tools/srt-remove
Original file line number Diff line number Diff line change
Expand Up @@ -11,55 +11,34 @@ log = logging.getLogger(__name__)

def parse_args():
examples = {
"Remove a single caption at index 10": "srt remove -i example.srt --t1 10 --t2 11",
"Remove captions within :05 - :08": "srt remove -i example.srt --t1 00:00:5,00 --t2 00:00:8,00",
"Remove captions from index 10 to the end of the file.": "srt remove -i example.srt --t1 10",
"Remove captions from index 0 to index 10.": "srt remove -i example.srt --t2 10",
"Remove captions non-sequentially": "srt remove -i example.srt --t1 00:00:8,00 --t2 00:00:5,00",
"Remove captions from :16 to the end of the file.": "srt remove -i example.srt --t1 00:00:16,00",
"Remove captions from index 2 to the 3rd to last index:": "srt remove -i example.srt --t1 2 --t2 -3",
"Remove captions from index 5 to timestamp :17": "srt remove -i example.srt --t1 5 --t2 00:00:17,00",
"Remove captions non-sequentially": "srt remove -i example.srt --t1 24 --t2 6",
"Remove captions from :00 to :16": "srt remove -i example.srt --t2 00:00:16,00",
"Remove every caption": "srt remove -i example.srt"
}
parser = srt_tools.utils.basic_parser(description=__doc__, examples=examples)
parser.add_argument(
"--start",
"--t1",
metavar=("INDEX or TIMESTAMP"),
default=0,
metavar=("TIMESTAMP"),
type=lambda arg: srt.srt_timestamp_to_timedelta(arg),
default=datetime.timedelta(0),
nargs='?',
help="The index (from 0) or timestamp to start removing from."
help="The timestamp to start removing from."
)
parser.add_argument(
"--end",
"--t2",
metavar=("INDEX or TIMESTAMP"),
default=0,
metavar=("TIMESTAMP"),
type=lambda arg: srt.srt_timestamp_to_timedelta(arg),
default=datetime.timedelta(0),
nargs='?',
help="The index (from 0) or timestamp to stop removing at."
help="The timestamp to stop removing at."
)
return parser.parse_args()


def get_timestamp(subs, obj):
"""Parse an object for a datetime.timedelta"""
if isinstance(obj, datetime.timedelta):
return obj

try:
idx = int(obj)
list_subs = subs if isinstance(subs, list) else list(subs)
len_subs = len(list_subs)
if (idx >= 0 and idx >= len_subs) or (idx < 0 and -idx > len_subs):
raise IndexError("There is no caption at the specified index.")
return list_subs[idx].start
except ValueError:
try:
return srt.srt_timestamp_to_timedelta(obj)
except srt.TimestampParseError:
raise ValueError("You must enter a whole number index or timestamp (hh:mm:ss,ms) for caption arguments.")


def split(subs, timestamp):
"""
Splits subtitles at a given timestamp.
Expand Down Expand Up @@ -107,8 +86,8 @@ def split(subs, timestamp):
def remove_caption_timestamp(subs, timestamp_one=datetime.timedelta(0), timestamp_two=datetime.timedelta(0)):
"""
Removes captions from subtitles by timestamp.
When timestamp one > timestamp two, captions up to index two will be removed
and captions after index one will be removed.
When timestamp one > timestamp two, captions up to timestamp two will be removed
and captions after timestamp one will be removed.
:param int timestamp_one: The timestamp to remove from.
:param int timestamp_two: The timestamp to remove to.
Expand Down Expand Up @@ -191,8 +170,7 @@ def main():
logging.basicConfig(level=args.log_level)
srt_tools.utils.set_basic_args(args)

subs = list(args.input)
removed_subs = remove_caption_timestamp(subs, get_timestamp(subs, args.start), get_timestamp(subs, args.end))
removed_subs = remove_caption_timestamp(args.input, args.start, args.end)

output = srt_tools.utils.compose_suggest_on_fail(removed_subs, strict=args.strict)

Expand Down
37 changes: 0 additions & 37 deletions srt_tools/tests/test_srt_remove.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,43 +48,6 @@ def sort(subs):
return list(srt.sort_and_reindex(subs))


class TestHelpers(unittest.TestCase):
def setUp(self):
self.subs = create_blocks
self.x = list(create_blocks())
self.y = list(create_blocks(1))
self.r = self.x[0]

def tearDown(self):
pass

def test_get_timestamp(self):
# Indexes
self.assertEqual(get_timestamp(self.subs(), 0), self.x[0].start)
self.assertEqual(get_timestamp(self.subs(), 4), self.x[4].start)
self.assertEqual(get_timestamp(self.subs(), -1), self.x[-1].start)
self.assertEqual(get_timestamp(self.subs(), -4), self.x[-4].start)
self.assertEqual(get_timestamp(self.subs(), -5), self.x[0].start)
with self.assertRaises(IndexError):
get_timestamp(self.subs(), 5)
get_timestamp(self.subs(), -6)

# Strings
self.assertEqual(get_timestamp(
self.subs(), t('00:00:11,000')), self.x[0].start)
self.assertEqual(get_timestamp(
self.subs(), t('00:00:0,000')), t('00:00:0,000'))
self.assertEqual(get_timestamp(
self.subs(), t('00:00:30,000')), t('00:00:30,000'))
with self.assertRaises(srt.TimestampParseError):
self.assertEqual(get_timestamp(self.subs(), t('-00:00:50,000')))
self.assertEqual(get_timestamp(self.subs(), t('00:00:-10,000')))

# Date Time
self.assertEqual(get_timestamp(
self.subs(), t('00:00:11,000')), self.x[0].start)


class TestCaptionTools(unittest.TestCase):
def setUp(self):
self.subs = create_blocks
Expand Down

0 comments on commit e6f37aa

Please sign in to comment.