2019-02-21 01:57:43 -08:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2022-05-05 18:10:34 -05:00
|
|
|
using System.IO;
|
2022-11-06 21:20:52 -06:00
|
|
|
using System.Text.RegularExpressions;
|
2022-04-29 23:52:50 -05:00
|
|
|
using ConfusedPolarBear.Plugin.IntroSkipper.Configuration;
|
2019-02-21 01:57:43 -08:00
|
|
|
using MediaBrowser.Common.Configuration;
|
|
|
|
using MediaBrowser.Common.Plugins;
|
2022-06-09 14:07:40 -05:00
|
|
|
using MediaBrowser.Controller.Configuration;
|
2022-07-29 03:34:55 -05:00
|
|
|
using MediaBrowser.Controller.Entities;
|
2022-10-31 01:00:39 -05:00
|
|
|
using MediaBrowser.Controller.Entities.TV;
|
2022-06-15 01:00:03 -05:00
|
|
|
using MediaBrowser.Controller.Library;
|
2022-11-24 00:43:23 -06:00
|
|
|
using MediaBrowser.Controller.Persistence;
|
|
|
|
using MediaBrowser.Model.Entities;
|
2019-02-21 01:57:43 -08:00
|
|
|
using MediaBrowser.Model.Plugins;
|
|
|
|
using MediaBrowser.Model.Serialization;
|
2022-09-27 20:31:18 -05:00
|
|
|
using Microsoft.Extensions.Logging;
|
2019-02-21 01:57:43 -08:00
|
|
|
|
2022-04-29 23:52:50 -05:00
|
|
|
namespace ConfusedPolarBear.Plugin.IntroSkipper;
|
2021-12-13 16:58:05 -07:00
|
|
|
|
|
|
|
/// <summary>
|
2022-05-01 01:24:57 -05:00
|
|
|
/// Intro skipper plugin. Uses audio analysis to find common sequences of audio shared between episodes.
|
2021-12-13 16:58:05 -07:00
|
|
|
/// </summary>
|
|
|
|
public class Plugin : BasePlugin<PluginConfiguration>, IHasWebPages
|
2019-02-21 01:57:43 -08:00
|
|
|
{
|
2022-10-28 02:25:57 -05:00
|
|
|
private readonly object _serializationLock = new();
|
|
|
|
private readonly object _introsLock = new();
|
2022-05-05 18:10:34 -05:00
|
|
|
private IXmlSerializer _xmlSerializer;
|
2022-06-15 01:00:03 -05:00
|
|
|
private ILibraryManager _libraryManager;
|
2022-11-24 00:43:23 -06:00
|
|
|
private IItemRepository _itemRepository;
|
2022-09-27 20:31:18 -05:00
|
|
|
private ILogger<Plugin> _logger;
|
2022-05-05 18:10:34 -05:00
|
|
|
private string _introPath;
|
2022-11-24 00:43:23 -06:00
|
|
|
private string _creditsPath;
|
2022-05-05 18:10:34 -05:00
|
|
|
|
2022-05-01 01:24:57 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the <see cref="Plugin"/> class.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="applicationPaths">Instance of the <see cref="IApplicationPaths"/> interface.</param>
|
|
|
|
/// <param name="xmlSerializer">Instance of the <see cref="IXmlSerializer"/> interface.</param>
|
2022-06-09 14:07:40 -05:00
|
|
|
/// <param name="serverConfiguration">Server configuration manager.</param>
|
2022-06-15 01:00:03 -05:00
|
|
|
/// <param name="libraryManager">Library manager.</param>
|
2022-11-24 00:43:23 -06:00
|
|
|
/// <param name="itemRepository">Item repository.</param>
|
2022-09-27 20:31:18 -05:00
|
|
|
/// <param name="logger">Logger.</param>
|
2022-06-09 14:07:40 -05:00
|
|
|
public Plugin(
|
|
|
|
IApplicationPaths applicationPaths,
|
|
|
|
IXmlSerializer xmlSerializer,
|
2022-06-15 01:00:03 -05:00
|
|
|
IServerConfigurationManager serverConfiguration,
|
2022-09-27 20:31:18 -05:00
|
|
|
ILibraryManager libraryManager,
|
2022-11-24 00:43:23 -06:00
|
|
|
IItemRepository itemRepository,
|
2022-09-27 20:31:18 -05:00
|
|
|
ILogger<Plugin> logger)
|
2022-05-01 01:24:57 -05:00
|
|
|
: base(applicationPaths, xmlSerializer)
|
|
|
|
{
|
2022-09-27 21:03:27 -05:00
|
|
|
Instance = this;
|
|
|
|
|
2022-05-05 18:10:34 -05:00
|
|
|
_xmlSerializer = xmlSerializer;
|
2022-06-15 01:00:03 -05:00
|
|
|
_libraryManager = libraryManager;
|
2022-11-24 00:43:23 -06:00
|
|
|
_itemRepository = itemRepository;
|
2022-09-27 20:31:18 -05:00
|
|
|
_logger = logger;
|
2022-05-05 18:10:34 -05:00
|
|
|
|
2022-09-27 21:03:27 -05:00
|
|
|
FFmpegPath = serverConfiguration.GetEncodingOptions().EncoderAppPathDisplay;
|
2022-11-06 21:20:52 -06:00
|
|
|
|
|
|
|
var introsDirectory = Path.Join(applicationPaths.PluginConfigurationsPath, "intros");
|
|
|
|
FingerprintCachePath = Path.Join(introsDirectory, "cache");
|
2022-09-27 21:03:27 -05:00
|
|
|
_introPath = Path.Join(applicationPaths.PluginConfigurationsPath, "intros", "intros.xml");
|
2022-11-24 00:43:23 -06:00
|
|
|
_creditsPath = Path.Join(applicationPaths.PluginConfigurationsPath, "intros", "credits.xml");
|
2022-09-27 21:03:27 -05:00
|
|
|
|
|
|
|
// Create the base & cache directories (if needed).
|
2022-05-05 18:10:34 -05:00
|
|
|
if (!Directory.Exists(FingerprintCachePath))
|
|
|
|
{
|
|
|
|
Directory.CreateDirectory(FingerprintCachePath);
|
|
|
|
}
|
|
|
|
|
2022-06-14 14:36:05 -05:00
|
|
|
ConfigurationChanged += OnConfigurationChanged;
|
|
|
|
|
2022-09-27 21:03:27 -05:00
|
|
|
// TODO: remove when https://github.com/jellyfin/jellyfin-meta/discussions/30 is complete
|
2022-09-27 20:31:18 -05:00
|
|
|
try
|
|
|
|
{
|
|
|
|
RestoreTimestamps();
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
_logger.LogWarning("Unable to load introduction timestamps: {Exception}", ex);
|
|
|
|
}
|
2022-11-06 21:20:52 -06:00
|
|
|
|
|
|
|
// Inject the skip intro button code into the web interface.
|
|
|
|
var indexPath = Path.Join(applicationPaths.WebPath, "index.html");
|
|
|
|
try
|
|
|
|
{
|
2024-03-01 16:53:41 -05:00
|
|
|
InjectSkipButton(indexPath);
|
2022-11-06 21:20:52 -06:00
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
2022-11-06 21:25:23 -06:00
|
|
|
WarningManager.SetFlag(PluginWarning.UnableToAddSkipButton);
|
2022-11-06 21:20:52 -06:00
|
|
|
|
|
|
|
if (ex is UnauthorizedAccessException)
|
|
|
|
{
|
|
|
|
var suggestion = OperatingSystem.IsLinux() ?
|
|
|
|
"running `sudo chown jellyfin PATH` (if this is a native installation)" :
|
|
|
|
"changing the permissions of PATH";
|
|
|
|
|
|
|
|
suggestion = suggestion.Replace("PATH", indexPath, StringComparison.Ordinal);
|
|
|
|
|
|
|
|
_logger.LogError(
|
|
|
|
"Failed to add skip button to web interface. Try {Suggestion} and restarting the server. Error: {Error}",
|
|
|
|
suggestion,
|
|
|
|
ex);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_logger.LogError("Unknown error encountered while adding skip button: {Error}", ex);
|
|
|
|
}
|
|
|
|
}
|
2022-05-05 18:10:34 -05:00
|
|
|
}
|
|
|
|
|
2022-06-14 14:36:05 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Fired after configuration has been saved so the auto skip timer can be stopped or started.
|
|
|
|
/// </summary>
|
|
|
|
public event EventHandler? AutoSkipChanged;
|
|
|
|
|
2022-05-09 22:56:03 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Gets the results of fingerprinting all episodes.
|
|
|
|
/// </summary>
|
2022-09-27 21:03:27 -05:00
|
|
|
public Dictionary<Guid, Intro> Intros { get; } = new();
|
2022-05-09 22:56:03 -05:00
|
|
|
|
|
|
|
/// <summary>
|
2022-11-24 00:43:23 -06:00
|
|
|
/// Gets all discovered ending credits.
|
2022-05-09 22:56:03 -05:00
|
|
|
/// </summary>
|
2022-11-24 00:43:23 -06:00
|
|
|
public Dictionary<Guid, Intro> Credits { get; } = new();
|
|
|
|
|
2022-05-09 22:56:03 -05:00
|
|
|
/// <summary>
|
2022-11-23 02:34:28 -06:00
|
|
|
/// Gets the most recent media item queue.
|
2022-05-09 22:56:03 -05:00
|
|
|
/// </summary>
|
2022-11-23 02:34:28 -06:00
|
|
|
public Dictionary<Guid, List<QueuedEpisode>> QueuedMediaItems { get; } = new();
|
2022-05-09 22:56:03 -05:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets the total number of episodes in the queue.
|
|
|
|
/// </summary>
|
|
|
|
public int TotalQueued { get; set; }
|
|
|
|
|
2023-06-08 00:51:18 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets the number of seasons in the queue.
|
|
|
|
/// </summary>
|
|
|
|
public int TotalSeasons { get; set; }
|
|
|
|
|
2022-05-09 22:56:03 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Gets the directory to cache fingerprints in.
|
|
|
|
/// </summary>
|
|
|
|
public string FingerprintCachePath { get; private set; }
|
|
|
|
|
2022-06-09 14:07:40 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Gets the full path to FFmpeg.
|
|
|
|
/// </summary>
|
|
|
|
public string FFmpegPath { get; private set; }
|
|
|
|
|
2022-05-09 22:56:03 -05:00
|
|
|
/// <inheritdoc />
|
|
|
|
public override string Name => "Intro Skipper";
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
public override Guid Id => Guid.Parse("c83d86bb-a1e0-4c35-a113-e2101cf4ee6b");
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets the plugin instance.
|
|
|
|
/// </summary>
|
|
|
|
public static Plugin? Instance { get; private set; }
|
|
|
|
|
2022-05-05 18:10:34 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Save timestamps to disk.
|
|
|
|
/// </summary>
|
|
|
|
public void SaveTimestamps()
|
|
|
|
{
|
2022-07-17 01:54:05 -05:00
|
|
|
lock (_serializationLock)
|
2022-05-05 18:10:34 -05:00
|
|
|
{
|
2022-07-17 01:54:05 -05:00
|
|
|
var introList = new List<Intro>();
|
2022-05-05 18:10:34 -05:00
|
|
|
|
2022-11-24 00:43:23 -06:00
|
|
|
// Serialize intros
|
2022-07-17 01:54:05 -05:00
|
|
|
foreach (var intro in Plugin.Instance!.Intros)
|
|
|
|
{
|
|
|
|
introList.Add(intro.Value);
|
|
|
|
}
|
|
|
|
|
|
|
|
_xmlSerializer.SerializeToFile(introList, _introPath);
|
2022-11-24 00:43:23 -06:00
|
|
|
|
|
|
|
// Serialize credits
|
|
|
|
introList.Clear();
|
|
|
|
|
|
|
|
foreach (var intro in Plugin.Instance!.Credits)
|
|
|
|
{
|
|
|
|
introList.Add(intro.Value);
|
|
|
|
}
|
|
|
|
|
|
|
|
_xmlSerializer.SerializeToFile(introList, _creditsPath);
|
2022-07-17 01:54:05 -05:00
|
|
|
}
|
2022-05-05 18:10:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Restore previous analysis results from disk.
|
|
|
|
/// </summary>
|
|
|
|
public void RestoreTimestamps()
|
|
|
|
{
|
2022-11-24 00:43:23 -06:00
|
|
|
if (File.Exists(_introPath))
|
2022-05-05 18:10:34 -05:00
|
|
|
{
|
2022-11-24 00:43:23 -06:00
|
|
|
// Since dictionaries can't be easily serialized, analysis results are stored on disk as a list.
|
|
|
|
var introList = (List<Intro>)_xmlSerializer.DeserializeFromFile(
|
|
|
|
typeof(List<Intro>),
|
|
|
|
_introPath);
|
2022-05-05 18:10:34 -05:00
|
|
|
|
2022-11-24 00:43:23 -06:00
|
|
|
foreach (var intro in introList)
|
|
|
|
{
|
|
|
|
Plugin.Instance!.Intros[intro.EpisodeId] = intro;
|
|
|
|
}
|
|
|
|
}
|
2022-05-05 18:10:34 -05:00
|
|
|
|
2022-11-24 00:43:23 -06:00
|
|
|
if (File.Exists(_creditsPath))
|
2022-05-05 18:10:34 -05:00
|
|
|
{
|
2022-11-24 00:43:23 -06:00
|
|
|
var creditList = (List<Intro>)_xmlSerializer.DeserializeFromFile(
|
|
|
|
typeof(List<Intro>),
|
|
|
|
_creditsPath);
|
|
|
|
|
|
|
|
foreach (var credit in creditList)
|
|
|
|
{
|
|
|
|
Plugin.Instance!.Credits[credit.EpisodeId] = credit;
|
|
|
|
}
|
2022-05-05 18:10:34 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-06 21:20:52 -06:00
|
|
|
/// <inheritdoc />
|
|
|
|
public IEnumerable<PluginPageInfo> 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"
|
|
|
|
},
|
|
|
|
new PluginPageInfo
|
|
|
|
{
|
|
|
|
Name = "skip-intro-button.js",
|
|
|
|
EmbeddedResourcePath = GetType().Namespace + ".Configuration.inject.js"
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-11-23 01:20:48 -06:00
|
|
|
/// <summary>
|
|
|
|
/// Gets the commit used to build the plugin.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>Commit.</returns>
|
|
|
|
public string GetCommit()
|
|
|
|
{
|
|
|
|
var commit = string.Empty;
|
|
|
|
|
|
|
|
var path = GetType().Namespace + ".Configuration.version.txt";
|
|
|
|
using var stream = GetType().Assembly.GetManifestResourceStream(path);
|
|
|
|
if (stream is null)
|
|
|
|
{
|
|
|
|
_logger.LogWarning("Unable to read embedded version information");
|
|
|
|
return commit;
|
|
|
|
}
|
|
|
|
|
|
|
|
using var reader = new StreamReader(stream);
|
|
|
|
commit = reader.ReadToEnd().TrimEnd();
|
|
|
|
|
|
|
|
if (commit == "unknown")
|
|
|
|
{
|
|
|
|
_logger.LogTrace("Embedded version information was not valid, ignoring");
|
|
|
|
return string.Empty;
|
|
|
|
}
|
|
|
|
|
|
|
|
_logger.LogInformation("Unstable plugin version built from commit {Commit}", commit);
|
|
|
|
return commit;
|
|
|
|
}
|
|
|
|
|
2022-07-29 03:34:55 -05:00
|
|
|
internal BaseItem GetItem(Guid id)
|
|
|
|
{
|
|
|
|
return _libraryManager.GetItemById(id);
|
|
|
|
}
|
|
|
|
|
2022-06-15 01:00:03 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Gets the full path for an item.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="id">Item id.</param>
|
|
|
|
/// <returns>Full path to item.</returns>
|
|
|
|
internal string GetItemPath(Guid id)
|
|
|
|
{
|
2022-07-29 03:34:55 -05:00
|
|
|
return GetItem(id).Path;
|
2022-06-15 01:00:03 -05:00
|
|
|
}
|
|
|
|
|
2022-11-24 00:43:23 -06:00
|
|
|
/// <summary>
|
|
|
|
/// Gets all chapters for this item.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="id">Item id.</param>
|
|
|
|
/// <returns>List of chapters.</returns>
|
|
|
|
internal List<ChapterInfo> GetChapters(Guid id)
|
2022-10-28 02:25:57 -05:00
|
|
|
{
|
2022-11-24 00:43:23 -06:00
|
|
|
return _itemRepository.GetChapters(GetItem(id));
|
|
|
|
}
|
|
|
|
|
|
|
|
internal void UpdateTimestamps(Dictionary<Guid, Intro> newTimestamps, AnalysisMode mode)
|
2022-10-28 02:25:57 -05:00
|
|
|
{
|
|
|
|
lock (_introsLock)
|
|
|
|
{
|
2022-11-24 00:43:23 -06:00
|
|
|
foreach (var intro in newTimestamps)
|
2022-10-28 02:25:57 -05:00
|
|
|
{
|
2022-11-24 00:43:23 -06:00
|
|
|
if (mode == AnalysisMode.Introduction)
|
2022-10-31 01:00:39 -05:00
|
|
|
{
|
2022-11-24 00:43:23 -06:00
|
|
|
Plugin.Instance!.Intros[intro.Key] = intro.Value;
|
2022-10-31 01:00:39 -05:00
|
|
|
}
|
2022-11-24 00:43:23 -06:00
|
|
|
else if (mode == AnalysisMode.Credits)
|
2022-10-31 01:00:39 -05:00
|
|
|
{
|
2022-11-24 00:43:23 -06:00
|
|
|
Plugin.Instance!.Credits[intro.Key] = intro.Value;
|
2022-10-31 01:00:39 -05:00
|
|
|
}
|
2022-10-28 02:25:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
Plugin.Instance!.SaveTimestamps();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-06 21:20:52 -06:00
|
|
|
private void OnConfigurationChanged(object? sender, BasePluginConfiguration e)
|
2022-05-05 18:10:34 -05:00
|
|
|
{
|
2022-11-06 21:20:52 -06:00
|
|
|
AutoSkipChanged?.Invoke(this, EventArgs.Empty);
|
2022-05-01 01:24:57 -05:00
|
|
|
}
|
2022-06-14 14:36:05 -05:00
|
|
|
|
2022-11-06 21:20:52 -06:00
|
|
|
/// <summary>
|
|
|
|
/// Inject the skip button script into the web interface.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="indexPath">Full path to index.html.</param>
|
2024-03-01 16:53:41 -05:00
|
|
|
private void InjectSkipButton(string indexPath)
|
2022-06-14 14:36:05 -05:00
|
|
|
{
|
2022-11-06 21:20:52 -06:00
|
|
|
// Parts of this code are based off of JellyScrub's script injection code.
|
2024-03-03 21:46:52 -05:00
|
|
|
// https://github.com/nicknsy/jellyscrub/blob/main/Nick.Plugin.Jellyscrub/JellyscrubPlugin.cs#L38
|
2022-11-06 21:20:52 -06:00
|
|
|
|
|
|
|
_logger.LogDebug("Reading index.html from {Path}", indexPath);
|
|
|
|
var contents = File.ReadAllText(indexPath);
|
|
|
|
_logger.LogDebug("Successfully read index.html");
|
|
|
|
|
|
|
|
var scriptTag = "<script src=\"configurationpage?name=skip-intro-button.js\"></script>";
|
|
|
|
|
|
|
|
// Only inject the script tag once
|
|
|
|
if (contents.Contains(scriptTag, StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
_logger.LogInformation("Skip button already added");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Inject a link to the script at the end of the <head> section.
|
|
|
|
// A regex is used here to ensure the replacement is only done once.
|
|
|
|
_logger.LogDebug("Injecting script tag");
|
|
|
|
var headEnd = new Regex("</head>", RegexOptions.IgnoreCase);
|
|
|
|
contents = headEnd.Replace(contents, scriptTag + "</head>", 1);
|
|
|
|
|
|
|
|
// Write the modified file contents
|
|
|
|
_logger.LogDebug("Saving modified file");
|
|
|
|
File.WriteAllText(indexPath, contents);
|
|
|
|
|
2024-03-02 11:53:34 -05:00
|
|
|
_logger.LogInformation("Skip intro button successfully added");
|
2022-06-14 14:36:05 -05:00
|
|
|
}
|
2019-03-10 08:53:30 +09:00
|
|
|
}
|