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;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
using ConfusedPolarBear.Plugin.IntroSkipper.Configuration;
|
using ConfusedPolarBear.Plugin.IntroSkipper.Configuration;
|
||||||
using ConfusedPolarBear.Plugin.IntroSkipper.Data;
|
using ConfusedPolarBear.Plugin.IntroSkipper.Data;
|
||||||
using MediaBrowser.Model.Entities;
|
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
namespace ConfusedPolarBear.Plugin.IntroSkipper;
|
namespace ConfusedPolarBear.Plugin.IntroSkipper;
|
||||||
@ -38,37 +38,22 @@ public class AnalyzerHelper
|
|||||||
IReadOnlyDictionary<Guid, Segment> originalIntros,
|
IReadOnlyDictionary<Guid, Segment> originalIntros,
|
||||||
AnalysisMode mode)
|
AnalysisMode mode)
|
||||||
{
|
{
|
||||||
var modifiedIntros = new Dictionary<Guid, Segment>();
|
return episodes
|
||||||
|
.Where(episode => originalIntros.TryGetValue(episode.EpisodeId, out var _))
|
||||||
foreach (var episode in episodes)
|
.ToDictionary(
|
||||||
{
|
episode => episode.EpisodeId,
|
||||||
_logger.LogTrace("Adjusting introduction end time for {Name} ({Id})", episode.Name, episode.EpisodeId);
|
episode => AdjustIntroForEpisode(episode, originalIntros[episode.EpisodeId], mode));
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private Segment AdjustIntroForEpisode(QueuedEpisode episode, Segment originalIntro, AnalysisMode mode)
|
private Segment AdjustIntroForEpisode(QueuedEpisode episode, Segment originalIntro, AnalysisMode mode)
|
||||||
{
|
{
|
||||||
var chapters = GetChaptersWithVirtualEnd(episode);
|
_logger.LogTrace("{Name} original intro: {Start} - {End}", episode.Name, originalIntro.Start, originalIntro.End);
|
||||||
var adjustedIntro = new Segment(originalIntro);
|
|
||||||
|
|
||||||
|
var adjustedIntro = new Segment(originalIntro);
|
||||||
var originalIntroStart = new TimeRange(Math.Max(0, (int)originalIntro.Start - 5), (int)originalIntro.Start + 10);
|
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));
|
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, adjustedIntro, originalIntroStart, originalIntroEnd) && mode == AnalysisMode.Introduction)
|
||||||
|
|
||||||
if (!AdjustIntroBasedOnChapters(episode, chapters, adjustedIntro, originalIntroStart, originalIntroEnd)
|
|
||||||
&& mode == AnalysisMode.Introduction)
|
|
||||||
{
|
{
|
||||||
AdjustIntroBasedOnSilence(episode, adjustedIntro, originalIntroEnd);
|
AdjustIntroBasedOnSilence(episode, adjustedIntro, originalIntroEnd);
|
||||||
}
|
}
|
||||||
@ -76,31 +61,31 @@ public class AnalyzerHelper
|
|||||||
return adjustedIntro;
|
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) ?? [];
|
var chapters = Plugin.Instance?.GetChapters(episode.EpisodeId) ?? [];
|
||||||
chapters.Add(new ChapterInfo { StartPositionTicks = TimeSpan.FromSeconds(episode.Duration).Ticks });
|
double previousTime = 0;
|
||||||
return chapters;
|
|
||||||
|
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)
|
adjustedIntro.End = currentTime;
|
||||||
{
|
_logger.LogTrace("{Name} chapter found close to intro end: {End}", episode.Name, currentTime);
|
||||||
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);
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
previousTime = currentTime;
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
|
@ -52,7 +52,7 @@ public class ChapterAnalyzer(ILogger<ChapterAnalyzer> logger) : IMediaFileAnalyz
|
|||||||
|
|
||||||
var skipRange = FindMatchingChapter(
|
var skipRange = FindMatchingChapter(
|
||||||
episode,
|
episode,
|
||||||
new(Plugin.Instance.GetChapters(episode.EpisodeId)),
|
Plugin.Instance.GetChapters(episode.EpisodeId),
|
||||||
expression,
|
expression,
|
||||||
mode);
|
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>
|
/// <returns>Intro object containing skippable time range, or null if no chapter matched.</returns>
|
||||||
public Segment? FindMatchingChapter(
|
public Segment? FindMatchingChapter(
|
||||||
QueuedEpisode episode,
|
QueuedEpisode episode,
|
||||||
Collection<ChapterInfo> chapters,
|
IReadOnlyList<ChapterInfo> chapters,
|
||||||
string expression,
|
string expression,
|
||||||
AnalysisMode mode)
|
AnalysisMode mode)
|
||||||
{
|
{
|
||||||
|
@ -350,7 +350,7 @@ public class Plugin : BasePlugin<PluginConfiguration>, IHasWebPages
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="id">Item id.</param>
|
/// <param name="id">Item id.</param>
|
||||||
/// <returns>List of chapters.</returns>
|
/// <returns>List of chapters.</returns>
|
||||||
internal List<ChapterInfo> GetChapters(Guid id)
|
internal IReadOnlyList<ChapterInfo> GetChapters(Guid id)
|
||||||
{
|
{
|
||||||
var item = GetItem(id);
|
var item = GetItem(id);
|
||||||
if (item == null)
|
if (item == null)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user