intro-skipper/IntroSkipper/Providers/SegmentProvider.cs

73 lines
2.9 KiB
C#
Raw Normal View History

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
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using IntroSkipper.Data;
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)
{
ArgumentNullException.ThrowIfNull(request);
ArgumentNullException.ThrowIfNull(Plugin.Instance);
var segments = new List<MediaSegmentDto>();
var remainingTicks = Plugin.Instance.Configuration.RemainingSecondsOfIntro * TimeSpan.TicksPerSecond;
var itemSegments = Plugin.Instance.GetSegmentsById(request.ItemId);
// Add intro segment if found
if (itemSegments.TryGetValue(AnalysisMode.Introduction, out var introSegment) && introSegment.Valid)
{
segments.Add(new MediaSegmentDto
{
StartTicks = (long)(introSegment.Start * TimeSpan.TicksPerSecond),
EndTicks = (long)(introSegment.End * TimeSpan.TicksPerSecond) - remainingTicks,
ItemId = request.ItemId,
Type = MediaSegmentType.Intro
});
}
// Add outro/credits segment if found
if (itemSegments.TryGetValue(AnalysisMode.Introduction, out var creditSegment) && creditSegment.Valid)
{
var creditEndTicks = (long)(creditSegment.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)(creditSegment.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);
}
}