fix(chapters): drop duplicate timestamps in the EXAMPLE template and docs

Log a get_chapters failure at debug level on the API tracks route.
This commit is contained in:
imSp4rky
2026-11-23 14:01:58 -07:00
parent 91fbe67fa6
commit 530f78ecf5
3 changed files with 15 additions and 8 deletions
+5 -2
View File
@@ -550,7 +550,10 @@ exists. Timestamps take `"HH:MM:SS[.mmm]"`, int milliseconds, or float seconds.
```python
def get_chapters(self, title: Title_T) -> Chapters:
chapters = [Chapter(name=c.get("name"), timestamp=c["start"]) for c in title.data.get("chapters", [])]
return Chapters(sorted(chapters, key=lambda c: c.timestamp))
unique: dict[str, Chapter] = {}
for chapter in sorted(chapters, key=lambda c: not c.named):
unique.setdefault(chapter.timestamp, chapter)
return Chapters(sorted(unique.values(), key=lambda c: c.timestamp))
```
!!! warning "Sort by timestamp before you build the `Chapters`"
@@ -559,7 +562,7 @@ def get_chapters(self, title: Title_T) -> Chapters:
lands before it.
You never need to add `Chapter(0)`. An API chapter at `0` keeps its name. Drop
duplicate timestamps.
duplicate timestamps and keep the named chapter, as the example does.
!!! note "Close a named range with an unnamed chapter"
An unnamed `Chapter` ends the range before it: `Chapter(name="Intro")` at the
+2 -1
View File
@@ -4290,7 +4290,8 @@ async def session_tracks_handler(
try:
chapters = await asyncio.to_thread(service_instance.get_chapters, title)
session.chapters_by_title[str(title_id)] = chapters if chapters else []
except (Exception, SystemExit):
except (Exception, SystemExit) as e:
log.debug("get_chapters failed for title %s: %s", title_id, e)
session.chapters_by_title[str(title_id)] = []
snapshot = await asyncio.to_thread(capture, tracks, config.serve.get("compression_level", 1))
+8 -5
View File
@@ -443,14 +443,17 @@ class EXAMPLE(Service):
return tracks
def get_chapters(self, title: Title_T) -> Chapters:
chapters = Chapters()
chapters = []
for chapter in title.data.get("chapters", []):
if chapter["name"] == "Intro":
chapters.add(Chapter(timestamp=chapter["start"], name="Opening"))
chapters.add(Chapter(timestamp=chapter["end"])) # unnamed marker = chapter break
chapters.append(Chapter(timestamp=chapter["start"], name="Opening"))
chapters.append(Chapter(timestamp=chapter["end"])) # unnamed marker = chapter break
elif chapter["name"] == "Credits":
chapters.add(Chapter(timestamp=chapter["start"], name="Credits"))
return chapters
chapters.append(Chapter(timestamp=chapter["start"], name="Credits"))
unique: dict[str, Chapter] = {}
for chapter in sorted(chapters, key=lambda c: not c.named):
unique.setdefault(chapter.timestamp, chapter)
return Chapters(sorted(unique.values(), key=lambda c: c.timestamp))
def get_widevine_service_certificate(self, **_: Any) -> Optional[str]:
# Returning the service cert enables privacy-mode license requests.