using System; using System.Collections.Generic; using System.IO; using ConfusedPolarBear.Plugin.IntroSkipper.Configuration; using MediaBrowser.Common.Configuration; using MediaBrowser.Common.Plugins; using MediaBrowser.Controller.Configuration; using MediaBrowser.Controller.Library; using MediaBrowser.Model.Plugins; using MediaBrowser.Model.Serialization; namespace ConfusedPolarBear.Plugin.IntroSkipper; /// /// Intro skipper plugin. Uses audio analysis to find common sequences of audio shared between episodes. /// public class Plugin : BasePlugin, IHasWebPages { private readonly object _serializationLock = new object(); private IXmlSerializer _xmlSerializer; private ILibraryManager _libraryManager; private string _introPath; /// /// Initializes a new instance of the class. /// /// Instance of the interface. /// Instance of the interface. /// Server configuration manager. /// Library manager. public Plugin( IApplicationPaths applicationPaths, IXmlSerializer xmlSerializer, IServerConfigurationManager serverConfiguration, ILibraryManager libraryManager) : base(applicationPaths, xmlSerializer) { _xmlSerializer = xmlSerializer; _libraryManager = libraryManager; // Create the base & cache directories (if needed). FingerprintCachePath = Path.Join(applicationPaths.PluginConfigurationsPath, "intros", "cache"); if (!Directory.Exists(FingerprintCachePath)) { Directory.CreateDirectory(FingerprintCachePath); } _introPath = Path.Join(applicationPaths.PluginConfigurationsPath, "intros", "intros.xml"); // Get the path to FFmpeg. FFmpegPath = serverConfiguration.GetEncodingOptions().EncoderAppPathDisplay; Intros = new Dictionary(); AnalysisQueue = new Dictionary>(); Instance = this; ConfigurationChanged += OnConfigurationChanged; RestoreTimestamps(); } /// /// Fired after configuration has been saved so the auto skip timer can be stopped or started. /// public event EventHandler? AutoSkipChanged; /// /// Gets the results of fingerprinting all episodes. /// public Dictionary Intros { get; } /// /// Gets the mapping of season ids to episodes that have been queued for fingerprinting. /// public Dictionary> AnalysisQueue { get; } /// /// Gets or sets the total number of episodes in the queue. /// public int TotalQueued { get; set; } /// /// Gets or sets the statistics from the most recent analysis run. /// public AnalysisStatistics AnalysisStatistics { get; set; } = new AnalysisStatistics(); /// /// Gets the directory to cache fingerprints in. /// public string FingerprintCachePath { get; private set; } /// /// Gets the full path to FFmpeg. /// public string FFmpegPath { get; private set; } /// public override string Name => "Intro Skipper"; /// public override Guid Id => Guid.Parse("c83d86bb-a1e0-4c35-a113-e2101cf4ee6b"); /// /// Gets the plugin instance. /// public static Plugin? Instance { get; private set; } /// /// Save timestamps to disk. /// public void SaveTimestamps() { lock (_serializationLock) { var introList = new List(); foreach (var intro in Plugin.Instance!.Intros) { introList.Add(intro.Value); } _xmlSerializer.SerializeToFile(introList, _introPath); } } /// /// Restore previous analysis results from disk. /// public void RestoreTimestamps() { if (!File.Exists(_introPath)) { return; } // Since dictionaries can't be easily serialized, analysis results are stored on disk as a list. var introList = (List)_xmlSerializer.DeserializeFromFile(typeof(List), _introPath); foreach (var intro in introList) { Plugin.Instance!.Intros[intro.EpisodeId] = intro; } } /// /// Gets the full path for an item. /// /// Item id. /// Full path to item. internal string GetItemPath(Guid id) { return _libraryManager.GetItemById(id).Path; } /// public IEnumerable GetPages() { return new[] { new PluginPageInfo { Name = this.Name, EmbeddedResourcePath = GetType().Namespace + ".Configuration.configPage.html" }, new PluginPageInfo { Name = "visualizer.js", EmbeddedResourcePath = GetType().Namespace + ".Configuration.visualizer.js" } }; } private void OnConfigurationChanged(object? sender, BasePluginConfiguration e) { AutoSkipChanged?.Invoke(this, EventArgs.Empty); } }