Keep projects distinct within each commercial break

This commit is contained in:
Josh
2026-11-13 17:32:45 -05:00
parent 19c0751a4a
commit 183c96fbff
2 changed files with 39 additions and 4 deletions

View File

@@ -19,7 +19,7 @@ Place commercial clips in `data/commercials/`. Compose mounts that folder read-o
Scan the source after adding clips, then create a **Commercials** filler list under **Library → Fillers**. For breaks between episodes, assign the list as **Post** filler in a slot schedule. For breaks during episodes, assign it as **Mid** filler and set the break timing. Channel-level Flex uses filler only when the lineup contains Flex time.
On this host, channels 1–11 use `scripts/channel-commercials.py` from the user's crontab every minute. The script scans changed files after their sizes and modification times stabilize and applies the refreshed lineup when each channel has no active session. It uses one clip of at least 60 seconds between episodes and two clips of at most 30 seconds at the 25%, 50%, and 75% marks. Each duration group uses shuffled rounds with equal play counts to within one per lineup cycle. Clips longer than 30 seconds and shorter than 60 seconds are outside both groups.
On this host, channels 1–11 use `scripts/channel-commercials.py` from the user's crontab every minute. The script scans changed files after their sizes and modification times stabilize and applies the refreshed lineup when each channel has no active session. It uses one clip of at least 60 seconds between episodes and two clips of at most 30 seconds at the 25%, 50%, and 75% marks. Each duration group uses shuffled rounds with equal play counts to within one per lineup cycle when the project mix allows it. Name ads with a shared project prefix before the first hyphen, such as `burnengine-teaser.mp4`. The scheduler pairs different prefixes and uses a single-clip break when there are too few eligible clips from other projects. Clips longer than 30 seconds and shorter than 60 seconds are outside both groups.
The script reads the original ordered episode lineups from `data/channel-commercials/`, which is local deployment state. Run `python3 scripts/channel-commercials.py --apply-active` for an immediate update that can restart affected channels if the indexed library has changed.

View File

@@ -3,6 +3,8 @@
import argparse
import collections
import fcntl
import heapq
from itertools import islice
import json
import os
from pathlib import Path
@@ -58,9 +60,41 @@ def rotation(clips, rng):
yield clip
def project(clip):
return Path(clip["externalId"]).stem.split("-", 1)[0].casefold()
def short_breaks(clips, count, rng):
groups = collections.defaultdict(list)
for clip in islice(rotation(clips, rng), count * 2):
groups[project(clip)].append(clip)
pending = []
for name, group in groups.items():
rng.shuffle(group)
heapq.heappush(pending, (-len(group), rng.random(), name))
breaks = []
while len(pending) >= 2:
pair = []
chosen = [heapq.heappop(pending), heapq.heappop(pending)]
for remaining, _, name in chosen:
pair.append(groups[name].pop())
if remaining < -1:
heapq.heappush(pending, (remaining + 1, rng.random(), name))
rng.shuffle(pair)
breaks.append(pair)
if pending:
_, _, name = pending[0]
breaks.extend([clip] for clip in groups[name][:count - len(breaks)])
rng.shuffle(breaks)
return iter(breaks)
def build_lineup(episodes, short, long, filler):
rng = random.Random("|".join(clip["uuid"] for clip in short + long))
short_cycle, long_cycle = rotation(short, rng), rotation(long, rng)
breaks = short_breaks(short, len(episodes) * 3, rng)
long_cycle = rotation(long, rng)
def ad(clip, position):
return {"type": "filler", "id": clip["uuid"], "duration": clip["duration"],
@@ -73,7 +107,7 @@ def build_lineup(episodes, short, long, filler):
lineup.append(ad(next(long_cycle), "pre"))
for index, (start, stop) in enumerate(zip(points, points[1:])):
if index:
lineup.extend(ad(next(short_cycle), "mid") for _ in range(2))
lineup.extend(ad(clip, "mid") for clip in next(breaks))
lineup.append({"type": "content", "id": episode["id"],
"duration": stop - start, "startOffsetMs": start})
return lineup
@@ -120,7 +154,8 @@ def refresh(apply_active):
if not short or not long:
return
signature = [[clip["uuid"], clip["duration"]] for clip in clips]
signature = {"distinct_projects": [[clip["uuid"], clip["duration"], project(clip)]
for clip in clips]}
filler = next(item for item in api("/filler-lists") if item["name"] == "Commercials")
filler_path = f'/filler-lists/{filler["id"]}'
members = [{"type": "content", "id": clip["uuid"], "duration": clip["duration"],