Update AnalyzerHelper (#335)
* Update AnalyzerHelper * single loop * Update AnalyzerHelper.cs --------- Co-authored-by: rlauu <46294892+rlauu@users.noreply.github.com>
This commit is contained in:
parent
93f0bee301
commit
3c02426532
@ -1,8 +1,8 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using ConfusedPolarBear.Plugin.IntroSkipper.Configuration;
|
||||
using ConfusedPolarBear.Plugin.IntroSkipper.Data;
|
||||
using MediaBrowser.Model.Entities;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace ConfusedPolarBear.Plugin.IntroSkipper;
|
||||
@ -38,37 +38,22 @@ public class AnalyzerHelper
|
||||
IReadOnlyDictionary<Guid, Segment> originalIntros,
|
||||
AnalysisMode mode)
|
||||
{
|
||||
var modifiedIntros = new Dictionary<Guid, Segment>();
|
||||
|
||||
foreach (var episode in episodes)
|
||||
{
|
||||
_logger.LogTrace("Adjusting introduction end time for {Name} ({Id})", episode.Name, episode.EpisodeId);
|
||||
|
||||
if (!originalIntros.TryGetValue(episode.EpisodeId, out var originalIntro))
|
||||
{
|
||||
_logger.LogTrace("{Name} does not have an intro", episode.Name);
|
||||
continue;
|
||||
}
|
||||
|
||||
var adjustedIntro = AdjustIntroForEpisode(episode, originalIntro, mode);
|
||||
modifiedIntros[episode.EpisodeId] = adjustedIntro;
|
||||
}
|
||||
|
||||
return modifiedIntros;
|
||||
return episodes
|
||||
.Where(episode => originalIntros.TryGetValue(episode.EpisodeId, out var _))
|
||||
.ToDictionary(
|
||||
episode => episode.EpisodeId,
|
||||
episode => AdjustIntroForEpisode(episode, originalIntros[episode.EpisodeId], mode));
|
||||
}
|
||||
|
||||
private Segment AdjustIntroForEpisode(QueuedEpisode episode, Segment originalIntro, AnalysisMode mode)
|
||||
{
|
||||
var chapters = GetChaptersWithVirtualEnd(episode);
|
||||
var adjustedIntro = new Segment(originalIntro);
|
||||
_logger.LogTrace("{Name} original intro: {Start} - {End}", episode.Name, originalIntro.Start, originalIntro.End);
|
||||
|
||||
var adjustedIntro = new Segment(originalIntro);
|
||||
var originalIntroStart = new TimeRange(Math.Max(0, (int)originalIntro.Start - 5), (int)originalIntro.Start + 10);
|
||||
var originalIntroEnd = new TimeRange((int)originalIntro.End - 10, Math.Min(episode.Duration, (int)originalIntro.End + 5));
|
||||
|
||||
_logger.LogTrace("{Name} original intro: {Start} - {End}", episode.Name, originalIntro.Start, originalIntro.End);
|
||||
|
||||
if (!AdjustIntroBasedOnChapters(episode, chapters, adjustedIntro, originalIntroStart, originalIntroEnd)
|
||||
&& mode == AnalysisMode.Introduction)
|
||||
if (!AdjustIntroBasedOnChapters(episode, adjustedIntro, originalIntroStart, originalIntroEnd) && mode == AnalysisMode.Introduction)
|
||||
{
|
||||
AdjustIntroBasedOnSilence(episode, adjustedIntro, originalIntroEnd);
|
||||
}
|
||||
@ -76,31 +61,31 @@ public class AnalyzerHelper
|
||||
return adjustedIntro;
|
||||
}
|
||||
|
||||
private static List<ChapterInfo> GetChaptersWithVirtualEnd(QueuedEpisode episode)
|
||||
private bool AdjustIntroBasedOnChapters(QueuedEpisode episode, Segment adjustedIntro, TimeRange originalIntroStart, TimeRange originalIntroEnd)
|
||||
{
|
||||
var chapters = Plugin.Instance?.GetChapters(episode.EpisodeId) ?? [];
|
||||
chapters.Add(new ChapterInfo { StartPositionTicks = TimeSpan.FromSeconds(episode.Duration).Ticks });
|
||||
return chapters;
|
||||
double previousTime = 0;
|
||||
|
||||
for (int i = 0; i <= chapters.Count; i++)
|
||||
{
|
||||
double currentTime = i < chapters.Count
|
||||
? TimeSpan.FromTicks(chapters[i].StartPositionTicks).TotalSeconds
|
||||
: episode.Duration;
|
||||
|
||||
if (originalIntroStart.Start < previousTime && previousTime < originalIntroStart.End)
|
||||
{
|
||||
adjustedIntro.Start = previousTime;
|
||||
_logger.LogTrace("{Name} chapter found close to intro start: {Start}", episode.Name, previousTime);
|
||||
}
|
||||
|
||||
private bool AdjustIntroBasedOnChapters(QueuedEpisode episode, List<ChapterInfo> chapters, Segment adjustedIntro, TimeRange originalIntroStart, TimeRange originalIntroEnd)
|
||||
if (originalIntroEnd.Start < currentTime && currentTime < originalIntroEnd.End)
|
||||
{
|
||||
foreach (var chapter in chapters)
|
||||
{
|
||||
var chapterStartSeconds = TimeSpan.FromTicks(chapter.StartPositionTicks).TotalSeconds;
|
||||
|
||||
if (originalIntroStart.Start < chapterStartSeconds && chapterStartSeconds < originalIntroStart.End)
|
||||
{
|
||||
adjustedIntro.Start = chapterStartSeconds;
|
||||
_logger.LogTrace("{Name} chapter found close to intro start: {Start}", episode.Name, chapterStartSeconds);
|
||||
}
|
||||
|
||||
if (originalIntroEnd.Start < chapterStartSeconds && chapterStartSeconds < originalIntroEnd.End)
|
||||
{
|
||||
adjustedIntro.End = chapterStartSeconds;
|
||||
_logger.LogTrace("{Name} chapter found close to intro end: {End}", episode.Name, chapterStartSeconds);
|
||||
adjustedIntro.End = currentTime;
|
||||
_logger.LogTrace("{Name} chapter found close to intro end: {End}", episode.Name, currentTime);
|
||||
return true;
|
||||
}
|
||||
|
||||
previousTime = currentTime;
|
||||
}
|
||||
|
||||
return false;
|
||||
|
@ -52,7 +52,7 @@ public class ChapterAnalyzer(ILogger<ChapterAnalyzer> logger) : IMediaFileAnalyz
|
||||
|
||||
var skipRange = FindMatchingChapter(
|
||||
episode,
|
||||
new(Plugin.Instance.GetChapters(episode.EpisodeId)),
|
||||
Plugin.Instance.GetChapters(episode.EpisodeId),
|
||||
expression,
|
||||
mode);
|
||||
|
||||
@ -81,7 +81,7 @@ public class ChapterAnalyzer(ILogger<ChapterAnalyzer> logger) : IMediaFileAnalyz
|
||||
/// <returns>Intro object containing skippable time range, or null if no chapter matched.</returns>
|
||||
public Segment? FindMatchingChapter(
|
||||
QueuedEpisode episode,
|
||||
Collection<ChapterInfo> chapters,
|
||||
IReadOnlyList<ChapterInfo> chapters,
|
||||
string expression,
|
||||
AnalysisMode mode)
|
||||
{
|
||||
|
@ -350,7 +350,7 @@ public class Plugin : BasePlugin<PluginConfiguration>, IHasWebPages
|
||||
/// </summary>
|
||||
/// <param name="id">Item id.</param>
|
||||
/// <returns>List of chapters.</returns>
|
||||
internal List<ChapterInfo> GetChapters(Guid id)
|
||||
internal IReadOnlyList<ChapterInfo> GetChapters(Guid id)
|
||||
{
|
||||
var item = GetItem(id);
|
||||
if (item == null)
|
||||
|
Loading…
x
Reference in New Issue
Block a user