feat: add TV show trailer downloads, episode trailers, and movie/TV trailer separation
All checks were successful
Publish Release / release (push) Successful in 23s
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:
@@ -8,6 +8,7 @@ using Jellyfin.Data.Enums;
|
||||
using Jellyfin.Plugin.CinemaTrailers4Jellyfins.Services;
|
||||
using MediaBrowser.Controller.Entities;
|
||||
using MediaBrowser.Controller.Entities.Movies;
|
||||
using MediaBrowser.Controller.Entities.TV;
|
||||
using MediaBrowser.Controller.Library;
|
||||
using MediaBrowser.Model.Entities;
|
||||
using MediaBrowser.Model.Tasks;
|
||||
@@ -25,7 +26,7 @@ namespace Jellyfin.Plugin.CinemaTrailers4Jellyfins.ScheduledTasks
|
||||
|
||||
public string Name => "Download TMDB Trailers";
|
||||
public string Key => "CinemaTrailers4JellyfinsDownload";
|
||||
public string Description => "Downloads trailers from TMDB for upcoming and recently released movies not in your library, packaged as fake-movie folders for use with a Cinema Mode / trailer pre-roll plugin.";
|
||||
public string Description => "Downloads trailers from TMDB for upcoming and recently released movies and TV shows not in your library, packaged as fake-movie folders for use with a Cinema Mode / trailer pre-roll plugin.";
|
||||
public string Category => "CinemaTrailers4Jellyfins";
|
||||
|
||||
public DownloadTrailersTask(
|
||||
@@ -67,9 +68,12 @@ namespace Jellyfin.Plugin.CinemaTrailers4Jellyfins.ScheduledTasks
|
||||
return;
|
||||
}
|
||||
|
||||
if (!config.SourceNowPlaying && !config.SourceUpcoming && !config.SourcePopular && !config.SourceTopRated)
|
||||
var downloadMovies = config.MaxTrailersToDownload > 0;
|
||||
var downloadTvShows = config.MaxTvTrailersToDownload > 0;
|
||||
|
||||
if (!downloadMovies && !downloadTvShows)
|
||||
{
|
||||
_logger.LogWarning("|CinemaTrailers4Jellyfins| No TMDB sources selected. Enable at least one source. Skipping task.");
|
||||
_logger.LogWarning("|CinemaTrailers4Jellyfins| Both 'Max movie trailers' and 'Max TV show trailers' are 0. Skipping task.");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -79,26 +83,64 @@ namespace Jellyfin.Plugin.CinemaTrailers4Jellyfins.ScheduledTasks
|
||||
|
||||
progress.Report(5);
|
||||
|
||||
var libraryTmdbIds = config.SkipMoviesInLibrary
|
||||
? GetLibraryTmdbIds()
|
||||
: new HashSet<string>(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
_logger.LogInformation("|CinemaTrailers4Jellyfins| Library contains {Count} movies with TMDB IDs (will skip these)", libraryTmdbIds.Count);
|
||||
|
||||
progress.Report(10);
|
||||
|
||||
var allowedLanguages = string.IsNullOrWhiteSpace(config.AllowedLanguages)
|
||||
? null
|
||||
: new HashSet<string>(
|
||||
config.AllowedLanguages.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries),
|
||||
StringComparer.OrdinalIgnoreCase) as IReadOnlySet<string>;
|
||||
|
||||
_logger.LogInformation("|CinemaTrailers4Jellyfins| Fetching candidates from TMDB...");
|
||||
int totalDownloaded = 0;
|
||||
|
||||
if (downloadMovies)
|
||||
{
|
||||
if (!config.SourceNowPlaying && !config.SourceUpcoming && !config.SourcePopular && !config.SourceTopRated)
|
||||
{
|
||||
_logger.LogWarning("|CinemaTrailers4Jellyfins| No movie TMDB sources selected. Skipping movie trailers.");
|
||||
}
|
||||
else
|
||||
{
|
||||
var progressEnd = downloadTvShows ? 50 : 95;
|
||||
totalDownloaded += await DownloadMoviesAsync(config, allowedLanguages, progress, 5, progressEnd, cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
if (downloadTvShows)
|
||||
{
|
||||
if (!config.SourceTvAiringToday && !config.SourceTvOnTheAir && !config.SourceTvPopular && !config.SourceTvTopRated)
|
||||
{
|
||||
_logger.LogWarning("|CinemaTrailers4Jellyfins| No TV show TMDB sources selected. Skipping TV show trailers.");
|
||||
}
|
||||
else
|
||||
{
|
||||
var progressStart = downloadMovies ? 50 : 5;
|
||||
totalDownloaded += await DownloadTvShowsAsync(config, allowedLanguages, progress, progressStart, 95, cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
_logger.LogInformation("|CinemaTrailers4Jellyfins| Task complete. Downloaded {Count} trailer(s).", totalDownloaded);
|
||||
progress.Report(100);
|
||||
}
|
||||
|
||||
private async Task<int> DownloadMoviesAsync(
|
||||
Configuration.PluginConfiguration config,
|
||||
IReadOnlySet<string>? allowedLanguages,
|
||||
IProgress<double> progress,
|
||||
double progressStart,
|
||||
double progressEnd,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var libraryTmdbIds = config.SkipMoviesInLibrary
|
||||
? GetLibraryMovieTmdbIds()
|
||||
: new HashSet<string>(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
_logger.LogInformation("|CinemaTrailers4Jellyfins| Library contains {Count} movies with TMDB IDs (will skip these)", libraryTmdbIds.Count);
|
||||
|
||||
_logger.LogInformation("|CinemaTrailers4Jellyfins| Fetching movie candidates from TMDB...");
|
||||
var candidates = await _tmdbService.GetCandidateMoviesAsync(config, cancellationToken).ConfigureAwait(false);
|
||||
_logger.LogInformation("|CinemaTrailers4Jellyfins| Found {Count} candidate movies across all sources", candidates.Count);
|
||||
|
||||
progress.Report(20);
|
||||
|
||||
if (config.SkipMoviesInLibrary)
|
||||
{
|
||||
candidates = candidates
|
||||
@@ -109,9 +151,8 @@ namespace Jellyfin.Plugin.CinemaTrailers4Jellyfins.ScheduledTasks
|
||||
|
||||
if (candidates.Count == 0)
|
||||
{
|
||||
_logger.LogInformation("|CinemaTrailers4Jellyfins| No new candidates to download. All done.");
|
||||
progress.Report(100);
|
||||
return;
|
||||
_logger.LogInformation("|CinemaTrailers4Jellyfins| No new movie candidates to download.");
|
||||
return 0;
|
||||
}
|
||||
|
||||
int downloaded = 0;
|
||||
@@ -124,20 +165,20 @@ namespace Jellyfin.Plugin.CinemaTrailers4Jellyfins.ScheduledTasks
|
||||
if (downloaded >= config.MaxTrailersToDownload)
|
||||
break;
|
||||
|
||||
double taskProgress = 20 + (80.0 * processed / candidates.Count);
|
||||
double taskProgress = progressStart + (progressEnd - progressStart) * processed / candidates.Count;
|
||||
progress.Report(taskProgress);
|
||||
processed++;
|
||||
|
||||
var paths = BuildMoviePaths(movie.Title, movie.Year, config);
|
||||
var paths = BuildMediaPaths(movie.Title, movie.Year, config);
|
||||
|
||||
if (config.SkipAlreadyDownloaded && Directory.Exists(paths.MovieFolder))
|
||||
if (config.SkipAlreadyDownloaded && Directory.Exists(paths.MediaFolder))
|
||||
{
|
||||
_logger.LogDebug("|CinemaTrailers4Jellyfins| Already downloaded: {Path}", paths.MovieFolder);
|
||||
_logger.LogDebug("|CinemaTrailers4Jellyfins| Already downloaded: {Path}", paths.MediaFolder);
|
||||
continue;
|
||||
}
|
||||
|
||||
var trailers = await _tmdbService.GetTrailersAsync(
|
||||
movie.Id.ToString(), config.TmdbApiKey, allowedLanguages, cancellationToken).ConfigureAwait(false);
|
||||
"movie", movie.Id.ToString(), config.TmdbApiKey, allowedLanguages, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
if (trailers.Count == 0)
|
||||
{
|
||||
@@ -163,27 +204,128 @@ namespace Jellyfin.Plugin.CinemaTrailers4Jellyfins.ScheduledTasks
|
||||
{
|
||||
_logger.LogError(
|
||||
"|CinemaTrailers4Jellyfins| Could not create the fake movie file for '{Movie}'. Removing incomplete folder {Folder}.",
|
||||
movie.Title, paths.MovieFolder);
|
||||
TryDeleteFolder(paths.MovieFolder);
|
||||
movie.Title, paths.MediaFolder);
|
||||
TryDeleteFolder(paths.MediaFolder);
|
||||
continue;
|
||||
}
|
||||
|
||||
var metadata = await _tmdbService.GetMovieMetadataAsync(
|
||||
movie.Id.ToString(), config.TmdbApiKey, cancellationToken).ConfigureAwait(false);
|
||||
var metadata = await _tmdbService.GetMediaMetadataAsync(
|
||||
"movie", movie.Id.ToString(), config.TmdbApiKey, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
_fakeMovieService.WriteNfo(paths.NfoPath, movie.Title, movie.Year, metadata.Genres, metadata.Certification);
|
||||
|
||||
downloaded++;
|
||||
_logger.LogInformation(
|
||||
"|CinemaTrailers4Jellyfins| [{Done}/{Max}] Saved trailer for '{Movie}' → {Path}",
|
||||
downloaded, config.MaxTrailersToDownload, movie.Title, paths.MovieFolder);
|
||||
downloaded, config.MaxTrailersToDownload, movie.Title, paths.MediaFolder);
|
||||
}
|
||||
|
||||
_logger.LogInformation("|CinemaTrailers4Jellyfins| Task complete. Downloaded {Count} trailer(s).", downloaded);
|
||||
progress.Report(100);
|
||||
_logger.LogInformation("|CinemaTrailers4Jellyfins| Movie trailers complete. Downloaded {Count}.", downloaded);
|
||||
return downloaded;
|
||||
}
|
||||
|
||||
private HashSet<string> GetLibraryTmdbIds()
|
||||
private async Task<int> DownloadTvShowsAsync(
|
||||
Configuration.PluginConfiguration config,
|
||||
IReadOnlySet<string>? allowedLanguages,
|
||||
IProgress<double> progress,
|
||||
double progressStart,
|
||||
double progressEnd,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var librarySeriesTmdbIds = config.SkipMoviesInLibrary
|
||||
? GetLibrarySeriesTmdbIds()
|
||||
: new HashSet<string>(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
_logger.LogInformation("|CinemaTrailers4Jellyfins| Library contains {Count} TV shows with TMDB IDs (will skip these)", librarySeriesTmdbIds.Count);
|
||||
|
||||
_logger.LogInformation("|CinemaTrailers4Jellyfins| Fetching TV show candidates from TMDB...");
|
||||
var candidates = await _tmdbService.GetCandidateTvShowsAsync(config, cancellationToken).ConfigureAwait(false);
|
||||
_logger.LogInformation("|CinemaTrailers4Jellyfins| Found {Count} candidate TV shows across all sources", candidates.Count);
|
||||
|
||||
if (config.SkipMoviesInLibrary)
|
||||
{
|
||||
candidates = candidates
|
||||
.Where(s => !librarySeriesTmdbIds.Contains(s.Id.ToString()))
|
||||
.ToList();
|
||||
_logger.LogInformation("|CinemaTrailers4Jellyfins| {Count} candidates remain after filtering library TV shows", candidates.Count);
|
||||
}
|
||||
|
||||
if (candidates.Count == 0)
|
||||
{
|
||||
_logger.LogInformation("|CinemaTrailers4Jellyfins| No new TV show candidates to download.");
|
||||
return 0;
|
||||
}
|
||||
|
||||
int downloaded = 0;
|
||||
int processed = 0;
|
||||
|
||||
foreach (var show in candidates)
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
if (downloaded >= config.MaxTvTrailersToDownload)
|
||||
break;
|
||||
|
||||
double taskProgress = progressStart + (progressEnd - progressStart) * processed / candidates.Count;
|
||||
progress.Report(taskProgress);
|
||||
processed++;
|
||||
|
||||
var paths = BuildMediaPaths(show.Title, show.Year, config);
|
||||
|
||||
if (config.SkipAlreadyDownloaded && Directory.Exists(paths.MediaFolder))
|
||||
{
|
||||
_logger.LogDebug("|CinemaTrailers4Jellyfins| Already downloaded: {Path}", paths.MediaFolder);
|
||||
continue;
|
||||
}
|
||||
|
||||
var trailers = await _tmdbService.GetTrailersAsync(
|
||||
"tv", show.Id.ToString(), config.TmdbApiKey, allowedLanguages, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
if (trailers.Count == 0)
|
||||
{
|
||||
_logger.LogDebug("|CinemaTrailers4Jellyfins| No YouTube trailers on TMDB for '{Title}'", show.Title);
|
||||
continue;
|
||||
}
|
||||
|
||||
var trailer = trailers[0];
|
||||
_logger.LogInformation("|CinemaTrailers4Jellyfins| Downloading '{Trailer}' for '{Show}'", trailer.Name, show.Title);
|
||||
|
||||
var success = await _downloadService.DownloadAsync(
|
||||
trailer.Key,
|
||||
paths.TrailerPath,
|
||||
config.PreferredVideoHeight,
|
||||
config.YtDlpPath,
|
||||
cancellationToken).ConfigureAwait(false);
|
||||
|
||||
if (!success)
|
||||
continue;
|
||||
|
||||
var fakeMovieReady = await _fakeMovieService.CopyFakeMovieAsync(paths.FakeMoviePath, cancellationToken).ConfigureAwait(false);
|
||||
if (!fakeMovieReady)
|
||||
{
|
||||
_logger.LogError(
|
||||
"|CinemaTrailers4Jellyfins| Could not create the fake movie file for '{Show}'. Removing incomplete folder {Folder}.",
|
||||
show.Title, paths.MediaFolder);
|
||||
TryDeleteFolder(paths.MediaFolder);
|
||||
continue;
|
||||
}
|
||||
|
||||
var metadata = await _tmdbService.GetMediaMetadataAsync(
|
||||
"tv", show.Id.ToString(), config.TmdbApiKey, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
_fakeMovieService.WriteNfo(paths.NfoPath, show.Title, show.Year, metadata.Genres, metadata.Certification, new[] { TrailerTags.TvShow });
|
||||
|
||||
downloaded++;
|
||||
_logger.LogInformation(
|
||||
"|CinemaTrailers4Jellyfins| [{Done}/{Max}] Saved trailer for '{Show}' → {Path}",
|
||||
downloaded, config.MaxTvTrailersToDownload, show.Title, paths.MediaFolder);
|
||||
}
|
||||
|
||||
_logger.LogInformation("|CinemaTrailers4Jellyfins| TV show trailers complete. Downloaded {Count}.", downloaded);
|
||||
return downloaded;
|
||||
}
|
||||
|
||||
private HashSet<string> GetLibraryMovieTmdbIds()
|
||||
{
|
||||
var ids = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
@@ -201,7 +343,25 @@ namespace Jellyfin.Plugin.CinemaTrailers4Jellyfins.ScheduledTasks
|
||||
return ids;
|
||||
}
|
||||
|
||||
// Each trailer lives in its own "{Movie Title} ({Year})" folder alongside a fake
|
||||
private HashSet<string> GetLibrarySeriesTmdbIds()
|
||||
{
|
||||
var ids = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
var series = _libraryManager
|
||||
.GetItemList(new InternalItemsQuery { IncludeItemTypes = new[] { BaseItemKind.Series }, Recursive = true })
|
||||
.OfType<Series>();
|
||||
|
||||
foreach (var s in series)
|
||||
{
|
||||
var tmdbId = s.GetProviderId(MetadataProvider.Tmdb);
|
||||
if (!string.IsNullOrEmpty(tmdbId))
|
||||
ids.Add(tmdbId);
|
||||
}
|
||||
|
||||
return ids;
|
||||
}
|
||||
|
||||
// Each trailer lives in its own "{Title} ({Year})" folder alongside a fake
|
||||
// movie file and NFO. Removing a trailer always means removing that whole folder.
|
||||
private void CleanupOldTrailers(Configuration.PluginConfiguration config)
|
||||
{
|
||||
@@ -238,20 +398,20 @@ namespace Jellyfin.Plugin.CinemaTrailers4Jellyfins.ScheduledTasks
|
||||
}
|
||||
}
|
||||
|
||||
private readonly record struct MoviePaths(string MovieFolder, string FakeMoviePath, string TrailerPath, string NfoPath);
|
||||
private readonly record struct MediaPaths(string MediaFolder, string FakeMoviePath, string TrailerPath, string NfoPath);
|
||||
|
||||
private static MoviePaths BuildMoviePaths(string title, int? year, Configuration.PluginConfiguration config)
|
||||
private static MediaPaths BuildMediaPaths(string title, int? year, Configuration.PluginConfiguration config)
|
||||
{
|
||||
var safeTitle = string.Concat(title.Split(Path.GetInvalidFileNameChars())).Trim();
|
||||
var folderName = year.HasValue ? $"{safeTitle} ({year.Value})" : safeTitle;
|
||||
|
||||
var movieFolder = Path.Combine(config.DownloadFolder, folderName);
|
||||
var mediaFolder = Path.Combine(config.DownloadFolder, folderName);
|
||||
|
||||
return new MoviePaths(
|
||||
MovieFolder: movieFolder,
|
||||
FakeMoviePath: Path.Combine(movieFolder, $"{folderName}.mp4"),
|
||||
TrailerPath: Path.Combine(movieFolder, $"{folderName}-trailer.mp4"),
|
||||
NfoPath: Path.Combine(movieFolder, $"{folderName}.nfo"));
|
||||
return new MediaPaths(
|
||||
MediaFolder: mediaFolder,
|
||||
FakeMoviePath: Path.Combine(mediaFolder, $"{folderName}.mp4"),
|
||||
TrailerPath: Path.Combine(mediaFolder, $"{folderName}-trailer.mp4"),
|
||||
NfoPath: Path.Combine(mediaFolder, $"{folderName}.nfo"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user