2024-10-11 21:08:12 +02:00
|
|
|
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.Model;
|
|
|
|
using MediaBrowser.Model.MediaSegments;
|
|
|
|
|
|
|
|
namespace ConfusedPolarBear.Plugin.IntroSkipper.Providers
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Introskipper media segment provider.
|
|
|
|
/// </summary>
|
|
|
|
public class SegmentProvider : IMediaSegmentProvider
|
|
|
|
{
|
2024-10-19 22:49:47 +02:00
|
|
|
private static long RemainingTicks => TimeSpan.FromSeconds(Plugin.Instance?.Configuration.RemainingSecondsOfIntro ?? 2).Ticks;
|
2024-10-11 21:08:12 +02:00
|
|
|
|
|
|
|
/// <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,
|
2024-10-19 22:49:47 +02:00
|
|
|
EndTicks = TimeSpan.FromSeconds(introValue.End).Ticks - RemainingTicks,
|
2024-10-11 21:08:12 +02:00
|
|
|
ItemId = request.ItemId,
|
|
|
|
Type = MediaSegmentType.Intro
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Plugin.Instance!.Credits.TryGetValue(request.ItemId, out var creditValue))
|
|
|
|
{
|
2024-10-18 14:15:09 +02:00
|
|
|
var outroSegment = new MediaSegmentDto
|
2024-10-11 21:08:12 +02:00
|
|
|
{
|
|
|
|
StartTicks = TimeSpan.FromSeconds(creditValue.Start).Ticks,
|
|
|
|
ItemId = request.ItemId,
|
|
|
|
Type = MediaSegmentType.Outro
|
2024-10-18 14:15:09 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
var creditEndTicks = TimeSpan.FromSeconds(creditValue.End).Ticks;
|
|
|
|
|
|
|
|
if (Plugin.Instance.GetItem(request.ItemId) is IHasMediaSources item && creditEndTicks >= item.RunTimeTicks - TimeSpan.TicksPerSecond)
|
|
|
|
{
|
|
|
|
outroSegment.EndTicks = item.RunTimeTicks ?? creditEndTicks;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-10-19 22:49:47 +02:00
|
|
|
outroSegment.EndTicks = creditEndTicks - RemainingTicks;
|
2024-10-18 14:15:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
segments.Add(outroSegment);
|
2024-10-11 21:08:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return Task.FromResult<IReadOnlyList<MediaSegmentDto>>(segments);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc/>
|
2024-10-18 14:15:09 +02:00
|
|
|
public ValueTask<bool> Supports(BaseItem item) => ValueTask.FromResult(item is IHasMediaSources);
|
2024-10-11 21:08:12 +02:00
|
|
|
}
|
|
|
|
}
|