2024-10-25 14:31:50 -04:00
|
|
|
// Copyright (C) 2024 Intro-Skipper contributors <intro-skipper.org>
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-only.
|
2024-10-25 14:15:12 -04:00
|
|
|
|
2024-10-11 21:08:12 +02:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
2024-11-02 18:17:22 +01:00
|
|
|
using IntroSkipper.Data;
|
2024-10-11 21:08:12 +02:00
|
|
|
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;
|
2024-10-11 21:08:12 +02:00
|
|
|
using MediaBrowser.Model;
|
|
|
|
using MediaBrowser.Model.MediaSegments;
|
|
|
|
|
2024-10-19 23:50:41 +02:00
|
|
|
namespace IntroSkipper.Providers
|
2024-10-11 21:08:12 +02:00
|
|
|
{
|
|
|
|
/// <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)
|
|
|
|
{
|
2024-11-02 18:17:22 +01:00
|
|
|
ArgumentNullException.ThrowIfNull(request);
|
|
|
|
ArgumentNullException.ThrowIfNull(Plugin.Instance);
|
|
|
|
|
2024-10-11 21:08:12 +02:00
|
|
|
var segments = new List<MediaSegmentDto>();
|
2024-11-02 18:17:22 +01:00
|
|
|
var remainingTicks = Plugin.Instance.Configuration.RemainingSecondsOfIntro * TimeSpan.TicksPerSecond;
|
2024-11-21 15:42:55 +01:00
|
|
|
var itemSegments = Plugin.Instance.GetTimestamps(request.ItemId);
|
|
|
|
var runTimeTicks = Plugin.Instance.GetItem(request.ItemId)?.RunTimeTicks ?? 0;
|
2024-10-11 21:08:12 +02:00
|
|
|
|
2024-11-21 15:42:55 +01:00
|
|
|
// Define mappings between AnalysisMode and MediaSegmentType
|
|
|
|
var segmentMappings = new List<(AnalysisMode Mode, MediaSegmentType Type)>
|
2024-10-11 21:08:12 +02:00
|
|
|
{
|
2024-11-21 15:42:55 +01:00
|
|
|
(AnalysisMode.Introduction, MediaSegmentType.Intro),
|
|
|
|
(AnalysisMode.Recap, MediaSegmentType.Recap),
|
|
|
|
(AnalysisMode.Preview, MediaSegmentType.Preview),
|
|
|
|
(AnalysisMode.Credits, MediaSegmentType.Outro)
|
|
|
|
};
|
|
|
|
|
|
|
|
foreach (var (mode, type) in segmentMappings)
|
|
|
|
{
|
|
|
|
if (itemSegments.TryGetValue(mode, out var segment) && segment.Valid)
|
2024-10-11 21:08:12 +02:00
|
|
|
{
|
2024-11-21 15:42:55 +01:00
|
|
|
long startTicks = (long)(segment.Start * TimeSpan.TicksPerSecond);
|
|
|
|
long endTicks = CalculateEndTicks(mode, segment, runTimeTicks, remainingTicks);
|
|
|
|
|
|
|
|
segments.Add(new MediaSegmentDto
|
|
|
|
{
|
|
|
|
StartTicks = startTicks,
|
|
|
|
EndTicks = endTicks,
|
|
|
|
ItemId = request.ItemId,
|
|
|
|
Type = type
|
|
|
|
});
|
|
|
|
}
|
2024-10-11 21:08:12 +02:00
|
|
|
}
|
|
|
|
|
2024-11-21 15:42:55 +01:00
|
|
|
return Task.FromResult<IReadOnlyList<MediaSegmentDto>>(segments);
|
|
|
|
}
|
2024-11-02 18:17:22 +01:00
|
|
|
|
2024-11-21 15:42:55 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Calculates the end ticks based on the segment type and runtime.
|
|
|
|
/// </summary>
|
|
|
|
private static long CalculateEndTicks(AnalysisMode mode, Segment segment, long runTimeTicks, long remainingTicks)
|
|
|
|
{
|
|
|
|
long endTicks = (long)(segment.End * TimeSpan.TicksPerSecond);
|
|
|
|
|
|
|
|
if (mode is AnalysisMode.Preview or AnalysisMode.Credits)
|
|
|
|
{
|
|
|
|
if (runTimeTicks > 0 && runTimeTicks < endTicks + TimeSpan.TicksPerSecond)
|
2024-10-20 13:27:05 +02:00
|
|
|
{
|
2024-11-21 15:42:55 +01:00
|
|
|
return Math.Max(runTimeTicks, endTicks);
|
|
|
|
}
|
|
|
|
|
|
|
|
return endTicks - remainingTicks;
|
2024-10-11 21:08:12 +02:00
|
|
|
}
|
|
|
|
|
2024-11-21 15:42:55 +01:00
|
|
|
return endTicks - remainingTicks;
|
2024-10-11 21:08:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc/>
|
2024-10-20 13:27:05 +02:00
|
|
|
public ValueTask<bool> Supports(BaseItem item) => ValueTask.FromResult(item is Episode or Movie);
|
2024-10-11 21:08:12 +02:00
|
|
|
}
|
|
|
|
}
|