feat: add Trailer Pre-Roll and Feature Pre-Roll bumpers
All checks were successful
Publish Release / release (push) Successful in 14s

Adds two optional IIntroProvider bumper slots, mirroring CherryFloors'
cinema mode plugin: a "Trailer Pre-Roll" played before the trailer block
and a "Feature Pre-Roll" played right before the movie/episode. Each is
configured by picking an existing Jellyfin Movie library, from which a
random Movie is injected as the bumper.

Bump version to 1.0.0.5.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Martin
2026-06-10 01:13:52 -04:00
parent 18c3c49a26
commit a0bddac48d
6 changed files with 226 additions and 66 deletions

View File

@@ -41,7 +41,14 @@ namespace Jellyfin.Plugin.CinemaTrailers4Jellyfins.Services
public Task<IEnumerable<IntroInfo>> GetIntros(BaseItem item, User user)
{
var config = Plugin.Instance?.Configuration;
if (config == null || config.TrailersPerMovie <= 0)
if (config == null)
return Task.FromResult(Enumerable.Empty<IntroInfo>());
var trailersEnabled = config.TrailersPerMovie > 0;
var preRollEnabled = !string.IsNullOrEmpty(config.TrailerPreRollLibraryId);
var featurePreRollEnabled = !string.IsNullOrEmpty(config.FeaturePreRollLibraryId);
if (!trailersEnabled && !preRollEnabled && !featurePreRollEnabled)
return Task.FromResult(Enumerable.Empty<IntroInfo>());
// The item used for genre/rating filtering. For episodes this is the
@@ -73,57 +80,104 @@ namespace Jellyfin.Plugin.CinemaTrailers4Jellyfins.Services
var outputFolder = config.DownloadFolder?.TrimEnd(
Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
if (string.IsNullOrEmpty(outputFolder))
return Task.FromResult(Enumerable.Empty<IntroInfo>());
// Break recursion: don't inject intros before items from our own output folder
// (covers both the fake-movie files and their trailer extras).
if (item.Path?.StartsWith(outputFolder, StringComparison.OrdinalIgnoreCase) == true)
if (!string.IsNullOrEmpty(outputFolder)
&& item.Path?.StartsWith(outputFolder, StringComparison.OrdinalIgnoreCase) == true)
return Task.FromResult(Enumerable.Empty<IntroInfo>());
// Server-scoped query so hidden libraries don't block trailer discovery.
var pairs = _libraryManager
var intros = new List<IntroInfo>();
if (preRollEnabled)
{
var preRoll = GetRandomLibraryMovieIntro(config.TrailerPreRollLibraryId, item.Id);
if (preRoll != null)
intros.Add(preRoll);
}
if (trailersEnabled && !string.IsNullOrEmpty(outputFolder))
{
// Server-scoped query so hidden libraries don't block trailer discovery.
var pairs = _libraryManager
.GetItemList(new InternalItemsQuery
{
IncludeItemTypes = new[] { BaseItemKind.Movie },
Recursive = true,
})
.OfType<Movie>()
.Where(m =>
m.Path?.StartsWith(outputFolder, StringComparison.OrdinalIgnoreCase) == true
&& 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 {Kind} trailers in {Folder}. "
+ "Ensure the output folder is added as a Jellyfin Movies library and scanned.",
isEpisode ? "TV show" : "movie",
outputFolder);
}
else
{
// Apply enabled filters. If nothing survives, fall back to the full pool.
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.
pool = ApplyAvoidRepeats(pool, user.Id, config.AvoidRepeats);
var selected = pool
.OrderBy(_ => _rng.Next())
.Take(config.TrailersPerMovie)
.ToList();
if (config.AvoidRepeats)
MarkSeen(user.Id, selected.Select(p => p.Trailer.Id));
intros.AddRange(selected.Select(p => new IntroInfo { ItemId = p.Trailer.Id, Path = p.Trailer.Path }));
}
}
if (featurePreRollEnabled)
{
var featureRoll = GetRandomLibraryMovieIntro(config.FeaturePreRollLibraryId, item.Id);
if (featureRoll != null)
intros.Add(featureRoll);
}
return Task.FromResult<IEnumerable<IntroInfo>>(intros);
}
/// <summary>
/// Picks a random Movie from the given Jellyfin library (VirtualFolder ItemId) to use as a
/// pre-roll/post-roll bumper, excluding the item currently being played.
/// </summary>
private IntroInfo? GetRandomLibraryMovieIntro(string libraryId, Guid excludeId)
{
if (!Guid.TryParse(libraryId, out var parsedId))
return null;
var movies = _libraryManager
.GetItemList(new InternalItemsQuery
{
IncludeItemTypes = new[] { BaseItemKind.Movie },
TopParentIds = new[] { parsedId },
Recursive = true,
})
.OfType<Movie>()
.Where(m =>
m.Path?.StartsWith(outputFolder, StringComparison.OrdinalIgnoreCase) == true
&& 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)))
.Where(m => m.Id != excludeId)
.ToList();
if (pairs.Count == 0)
{
_logger.LogDebug(
"|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>());
}
if (movies.Count == 0)
return null;
// Apply enabled filters. If nothing survives, fall back to the full pool.
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.
pool = ApplyAvoidRepeats(pool, user.Id, config.AvoidRepeats);
var selected = pool
.OrderBy(_ => _rng.Next())
.Take(config.TrailersPerMovie)
.ToList();
if (config.AvoidRepeats)
MarkSeen(user.Id, selected.Select(p => p.Trailer.Id));
var intros = selected.Select(p => new IntroInfo { ItemId = p.Trailer.Id, Path = p.Trailer.Path });
return Task.FromResult<IEnumerable<IntroInfo>>(intros.ToList());
var movie = movies[_rng.Next(movies.Count)];
return new IntroInfo { ItemId = movie.Id, Path = movie.Path };
}
private static List<(Movie Movie, BaseItem Trailer)> ApplyFilters(