Support duplicate chapter timestamps by merging (#1179)

Co-authored-by: Tyler Dakin <tyler@dakin.one>
This commit is contained in:
Tyler Dakin 2025-02-21 11:34:46 -05:00 committed by GitHub
parent 0ebcfb6a57
commit bb2bc49a49
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 28 additions and 1 deletions

View file

@ -220,10 +220,17 @@ class Chapters:
# Timestamp captured, store it
if match := Timestamp.TIMESTAMP_REGEX.search(line):
timestamp_str = match.group(1)
timestamps.append(Timestamp.from_str(timestamp_str))
# Remove timestamp and surrounding whitespace from it
title_str = re.sub(f"\\s*{re.escape(timestamp_str)}\\s*", " ", line).strip()
timestamp = Timestamp.from_str(timestamp_str)
if timestamps and (timestamps[-1].timestamp_sec == timestamp.timestamp_sec):
# duplicate timestamp... combine into one chapter
titles[-1] += " // " + title_str
continue
timestamps.append(timestamp)
titles.append(title_str)
# If more than 3 timestamps were parsed, return it

View file

@ -86,6 +86,21 @@ def chapter_description_3() -> str:
"""
@pytest.fixture
def chapter_duplicate_timecodes() -> str:
return """01. 00:00 Ocean
02:41 Dreams
02:41 Nightmares
05:16 Future Tales
08:50 Mind Travelling
11:05 Love Supreme
14:17 Reflections
14:17 Reflections VIP
16:32 Moonlight Fading
19:40 Between Two Worlds
"""
class TestChapters:
def test_chapters_from_str_1(self, chapter_description_1):
chapters = Chapters.from_string(chapter_description_1)
@ -104,3 +119,8 @@ class TestChapters:
chapters.titles[8]
== "(Nightcore) Electro-Light & Jordan Kelvin James - Wait For You (feat. Anna Yvette)"
)
def test_chapters_from_str_with_duplicate_timecodes(self, chapter_duplicate_timecodes):
chapters = Chapters.from_string(chapter_duplicate_timecodes)
assert len(chapters) == 8
assert chapters.titles[5] == "Reflections // Reflections VIP"