feat: add TV show trailer downloads, episode trailers, and movie/TV trailer separation
All checks were successful
Publish Release / release (push) Successful in 23s

- Download trailers for TV shows from TMDB with separate sources and an
  independent max-count cap (0 disables a category)
- Play trailers before TV episodes via IIntroProvider, limited to the first
  episode a user watches each day
- Tag TV show trailers in their NFO so movies only get movie trailers and
  episodes only get TV show trailers

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Martin
2026-06-10 00:41:37 -04:00
parent f769e33b8d
commit f49c32f181
9 changed files with 542 additions and 127 deletions

View File

@@ -8,6 +8,7 @@ using Jellyfin.Data.Enums;
using Jellyfin.Database.Implementations.Entities;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Movies;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Library;
using Microsoft.Extensions.Logging;
@@ -22,6 +23,10 @@ namespace Jellyfin.Plugin.CinemaTrailers4Jellyfins.Services
private static readonly ConcurrentDictionary<Guid, HashSet<Guid>> _seenByUser = new();
private static readonly object _seenLock = new();
// Per-user date of the last episode that received trailers, so only the
// first episode a user watches each day gets them.
private static readonly ConcurrentDictionary<Guid, DateTime> _lastEpisodeTrailerDateByUser = new();
private readonly ILibraryManager _libraryManager;
private readonly ILogger<TrailerIntroProvider> _logger;
@@ -39,8 +44,32 @@ namespace Jellyfin.Plugin.CinemaTrailers4Jellyfins.Services
if (config == null || config.TrailersPerMovie <= 0)
return Task.FromResult(Enumerable.Empty<IntroInfo>());
if (item is not Movie)
return Task.FromResult(Enumerable.Empty<IntroInfo>());
// The item used for genre/rating filtering. For episodes this is the
// parent series, since episodes rarely carry their own genre metadata.
BaseItem feature;
bool isEpisode;
switch (item)
{
case Movie:
feature = item;
isEpisode = false;
break;
case Episode episode:
if (!config.TrailersForEpisodes)
return Task.FromResult(Enumerable.Empty<IntroInfo>());
if (!IsFirstEpisodeOfDay(user.Id))
return Task.FromResult(Enumerable.Empty<IntroInfo>());
feature = episode.Series ?? (BaseItem)episode;
isEpisode = true;
break;
default:
return Task.FromResult(Enumerable.Empty<IntroInfo>());
}
var outputFolder = config.DownloadFolder?.TrimEnd(
Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
@@ -62,21 +91,24 @@ namespace Jellyfin.Plugin.CinemaTrailers4Jellyfins.Services
.OfType<Movie>()
.Where(m =>
m.Path?.StartsWith(outputFolder, StringComparison.OrdinalIgnoreCase) == true
&& m.LocalTrailers.Count > 0)
&& m.LocalTrailers.Count > 0
// Keep TV show trailers for episodes and movie trailers for movies separate.
&& m.Tags.Contains(TrailerTags.TvShow, StringComparer.OrdinalIgnoreCase) == isEpisode)
.SelectMany(m => m.LocalTrailers.Select(t => (Movie: m, Trailer: t)))
.ToList();
if (pairs.Count == 0)
{
_logger.LogDebug(
"|CinemaTrailers4Jellyfins| No indexed trailers in {Folder}. "
"|CinemaTrailers4Jellyfins| No indexed {Kind} trailers in {Folder}. "
+ "Ensure the output folder is added as a Jellyfin Movies library and scanned.",
isEpisode ? "TV show" : "movie",
outputFolder);
return Task.FromResult(Enumerable.Empty<IntroInfo>());
}
// Apply enabled filters. If nothing survives, fall back to the full pool.
var filtered = ApplyFilters(pairs, item, config);
var filtered = ApplyFilters(pairs, feature, config);
var pool = filtered.Count > 0 ? filtered : pairs;
// Prefer unseen trailers within the pool; reset the cycle when all have been shown.
@@ -149,6 +181,36 @@ namespace Jellyfin.Plugin.CinemaTrailers4Jellyfins.Services
}
}
/// <summary>
/// Returns true (and records today's date) only the first time this is called
/// for a given user on a given day.
/// </summary>
private static bool IsFirstEpisodeOfDay(Guid userId)
{
var today = DateTime.Now.Date;
var isFirst = false;
_lastEpisodeTrailerDateByUser.AddOrUpdate(
userId,
_ =>
{
isFirst = true;
return today;
},
(_, lastDate) =>
{
if (lastDate < today)
{
isFirst = true;
return today;
}
return lastDate;
});
return isFirst;
}
private static void MarkSeen(Guid userId, IEnumerable<Guid> trailerIds)
{
lock (_seenLock)