Basic Media Segments (#341)

* Basic Media Segment

* add Condition based on env SOURCE_BUILD

---------

Co-authored-by: rlauu <46294892+rlauu@users.noreply.github.com>
Co-authored-by: Kilian von Pflugk <github@jumoog.io>
This commit is contained in:
rlauuzo 2024-10-11 21:08:12 +02:00 committed by GitHub
parent 71e8644483
commit 6c04f26407
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 67 additions and 0 deletions

View File

@ -1,3 +1,4 @@
using ConfusedPolarBear.Plugin.IntroSkipper.Providers;
using MediaBrowser.Controller; using MediaBrowser.Controller;
using MediaBrowser.Controller.Plugins; using MediaBrowser.Controller.Plugins;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
@ -15,6 +16,7 @@ namespace ConfusedPolarBear.Plugin.IntroSkipper
serviceCollection.AddHostedService<AutoSkip>(); serviceCollection.AddHostedService<AutoSkip>();
serviceCollection.AddHostedService<AutoSkipCredits>(); serviceCollection.AddHostedService<AutoSkipCredits>();
serviceCollection.AddHostedService<Entrypoint>(); serviceCollection.AddHostedService<Entrypoint>();
serviceCollection.AddSingleton<IMediaSegmentProvider, SegmentProvider>();
} }
} }
} }

View File

@ -0,0 +1,65 @@
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Jellyfin.Data.Enums;
using MediaBrowser.Controller;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Model;
using MediaBrowser.Model.MediaSegments;
namespace ConfusedPolarBear.Plugin.IntroSkipper.Providers
{
/// <summary>
/// Introskipper media segment provider.
/// </summary>
public class SegmentProvider : IMediaSegmentProvider
{
private readonly int _remainingSecondsOfIntro;
/// <summary>
/// Initializes a new instance of the <see cref="SegmentProvider"/> class.
/// </summary>
public SegmentProvider()
{
_remainingSecondsOfIntro = Plugin.Instance?.Configuration.RemainingSecondsOfIntro ?? 2;
}
/// <inheritdoc/>
public string Name => Plugin.Instance!.Name;
/// <inheritdoc/>
public Task<IReadOnlyList<MediaSegmentDto>> GetMediaSegments(MediaSegmentGenerationRequest request, CancellationToken cancellationToken)
{
var segments = new List<MediaSegmentDto>();
if (Plugin.Instance!.Intros.TryGetValue(request.ItemId, out var introValue))
{
segments.Add(new MediaSegmentDto
{
StartTicks = TimeSpan.FromSeconds(introValue.Start).Ticks,
EndTicks = TimeSpan.FromSeconds(introValue.End - _remainingSecondsOfIntro).Ticks,
ItemId = request.ItemId,
Type = MediaSegmentType.Intro
});
}
if (Plugin.Instance!.Credits.TryGetValue(request.ItemId, out var creditValue))
{
segments.Add(new MediaSegmentDto
{
StartTicks = TimeSpan.FromSeconds(creditValue.Start).Ticks,
EndTicks = TimeSpan.FromSeconds(creditValue.End - _remainingSecondsOfIntro).Ticks,
ItemId = request.ItemId,
Type = MediaSegmentType.Outro
});
}
return Task.FromResult<IReadOnlyList<MediaSegmentDto>>(segments);
}
/// <inheritdoc/>
public ValueTask<bool> Supports(BaseItem item) => ValueTask.FromResult(item is Episode);
}
}