intro-skipper/IntroSkipper/Providers/SegmentProvider.cs

62 lines
2.4 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Jellyfin.Data.Enums;
using MediaBrowser.Controller;
using MediaBrowser.Controller.Entities;
2024-10-20 13:27:05 +02:00
using MediaBrowser.Controller.Entities.Movies;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Model;
using MediaBrowser.Model.MediaSegments;
namespace IntroSkipper.Providers
{
/// <summary>
/// Introskipper media segment provider.
/// </summary>
public class SegmentProvider : IMediaSegmentProvider
{
/// <inheritdoc/>
public string Name => Plugin.Instance!.Name;
/// <inheritdoc/>
public Task<IReadOnlyList<MediaSegmentDto>> GetMediaSegments(MediaSegmentGenerationRequest request, CancellationToken cancellationToken)
{
var segments = new List<MediaSegmentDto>();
var remainingTicks = (Plugin.Instance?.Configuration.RemainingSecondsOfIntro ?? 2) * TimeSpan.TicksPerSecond;
if (Plugin.Instance!.Intros.TryGetValue(request.ItemId, out var introValue))
{
segments.Add(new MediaSegmentDto
{
StartTicks = (long)(introValue.Start * TimeSpan.TicksPerSecond),
EndTicks = (long)(introValue.End * TimeSpan.TicksPerSecond) - remainingTicks,
ItemId = request.ItemId,
Type = MediaSegmentType.Intro
});
}
if (Plugin.Instance!.Credits.TryGetValue(request.ItemId, out var creditValue))
{
var creditEndTicks = (long)(creditValue.End * TimeSpan.TicksPerSecond);
var runTimeTicks = Plugin.Instance.GetItem(request.ItemId)?.RunTimeTicks ?? long.MaxValue;
2024-10-20 13:27:05 +02:00
segments.Add(new MediaSegmentDto
{
StartTicks = (long)(creditValue.Start * TimeSpan.TicksPerSecond),
EndTicks = runTimeTicks > creditEndTicks + TimeSpan.TicksPerSecond
? creditEndTicks - remainingTicks
: runTimeTicks,
2024-10-20 13:27:05 +02:00
ItemId = request.ItemId,
Type = MediaSegmentType.Outro
});
}
return Task.FromResult<IReadOnlyList<MediaSegmentDto>>(segments);
}
/// <inheritdoc/>
2024-10-20 13:27:05 +02:00
public ValueTask<bool> Supports(BaseItem item) => ValueTask.FromResult(item is Episode or Movie);
}
}