[
  {
    "path": ".editorconfig",
    "content": "# With more recent updates Visual Studio 2017 supports EditorConfig files out of the box\n# Visual Studio Code needs an extension: https://github.com/editorconfig/editorconfig-vscode\n# For emacs, vim, np++ and other editors, see here: https://github.com/editorconfig\n###############################\n# Core EditorConfig Options   #\n###############################\nroot = true\n# All files\n[*]\nindent_style = space\nindent_size = 4\ncharset = utf-8\ntrim_trailing_whitespace = true\ninsert_final_newline = true\nend_of_line = lf\nmax_line_length = off\n\n# YAML indentation\n[*.{yml,yaml}]\nindent_size = 2\n\n# XML indentation\n[*.{csproj,xml}]\nindent_size = 2\n\n###############################\n# .NET Coding Conventions     #\n###############################\n[*.{cs,vb}]\n# Organize usings\ndotnet_sort_system_directives_first = true\n# this. preferences\ndotnet_style_qualification_for_field = false:silent\ndotnet_style_qualification_for_property = false:silent\ndotnet_style_qualification_for_method = false:silent\ndotnet_style_qualification_for_event = false:silent\n# Language keywords vs BCL types preferences\ndotnet_style_predefined_type_for_locals_parameters_members = true:silent\ndotnet_style_predefined_type_for_member_access = true:silent\n# Parentheses preferences\ndotnet_style_parentheses_in_arithmetic_binary_operators = always_for_clarity:silent\ndotnet_style_parentheses_in_relational_binary_operators = always_for_clarity:silent\ndotnet_style_parentheses_in_other_binary_operators = always_for_clarity:silent\ndotnet_style_parentheses_in_other_operators = never_if_unnecessary:silent\n# Modifier preferences\ndotnet_style_require_accessibility_modifiers = for_non_interface_members:silent\ndotnet_style_readonly_field = true:suggestion\n# Expression-level preferences\ndotnet_style_object_initializer = true:suggestion\ndotnet_style_collection_initializer = true:suggestion\ndotnet_style_explicit_tuple_names = true:suggestion\ndotnet_style_null_propagation = true:suggestion\ndotnet_style_coalesce_expression = true:suggestion\ndotnet_style_prefer_is_null_check_over_reference_equality_method = true:silent\ndotnet_style_prefer_inferred_tuple_names = true:suggestion\ndotnet_style_prefer_inferred_anonymous_type_member_names = true:suggestion\ndotnet_style_prefer_auto_properties = true:silent\ndotnet_style_prefer_conditional_expression_over_assignment = true:silent\ndotnet_style_prefer_conditional_expression_over_return = true:silent\n\n###############################\n# Naming Conventions          #\n###############################\n# Style Definitions (From Roslyn)\n\n# Non-private static fields are PascalCase\ndotnet_naming_rule.non_private_static_fields_should_be_pascal_case.severity = suggestion\ndotnet_naming_rule.non_private_static_fields_should_be_pascal_case.symbols = non_private_static_fields\ndotnet_naming_rule.non_private_static_fields_should_be_pascal_case.style = non_private_static_field_style\n\ndotnet_naming_symbols.non_private_static_fields.applicable_kinds = field\ndotnet_naming_symbols.non_private_static_fields.applicable_accessibilities = public, protected, internal, protected_internal, private_protected\ndotnet_naming_symbols.non_private_static_fields.required_modifiers = static\n\ndotnet_naming_style.non_private_static_field_style.capitalization = pascal_case\n\n# Constants are PascalCase\ndotnet_naming_rule.constants_should_be_pascal_case.severity = suggestion\ndotnet_naming_rule.constants_should_be_pascal_case.symbols = constants\ndotnet_naming_rule.constants_should_be_pascal_case.style = constant_style\n\ndotnet_naming_symbols.constants.applicable_kinds = field, local\ndotnet_naming_symbols.constants.required_modifiers = const\n\ndotnet_naming_style.constant_style.capitalization = pascal_case\n\n# Static fields are camelCase and start with s_\ndotnet_naming_rule.static_fields_should_be_camel_case.severity = suggestion\ndotnet_naming_rule.static_fields_should_be_camel_case.symbols = static_fields\ndotnet_naming_rule.static_fields_should_be_camel_case.style = static_field_style\n\ndotnet_naming_symbols.static_fields.applicable_kinds = field\ndotnet_naming_symbols.static_fields.required_modifiers = static\n\ndotnet_naming_style.static_field_style.capitalization = camel_case\ndotnet_naming_style.static_field_style.required_prefix = _\n\n# Instance fields are camelCase and start with _\ndotnet_naming_rule.instance_fields_should_be_camel_case.severity = suggestion\ndotnet_naming_rule.instance_fields_should_be_camel_case.symbols = instance_fields\ndotnet_naming_rule.instance_fields_should_be_camel_case.style = instance_field_style\n\ndotnet_naming_symbols.instance_fields.applicable_kinds = field\n\ndotnet_naming_style.instance_field_style.capitalization = camel_case\ndotnet_naming_style.instance_field_style.required_prefix = _\n\n# Locals and parameters are camelCase\ndotnet_naming_rule.locals_should_be_camel_case.severity = suggestion\ndotnet_naming_rule.locals_should_be_camel_case.symbols = locals_and_parameters\ndotnet_naming_rule.locals_should_be_camel_case.style = camel_case_style\n\ndotnet_naming_symbols.locals_and_parameters.applicable_kinds = parameter, local\n\ndotnet_naming_style.camel_case_style.capitalization = camel_case\n\n# Local functions are PascalCase\ndotnet_naming_rule.local_functions_should_be_pascal_case.severity = suggestion\ndotnet_naming_rule.local_functions_should_be_pascal_case.symbols = local_functions\ndotnet_naming_rule.local_functions_should_be_pascal_case.style = local_function_style\n\ndotnet_naming_symbols.local_functions.applicable_kinds = local_function\n\ndotnet_naming_style.local_function_style.capitalization = pascal_case\n\n# By default, name items with PascalCase\ndotnet_naming_rule.members_should_be_pascal_case.severity = suggestion\ndotnet_naming_rule.members_should_be_pascal_case.symbols = all_members\ndotnet_naming_rule.members_should_be_pascal_case.style = pascal_case_style\n\ndotnet_naming_symbols.all_members.applicable_kinds = *\n\ndotnet_naming_style.pascal_case_style.capitalization = pascal_case\n\n###############################\n# C# Coding Conventions       #\n###############################\n[*.cs]\n# var preferences\ncsharp_style_var_for_built_in_types = true:silent\ncsharp_style_var_when_type_is_apparent = true:silent\ncsharp_style_var_elsewhere = true:silent\n# Expression-bodied members\ncsharp_style_expression_bodied_methods = false:silent\ncsharp_style_expression_bodied_constructors = false:silent\ncsharp_style_expression_bodied_operators = false:silent\ncsharp_style_expression_bodied_properties = true:silent\ncsharp_style_expression_bodied_indexers = true:silent\ncsharp_style_expression_bodied_accessors = true:silent\n# Pattern matching preferences\ncsharp_style_pattern_matching_over_is_with_cast_check = true:suggestion\ncsharp_style_pattern_matching_over_as_with_null_check = true:suggestion\n# Null-checking preferences\ncsharp_style_throw_expression = true:suggestion\ncsharp_style_conditional_delegate_call = true:suggestion\n# Modifier preferences\ncsharp_preferred_modifier_order = public,private,protected,internal,static,extern,new,virtual,abstract,sealed,override,readonly,unsafe,volatile,async:suggestion\n# Expression-level preferences\ncsharp_prefer_braces = true:silent\ncsharp_style_deconstructed_variable_declaration = true:suggestion\ncsharp_prefer_simple_default_expression = true:suggestion\ncsharp_style_pattern_local_over_anonymous_function = true:suggestion\ncsharp_style_inlined_variable_declaration = true:suggestion\n\n###############################\n# C# Formatting Rules         #\n###############################\n# New line preferences\ncsharp_new_line_before_open_brace = all\ncsharp_new_line_before_else = true\ncsharp_new_line_before_catch = true\ncsharp_new_line_before_finally = true\ncsharp_new_line_before_members_in_object_initializers = true\ncsharp_new_line_before_members_in_anonymous_types = true\ncsharp_new_line_between_query_expression_clauses = true\n# Indentation preferences\ncsharp_indent_case_contents = true\ncsharp_indent_switch_labels = true\ncsharp_indent_labels = flush_left\n# Space preferences\ncsharp_space_after_cast = false\ncsharp_space_after_keywords_in_control_flow_statements = true\ncsharp_space_between_method_call_parameter_list_parentheses = false\ncsharp_space_between_method_declaration_parameter_list_parentheses = false\ncsharp_space_between_parentheses = false\ncsharp_space_before_colon_in_inheritance_clause = true\ncsharp_space_after_colon_in_inheritance_clause = true\ncsharp_space_around_binary_operators = before_and_after\ncsharp_space_between_method_declaration_empty_parameter_list_parentheses = false\ncsharp_space_between_method_call_name_and_opening_parenthesis = false\ncsharp_space_between_method_call_empty_parameter_list_parentheses = false\n# Wrapping preferences\ncsharp_preserve_single_line_statements = true\ncsharp_preserve_single_line_blocks = true\n"
  },
  {
    "path": ".github/ISSUE_TEMPLATE/bug_report.yml",
    "content": "name: \"Bug report\"\ndescription: \"Create a report to help us improve\"\nlabels: [bug]\nbody:\n  - type: textarea\n    attributes:\n      label: Describe the bug\n      description: Also tell us, what did you expect to happen?\n      placeholder: |\n        The more information that you are able to provide, the better. Did you do anything before this happened? Did you upgrade or change anything? Any screenshots or logs you can provide will be helpful.\n\n        This is my issue.\n\n        Steps to Reproduce\n          1. In this environment...\n          2. With this config...\n          3. Run '...'\n          4. See error...\n    validations:\n      required: true\n\n  - type: input\n    attributes:\n      label: Operating system\n      placeholder: Debian 11, Windows 11, etc.\n    validations:\n      required: true\n\n  - type: input\n    attributes:\n      label: Jellyfin installation method\n      placeholder: Docker, Windows installer, etc.\n    validations:\n      required: true\n\n  - type: textarea\n    attributes:\n      label: Support Bundle\n      placeholder: go to Dashboard -> Plugins -> Media Analyzer -> Support Bundle (at the bottom of the page) and paste the contents of the textbox here\n    validations:\n      required: true\n\n  - type: textarea\n    attributes:\n      label: Jellyfin logs\n      placeholder: Paste any relevant logs here\n      render: shell\n\n"
  },
  {
    "path": ".github/ISSUE_TEMPLATE/feature_request.md",
    "content": "---\nname: Feature request\nabout: Suggest an idea for this project\nlabels: enhancement\nassignees: ''\n\n---\n\n**Describe the feature you'd like added**\nA clear and concise description of what you would like to see added.\n\n**Additional context**\nAdd any other context or screenshots about the feature request here.\n"
  },
  {
    "path": ".github/dependabot.yml",
    "content": "version: 2\nupdates:\n  # Fetch and update latest `nuget` pkgs\n  - package-ecosystem: nuget\n    directory: /\n    schedule:\n      interval: weekly\n    open-pull-requests-limit: 10\n    labels:\n      - chore\n      - dependency\n      - nuget\n    commit-message:\n      prefix: chore\n      include: scope\n\n  # Fetch and update latest `github-actions` pkgs\n  - package-ecosystem: github-actions\n    directory: /\n    schedule:\n      interval: monthly\n    open-pull-requests-limit: 10\n    labels:\n      - ci\n      - dependency\n      - github_actions\n    commit-message:\n      prefix: ci\n      include: scope\n"
  },
  {
    "path": ".github/workflows/build.yml",
    "content": "name: \"Build Plugin\"\n\non:\n  push:\n    branches: [\"master\", \"analyzers\"]\n  pull_request:\n    branches: [\"master\"]\n\njobs:\n  build:\n    runs-on: ubuntu-latest\n    steps:\n      - uses: actions/checkout@v4\n\n      - name: Setup .NET\n        uses: actions/setup-dotnet@v4\n        with:\n          dotnet-version: 8.0.x\n\n      - name: Restore dependencies\n        run: dotnet restore\n\n      - name: Embed version info\n        run: echo \"${{ github.sha }}\" > Jellyfin.Plugin.MediaAnalyzer/Configuration/version.txt\n\n      - name: Build\n        run: dotnet build --no-restore\n\n      - name: Upload artifact\n        uses: actions/upload-artifact@v4\n        with:\n          name: Jellyfin.Plugin.MediaAnalyzer-${{ github.sha }}.dll\n          path: Jellyfin.Plugin.MediaAnalyzer/bin/Debug/net8.0/Jellyfin.Plugin.MediaAnalyzer.dll\n          if-no-files-found: error\n"
  },
  {
    "path": ".github/workflows/package.sh",
    "content": "#!/bin/bash\n\n# Check argument count\nif [[ $# -ne 1 ]]; then\n    echo \"Usage: $0 VERSION\"\n    exit 1\nfi\n\n# Use provided tag to derive archive filename and short tag\nversion=\"$1\"\nzip=\"jellyfin-plugin-mediaanalyzer-$version.zip\"\nshort=\"$(echo \"$version\" | sed \"s/^v//\")\"\n\n# Get the assembly version\nCSPROJ=\"Jellyfin.Plugin.MediaAnalyzer/Jellyfin.Plugin.MediaAnalyzer.csproj\"\nassemblyVersion=\"$(grep -m1 -oE \"([0-9]\\.){3}[0-9]\" \"$CSPROJ\")\"\n\n# Get the date\ndate=\"$(date --utc -Iseconds | sed \"s/\\+00:00/Z/\")\"\n\n# Debug\necho \"Version: $version ($short)\"\necho \"Archive: $zip\"\necho\n\necho \"Running unit tests\"\ndotnet test -p:DefineConstants=SKIP_FFMPEG_TESTS || exit 1\necho\n\necho \"Building plugin in Release mode\"\ndotnet build -c Release || exit 1\necho\n\n# Create packaging directory\nmkdir package\ncd package || exit 1\n\n# Copy the freshly built plugin DLL to the packaging directory and archive\ncp \"../Jellyfin.Plugin.MediaAnalyzer/bin/Release/net8.0/Jellyfin.Plugin.MediaAnalyzer.dll\" ./ || exit 1\nzip \"$zip\" Jellyfin.Plugin.MediaAnalyzer.dll || exit 1\n\n# Calculate the checksum of the archive\nchecksum=\"$(md5sum \"$zip\" | cut -f 1 -d \" \")\"\n\n# Generate the manifest entry for this plugin\ncat > manifest.json <<'EOF'\n{\n    \"version\": \"ASSEMBLY\",\n    \"changelog\": \"- See the full changelog at [GitHub](https://github.com/Endrl/jellyfin-plugin-media-analyzer/blob/master/CHANGELOG.md)\\n\",\n    \"targetAbi\": \"10.9.0.0\",\n    \"sourceUrl\": \"https://github.com/Endrl/jellyfin-plugin-media-analyzer/releases/download/VERSION/ZIP\",\n    \"checksum\": \"CHECKSUM\",\n    \"timestamp\": \"DATE\"\n}\nEOF\n\nsed -i \"s/ASSEMBLY/$assemblyVersion/\" manifest.json\nsed -i \"s/VERSION/$version/\" manifest.json\nsed -i \"s/ZIP/$zip/\" manifest.json\nsed -i \"s/CHECKSUM/$checksum/\" manifest.json\nsed -i \"s/DATE/$date/\" manifest.json\n"
  },
  {
    "path": ".github/workflows/publish.yml",
    "content": "name: \"Package plugin\"\n\non:\n  workflow_dispatch:\n\njobs:\n  build:\n    runs-on: ubuntu-latest\n    steps:\n      # set fetch-depth to 0 in order to clone all tags instead of just the current commit\n      - uses: actions/checkout@v4\n        with:\n          fetch-depth: 0\n\n      - name: Checkout latest tag\n        id: tag\n        run: |\n          tag=\"$(git tag --sort=committerdate | tail -n 1)\"\n          git checkout \"$tag\"\n          echo \"tag=$tag\" >> $GITHUB_OUTPUT\n\n      - name: Setup .NET\n        uses: actions/setup-dotnet@v4\n        with:\n          dotnet-version: 8.0.x\n\n      - name: Restore dependencies\n        run: dotnet restore\n\n      - name: Package\n        run: .github/workflows/package.sh ${{ steps.tag.outputs.tag }}\n\n      - name: Upload plugin archive\n        uses: actions/upload-artifact@v4\n        with:\n          name: jellyfin-plugin-mediaanalyzer-bundle-${{ steps.tag.outputs.tag }}.zip\n          path: |\n            package/*.zip\n            package/*.json\n          if-no-files-found: error\n"
  },
  {
    "path": ".gitignore",
    "content": "bin/\nobj/\nBenchmarkDotNet.Artifacts/\n/package/\n\n# Ignore pre compiled web interface\ndocker/dist\n"
  },
  {
    "path": ".vscode/launch.json",
    "content": " {\n     // Paths and plugin names are configured in settings.json\n     \"version\": \"0.2.0\",\n     \"configurations\": [\n         {\n             \"type\": \"coreclr\",\n             \"name\": \"Launch\",\n             \"request\": \"launch\",\n             \"preLaunchTask\": \"build-and-copy\",\n             \"program\": \"${config:jellyfinDir}/bin/Debug/net8.0/jellyfin.dll\",\n             \"args\": [\n             //\"--nowebclient\"\n             \"--webdir\",\n             \"${config:jellyfinWebDir}/dist/\"\n             ],\n             \"cwd\": \"${config:jellyfinDir}\",\n         }\n     ]\n }\n"
  },
  {
    "path": ".vscode/settings.json",
    "content": "{\n    // jellyfinDir : The directory of the cloned jellyfin server project\n    // This needs to be built once before it can be used\n    \"jellyfinDir\": \"${workspaceFolder}/../jellyfin/Jellyfin.Server\",\n    // jellyfinWebDir : The directory of the cloned jellyfin-web project\n    // This needs to be built once before it can be used\n    \"jellyfinWebDir\": \"${workspaceFolder}/../jellyfin-web\",\n    // jellyfinDataDir : the root data directory for a running jellyfin instance\n    // This is where jellyfin stores its configs, plugins, metadata etc\n    // This is platform specific by default, but on Windows defaults to\n    // ${env:LOCALAPPDATA}/jellyfin\n    \"jellyfinDataDir\": \"${env:LOCALAPPDATA}/jellyfin\",\n    \"jellyfinDataDirWin\": \"${env:LOCALAPPDATA}\\\\jellyfin\",\n    // The name of the plugin\n    \"pluginName\": \"Jellyfin.Plugin.MediaAnalyzer\",\n    \"cmake.configureOnOpen\": false\n}"
  },
  {
    "path": ".vscode/tasks.json",
    "content": "{\n    // Paths and plugin name are configured in settings.json\n    \"version\": \"2.0.0\",\n    \"tasks\": [\n        {\n            // A chain task - build the plugin, then copy it to your\n            // jellyfin server's plugin directory\n            \"label\": \"build-and-copy\",\n            \"dependsOrder\": \"sequence\",\n            \"dependsOn\": [\"build\",\"copy-dll-win\"],\n            \"windows\": {\n                \"dependsOn\": [\"build\",\"copy-dll-win\"]\n            }\n        },\n        {\n            // Build the plugin\n            \"label\": \"build\",\n            \"command\": \"dotnet\",\n            \"type\": \"shell\",\n            \"args\": [\n                \"publish\",\n                \"${workspaceFolder}/${config:pluginName}.sln\",\n                \"/property:GenerateFullPaths=true\",\n                \"/consoleloggerparameters:NoSummary\"\n            ],\n            \"group\": \"build\",\n            \"presentation\": {\n                \"reveal\": \"silent\"\n            },\n            \"problemMatcher\": \"$msCompile\"\n        },\n        {\n            // Ensure the plugin directory exists before trying to use it\n            \"label\": \"make-plugin-dir\",\n            \"type\": \"shell\",\n            \"command\": \"mkdir\",\n            \"args\": [\n            \"-Force\",\n            \"-Path\",\n            \"${config:jellyfinDataDir}/plugins/${config:pluginName}/\"\n            ]\n        },\n        {\n            // Copy the plugin dll to the jellyfin plugin install path\n            // This command copies every .dll from the build directory to the plugin dir\n            // Usually, you probablly only need ${config:pluginName}.dll\n            // But some plugins may bundle extra requirements\n            \"label\": \"copy-dll-win\",\n            \"type\": \"shell\",\n            \"command\": \"xcopy\",\n            \"args\": [\n            \"/I\",\n            \"/y\",\n            \".\\\\${config:pluginName}\\\\bin\\\\Debug\\\\net8.0\\\\publish\",\n            \"${config:jellyfinDataDirWin}\\\\plugins\\\\${config:pluginName}\\\\\"\n            ]\n\n        },\n        {\n            // Copy the plugin dll to the jellyfin plugin install path\n            // This command copies every .dll from the build directory to the plugin dir\n            // Usually, you probablly only need ${config:pluginName}.dll\n            // But some plugins may bundle extra requirements\n            \"label\": \"copy-dll\",\n            \"type\": \"shell\",\n            \"command\": \"cp\",\n            \"args\": [\n            \"./${config:pluginName}/bin/Debug/net8.0/publish/*\",\n            \"${config:jellyfinDataDir}/plugins/${config:pluginName}/\"\n            ]\n\n        },\n    ]\n}\n"
  },
  {
    "path": "ACKNOWLEDGEMENTS.md",
    "content": "Intro Skipper is made possible by the following open source projects:\n\n* [acoustid-match](https://github.com/dnknth/acoustid-match) (MIT)\n* [chromaprint](https://github.com/acoustid/chromaprint) (LGPL 2.1)\n* [JellyScrub](https://github.com/nicknsy/jellyscrub) (MIT)\n* [Jellyfin](https://github.com/jellyfin/jellyfin) (GPL)\n"
  },
  {
    "path": "CHANGELOG.md",
    "content": "# Changelog\n\n## Unreleased\n\n### Changed\n\n* Task Timer is no longer configured by deafult. We listen for MediaLibrary changes instead. (Change back in options)\n\n### Fixed\n\n* Prevents a crash during save when start >= end time\n\n## v0.4.0.0 (2023-11-02)\n\n* Remove creatorId (sync with server implementation)\n\n## v0.3.0.0 (2023-09-25)\n\n* Add options to control listener\n* Round to two decimal places\n* Improve log messages\n\n## v0.2.0.0 (2023-05-28)\n\n* Blacklisting with db\n* Enable movies credits detection\n\n## v0.1.0.0 (2023-05-04)\n\n* Outdated, removed from repository!\n* Initial release\n* New features\n  * Detect ending credits in television episodes\n  * Add support for using chapter names to locate introductions and ending credits\n  * Add support for using black frames to locate ending credits\n* Internal changes\n  * Move Chromaprint analysis code out of the episode analysis task\n  * Add support for multiple analysis techinques\n* Breaking Change\n  * Removed all server and frontend influencing mods\n  * Removed EDL handling\n"
  },
  {
    "path": "Jellyfin.Plugin.MediaAnalyzer/Analyzers/BlackFrameAnalyzer.cs",
    "content": "namespace Jellyfin.Plugin.MediaAnalyzer;\n\nusing System;\nusing System.Collections.Generic;\nusing System.Collections.ObjectModel;\nusing System.Linq;\nusing System.Threading;\nusing System.Threading.Tasks;\nusing Jellyfin.Data.Enums;\nusing Microsoft.Extensions.Logging;\n\n/// <summary>\n/// Media file analyzer used to detect end credits that consist of text overlaid on a black background.\n/// Bisects the end of the video file to perform an efficient search.\n/// </summary>\npublic class BlackFrameAnalyzer : IMediaFileAnalyzer\n{\n    private readonly TimeSpan _maximumError = new(0, 0, 4);\n\n    private readonly ILogger<BlackFrameAnalyzer> _logger;\n\n    /// <summary>\n    /// Initializes a new instance of the <see cref=\"BlackFrameAnalyzer\"/> class.\n    /// </summary>\n    /// <param name=\"logger\">Logger.</param>\n    public BlackFrameAnalyzer(ILogger<BlackFrameAnalyzer> logger)\n    {\n        _logger = logger;\n    }\n\n    /// <inheritdoc />\n    public async Task<(ReadOnlyCollection<QueuedMedia> NotAnalyzed, ReadOnlyDictionary<Guid, Segment> Analyzed, ReadOnlyDictionary<Guid, SegmentMetadata> SegmentMetadata)> AnalyzeMediaFilesAsync(\n        ReadOnlyCollection<QueuedMedia> analysisQueue,\n        MediaSegmentType mode,\n        CancellationToken cancellationToken)\n    {\n        if (mode != MediaSegmentType.Outro)\n        {\n            throw new NotImplementedException(\"Blackframe analyzing is just suitable for Credits/Outro\");\n        }\n\n        var creditTimes = new Dictionary<Guid, Segment>();\n        var metadata = new Dictionary<Guid, SegmentMetadata>();\n\n        foreach (var episode in analysisQueue)\n        {\n            if (cancellationToken.IsCancellationRequested)\n            {\n                break;\n            }\n\n            var meta = await Plugin.Instance!.GetMetadataDb().GetSegments(episode.ItemId, mode, AnalyzerType.BlackFrameAnalyzer);\n\n            var intro = AnalyzeMediaFile(\n                episode,\n                mode,\n                Plugin.Instance!.Configuration.BlackFrameMinimumPercentage);\n\n            if (intro is null)\n            {\n                continue;\n            }\n\n            // protect against broken timestamps\n            if (intro.Start >= intro.End)\n            {\n                continue;\n            }\n\n            creditTimes[episode.ItemId] = intro;\n            if (meta is null)\n            {\n                metadata[episode.ItemId] = new SegmentMetadata(episode, mode, AnalyzerType.BlackFrameAnalyzer);\n            }\n        }\n\n        return (analysisQueue\n            .Where(x => !creditTimes.ContainsKey(x.ItemId))\n            .ToList()\n            .AsReadOnly(), creditTimes.AsReadOnly(), metadata.AsReadOnly());\n    }\n\n    /// <summary>\n    /// Analyzes an individual media file. Only public because of unit tests.\n    /// </summary>\n    /// <param name=\"episode\">Media file to analyze.</param>\n    /// <param name=\"mode\">Analysis mode.</param>\n    /// <param name=\"minimum\">Percentage of the frame that must be black.</param>\n    /// <returns>Credits timestamp.</returns>\n    public Segment? AnalyzeMediaFile(QueuedMedia episode, MediaSegmentType mode, int minimum)\n    {\n        var config = Plugin.Instance?.Configuration ?? new Configuration.PluginConfiguration();\n\n        // Start by analyzing the last N minutes of the file.\n        var start = TimeSpan.FromSeconds(config.MaximumEpisodeCreditsDuration);\n        var end = TimeSpan.FromSeconds(config.MinimumCreditsDuration);\n        var firstFrameTime = 0.0;\n\n        // Continue bisecting the end of the file until the range that contains the first black\n        // frame is smaller than the maximum permitted error.\n        while (start - end > _maximumError)\n        {\n            // Analyze the middle two seconds from the current bisected range\n            var midpoint = (start + end) / 2;\n            var scanTime = episode.Duration - midpoint.TotalSeconds;\n            var tr = new TimeRange(scanTime, scanTime + 2);\n\n            _logger.LogTrace(\n                \"{Episode}, dur {Duration}, bisect [{BStart}, {BEnd}], time [{Start}, {End}]\",\n                episode.Name,\n                episode.Duration,\n                start,\n                end,\n                tr.Start,\n                tr.End);\n\n            var frames = FFmpegWrapper.DetectBlackFrames(episode, tr, minimum);\n            _logger.LogTrace(\n                \"{Episode} at {Start} has {Count} black frames\",\n                episode.Name,\n                tr.Start,\n                frames.Length);\n\n            if (frames.Length == 0)\n            {\n                // Since no black frames were found, slide the range closer to the end\n                start = midpoint;\n            }\n            else\n            {\n                // Some black frames were found, slide the range closer to the start\n                end = midpoint;\n                firstFrameTime = frames[0].Time + scanTime;\n            }\n        }\n\n        if (firstFrameTime > 0)\n        {\n            return new(episode.ItemId, episode.IsEpisode(), new TimeRange(firstFrameTime, episode.Duration));\n        }\n\n        return null;\n    }\n}\n"
  },
  {
    "path": "Jellyfin.Plugin.MediaAnalyzer/Analyzers/ChapterAnalyzer.cs",
    "content": "namespace Jellyfin.Plugin.MediaAnalyzer;\n\nusing System;\nusing System.Collections.Generic;\nusing System.Collections.ObjectModel;\nusing System.Globalization;\nusing System.Linq;\nusing System.Text.RegularExpressions;\nusing System.Threading;\nusing System.Threading.Tasks;\nusing Jellyfin.Data.Enums;\nusing MediaBrowser.Model.Entities;\nusing Microsoft.Extensions.Logging;\n\n/// <summary>\n/// Chapter name analyzer.\n/// </summary>\npublic class ChapterAnalyzer : IMediaFileAnalyzer\n{\n    private ILogger<ChapterAnalyzer> _logger;\n\n    /// <summary>\n    /// Initializes a new instance of the <see cref=\"ChapterAnalyzer\"/> class.\n    /// </summary>\n    /// <param name=\"logger\">Logger.</param>\n    public ChapterAnalyzer(ILogger<ChapterAnalyzer> logger)\n    {\n        _logger = logger;\n    }\n\n    /// <inheritdoc />\n    public async Task<(ReadOnlyCollection<QueuedMedia> NotAnalyzed, ReadOnlyDictionary<Guid, Segment> Analyzed, ReadOnlyDictionary<Guid, SegmentMetadata> SegmentMetadata)> AnalyzeMediaFilesAsync(\n        ReadOnlyCollection<QueuedMedia> analysisQueue,\n        MediaSegmentType mode,\n        CancellationToken cancellationToken)\n    {\n        var skippableRanges = new Dictionary<Guid, Segment>();\n        var metadata = new Dictionary<Guid, SegmentMetadata>();\n\n        var expression = mode == MediaSegmentType.Intro ?\n            Plugin.Instance!.Configuration.ChapterAnalyzerIntroductionPattern :\n            Plugin.Instance!.Configuration.ChapterAnalyzerEndCreditsPattern;\n\n        if (string.IsNullOrWhiteSpace(expression))\n        {\n            return (analysisQueue, skippableRanges.AsReadOnly(), metadata.AsReadOnly());\n        }\n\n        foreach (var episode in analysisQueue)\n        {\n            if (cancellationToken.IsCancellationRequested)\n            {\n                break;\n            }\n\n            var meta = await Plugin.Instance!.GetMetadataDb().GetSegments(episode.ItemId, mode, AnalyzerType.ChapterAnalyzer);\n\n            var skipRange = FindMatchingChapter(\n                episode,\n                new(Plugin.Instance!.GetChapters(episode.ItemId)),\n                expression,\n                mode);\n\n            if (skipRange is null)\n            {\n                continue;\n            }\n\n            // protect against broken timestamps\n            if (skipRange.Start >= skipRange.End)\n            {\n                continue;\n            }\n\n            skippableRanges.Add(episode.ItemId, skipRange);\n            if (meta is null)\n            {\n                metadata[episode.ItemId] = new SegmentMetadata(episode, mode, AnalyzerType.ChapterAnalyzer);\n            }\n        }\n\n        return (analysisQueue\n            .Where(x => !skippableRanges.ContainsKey(x.ItemId))\n            .ToList()\n            .AsReadOnly(), skippableRanges.AsReadOnly(), metadata.AsReadOnly());\n    }\n\n    /// <summary>\n    /// Searches a list of chapter names for one that matches the provided regular expression.\n    /// Only public to allow for unit testing.\n    /// </summary>\n    /// <param name=\"episode\">Episode.</param>\n    /// <param name=\"chapters\">Media item chapters.</param>\n    /// <param name=\"expression\">Regular expression pattern.</param>\n    /// <param name=\"mode\">Analysis mode.</param>\n    /// <returns>Intro object containing skippable time range, or null if no chapter matched.</returns>\n    public Segment? FindMatchingChapter(\n        QueuedMedia episode,\n        Collection<ChapterInfo> chapters,\n        string expression,\n        MediaSegmentType mode)\n    {\n        Segment? matchingChapter = null;\n\n        var config = Plugin.Instance?.Configuration ?? new Configuration.PluginConfiguration();\n\n        var minDuration = config.MinimumIntroDuration;\n        int maxDuration = mode == MediaSegmentType.Intro ?\n            config.MaximumIntroDuration :\n            config.MaximumEpisodeCreditsDuration;\n\n        if (mode == MediaSegmentType.Outro)\n        {\n            // Since the ending credits chapter may be the last chapter in the file, append a virtual\n            // chapter at the very end of the file.\n            chapters.Add(new()\n            {\n                StartPositionTicks = TimeSpan.FromSeconds(episode.Duration).Ticks\n            });\n        }\n\n        // Check all chapters\n        for (int i = 0; i < chapters.Count - 1; i++)\n        {\n            var current = chapters[i];\n            var next = chapters[i + 1];\n\n            if (string.IsNullOrWhiteSpace(current.Name))\n            {\n                continue;\n            }\n\n            var currentRange = new TimeRange(\n                TimeSpan.FromTicks(current.StartPositionTicks).TotalSeconds,\n                TimeSpan.FromTicks(next.StartPositionTicks).TotalSeconds);\n\n            var baseMessage = string.Format(\n                CultureInfo.InvariantCulture,\n                \"{0}: Chapter \\\"{1}\\\" ({2} - {3})\",\n                episode.Path,\n                current.Name,\n                currentRange.Start,\n                currentRange.End);\n\n            if (currentRange.Duration < minDuration || currentRange.Duration > maxDuration)\n            {\n                _logger.LogTrace(\"{Base}: ignoring (invalid duration)\", baseMessage);\n                continue;\n            }\n\n            // Regex.IsMatch() is used here in order to allow the runtime to cache the compiled regex\n            // between function invocations.\n            var match = Regex.IsMatch(\n                current.Name,\n                expression,\n                RegexOptions.None,\n                TimeSpan.FromSeconds(1));\n\n            if (!match)\n            {\n                _logger.LogTrace(\"{Base}: ignoring (does not match regular expression)\", baseMessage);\n                continue;\n            }\n\n            matchingChapter = new(episode.ItemId, episode.IsEpisode(), currentRange);\n            _logger.LogTrace(\"{Base}: okay\", baseMessage);\n            break;\n        }\n\n        return matchingChapter;\n    }\n}\n"
  },
  {
    "path": "Jellyfin.Plugin.MediaAnalyzer/Analyzers/ChromaprintAnalyzer.cs",
    "content": "namespace Jellyfin.Plugin.MediaAnalyzer;\n\nusing System;\nusing System.Collections.Generic;\nusing System.Collections.ObjectModel;\nusing System.Linq;\nusing System.Numerics;\nusing System.Threading;\nusing System.Threading.Tasks;\nusing Jellyfin.Data.Enums;\nusing Microsoft.Extensions.Logging;\n\n/// <summary>\n/// Chromaprint audio analyzer.\n/// </summary>\npublic class ChromaprintAnalyzer : IMediaFileAnalyzer\n{\n    /// <summary>\n    /// Seconds of audio in one fingerprint point.\n    /// This value is defined by the Chromaprint library and should not be changed.\n    /// </summary>\n    private const double SamplesToSeconds = 0.128;\n\n    private int minimumIntroDuration;\n\n    private int maximumDifferences;\n\n    private int invertedIndexShift;\n\n    private double maximumTimeSkip;\n\n    private double silenceDetectionMinimumDuration;\n\n    private ILogger<ChromaprintAnalyzer> _logger;\n\n    private MediaSegmentType _analyzingType;\n\n    /// <summary>\n    /// Initializes a new instance of the <see cref=\"ChromaprintAnalyzer\"/> class.\n    /// </summary>\n    /// <param name=\"logger\">Logger.</param>\n    public ChromaprintAnalyzer(ILogger<ChromaprintAnalyzer> logger)\n    {\n        var config = Plugin.Instance?.Configuration ?? new Configuration.PluginConfiguration();\n        maximumDifferences = config.MaximumFingerprintPointDifferences;\n        invertedIndexShift = config.InvertedIndexShift;\n        maximumTimeSkip = config.MaximumTimeSkip;\n        silenceDetectionMinimumDuration = config.SilenceDetectionMinimumDuration;\n        minimumIntroDuration = config.MinimumIntroDuration;\n\n        _logger = logger;\n    }\n\n    /// <inheritdoc />\n    public async Task<(ReadOnlyCollection<QueuedMedia> NotAnalyzed, ReadOnlyDictionary<Guid, Segment> Analyzed, ReadOnlyDictionary<Guid, SegmentMetadata> SegmentMetadata)> AnalyzeMediaFilesAsync(\n        ReadOnlyCollection<QueuedMedia> analysisQueue,\n        MediaSegmentType mode,\n        CancellationToken cancellationToken)\n    {\n        // All segments for this season.\n        var seasonSegments = new Dictionary<Guid, Segment>();\n\n        // Cache of all fingerprints for this season.\n        var fingerprintCache = new Dictionary<Guid, uint[]>();\n\n        // Episode analysis queue based on not analyzed episodes\n        var episodeAnalysisQueue = analysisQueue.ToList().Where(m => !m.IsAnalyzed).ToList();\n\n        // Episodes that were analyzed and do not have an introduction.\n        var episodesWithoutSegments = new List<QueuedMedia>();\n\n        var metadata = new Dictionary<Guid, SegmentMetadata>();\n\n        this._analyzingType = mode;\n\n        // we need at least two episodes, it's possible to use an already analzyed one as reference\n        if (episodeAnalysisQueue.Count == 1 && analysisQueue.Count > 1)\n        {\n            var item = analysisQueue.Where(i => !episodeAnalysisQueue.Contains(i)).FirstOrDefault();\n\n            if (item is not null)\n            {\n                episodeAnalysisQueue.Add(item);\n            }\n        }\n\n        // If we have just one episode, we can abort and flag the episode to skip blacklisting\n        if (episodeAnalysisQueue.Count == 1)\n        {\n            var item = episodeAnalysisQueue.First();\n            _logger.LogInformation(\"Found just one episode for {Series}: S{Season}. Skipping as we need at least two.\", item.SeriesName, item.SeasonNumber);\n\n            item.SkipPreventAnalyzing = true;\n            episodesWithoutSegments.Add(item);\n            return (episodesWithoutSegments.AsReadOnly(), seasonSegments.AsReadOnly(), metadata.AsReadOnly());\n        }\n\n        // Compute fingerprints for all episodes in the season\n        foreach (var episode in episodeAnalysisQueue)\n        {\n            try\n            {\n                fingerprintCache[episode.ItemId] = FFmpegWrapper.Fingerprint(episode, mode);\n\n                if (cancellationToken.IsCancellationRequested)\n                {\n                    return (analysisQueue, seasonSegments.AsReadOnly(), metadata.AsReadOnly());\n                }\n            }\n            catch (FingerprintException ex)\n            {\n                _logger.LogDebug(\"Caught fingerprint error: {Ex}\", ex);\n                WarningManager.SetFlag(PluginWarning.InvalidChromaprintFingerprint);\n\n                // Fallback to an empty fingerprint on any error\n                fingerprintCache[episode.ItemId] = Array.Empty<uint>();\n            }\n        }\n\n        // While there are still episodes in the queue\n        while (episodeAnalysisQueue.Count > 0)\n        {\n            // Pop the first episode from the queue\n            var currentEpisode = episodeAnalysisQueue[0];\n            episodeAnalysisQueue.RemoveAt(0);\n\n            // Search through all remaining episodes.\n            foreach (var remainingEpisode in episodeAnalysisQueue)\n            {\n                // Compare the current episode to all remaining episodes in the queue.\n                var (currentIntro, remainingIntro) = CompareEpisodes(\n                    currentEpisode.ItemId,\n                    fingerprintCache[currentEpisode.ItemId],\n                    remainingEpisode.ItemId,\n                    fingerprintCache[remainingEpisode.ItemId]);\n\n                // Ignore this comparison result if:\n                // - one of the intros isn't valid, or\n                // - the introduction exceeds the configured limit\n                if (\n                    !remainingIntro.Valid ||\n                    remainingIntro.Duration > Plugin.Instance!.Configuration.MaximumIntroDuration)\n                {\n                    continue;\n                }\n\n                /* Since the Fingerprint() function returns an array of Chromaprint points without time\n                 * information, the times reported from the index search function start from 0.\n                 *\n                 * While this is desired behavior for detecting introductions, it breaks credit\n                 * detection, as the audio we're analyzing was extracted from some point into the file.\n                 *\n                 * To fix this, add the starting time of the fingerprint to the reported time range.\n                 */\n                if (this._analyzingType == MediaSegmentType.Outro)\n                {\n                    currentIntro.Start += currentEpisode.CreditsFingerprintStart;\n                    currentIntro.End += currentEpisode.CreditsFingerprintStart;\n                    remainingIntro.Start += remainingEpisode.CreditsFingerprintStart;\n                    remainingIntro.End += remainingEpisode.CreditsFingerprintStart;\n                }\n\n                // Only save the discovered intro if it is:\n                // - the first intro discovered for this episode\n                // - longer than the previously discovered intro\n                if (\n                    !seasonSegments.TryGetValue(currentIntro.ItemId, out var savedCurrentIntro) ||\n                    currentIntro.Duration > savedCurrentIntro.Duration)\n                {\n                    if (ValidateTime(currentIntro))\n                    {\n                        var meta = await Plugin.Instance!.GetMetadataDb().GetSegments(currentEpisode.ItemId, mode, AnalyzerType.ChromaprintAnalyzer);\n\n                        if (meta is null)\n                        {\n                            metadata[currentIntro.ItemId] = new SegmentMetadata(currentEpisode, mode, AnalyzerType.ChromaprintAnalyzer);\n                        }\n\n                        seasonSegments[currentIntro.ItemId] = currentIntro;\n                    }\n                }\n\n                if (\n                    !seasonSegments.TryGetValue(remainingIntro.ItemId, out var savedRemainingIntro) ||\n                    remainingIntro.Duration > savedRemainingIntro.Duration)\n                {\n                    if (ValidateTime(currentIntro))\n                    {\n                        var meta = await Plugin.Instance!.GetMetadataDb().GetSegments(remainingEpisode.ItemId, mode, AnalyzerType.ChromaprintAnalyzer);\n                        if (meta is null)\n                        {\n                            metadata[remainingIntro.ItemId] = new SegmentMetadata(remainingEpisode, mode, AnalyzerType.ChromaprintAnalyzer);\n                        }\n\n                        seasonSegments[remainingIntro.ItemId] = remainingIntro;\n                    }\n                }\n\n                break;\n            }\n\n            // If no intro is found at this point, the popped episode is not reinserted into the queue.\n            episodesWithoutSegments.Add(currentEpisode);\n        }\n\n        if (this._analyzingType == MediaSegmentType.Intro)\n        {\n            // Adjust all introduction end times so that they end at silence.\n            seasonSegments = AdjustIntroEndTimes(analysisQueue, seasonSegments);\n        }\n\n        // If cancellation was requested, report that no episodes were analyzed.\n        if (cancellationToken.IsCancellationRequested)\n        {\n            seasonSegments.Clear();\n            metadata.Clear();\n            return (analysisQueue, seasonSegments.AsReadOnly(), metadata.AsReadOnly());\n        }\n\n        return (episodesWithoutSegments.AsReadOnly(), seasonSegments.AsReadOnly(), metadata.AsReadOnly());\n    }\n\n    /// <summary>\n    /// Analyze two episodes to find an introduction sequence shared between them.\n    /// </summary>\n    /// <param name=\"lhsId\">First episode id.</param>\n    /// <param name=\"lhsPoints\">First episode fingerprint points.</param>\n    /// <param name=\"rhsId\">Second episode id.</param>\n    /// <param name=\"rhsPoints\">Second episode fingerprint points.</param>\n    /// <returns>Intros for the first and second episodes.</returns>\n    public (Segment Lhs, Segment Rhs) CompareEpisodes(\n        Guid lhsId,\n        uint[] lhsPoints,\n        Guid rhsId,\n        uint[] rhsPoints)\n    {\n        // Creates an inverted fingerprint point index for both episodes.\n        // For every point which is a 100% match, search for an introduction at that point.\n        var (lhsRanges, rhsRanges) = SearchInvertedIndex(lhsId, lhsPoints, rhsId, rhsPoints);\n\n        if (lhsRanges.Count > 0)\n        {\n            _logger.LogTrace(\"Index search successful\");\n\n            return GetLongestTimeRange(lhsId, lhsRanges, rhsId, rhsRanges);\n        }\n\n        _logger.LogTrace(\n            \"Unable to find a shared introduction sequence between {LHS} and {RHS}\",\n            lhsId,\n            rhsId);\n\n        return (new Segment(lhsId), new Segment(rhsId));\n    }\n\n    /// <summary>\n    /// Locates the longest range of similar audio and returns an Intro class for each range.\n    /// </summary>\n    /// <param name=\"lhsId\">First episode id.</param>\n    /// <param name=\"lhsRanges\">First episode shared timecodes.</param>\n    /// <param name=\"rhsId\">Second episode id.</param>\n    /// <param name=\"rhsRanges\">Second episode shared timecodes.</param>\n    /// <returns>Intros for the first and second episodes.</returns>\n    private (Segment Lhs, Segment Rhs) GetLongestTimeRange(\n        Guid lhsId,\n        List<TimeRange> lhsRanges,\n        Guid rhsId,\n        List<TimeRange> rhsRanges)\n    {\n        // Store the longest time range as the introduction.\n        lhsRanges.Sort();\n        rhsRanges.Sort();\n\n        var lhsIntro = lhsRanges[0];\n        var rhsIntro = rhsRanges[0];\n\n        // If the intro starts early in the episode, move it to the beginning.\n        if (lhsIntro.Start <= 5)\n        {\n            lhsIntro.Start = 0;\n        }\n\n        if (rhsIntro.Start <= 5)\n        {\n            rhsIntro.Start = 0;\n        }\n\n        // Create Intro classes for each time range.\n        return (new Segment(lhsId, lhsIntro), new Segment(rhsId, rhsIntro));\n    }\n\n    /// <summary>\n    /// Search for a shared introduction sequence using inverted indexes.\n    /// </summary>\n    /// <param name=\"lhsId\">LHS ID.</param>\n    /// <param name=\"lhsPoints\">Left episode fingerprint points.</param>\n    /// <param name=\"rhsId\">RHS ID.</param>\n    /// <param name=\"rhsPoints\">Right episode fingerprint points.</param>\n    /// <returns>List of shared TimeRanges between the left and right episodes.</returns>\n    private (List<TimeRange> Lhs, List<TimeRange> Rhs) SearchInvertedIndex(\n        Guid lhsId,\n        uint[] lhsPoints,\n        Guid rhsId,\n        uint[] rhsPoints)\n    {\n        var lhsRanges = new List<TimeRange>();\n        var rhsRanges = new List<TimeRange>();\n\n        // Generate inverted indexes for the left and right episodes.\n        var lhsIndex = FFmpegWrapper.CreateInvertedIndex(lhsId, lhsPoints);\n        var rhsIndex = FFmpegWrapper.CreateInvertedIndex(rhsId, rhsPoints);\n        var indexShifts = new HashSet<int>();\n\n        // For all audio points in the left episode, check if the right episode has a point which matches exactly.\n        // If an exact match is found, calculate the shift that must be used to align the points.\n        foreach (var kvp in lhsIndex)\n        {\n            var originalPoint = kvp.Key;\n\n            for (var i = -1 * invertedIndexShift; i <= invertedIndexShift; i++)\n            {\n                var modifiedPoint = (uint)(originalPoint + i);\n\n                if (rhsIndex.TryGetValue(modifiedPoint, out int modifiedPointValue))\n                {\n                    var lhsFirst = (int)lhsIndex[originalPoint];\n                    var rhsFirst = modifiedPointValue;\n                    indexShifts.Add(rhsFirst - lhsFirst);\n                }\n            }\n        }\n\n        // Use all discovered shifts to compare the episodes.\n        foreach (var shift in indexShifts)\n        {\n            var (lhsIndexContiguous, rhsIndexContiguous) = FindContiguous(lhsPoints, rhsPoints, shift);\n            if (lhsIndexContiguous.End > 0 && rhsIndexContiguous.End > 0)\n            {\n                lhsRanges.Add(lhsIndexContiguous);\n                rhsRanges.Add(rhsIndexContiguous);\n            }\n        }\n\n        return (lhsRanges, rhsRanges);\n    }\n\n    /// <summary>\n    /// Finds the longest contiguous region of similar audio between two fingerprints using the provided shift amount.\n    /// </summary>\n    /// <param name=\"lhs\">First fingerprint to compare.</param>\n    /// <param name=\"rhs\">Second fingerprint to compare.</param>\n    /// <param name=\"shiftAmount\">Amount to shift one fingerprint by.</param>\n    private (TimeRange Lhs, TimeRange Rhs) FindContiguous(\n        uint[] lhs,\n        uint[] rhs,\n        int shiftAmount)\n    {\n        var leftOffset = 0;\n        var rightOffset = 0;\n\n        // Calculate the offsets for the left and right hand sides.\n        if (shiftAmount < 0)\n        {\n            leftOffset -= shiftAmount;\n        }\n        else\n        {\n            rightOffset += shiftAmount;\n        }\n\n        // Store similar times for both LHS and RHS.\n        var lhsTimes = new List<double>();\n        var rhsTimes = new List<double>();\n        var upperLimit = Math.Min(lhs.Length, rhs.Length) - Math.Abs(shiftAmount);\n\n        // XOR all elements in LHS and RHS, using the shift amount from above.\n        for (var i = 0; i < upperLimit; i++)\n        {\n            // XOR both samples at the current position.\n            var lhsPosition = i + leftOffset;\n            var rhsPosition = i + rightOffset;\n            var diff = lhs[lhsPosition] ^ rhs[rhsPosition];\n\n            // If the difference between the samples is small, flag both times as similar.\n            if (CountBits(diff) > maximumDifferences)\n            {\n                continue;\n            }\n\n            var lhsTime = lhsPosition * SamplesToSeconds;\n            var rhsTime = rhsPosition * SamplesToSeconds;\n\n            lhsTimes.Add(lhsTime);\n            rhsTimes.Add(rhsTime);\n        }\n\n        // Ensure the last timestamp is checked\n        lhsTimes.Add(double.MaxValue);\n        rhsTimes.Add(double.MaxValue);\n\n        // Now that both fingerprints have been compared at this shift, see if there's a contiguous time range.\n        var lContiguous = TimeRangeHelpers.FindContiguous(lhsTimes.ToArray(), maximumTimeSkip);\n        if (lContiguous is null || lContiguous.Duration < minimumIntroDuration)\n        {\n            return (new TimeRange(), new TimeRange());\n        }\n\n        // Since LHS had a contiguous time range, RHS must have one also.\n        var rContiguous = TimeRangeHelpers.FindContiguous(rhsTimes.ToArray(), maximumTimeSkip)!;\n\n        if (this._analyzingType == MediaSegmentType.Intro)\n        {\n            // Tweak the end timestamps just a bit to ensure as little content as possible is skipped over.\n            // TODO: remove this\n            if (lContiguous.Duration >= 90)\n            {\n                lContiguous.End -= 2 * maximumTimeSkip;\n                rContiguous.End -= 2 * maximumTimeSkip;\n            }\n            else if (lContiguous.Duration >= 30)\n            {\n                lContiguous.End -= maximumTimeSkip;\n                rContiguous.End -= maximumTimeSkip;\n            }\n        }\n\n        return (lContiguous, rContiguous);\n    }\n\n    /// <summary>\n    /// Adjusts the end timestamps of all intros so that they end at silence.\n    /// </summary>\n    /// <param name=\"episodes\">QueuedEpisodes to adjust.</param>\n    /// <param name=\"originalIntros\">Original introductions.</param>\n    private Dictionary<Guid, Segment> AdjustIntroEndTimes(\n        ReadOnlyCollection<QueuedMedia> episodes,\n        Dictionary<Guid, Segment> originalIntros)\n    {\n        // The minimum duration of audio that must be silent before adjusting the intro's end.\n        var minimumSilence = Plugin.Instance!.Configuration.SilenceDetectionMinimumDuration;\n\n        Dictionary<Guid, Segment> modifiedIntros = new();\n\n        // For all episodes\n        foreach (var episode in episodes)\n        {\n            _logger.LogTrace(\n                \"Adjusting introduction end time for {Name} ({Id})\",\n                episode.Name,\n                episode.ItemId);\n\n            // If no intro was found for this episode, skip it.\n            if (!originalIntros.TryGetValue(episode.ItemId, out var originalIntro))\n            {\n                _logger.LogTrace(\"{Name} does not have an intro\", episode.Name);\n                continue;\n            }\n\n            // Only adjust the end timestamp of the intro\n            var originalIntroEnd = new TimeRange(originalIntro.End - 15, originalIntro.End);\n\n            _logger.LogTrace(\n                \"{Name} original intro: {Start} - {End}\",\n                episode.Name,\n                originalIntro.Start,\n                originalIntro.End);\n\n            // Detect silence in the media file up to the end of the intro.\n            var silence = FFmpegWrapper.DetectSilence(episode, (int)originalIntro.End + 2);\n\n            // For all periods of silence\n            foreach (var currentRange in silence)\n            {\n                _logger.LogTrace(\n                    \"{Name} silence: {Start} - {End}\",\n                    episode.Name,\n                    currentRange.Start,\n                    currentRange.End);\n\n                // Ignore any silence that:\n                // * doesn't intersect the ending of the intro, or\n                // * is shorter than the user defined minimum duration, or\n                // * starts before the introduction does\n                if (\n                    !originalIntroEnd.Intersects(currentRange) ||\n                    currentRange.Duration < silenceDetectionMinimumDuration ||\n                    currentRange.Start < originalIntro.Start)\n                {\n                    continue;\n                }\n\n                // Adjust the end timestamp of the intro to match the start of the silence region.\n                originalIntro.End = currentRange.Start;\n                break;\n            }\n\n            _logger.LogTrace(\n                \"{Name} adjusted intro: {Start} - {End}\",\n                episode.Name,\n                originalIntro.Start,\n                originalIntro.End);\n\n            // Add the (potentially) modified intro back.\n            modifiedIntros[episode.ItemId] = originalIntro;\n        }\n\n        return modifiedIntros;\n    }\n\n    /// <summary>\n    /// Count the number of bits that are set in the provided number.\n    /// </summary>\n    /// <param name=\"number\">Number to count bits in.</param>\n    /// <returns>Number of bits that are equal to 1.</returns>\n    public int CountBits(uint number)\n    {\n        return BitOperations.PopCount(number);\n    }\n\n    /// <summary>\n    /// Be sure the segment have a valid time.\n    /// </summary>\n    /// <param name=\"seg\">Segment.</param>\n    /// <returns>True is valid.</returns>\n    private bool ValidateTime(Segment seg)\n    {\n        return seg.End > seg.Start;\n    }\n}\n"
  },
  {
    "path": "Jellyfin.Plugin.MediaAnalyzer/Analyzers/IMediaFileAnalyzer.cs",
    "content": "namespace Jellyfin.Plugin.MediaAnalyzer;\n\nusing System;\nusing System.Collections.ObjectModel;\nusing System.Threading;\nusing System.Threading.Tasks;\nusing Jellyfin.Data.Enums;\n\n/// <summary>\n/// Media file analyzer interface.\n/// </summary>\npublic interface IMediaFileAnalyzer\n{\n    /// <summary>\n    /// Analyze media files for shared introductions or credits, returning all media files that were not analyzed, analyzed and optional metadata for both.\n    /// </summary>\n    /// <param name=\"analysisQueue\">Collection of unanalyzed media files.</param>\n    /// <param name=\"mode\">Analysis mode.</param>\n    /// <param name=\"cancellationToken\">Cancellation token from scheduled task.</param>\n    /// <returns>Collection of media files that were **unsuccessfully analyzed** and successfull.</returns>\n    public Task<(ReadOnlyCollection<QueuedMedia> NotAnalyzed, ReadOnlyDictionary<Guid, Segment> Analyzed, ReadOnlyDictionary<Guid, SegmentMetadata> SegmentMetadata)> AnalyzeMediaFilesAsync(\n        ReadOnlyCollection<QueuedMedia> analysisQueue,\n        MediaSegmentType mode,\n        CancellationToken cancellationToken);\n}\n"
  },
  {
    "path": "Jellyfin.Plugin.MediaAnalyzer/Configuration/PluginConfiguration.cs",
    "content": "using MediaBrowser.Model.Plugins;\n\nnamespace Jellyfin.Plugin.MediaAnalyzer.Configuration;\n\n/// <summary>\n/// Plugin configuration.\n/// </summary>\npublic class PluginConfiguration : BasePluginConfiguration\n{\n    /// <summary>\n    /// Initializes a new instance of the <see cref=\"PluginConfiguration\"/> class.\n    /// </summary>\n    public PluginConfiguration()\n    {\n    }\n\n    // ===== General settings =====\n\n    /// <summary>\n    /// Gets or sets a value indicating whether we run after a library scan.\n    /// </summary>\n    public bool RunAfterLibraryScan { get; set; } = true;\n\n    /// <summary>\n    /// Gets or sets a value indicating whether we run after library item added or updated events.\n    /// </summary>\n    public bool RunAfterAddOrUpdateEvent { get; set; } = true;\n\n    // ===== Analysis settings =====\n\n    /// <summary>\n    /// Gets or sets a value indicating whether the episode's fingerprint should be cached to the filesystem.\n    /// </summary>\n    public bool CacheFingerprints { get; set; } = false;\n\n    /// <summary>\n    /// Gets or sets a value indicating whether the Blacklist should be resetted.\n    /// </summary>\n    public bool ResetBlacklist { get; set; } = false;\n\n    /// <summary>\n    /// Gets or sets a value indicating whether the Blacklist should be created or used.\n    /// </summary>\n    public bool EnableBlacklist { get; set; } = true;\n\n    /// <summary>\n    /// Gets or sets the max degree of parallelism used when analyzing episodes.\n    /// </summary>\n    public int MaxParallelism { get; set; } = 2;\n\n    /// <summary>\n    /// Gets or sets the comma separated list of library names to analyze. If empty, all libraries will be analyzed.\n    /// </summary>\n    public string SelectedLibraries { get; set; } = string.Empty;\n\n    /// <summary>\n    /// Gets or sets the comma separated list of tv shows and seasons to skip the analyze. Format: \"My Show;S01;S02, Another Show\".\n    /// </summary>\n    public string SkippedTvShows { get; set; } = string.Empty;\n\n    /// <summary>\n    /// Gets or sets the comma separated list of movies to skip the analyze.\".\n    /// </summary>\n    public string SkippedMovies { get; set; } = string.Empty;\n\n    /// <summary>\n    /// Gets or sets a value indicating whether to analyze season 0.\n    /// </summary>\n    public bool AnalyzeSeasonZero { get; set; } = false;\n\n    // ===== Custom analysis settings =====\n\n    /// <summary>\n    /// Gets or sets the percentage of each episode's audio track to analyze.\n    /// </summary>\n    public int AnalysisPercent { get; set; } = 30;\n\n    /// <summary>\n    /// Gets or sets the upper limit (in minutes) on the length of each episode's audio track that will be analyzed.\n    /// </summary>\n    public int AnalysisLengthLimit { get; set; } = 15;\n\n    /// <summary>\n    /// Gets or sets the minimum length of similar audio that will be considered an introduction.\n    /// </summary>\n    public int MinimumIntroDuration { get; set; } = 15;\n\n    /// <summary>\n    /// Gets or sets the maximum length of similar audio that will be considered an introduction.\n    /// </summary>\n    public int MaximumIntroDuration { get; set; } = 120;\n\n    /// <summary>\n    /// Gets or sets the minimum length of similar audio that will be considered ending credits.\n    /// </summary>\n    public int MinimumCreditsDuration { get; set; } = 15;\n\n    /// <summary>\n    /// Gets or sets the upper limit (in seconds) on the length of each episode's audio track that will be analyzed when searching for ending credits.\n    /// </summary>\n    public int MaximumEpisodeCreditsDuration { get; set; } = 240;\n\n    /// <summary>\n    /// Gets or sets the upper limit (in seconds) on the length of each movie's audio track that will be analyzed when searching for ending credits.\n    /// </summary>\n    public int MaximumMovieCreditsDuration { get; set; } = 900;\n\n    /// <summary>\n    /// Gets or sets the minimum percentage of a frame that must consist of black pixels before it is considered a black frame.\n    /// </summary>\n    public int BlackFrameMinimumPercentage { get; set; } = 85;\n\n    /// <summary>\n    /// Gets or sets the regular expression used to detect introduction chapters.\n    /// </summary>\n    public string ChapterAnalyzerIntroductionPattern { get; set; } =\n        @\"(^|\\s)(Intro|Introduction|OP|Opening)(\\s|$)\";\n\n    /// <summary>\n    /// Gets or sets the regular expression used to detect ending credit chapters.\n    /// </summary>\n    public string ChapterAnalyzerEndCreditsPattern { get; set; } =\n        @\"(^|\\s)(Credits?|Ending|End|Outro)(\\s|$)\";\n\n    // ===== Internal algorithm settings =====\n\n    /// <summary>\n    /// Gets or sets the maximum number of bits (out of 32 total) that can be different between two Chromaprint points before they are considered dissimilar.\n    /// Defaults to 6 (81% similar).\n    /// </summary>\n    public int MaximumFingerprintPointDifferences { get; set; } = 6;\n\n    /// <summary>\n    /// Gets or sets the maximum number of seconds that can pass between two similar fingerprint points before a new time range is started.\n    /// </summary>\n    public double MaximumTimeSkip { get; set; } = 3.5;\n\n    /// <summary>\n    /// Gets or sets the amount to shift inverted indexes by.\n    /// </summary>\n    public int InvertedIndexShift { get; set; } = 2;\n\n    /// <summary>\n    /// Gets or sets the maximum amount of noise (in dB) that is considered silent.\n    /// Lowering this number will increase the filter's sensitivity to noise.\n    /// </summary>\n    public int SilenceDetectionMaximumNoise { get; set; } = -50;\n\n    /// <summary>\n    /// Gets or sets the minimum duration of audio (in seconds) that is considered silent.\n    /// </summary>\n    public double SilenceDetectionMinimumDuration { get; set; } = 0.33;\n}\n"
  },
  {
    "path": "Jellyfin.Plugin.MediaAnalyzer/Configuration/configPage.html",
    "content": "<!DOCTYPE html>\n<html lang=\"en\">\n    <head>\n        <meta charset=\"utf-8\" />\n    </head>\n\n    <body>\n        <div id=\"TemplateConfigPage\" data-role=\"page\" class=\"page type-interior pluginConfigurationPage\" data-require=\"emby-input,emby-button,emby-select,emby-checkbox\">\n            <div data-role=\"content\">\n                <div class=\"content-primary\">\n                    <form id=\"FingerprintConfigForm\">\n                        <fieldset class=\"verticalSection-extrabottompadding\">\n                            <legend>General</legend>\n\n                            <div class=\"checkboxContainer checkboxContainer-withDescription\">\n                                <label class=\"emby-checkbox-label\">\n                                    <input id=\"RunAfterLibraryScan\" type=\"checkbox\" is=\"emby-checkbox\" />\n                                    <span>Run after Library Scan</span>\n                                </label>\n\n                                <div class=\"fieldDescription\">Whenever the media library scan task ends, run the analyzer.</div>\n                            </div>\n\n                            <div class=\"checkboxContainer checkboxContainer-withDescription\">\n                                <label class=\"emby-checkbox-label\">\n                                    <input id=\"RunAfterAddOrUpdateEvent\" type=\"checkbox\" is=\"emby-checkbox\" />\n                                    <span>Run after Library update events</span>\n                                </label>\n\n                                <div class=\"fieldDescription\">When media folder watch is enabled the library will push events we can also listen for.</div>\n\n                            </div>\n\n                            <legend>Analysis</legend>\n\n                            <div class=\"checkboxContainer checkboxContainer-withDescription\">\n                                <label class=\"emby-checkbox-label\">\n                                    <input id=\"AnalyzeSeasonZero\" type=\"checkbox\" is=\"emby-checkbox\" />\n                                    <span>Analyze show extras</span>\n                                </label>\n\n                                <div class=\"fieldDescription\">Analyze show extras (specials), listed as season 0.</div>\n                            </div>\n\n                            <div class=\"checkboxContainer checkboxContainer-withDescription\">\n                                <label class=\"emby-checkbox-label\">\n                                    <input id=\"CacheFingerprints\" type=\"checkbox\" is=\"emby-checkbox\" />\n                                    <span>Cache Fringerprints</span>\n                                </label>\n\n                                <div class=\"fieldDescription\">Requires lot's of disk space. Should be just enabled for development or when you play around with intro/credits options (Also disable blacklisting!).</div>\n                            </div>\n\n                            <div class=\"checkboxContainer checkboxContainer-withDescription\">\n                                <label class=\"emby-checkbox-label\">\n                                    <input id=\"EnableBlacklist\" type=\"checkbox\" is=\"emby-checkbox\" />\n                                    <span>Enable Blacklisting</span>\n                                </label>\n\n                                <div class=\"fieldDescription\">If we can't find an Intro/Outro Segment for an episode or movie it is blacklisted to prevent repeated analysis.</div>\n                            </div>\n\n                            <div class=\"checkboxContainer checkboxContainer-withDescription\">\n                                <label class=\"emby-checkbox-label\">\n                                    <input id=\"ResetBlacklist\" type=\"checkbox\" is=\"emby-checkbox\" />\n                                    <span>Delete Blacklist</span>\n                                </label>\n\n                                <div class=\"fieldDescription\">Delete the blacklist of segments.</div>\n                            </div>\n\n                            <div class=\"inputContainer\">\n                                <label class=\"inputLabel inputLabelUnfocused\" for=\"MaxParallelism\"> Maximum degree of parallelism </label>\n                                <input id=\"MaxParallelism\" type=\"number\" is=\"emby-input\" min=\"1\" />\n                                <div class=\"fieldDescription\">Maximum degree of parallelism to use when analyzing.</div>\n                            </div>\n\n                            <div class=\"inputContainer\">\n                                <label class=\"inputLabel inputLabelUnfocused\" for=\"SelectedLibraries\"> Limit analysis to the following libraries </label>\n                                <input id=\"SelectedLibraries\" type=\"text\" is=\"emby-input\" />\n                                <div class=\"fieldDescription\">Enter the names of libraries to analyze, separated by commas. If this field is left blank, all libraries on the server containing movies or television episodes will be analyzed.</div>\n                            </div>\n\n                            <div class=\"inputContainer\">\n                                <label class=\"inputLabel inputLabelUnfocused\" for=\"SkippedTvShows\"> Skip TV Shows </label>\n                                <input id=\"SkippedTvShows\" type=\"text\" is=\"emby-input\" />\n                                <div class=\"fieldDescription\">Enter the names of tv shows to skip the analyze, separated by commas. You can also skip seasons. Format: \"My Show;S01;S02, Another Show, Third Show;S03;S05\"</div>\n                            </div>\n\n                            <div class=\"inputContainer\">\n                                <label class=\"inputLabel inputLabelUnfocused\" for=\"SkippedMovies\"> Skip Movies </label>\n                                <input id=\"SkippedMovies\" type=\"text\" is=\"emby-input\" />\n                                <div class=\"fieldDescription\">Enter the names of Movies to skip the analyze, separated by commas. Format: \"The Godfather, Spiderman, Matrix\"</div>\n                            </div>\n\n                            <details id=\"intro_reqs\">\n                                <summary>Modify introduction requirements</summary>\n\n                                <div class=\"inputContainer\">\n                                    <label class=\"inputLabel inputLabelUnfocused\" for=\"AnalysisPercent\"> Percent of audio to analyze </label>\n                                    <input id=\"AnalysisPercent\" type=\"number\" is=\"emby-input\" min=\"1\" max=\"90\" />\n                                    <div class=\"fieldDescription\">Analysis will be limited to this percentage of each episode's audio. For example, a value of 25 (the default) will limit analysis to the first quarter of each episode.</div>\n                                </div>\n\n                                <div class=\"inputContainer\">\n                                    <label class=\"inputLabel inputLabelUnfocused\" for=\"AnalysisLengthLimit\"> Maximum runtime of audio to analyze (in minutes) </label>\n                                    <input id=\"AnalysisLengthLimit\" type=\"number\" is=\"emby-input\" min=\"1\" />\n                                    <div class=\"fieldDescription\">Analysis will be limited to this amount of each episode's audio. For example, a value of 10 (the default) will limit analysis to the first 10 minutes of each episode.</div>\n                                </div>\n\n                                <div class=\"inputContainer\">\n                                    <label class=\"inputLabel inputLabelUnfocused\" for=\"MinimumIntroDuration\"> Minimum introduction duration (in seconds) </label>\n                                    <input id=\"MinimumIntroDuration\" type=\"number\" is=\"emby-input\" min=\"1\" />\n                                    <div class=\"fieldDescription\">Similar sounding audio which is shorter than this duration will not be considered an introduction.</div>\n                                </div>\n\n                                <div class=\"inputContainer\">\n                                    <label class=\"inputLabel inputLabelUnfocused\" for=\"MaximumIntroDuration\"> Maximum introduction duration (in seconds) </label>\n                                    <input id=\"MaximumIntroDuration\" type=\"number\" is=\"emby-input\" min=\"1\" />\n                                    <div class=\"fieldDescription\">Similar sounding audio which is longer than this duration will not be considered an introduction.</div>\n                                </div>\n\n                                <p>The amount of each episode's audio that will be analyzed is determined using both the percentage of audio and maximum runtime of audio to analyze. The minimum of (episode duration * percent, maximum runtime) is the amount of audio that will be analyzed.</p>\n\n                                <p>\n                                    If the audio percentage or maximum runtime settings are modified, the cached fingerprints and introduction timestamps for each season you want to analyze with the modified settings <strong>will have to be deleted.</strong>\n\n                                    Increasing either of these settings will cause episode analysis to take much longer.\n                                </p>\n                            </details>\n\n                            <details id=\"outros_reqs\">\n                                <summary>Modify Credits requirements</summary>\n\n                                <div class=\"inputContainer\">\n                                    <label class=\"inputLabel inputLabelUnfocused\" for=\"BlackFrameMinimumPercentage\"> Black frame (in percent) </label>\n                                    <input id=\"BlackFrameMinimumPercentage\" type=\"number\" is=\"emby-input\" min=\"60\" max=\"99\" />\n                                    <div class=\"fieldDescription\">Sets the minimum percentage of a frame that must consist of black pixels before it is considered a black frame.</div>\n                                </div>\n\n                                <div class=\"inputContainer\">\n                                    <label class=\"inputLabel inputLabelUnfocused\" for=\"MinimumCreditsDuration\"> Minimum credits duration (in seconds) </label>\n                                    <input id=\"MinimumCreditsDuration\" type=\"number\" is=\"emby-input\" min=\"1\" />\n                                    <div class=\"fieldDescription\">Sets the minimum length of similar audio that will be considered ending credits.</div>\n                                </div>\n\n                                <div class=\"inputContainer\">\n                                    <label class=\"inputLabel inputLabelUnfocused\" for=\"MaximumEpisodeCreditsDuration\"> Maximum episode credits duration (in seconds) </label>\n                                    <input id=\"MaximumEpisodeCreditsDuration\" type=\"number\" is=\"emby-input\" min=\"1\" />\n                                    <div class=\"fieldDescription\">Sets the upper limit on the length of each episode that will be analyzed when searching for ending credits.</div>\n                                </div>\n\n                                <div class=\"inputContainer\">\n                                    <label class=\"inputLabel inputLabelUnfocused\" for=\"MaximumMovieCreditsDuration\"> Maximum movie credits duration (in seconds) </label>\n                                    <input id=\"MaximumMovieCreditsDuration\" type=\"number\" is=\"emby-input\" min=\"1\" />\n                                    <div class=\"fieldDescription\">Sets the upper limit on the length of each movie that will be analyzed when searching for ending credits.</div>\n                                </div>\n                            </details>\n\n                            <details id=\"silence\">\n                                <summary>Silence detection options</summary>\n\n                                <div class=\"inputContainer\">\n                                    <label class=\"inputLabel inputLabelUnfocused\" for=\"SilenceDetectionMaximumNoise\"> Noise tolerance </label>\n                                    <input id=\"SilenceDetectionMaximumNoise\" type=\"number\" is=\"emby-input\" min=\"-90\" max=\"0\" />\n                                    <div class=\"fieldDescription\">Noise tolerance in negative decibels.</div>\n                                </div>\n\n                                <div class=\"inputContainer\">\n                                    <label class=\"inputLabel inputLabelUnfocused\" for=\"SilenceDetectionMinimumDuration\"> Minimum silence duration </label>\n                                    <input id=\"SilenceDetectionMinimumDuration\" type=\"number\" is=\"emby-input\" min=\"0\" step=\"0.01\" />\n                                    <div class=\"fieldDescription\">Minimum silence duration in seconds before adjusting introduction end time.</div>\n                                </div>\n                            </details>\n                        </fieldset>\n                        <div>\n                            <button is=\"emby-button\" type=\"submit\" class=\"raised button-submit block emby-button\">\n                                <span>Save</span>\n                            </button>\n                            <br />\n                        </div>\n                    </form>\n                </div>\n\n                <details id=\"support\">\n                    <summary>Support Bundle</summary>\n\n                    <textarea id=\"supportBundle\" rows=\"20\" cols=\"75\" readonly></textarea>\n                </details>\n\n                <details id=\"visualizer\">\n                    <summary>Advanced</summary>\n\n                    <h3 style=\"margin: 0\">Select episodes to manage</h3>\n                    <select id=\"troubleshooterShow\"></select>\n                    <select id=\"troubleshooterSeason\"></select>\n                    <br />\n\n                    <select id=\"troubleshooterEpisode1\"></select>\n                    <select id=\"troubleshooterEpisode2\"></select>\n                    <br />\n                    <br />\n\n                    <div id=\"timestampEditor\" style=\"display: none\">\n                        <h3 style=\"margin: 0\">Introduction timestamp editor</h3>\n                        <p style=\"margin: 0\">All times are in seconds.</p>\n\n                        <p id=\"editLeftEpisodeTitle\" style=\"margin-bottom: 0\"></p>\n                        <input style=\"width: 4em\" type=\"number\" min=\"0\" id=\"editLeftEpisodeStart\" /> to\n                        <input style=\"width: 4em; margin-bottom: 10px\" type=\"number\" min=\"0\" id=\"editLeftEpisodeEnd\" />\n\n                        <p id=\"editRightEpisodeTitle\" style=\"margin-top: 0; margin-bottom: 0\"></p>\n                        <input style=\"width: 4em\" type=\"number\" min=\"0\" id=\"editRightEpisodeStart\" /> to\n                        <input style=\"width: 4em; margin-bottom: 10px\" type=\"number\" min=\"0\" id=\"editRightEpisodeEnd\" />\n                        <br />\n\n                        <button id=\"btnUpdateTimestamps\" type=\"button\">Update timestamps</button>\n                        <br />\n                        <br />\n\n                        <button id=\"btnEraseSeasonTimestamps\" type=\"button\">Erase all timestamps for this season</button>\n                        <hr />\n                    </div>\n\n                    <button id=\"btnEraseIntroTimestamps\">Erase all introduction timestamps (globally)</button>\n                    <br />\n\n                    <button id=\"btnEraseCreditTimestamps\">Erase all end credits timestamps (globally)</button>\n                    <br />\n                    <br />\n\n                    <h3>Fingerprint Visualizer</h3>\n                    <p>\n                        Interactively compare the audio fingerprints of two episodes. <br />\n                        The blue and red bar to the right of the fingerprint diff turns blue when the corresponding fingerprint points are at least 80% similar.\n                    </p>\n                    <table>\n                        <thead>\n                            <td style=\"min-width: 100px; font-weight: bold\">Key</td>\n                            <td style=\"font-weight: bold\">Function</td>\n                        </thead>\n                        <tbody>\n                            <tr>\n                                <td>Up arrow</td>\n                                <td>Shift the left episode up by 0.128 seconds. Holding control will shift the episode by 10 seconds.</td>\n                            </tr>\n                            <tr>\n                                <td>Down arrow</td>\n                                <td>Shift the left episode down by 0.128 seconds. Holding control will shift the episode by 10 seconds.</td>\n                            </tr>\n                            <tr>\n                                <td>Right arrow</td>\n                                <td>Advance to the next pair of episodes.</td>\n                            </tr>\n                            <tr>\n                                <td>Left arrow</td>\n                                <td>Go back to the previous pair of episodes.</td>\n                            </tr>\n                        </tbody>\n                    </table>\n                    <br />\n\n                    <span>Shift amount:</span>\n                    <input type=\"number\" min=\"-3000\" max=\"3000\" value=\"0\" id=\"offset\" />\n                    <br />\n                    <span id=\"suggestedShifts\">Suggested shifts:</span>\n                    <br />\n                    <br />\n\n                    <canvas id=\"troubleshooter\"></canvas>\n                    <span id=\"timestampContainer\">\n                        <span id=\"timestamps\"></span> <br />\n                        <span id=\"intros\"></span>\n                    </span>\n                </details>\n            </div>\n\n            <script src=\"configurationpage?name=visualizer.js\"></script>\n\n            <script>\n                // first and second episodes to fingerprint & compare\n                var lhs = [];\n                var rhs = [];\n\n                // fingerprint point comparison & miminum similarity threshold (at most 6 bits out of 32 can be different)\n                var fprDiffs = [];\n                var fprDiffMinimum = (1 - 6 / 32) * 100;\n\n                // seasons grouped by show\n                var shows = {};\n\n                // settings elements\n                var visualizer = document.querySelector(\"details#visualizer\");\n                var support = document.querySelector(\"details#support\");\n                var btnEraseIntroTimestamps = document.querySelector(\"button#btnEraseIntroTimestamps\");\n                var btnEraseCreditTimestamps = document.querySelector(\"button#btnEraseCreditTimestamps\");\n\n                // all plugin configuration fields that can be get or set with .value (i.e. strings or numbers).\n                var configurationFields = [\n                    // General\n                    // analysis\n                    \"MaxParallelism\",\n                    \"SelectedLibraries\",\n                    \"SkippedTvShows\",\n                    \"SkippedMovies\",\n                    \"AnalysisPercent\",\n                    \"AnalysisLengthLimit\",\n                    \"MinimumIntroDuration\",\n                    \"MaximumIntroDuration\",\n                    \"MinimumCreditsDuration\",\n                    \"MaximumEpisodeCreditsDuration\",\n                    \"MaximumMovieCreditsDuration\",\n                    \"BlackFrameMinimumPercentage\",\n                    // internals\n                    \"SilenceDetectionMaximumNoise\",\n                    \"SilenceDetectionMinimumDuration\",\n                ];\n\n                var booleanConfigurationFields = [\"AnalyzeSeasonZero\",\"RunAfterLibraryScan\",\"RunAfterAddOrUpdateEvent\",\"CacheFingerprints\",\"ResetBlacklist\",\"EnableBlacklist\"];\n\n                // visualizer elements\n                var canvas = document.querySelector(\"canvas#troubleshooter\");\n                var selectShow = document.querySelector(\"select#troubleshooterShow\");\n                var selectSeason = document.querySelector(\"select#troubleshooterSeason\");\n                var selectEpisode1 = document.querySelector(\"select#troubleshooterEpisode1\");\n                var selectEpisode2 = document.querySelector(\"select#troubleshooterEpisode2\");\n                var txtOffset = document.querySelector(\"input#offset\");\n                var txtSuggested = document.querySelector(\"span#suggestedShifts\");\n                var btnUpdateTimestamps = document.querySelector(\"button#btnUpdateTimestamps\");\n                var timeContainer = document.querySelector(\"span#timestampContainer\");\n\n                var windowHashInterval = 0;\n\n                // when the fingerprint visualizer opens, populate show names\n                async function visualizerToggled() {\n                    if (!visualizer.open) {\n                        return;\n                    }\n\n                    // ensure the series select is empty\n                    while (selectShow.options.length > 0) {\n                        selectShow.remove(0);\n                    }\n\n                    Dashboard.showLoadingMsg();\n\n                    shows = await getJson(\"JellyfinPluginIntroSkip/Shows\");\n\n                    var sorted = [];\n                    for (var series in shows) {\n                        sorted.push(series);\n                    }\n                    sorted.sort();\n\n                    for (var show of sorted) {\n                        addItem(selectShow, show, show);\n                    }\n\n                    selectShow.value = \"\";\n\n                    Dashboard.hideLoadingMsg();\n                }\n\n                // fetch the support bundle whenever the detail section is opened.\n                async function supportToggled() {\n                    if (!support.open) {\n                        return;\n                    }\n\n                    // Fetch the support bundle\n                    const bundle = await fetchWithAuth(\"JellyfinPluginIntroSkipSupport/SupportBundle\", \"GET\", null);\n                    const bundleText = await bundle.text();\n\n                    // Display it to the user and select all\n                    const ta = document.querySelector(\"textarea#supportBundle\");\n                    ta.value = bundleText;\n                    ta.focus();\n                    ta.setSelectionRange(0, ta.value.length);\n\n                    // Attempt to copy it to the clipboard automatically, falling back\n                    // to prompting the user to press Ctrl + C.\n                    try {\n                        navigator.clipboard.writeText(bundleText);\n                        Dashboard.alert(\"Support bundle copied to clipboard\");\n                    } catch {\n                        Dashboard.alert(\"Press Ctrl+C to copy support bundle\");\n                    }\n                }\n\n                // show changed, populate seasons\n                async function showChanged() {\n                    clearSelect(selectSeason);\n\n                    // add all seasons from this show to the season select\n                    for (var season of shows[selectShow.value]) {\n                        addItem(selectSeason, season, season);\n                    }\n\n                    selectSeason.value = \"\";\n                }\n\n                // season changed, reload all episodes\n                async function seasonChanged() {\n                    const url = \"JellyfinPluginIntroSkip/Show/\" + encodeURI(selectShow.value) + \"/\" + selectSeason.value;\n                    const episodes = await getJson(url);\n\n                    clearSelect(selectEpisode1);\n                    clearSelect(selectEpisode2);\n\n                    let i = 1;\n                    for (let episode of episodes) {\n                        const strI = i.toLocaleString(\"en\", { minimumIntegerDigits: 2, maximumFractionDigits: 0 });\n                        addItem(selectEpisode1, strI + \": \" + episode.Name, episode.Id);\n                        addItem(selectEpisode2, strI + \": \" + episode.Name, episode.Id);\n                        i++;\n                    }\n\n                    setTimeout(() => {\n                        selectEpisode1.selectedIndex = 0;\n                        selectEpisode2.selectedIndex = 1;\n                        episodeChanged();\n                    }, 100);\n                }\n\n                // episode changed, get fingerprints & calculate diff\n                async function episodeChanged() {\n                    if (!selectEpisode1.value || !selectEpisode2.value) {\n                        return;\n                    }\n\n                    Dashboard.showLoadingMsg();\n\n                    lhs = await getJson(\"JellyfinPluginIntroSkip/Episode/\" + selectEpisode1.value + \"/Chromaprint\");\n                    rhs = await getJson(\"JellyfinPluginIntroSkip/Episode/\" + selectEpisode2.value + \"/Chromaprint\");\n\n                    Dashboard.hideLoadingMsg();\n\n                    txtOffset.value = \"0\";\n                    refreshBounds();\n                    renderTroubleshooter();\n                    findExactMatches();\n                    updateTimestampEditor();\n                }\n\n                // updates the timestamp editor\n                async function updateTimestampEditor() {\n                    // Get the title and ID of the left and right episodes\n                    const leftEpisode = selectEpisode1.options[selectEpisode1.selectedIndex];\n                    const rightEpisode = selectEpisode2.options[selectEpisode2.selectedIndex];\n\n                    // Try to get the timestamps of each intro, falling back a default value of zero if no intro was found\n                    let leftEpisodeIntro = await getJson(`MediaSegments/${leftEpisode.value}?includeSegmentTypes=[Intro]`);\n                    if (!leftEpisodeIntro.Items.length) {\n                        leftEpisodeIntro = { StartTicks: 0, EndTicks: 0 };\n                    } else {\n                        leftEpisodeIntro = { StartTicks: ticksToSeconds(leftEpisodeIntro.Items[0].StartTicks), EndTicks: ticksToSeconds(leftEpisodeIntro.Items[0].EndTicks) };\n                    }\n\n                    let rightEpisodeIntro = await getJson(`MediaSegments/${rightEpisode.value}?includeSegmentTypes=[Intro]`);\n                    if (!rightEpisodeIntro.Items.length) {\n                        rightEpisodeIntro = { StartTicks: 0, EndTicks: 0 };\n                    } else {\n                        rightEpisodeIntro = { StartTicks: ticksToSeconds(rightEpisodeIntro.Items[0].StartTicks), EndTicks: ticksToSeconds(rightEpisodeIntro.Items[0].EndTicks) };\n                    }\n\n                    // Update the editor for the first and second episodes\n                    document.querySelector(\"#timestampEditor\").style.display = \"unset\";\n                    document.querySelector(\"#editLeftEpisodeTitle\").textContent = leftEpisode.text;\n                    document.querySelector(\"#editLeftEpisodeStart\").value = Math.round(leftEpisodeIntro.StartTicks);\n                    document.querySelector(\"#editLeftEpisodeEnd\").value = Math.round(leftEpisodeIntro.EndTicks);\n\n                    document.querySelector(\"#editRightEpisodeTitle\").textContent = rightEpisode.text;\n                    document.querySelector(\"#editRightEpisodeStart\").value = Math.round(rightEpisodeIntro.StartTicks);\n                    document.querySelector(\"#editRightEpisodeEnd\").value = Math.round(rightEpisodeIntro.EndTicks);\n                }\n\n                // adds an item to a dropdown\n                function addItem(select, text, value) {\n                    let item = new Option(text, value);\n                    select.add(item);\n                }\n\n                // clear a select of items\n                function clearSelect(select) {\n                    let i,\n                        L = select.options.length - 1;\n                    for (i = L; i >= 0; i--) {\n                        select.remove(i);\n                    }\n                }\n\n                // convert between seconds and ticks\n                function ticksToSeconds(ticks) {\n                    return ticks / 10_000_000\n                }\n\n                // convert between seconds and ticks\n                function secondsToTicks(seconds) {\n                    return 10_000_000 * seconds\n                }\n\n                // make an authenticated GET to the server and parse the response as JSON\n                async function getJson(url) {\n                    return await fetchWithAuth(url, \"GET\").then((r) => {\n                        return r.json();\n                    });\n                }\n\n                // make an authenticated fetch to the server\n                async function fetchWithAuth(url, method, body) {\n                    url = ApiClient.serverAddress() + \"/\" + url;\n\n                    const reqInit = {\n                        method: method,\n                        headers: {\n                            Authorization: \"MediaBrowser Token=\" + ApiClient.accessToken(),\n                        },\n                        body: body,\n                    };\n\n                    if (method === \"POST\") {\n                        reqInit.headers[\"Content-Type\"] = \"application/json\";\n                    }\n\n                    return await fetch(url, reqInit);\n                }\n\n                // key pressed\n                function keyDown(e) {\n                    let episodeDelta = 0;\n                    let offsetDelta = 0;\n\n                    switch (e.key) {\n                        case \"ArrowDown\":\n                            // if the control key is pressed, shift LHS by 10s. Otherwise, shift by 1.\n                            offsetDelta = e.ctrlKey ? 10 / 0.128 : 1;\n                            break;\n\n                        case \"ArrowUp\":\n                            offsetDelta = e.ctrlKey ? -10 / 0.128 : -1;\n                            break;\n\n                        case \"ArrowRight\":\n                            episodeDelta = 2;\n                            break;\n\n                        case \"ArrowLeft\":\n                            episodeDelta = -2;\n                            break;\n\n                        default:\n                            return;\n                    }\n\n                    if (offsetDelta != 0) {\n                        txtOffset.value = Number(txtOffset.value) + Math.floor(offsetDelta);\n                    }\n\n                    if (episodeDelta != 0) {\n                        // calculate the number of episodes remaining in the LHS and RHS episode pickers\n                        const lhsRemaining = selectEpisode1.selectedIndex;\n                        const rhsRemaining = selectEpisode2.length - selectEpisode2.selectedIndex - 1;\n\n                        // if we're moving forward and the right episode picker is close to the end, don't move.\n                        if (episodeDelta > 0 && rhsRemaining <= 1) {\n                            return;\n                        } else if (episodeDelta < 0 && lhsRemaining <= 1) {\n                            return;\n                        }\n\n                        selectEpisode1.selectedIndex += episodeDelta;\n                        selectEpisode2.selectedIndex += episodeDelta;\n                        episodeChanged();\n                    }\n\n                    renderTroubleshooter();\n                    e.preventDefault();\n                }\n\n                // check that the user is still on the configuration page\n                function checkWindowHash() {\n                    const h = location.hash;\n                    if (h === \"#!/configurationpage?name=Media%20Analyzer\" || h.includes(\"#!/dialog\")) {\n                        return;\n                    }\n\n                    console.debug(\"navigated away from media analyzer configuration page\");\n                    document.removeEventListener(\"keydown\", keyDown);\n                    clearInterval(windowHashInterval);\n                }\n\n                // converts seconds to a readable timestamp (i.e. 127 becomes \"02:07\").\n                function secondsToString(seconds) {\n                    return new Date(seconds * 1000).toISOString().substr(14, 5);\n                }\n\n                // erase all intro/credits timestamps\n                function eraseTimestamps(mode) {\n                    const lower = mode.toLocaleLowerCase();\n                    const title = \"Confirm timestamp erasure\";\n                    const body = \"Are you sure you want to erase all previously discovered \" + mode.toLocaleLowerCase() + \" timestamps?\";\n\n                    Dashboard.confirm(body, title, (result) => {\n                        if (!result) {\n                            return;\n                        }\n                        // FIXME: Can't implement, no upstream support\n                        // fetchWithAuth(\"MediaSegment?type=\"+mode, \"DELETE\", null);\n                        // Dashboard.alert(mode + \" timestamps erased\");\n                        Dashboard.alert(\"Can't erase 'all' timestamps, there is no api in jellyfin\");\n                    });\n                }\n\n                document.querySelector(\"#TemplateConfigPage\").addEventListener(\"pageshow\", function () {\n                    Dashboard.showLoadingMsg();\n                    ApiClient.getPluginConfiguration(\"80885677-DACB-461B-AC97-EE7E971288AA\").then(function (config) {\n                        for (const field of configurationFields) {\n                            document.querySelector(\"#\" + field).value = config[field];\n                        }\n\n                        for (const field of booleanConfigurationFields) {\n                            document.querySelector(\"#\" + field).checked = config[field];\n                        }\n\n                        Dashboard.hideLoadingMsg();\n                    });\n                });\n\n                document.querySelector(\"#FingerprintConfigForm\").addEventListener(\"submit\", function (e) {\n                    Dashboard.showLoadingMsg();\n                    ApiClient.getPluginConfiguration(\"80885677-DACB-461B-AC97-EE7E971288AA\").then(function (config) {\n                        for (const field of configurationFields) {\n                            config[field] = document.querySelector(\"#\" + field).value;\n                        }\n\n                        for (const field of booleanConfigurationFields) {\n                            config[field] = document.querySelector(\"#\" + field).checked;\n                        }\n\n                        ApiClient.updatePluginConfiguration(\"80885677-DACB-461B-AC97-EE7E971288AA\", config).then(function (result) {\n                            Dashboard.processPluginConfigurationUpdateResult(result);\n                        });\n                    });\n\n                    e.preventDefault();\n                    return false;\n                });\n\n                visualizer.addEventListener(\"toggle\", visualizerToggled);\n                support.addEventListener(\"toggle\", supportToggled);\n                txtOffset.addEventListener(\"change\", renderTroubleshooter);\n                selectShow.addEventListener(\"change\", showChanged);\n                selectSeason.addEventListener(\"change\", seasonChanged);\n                selectEpisode1.addEventListener(\"change\", episodeChanged);\n                selectEpisode2.addEventListener(\"change\", episodeChanged);\n                btnEraseIntroTimestamps.addEventListener(\"click\", (e) => {\n                    eraseTimestamps(\"Intro\");\n                    e.preventDefault();\n                });\n                btnEraseCreditTimestamps.addEventListener(\"click\", (e) => {\n                    eraseTimestamps(\"Outro\");\n                    e.preventDefault();\n                });\n                btnUpdateTimestamps.addEventListener(\"click\", () => {\n                    const lhsId = selectEpisode1.options[selectEpisode1.selectedIndex].value;\n                    const newLhsIntro = {\n                        StartTicks: secondsToTicks(document.querySelector(\"#editLeftEpisodeStart\").value),\n                        EndTicks: secondsToTicks(document.querySelector(\"#editLeftEpisodeEnd\").value),\n                        ItemId: lhsId,\n                        Type: 'Intro'\n                    };\n\n                    const rhsId = selectEpisode2.options[selectEpisode2.selectedIndex].value;\n                    const newRhsIntro = {\n                        StartTicks: secondsToTicks(document.querySelector(\"#editRightEpisodeStart\").value),\n                        EndTicks: secondsToTicks(document.querySelector(\"#editRightEpisodeEnd\").value),\n                        ItemId: rhsId,\n                        Type: 'Intro'\n                    };\n                    // TODO: create new endpoint that can handle an update\n                    fetchWithAuth(\"JellyfinPluginIntroSkip/Episode/\" + lhsId + \"/UpdateIntroTimestamps\", \"POST\", JSON.stringify(newLhsIntro));\n                    fetchWithAuth(\"JellyfinPluginIntroSkip/Episode/\" + rhsId + \"/UpdateIntroTimestamps\", \"POST\", JSON.stringify(newRhsIntro));\n\n                    Dashboard.alert(\"New introduction timestamps saved\");\n                });\n                document.addEventListener(\"keydown\", keyDown);\n                windowHashInterval = setInterval(checkWindowHash, 2500);\n\n                canvas.addEventListener(\"mousemove\", (e) => {\n                    const rect = e.currentTarget.getBoundingClientRect();\n                    const y = e.clientY - rect.top;\n                    const shift = Number(txtOffset.value);\n\n                    let lTime, rTime, diffPos;\n                    if (shift < 0) {\n                        lTime = y * 0.128;\n                        rTime = (y + shift) * 0.128;\n                        diffPos = y + shift;\n                    } else {\n                        lTime = (y - shift) * 0.128;\n                        rTime = y * 0.128;\n                        diffPos = y - shift;\n                    }\n\n                    const diff = fprDiffs[Math.floor(diffPos)];\n\n                    if (!diff) {\n                        timeContainer.style.display = \"none\";\n                        return;\n                    } else {\n                        timeContainer.style.display = \"unset\";\n                    }\n\n                    const times = document.querySelector(\"span#timestamps\");\n\n                    // LHS timestamp, RHS timestamp, percent similarity\n                    times.textContent = secondsToString(lTime) + \", \" + secondsToString(rTime) + \", \" + Math.round(diff) + \"%\";\n\n                    timeContainer.style.position = \"relative\";\n                    timeContainer.style.left = \"25px\";\n                    timeContainer.style.top = (-1 * rect.height + y).toString() + \"px\";\n                });\n            </script>\n        </div>\n    </body>\n</html>\n"
  },
  {
    "path": "Jellyfin.Plugin.MediaAnalyzer/Configuration/version.txt",
    "content": "unknown\n"
  },
  {
    "path": "Jellyfin.Plugin.MediaAnalyzer/Configuration/visualizer.js",
    "content": "// re-render the troubleshooter with the latest offset\nfunction renderTroubleshooter() {\n    paintFingerprintDiff(canvas, lhs, rhs, Number(offset.value));\n    findIntros();\n}\n\n// refresh the upper & lower bounds for the offset\nfunction refreshBounds() {\n    const len = Math.min(lhs.length, rhs.length) - 1;\n    offset.min = -1 * len;\n    offset.max = len;\n}\n\nfunction findIntros() {\n    let times = [];\n\n    // get the times of all similar fingerprint points\n    for (let i in fprDiffs) {\n        if (fprDiffs[i] > fprDiffMinimum) {\n            times.push(i * 0.128);\n        }\n    }\n\n    // always close the last range\n    times.push(Number.MAX_VALUE);\n\n    let last = times[0];\n    let start = last;\n    let end = last;\n    let ranges = [];\n\n    for (let t of times) {\n        const diff = t - last;\n\n        if (diff <= 3.5) {\n            end = t;\n            last = t;\n            continue;\n        }\n\n        const dur = Math.round(end - start);\n        if (dur >= 15) {\n            ranges.push({\n                \"start\": start,\n                \"end\": end,\n                \"duration\": dur\n            });\n        }\n\n        start = t;\n        end = t;\n        last = t;\n    }\n\n    const introsLog = document.querySelector(\"span#intros\");\n    introsLog.style.position = \"relative\";\n    introsLog.style.left = \"115px\";\n    introsLog.innerHTML = \"\";\n\n    const offset = Number(txtOffset.value) * 0.128;\n    for (let r of ranges) {\n        let lStart, lEnd, rStart, rEnd;\n\n        if (offset < 0) {\n            // negative offset, the diff is aligned with the RHS\n            lStart = r.start - offset;\n            lEnd = r.end - offset;\n            rStart = r.start;\n            rEnd = r.end;\n\n        } else {\n            // positive offset, the diff is aligned with the LHS\n            lStart = r.start;\n            lEnd = r.end;\n            rStart = r.start + offset;\n            rEnd = r.end + offset;\n        }\n\n        const lTitle = selectEpisode1.options[selectEpisode1.selectedIndex].text;\n        const rTitle = selectEpisode2.options[selectEpisode2.selectedIndex].text;\n        introsLog.innerHTML += \"<span>\" + lTitle + \": \" +\n            secondsToString(lStart) + \" - \" + secondsToString(lEnd) + \"</span> <br />\";\n        introsLog.innerHTML += \"<span>\" + rTitle + \": \" +\n            secondsToString(rStart) + \" - \" + secondsToString(rEnd) + \"</span> <br />\";\n    }\n}\n\n// find all shifts which align exact matches of audio.\nfunction findExactMatches() {\n    let shifts = [];\n\n    for (let lhsIndex in lhs) {\n        let lhsPoint = lhs[lhsIndex];\n        let rhsIndex = rhs.findIndex((x) => x === lhsPoint);\n\n        if (rhsIndex === -1) {\n            continue;\n        }\n\n        let shift = rhsIndex - lhsIndex;\n        if (shifts.includes(shift)) {\n            continue;\n        }\n\n        shifts.push(shift);\n    }\n\n    // Only suggest up to 20 shifts\n    shifts = shifts.slice(0, 20);\n\n    txtSuggested.textContent = \"Suggested shifts: \";\n    if (shifts.length === 0) {\n        txtSuggested.textContent += \"none available\";\n    } else {\n        shifts.sort((a, b) => { return a - b });\n        txtSuggested.textContent += shifts.join(\", \");\n    }\n}\n\n// The below two functions were modified from https://github.com/dnknth/acoustid-match/blob/ffbf21d8c53c40d3b3b4c92238c35846545d3cd7/fingerprints/static/fingerprints/fputils.js\n// Originally licensed as MIT.\nfunction renderFingerprintData(ctx, fp, xor = false) {\n    const pixels = ctx.createImageData(32, fp.length);\n    let idx = 0;\n\n    for (let i = 0; i < fp.length; i++) {\n        for (let j = 0; j < 32; j++) {\n            if (fp[i] & (1 << j)) {\n                pixels.data[idx + 0] = 255;\n                pixels.data[idx + 1] = 255;\n                pixels.data[idx + 2] = 255;\n\n            } else {\n                pixels.data[idx + 0] = 0;\n                pixels.data[idx + 1] = 0;\n                pixels.data[idx + 2] = 0;\n            }\n\n            pixels.data[idx + 3] = 255;\n            idx += 4;\n        }\n    }\n\n    if (!xor) {\n        return pixels;\n    }\n\n    // if rendering the XOR of the fingerprints, count how many bits are different at each timecode\n    fprDiffs = [];\n\n    for (let i = 0; i < fp.length; i++) {\n        let count = 0;\n\n        for (let j = 0; j < 32; j++) {\n            if (fp[i] & (1 << j)) {\n                count++;\n            }\n        }\n\n        // push the percentage similarity\n        fprDiffs[i] = 100 - (count * 100) / 32;\n    }\n\n    return pixels;\n}\n\nfunction paintFingerprintDiff(canvas, fp1, fp2, offset) {\n    if (fp1.length == 0) {\n        return;\n    }\n\n    let leftOffset = 0, rightOffset = 0;\n    if (offset < 0) {\n        leftOffset -= offset;\n    } else {\n        rightOffset += offset;\n    }\n\n    let fpDiff = [];\n    fpDiff.length = Math.min(fp1.length, fp2.length) - Math.abs(offset);\n    for (let i = 0; i < fpDiff.length; i++) {\n        fpDiff[i] = fp1[i + leftOffset] ^ fp2[i + rightOffset];\n    }\n\n    const ctx = canvas.getContext('2d');\n    const pixels1 = renderFingerprintData(ctx, fp1);\n    const pixels2 = renderFingerprintData(ctx, fp2);\n    const pixelsDiff = renderFingerprintData(ctx, fpDiff, true);\n    const border = 4;\n\n    canvas.width = pixels1.width + border + // left fingerprint\n        pixels2.width + border +            // right fingerprint\n        pixelsDiff.width + border           // fingerprint diff\n        + 4;                                // if diff[x] >= fprDiffMinimum\n\n    canvas.height = Math.max(pixels1.height, pixels2.height) + Math.abs(offset);\n\n    ctx.rect(0, 0, canvas.width, canvas.height);\n    ctx.fillStyle = \"#C5C5C5\";\n    ctx.fill();\n\n    // draw left fingerprint\n    let dx = 0;\n    ctx.putImageData(pixels1, dx, rightOffset);\n    dx += pixels1.width + border;\n\n    // draw right fingerprint\n    ctx.putImageData(pixels2, dx, leftOffset);\n    dx += pixels2.width + border;\n\n    // draw fingerprint diff\n    ctx.putImageData(pixelsDiff, dx, Math.abs(offset));\n    dx += pixelsDiff.width + border;\n\n    // draw the fingerprint diff similarity indicator\n    // https://davidmathlogic.com/colorblind/#%23EA3535-%232C92EF\n    for (let i in fprDiffs) {\n        const j = Number(i);\n        const y = Math.abs(offset) + j;\n        const point = fprDiffs[j];\n\n        if (point >= 100) {\n            ctx.fillStyle = \"#002FFF\"\n        } else if (point >= fprDiffMinimum) {\n            ctx.fillStyle = \"#2C92EF\";\n        } else {\n            ctx.fillStyle = \"#EA3535\";\n        }\n\n        ctx.fillRect(dx, y, 4, 1);\n    }\n}\n"
  },
  {
    "path": "Jellyfin.Plugin.MediaAnalyzer/Controllers/MediaAnalyzerController.cs",
    "content": "using System;\nusing System.Collections.Generic;\nusing System.Collections.ObjectModel;\nusing System.ComponentModel.DataAnnotations;\nusing System.Linq;\nusing System.Net.Mime;\nusing System.Text.Json.Nodes;\nusing System.Threading;\nusing System.Threading.Tasks;\nusing Jellyfin.Data.Enums;\nusing MediaBrowser.Controller;\nusing MediaBrowser.Controller.Library;\nusing Microsoft.AspNetCore.Authorization;\nusing Microsoft.AspNetCore.Http;\nusing Microsoft.AspNetCore.Mvc;\nusing Microsoft.Extensions.Logging;\n\nnamespace Jellyfin.Plugin.MediaAnalyzer.Controllers;\n\n/// <summary>\n/// PluginEdl controller.\n/// </summary>\n[Authorize(Policy = \"RequiresElevation\")]\n[ApiController]\n[Produces(MediaTypeNames.Application.Json)]\n[Route(\"PluginMediaAnalyzer\")]\npublic class MediaAnalyzerController : ControllerBase\n{\n    private readonly ILoggerFactory _loggerFactory;\n    private readonly ILibraryManager _libraryManager;\n    private readonly IMediaSegmentManager _mediaSegmentManager;\n\n    /// <summary>\n    /// Initializes a new instance of the <see cref=\"MediaAnalyzerController\"/> class.\n    /// </summary>\n    /// <param name=\"loggerFactory\">Logger factory.</param>\n    /// <param name=\"libraryManager\">LibraryManager.</param>\n    /// <param name=\"mediaSegmentManager\">MediaSegmentsManager.</param>\n    public MediaAnalyzerController(\n        ILoggerFactory loggerFactory,\n        ILibraryManager libraryManager,\n        IMediaSegmentManager mediaSegmentManager)\n    {\n        _loggerFactory = loggerFactory;\n        _libraryManager = libraryManager;\n        _mediaSegmentManager = mediaSegmentManager;\n    }\n\n    /// <summary>\n    /// Plugin meta endpoint.\n    /// </summary>\n    /// <returns>The version info.</returns>\n    [HttpGet]\n    [ProducesResponseType(StatusCodes.Status200OK)]\n    public JsonResult GetPluginMetadata()\n    {\n        var json = new\n        {\n            version = Plugin.Instance!.Version.ToString(3),\n        };\n\n        return new JsonResult(json);\n    }\n\n    /// <summary>\n    /// Run analyzer based on itemIds and params and returns the segments + metadata.\n    /// </summary>\n    /// <param name=\"itemIds\">ItemIds.</param>\n    /// <param name=\"analyzerTypes\">Analyzers to use.</param>\n    /// <param name=\"mode\">Segment Type to search for.</param>\n    /// <returns>The found segments.</returns>\n    [HttpGet(\"Analyzers\")]\n    [ProducesResponseType(StatusCodes.Status200OK)]\n    public async Task<JsonResult> AnalyzeIds(\n        [FromQuery, Required] Guid[] itemIds,\n        [FromQuery, Required] AnalyzerType[] analyzerTypes,\n        [FromQuery, Required] MediaSegmentType mode)\n    {\n        var queueManager = new QueueManager(_loggerFactory.CreateLogger<QueueManager>(), _libraryManager, MediaSegmentType.Intro);\n\n        var errors = new JsonArray();\n        var analyzedItems = new Dictionary<Guid, Segment>();\n        var metadatas = new Dictionary<Guid, SegmentMetadata>();\n        var jsonObject = new JsonObject();\n\n        // get ItemIds\n        var mediaItems = queueManager.GetMediaItemsById(itemIds);\n        // setup analyzers\n        foreach (var (key, media) in mediaItems)\n        {\n            var items = media.AsReadOnly();\n            var totalItems = mediaItems.Count;\n            var first = items[0];\n\n            var analyzers = new Collection<IMediaFileAnalyzer>();\n\n            if (analyzerTypes.Contains(AnalyzerType.ChapterAnalyzer))\n            {\n                analyzers.Add(new ChapterAnalyzer(_loggerFactory.CreateLogger<ChapterAnalyzer>()));\n            }\n\n            // Movies don't use chromparint analyzer\n            if (first.IsEpisode() && analyzerTypes.Contains(AnalyzerType.ChromaprintAnalyzer))\n            {\n                if (items.Count == 1)\n                {\n                    errors.Add($\"Chromaprint needs at least two media files to compare, one provided: {first.GetFullName}\");\n                }\n                else\n                {\n                    analyzers.Add(new ChromaprintAnalyzer(_loggerFactory.CreateLogger<ChromaprintAnalyzer>()));\n                }\n            }\n\n            if (mode == MediaSegmentType.Outro && analyzerTypes.Contains(AnalyzerType.BlackFrameAnalyzer))\n            {\n                analyzers.Add(new BlackFrameAnalyzer(_loggerFactory.CreateLogger<BlackFrameAnalyzer>()));\n            }\n\n            // Use each analyzer to find skippable ranges in all media files, removing successfully\n            // analyzed items from the queue.\n            foreach (var analyzer in analyzers)\n            {\n                var cancellationToken = default(CancellationToken);\n                var (notAnalyzed, analyzed, metadata) = await analyzer.AnalyzeMediaFilesAsync(items, mode, cancellationToken);\n\n                var atype = analyzer is BlackFrameAnalyzer ? \"BlackFrameAnalyzer\" : analyzer is ChromaprintAnalyzer ? \"ChromaprintAnalyzer\" : analyzer is ChapterAnalyzer ? \"ChapterAnalyzer\" : throw new NotImplementedException(\"Unknown Analyzer type\");\n                jsonObject.Add(atype, BuildAnalyzerOutput(analyzed, metadata));\n            }\n        }\n\n        jsonObject.Add(\"Errors\", errors);\n\n        return new JsonResult(jsonObject);\n    }\n\n    /// <summary>\n    /// Fingerprint the provided episode and returns the uncompressed fingerprint data points.\n    /// </summary>\n    /// <param name=\"id\">Episode id.</param>\n    /// <param name=\"mode\">Type Intro or Outro.</param>\n    /// <returns>Read only collection of fingerprint points.</returns>\n    [HttpGet(\"Chromaprint/{Id}\")]\n    public ActionResult<uint[]> GetMediaFingerprint(\n        [FromRoute, Required] Guid id,\n        [FromQuery, Required] MediaSegmentType mode)\n    {\n        var queueManager = new QueueManager(_loggerFactory.CreateLogger<QueueManager>(), _libraryManager, mode);\n        var queuedMedia = queueManager.GetMediaItemsById([id]);\n\n        // Search through all queued episodes to find the requested id\n        foreach (var season in queuedMedia)\n        {\n            foreach (var needle in season.Value)\n            {\n                if (needle.ItemId == id)\n                {\n                    return FFmpegWrapper.Fingerprint(needle, mode);\n                }\n            }\n        }\n\n        return NotFound();\n    }\n\n    private static JsonObject BuildAnalyzerOutput(ReadOnlyDictionary<Guid, Segment> segments, ReadOnlyDictionary<Guid, SegmentMetadata> metadatas)\n    {\n        var itemsObject = new JsonObject();\n        Dictionary<Guid, SegmentMetadata> metadataLocal = metadatas.ToDictionary();\n\n        foreach (var item in segments)\n        {\n            if (metadatas.TryGetValue(item.Key, out var metadata))\n            {\n                metadataLocal.Remove(item.Key);\n\n                var json = new\n                {\n                    Segment = item.Value,\n                    Metadata = metadata\n                };\n                itemsObject.Add(item.Key.ToString(), json.ToString());\n            }\n        }\n\n        // we may have more metadata\n        foreach (var item in metadataLocal)\n        {\n            var json = new\n            {\n                Metadata = item.Value\n            };\n            itemsObject.Add(item.Key.ToString(), json.ToString());\n        }\n\n        return itemsObject;\n    }\n}\n"
  },
  {
    "path": "Jellyfin.Plugin.MediaAnalyzer/Controllers/TroubleshootingController.cs",
    "content": "using System.Net.Mime;\nusing System.Text;\nusing Microsoft.AspNetCore.Authorization;\nusing Microsoft.AspNetCore.Mvc;\nusing Microsoft.Extensions.Logging;\n\nnamespace Jellyfin.Plugin.MediaAnalyzer.Controllers;\n\n/// <summary>\n/// Troubleshooting controller.\n/// </summary>\n[Authorize(Policy = \"RequiresElevation\")]\n[ApiController]\n[Produces(MediaTypeNames.Application.Json)]\n[Route(\"JellyfinPluginIntroSkipSupport\")]\npublic class TroubleshootingController : ControllerBase\n{\n    // private readonly IApplicationHost _applicationHost;\n    private readonly ILogger<TroubleshootingController> _logger;\n\n    /// <summary>\n    /// Initializes a new instance of the <see cref=\"TroubleshootingController\"/> class.\n    /// </summary>\n    /// <param name=\"logger\">Logger.</param>\n    public TroubleshootingController(\n        ILogger<TroubleshootingController> logger)\n    {\n        _logger = logger;\n    }\n\n    /// <summary>\n    /// Gets a Markdown formatted support bundle.\n    /// </summary>\n    /// <response code=\"200\">Support bundle created.</response>\n    /// <returns>Support bundle.</returns>\n    [HttpGet(\"SupportBundle\")]\n    [Produces(MediaTypeNames.Text.Plain)]\n    public ActionResult<string> GetSupportBundle()\n    {\n        var bundle = new StringBuilder();\n\n        // bundle.Append(\"* Jellyfin version: \");\n        // bundle.Append(_applicationHost.ApplicationVersionString);\n        // bundle.Append('\\n');\n\n        var version = Plugin.Instance!.Version.ToString(3);\n\n        bundle.Append(\"* Plugin version: \");\n        bundle.Append(version);\n        bundle.Append('\\n');\n\n        bundle.Append(\"* Warnings: `\");\n        bundle.Append(WarningManager.GetWarnings());\n        bundle.Append(\"`\\n\");\n\n        bundle.Append(FFmpegWrapper.GetChromaprintLogs());\n\n        return bundle.ToString();\n    }\n}\n"
  },
  {
    "path": "Jellyfin.Plugin.MediaAnalyzer/Controllers/VisualizationController.cs",
    "content": "using System;\nusing System.Collections.Generic;\nusing System.Globalization;\nusing System.Net.Mime;\nusing Jellyfin.Data.Enums;\nusing MediaBrowser.Controller.Library;\nusing Microsoft.AspNetCore.Authorization;\nusing Microsoft.AspNetCore.Mvc;\nusing Microsoft.Extensions.Logging;\n\nnamespace Jellyfin.Plugin.MediaAnalyzer.Controllers;\n\n/// <summary>\n/// Audio fingerprint visualization controller. Allows browsing fingerprints on a per episode basis.\n/// </summary>\n[Authorize(Policy = \"RequiresElevation\")]\n[ApiController]\n[Produces(MediaTypeNames.Application.Json)]\n[Route(\"JellyfinPluginIntroSkip\")]\npublic class VisualizationController : ControllerBase\n{\n    private readonly ILoggerFactory _loggerFactory;\n    private readonly ILibraryManager _libraryManager;\n    private readonly ILogger<VisualizationController> _logger;\n\n    /// <summary>\n    /// Initializes a new instance of the <see cref=\"VisualizationController\"/> class.\n    /// </summary>\n    /// <param name=\"logger\">Logger.</param>\n    /// <param name=\"loggerFactory\">loggerFactory.</param>\n    /// <param name=\"libraryManager\">libraryManager.</param>\n    public VisualizationController(\n        ILogger<VisualizationController> logger,\n        ILoggerFactory loggerFactory,\n        ILibraryManager libraryManager)\n    {\n        _loggerFactory = loggerFactory;\n        _libraryManager = libraryManager;\n        _logger = logger;\n    }\n\n    /// <summary>\n    /// Returns all show names and seasons.\n    /// </summary>\n    /// <returns>Dictionary of show names to a list of season names.</returns>\n    [HttpGet(\"Shows\")]\n    public ActionResult<Dictionary<string, HashSet<string>>> GetShowSeasons()\n    {\n        _logger.LogDebug(\"Returning season names by series\");\n\n        var showSeasons = new Dictionary<string, HashSet<string>>();\n        var queueManager = new QueueManager(_loggerFactory.CreateLogger<QueueManager>(), _libraryManager, MediaSegmentType.Intro);\n        var queuedMedia = queueManager.GetMediaItems();\n\n        // Loop through all seasons in the analysis queue\n        foreach (var kvp in queuedMedia)\n        {\n            // Check that this season contains at least one episode.\n            var episodes = kvp.Value;\n            if (episodes is null || episodes.Count == 0)\n            {\n                _logger.LogDebug(\"Skipping season {Id} (null or empty)\", kvp.Key);\n                continue;\n            }\n\n            // Peek at the top episode from this season and store the series name and season number.\n            var first = episodes[0];\n            var series = first.SeriesName;\n            var season = GetSeasonName(first);\n\n            // Validate the series and season before attempting to store it.\n            if (string.IsNullOrWhiteSpace(series) || string.IsNullOrWhiteSpace(season))\n            {\n                _logger.LogDebug(\"Skipping season {Id} (no name or number)\", kvp.Key);\n                continue;\n            }\n\n            // TryAdd is used when adding the HashSet since it is a no-op if one was already created for this series.\n            showSeasons.TryAdd(series, new HashSet<string>());\n            showSeasons[series].Add(season);\n        }\n\n        return showSeasons;\n    }\n\n    /// <summary>\n    /// Returns the names and unique identifiers of all episodes in the provided season.\n    /// </summary>\n    /// <param name=\"series\">Show name.</param>\n    /// <param name=\"season\">Season name.</param>\n    /// <returns>List of episode titles.</returns>\n    [HttpGet(\"Show/{Series}/{Season}\")]\n    public ActionResult<List<EpisodeVisualization>> GetSeasonEpisodes(\n        [FromRoute] string series,\n        [FromRoute] string season)\n    {\n        var visualEpisodes = new List<EpisodeVisualization>();\n\n        if (!LookupSeasonByName(series, season, out var episodes))\n        {\n            return NotFound();\n        }\n\n        foreach (var e in episodes)\n        {\n            visualEpisodes.Add(new EpisodeVisualization(e.ItemId, e.Name));\n        }\n\n        return visualEpisodes;\n    }\n\n    /// <summary>\n    /// Fingerprint the provided episode and returns the uncompressed fingerprint data points.\n    /// </summary>\n    /// <param name=\"id\">Episode id.</param>\n    /// <returns>Read only collection of fingerprint points.</returns>\n    [HttpGet(\"Episode/{Id}/Chromaprint\")]\n    public ActionResult<uint[]> GetEpisodeFingerprint([FromRoute] Guid id)\n    {\n        var queueManager = new QueueManager(_loggerFactory.CreateLogger<QueueManager>(), _libraryManager, MediaSegmentType.Intro);\n        var queuedMedia = queueManager.GetMediaItems();\n\n        // Search through all queued episodes to find the requested id\n        foreach (var season in queuedMedia)\n        {\n            foreach (var needle in season.Value)\n            {\n                if (needle.ItemId == id)\n                {\n                    return FFmpegWrapper.Fingerprint(needle, MediaSegmentType.Intro);\n                }\n            }\n        }\n\n        return NotFound();\n    }\n\n    private string GetSeasonName(QueuedMedia episode)\n    {\n        return \"Season \" + episode.SeasonNumber.ToString(CultureInfo.InvariantCulture);\n    }\n\n    /// <summary>\n    /// Lookup a named season of a series and return all queued episodes.\n    /// </summary>\n    /// <param name=\"series\">Series name.</param>\n    /// <param name=\"season\">Season name.</param>\n    /// <param name=\"episodes\">Episodes.</param>\n    /// <returns>Boolean indicating if the requested season was found.</returns>\n    private bool LookupSeasonByName(string series, string season, out List<QueuedMedia> episodes)\n    {\n        var queueManager = new QueueManager(_loggerFactory.CreateLogger<QueueManager>(), _libraryManager, MediaSegmentType.Intro);\n        var queuedMedia = queueManager.GetMediaItems();\n\n        foreach (var queuedEpisodes in queuedMedia)\n        {\n            var first = queuedEpisodes.Value[0];\n            var firstSeasonName = GetSeasonName(first);\n\n            // Assert that the queued episode series and season are equal to what was requested\n            if (\n                !string.Equals(first.SeriesName, series, StringComparison.OrdinalIgnoreCase) ||\n                !string.Equals(firstSeasonName, season, StringComparison.OrdinalIgnoreCase))\n            {\n                continue;\n            }\n\n            episodes = queuedEpisodes.Value;\n            return true;\n        }\n\n        episodes = new List<QueuedMedia>();\n        return false;\n    }\n}\n"
  },
  {
    "path": "Jellyfin.Plugin.MediaAnalyzer/Data/AnalyzerType.cs",
    "content": "namespace Jellyfin.Plugin.MediaAnalyzer;\n\n/// <summary>\n/// Analyzer Type.\n/// </summary>\npublic enum AnalyzerType\n{\n    /// <summary>\n    /// No Analyzer.\n    /// </summary>\n    NotSet,\n\n    /// <summary>\n    /// Blackframe Analyzer.\n    /// </summary>\n    BlackFrameAnalyzer,\n\n    /// <summary>\n    /// Chapter Analyzer.\n    /// </summary>\n    ChapterAnalyzer,\n\n    /// <summary>\n    /// Chromaprint Analyzer.\n    /// </summary>\n    ChromaprintAnalyzer,\n}\n"
  },
  {
    "path": "Jellyfin.Plugin.MediaAnalyzer/Data/BlackFrame.cs",
    "content": "namespace Jellyfin.Plugin.MediaAnalyzer;\n\n/// <summary>\n/// A frame of video that partially (or entirely) consists of black pixels.\n/// </summary>\npublic class BlackFrame\n{\n    /// <summary>\n    /// Initializes a new instance of the <see cref=\"BlackFrame\"/> class.\n    /// </summary>\n    /// <param name=\"percent\">Percentage of the frame that is black.</param>\n    /// <param name=\"time\">Time this frame appears at.</param>\n    public BlackFrame(int percent, double time)\n    {\n        Percentage = percent;\n        Time = time;\n    }\n\n    /// <summary>\n    /// Gets or sets the percentage of the frame that is black.\n    /// </summary>\n    public int Percentage { get; set; }\n\n    /// <summary>\n    /// Gets or sets the time (in seconds) this frame appeared at.\n    /// </summary>\n    public double Time { get; set; }\n}\n"
  },
  {
    "path": "Jellyfin.Plugin.MediaAnalyzer/Data/EpisodeVisualization.cs",
    "content": "using System;\n\nnamespace Jellyfin.Plugin.MediaAnalyzer;\n\n/// <summary>\n/// Episode name and internal ID as returned by the visualization controller.\n/// </summary>\npublic class EpisodeVisualization\n{\n    /// <summary>\n    /// Initializes a new instance of the <see cref=\"EpisodeVisualization\"/> class.\n    /// </summary>\n    /// <param name=\"id\">Episode id.</param>\n    /// <param name=\"name\">Episode name.</param>\n    public EpisodeVisualization(Guid id, string name)\n    {\n        Id = id;\n        Name = name;\n    }\n\n    /// <summary>\n    /// Gets the id.\n    /// </summary>\n    public Guid Id { get; private set; }\n\n    /// <summary>\n    /// Gets the name.\n    /// </summary>\n    public string Name { get; private set; } = string.Empty;\n}\n"
  },
  {
    "path": "Jellyfin.Plugin.MediaAnalyzer/Data/FingerprintException.cs",
    "content": "using System;\n\nnamespace Jellyfin.Plugin.MediaAnalyzer;\n\n/// <summary>\n/// Exception raised when an error is encountered analyzing audio.\n/// </summary>\npublic class FingerprintException : Exception\n{\n    /// <summary>\n    /// Initializes a new instance of the <see cref=\"FingerprintException\"/> class.\n    /// </summary>\n    public FingerprintException()\n    {\n    }\n\n    /// <summary>\n    /// Initializes a new instance of the <see cref=\"FingerprintException\"/> class.\n    /// </summary>\n    /// <param name=\"message\">Exception message.</param>\n    public FingerprintException(string message) : base(message)\n    {\n    }\n\n    /// <summary>\n    /// Initializes a new instance of the <see cref=\"FingerprintException\"/> class.\n    /// </summary>\n    /// <param name=\"message\">Exception message.</param>\n    /// <param name=\"inner\">Inner exception.</param>\n    public FingerprintException(string message, Exception inner) : base(message, inner)\n    {\n    }\n}\n"
  },
  {
    "path": "Jellyfin.Plugin.MediaAnalyzer/Data/IntroWithMetadata.cs",
    "content": "using System;\nusing System.Text.Json.Serialization;\n\nnamespace Jellyfin.Plugin.MediaAnalyzer;\n\n/// <summary>\n/// An Segment class with episode metadata. Only used in end to end testing programs.\n/// </summary>\npublic class IntroWithMetadata : Segment\n{\n    /// <summary>\n    /// Initializes a new instance of the <see cref=\"IntroWithMetadata\"/> class.\n    /// </summary>\n    /// <param name=\"series\">Series name.</param>\n    /// <param name=\"season\">Season number.</param>\n    /// <param name=\"title\">Episode title.</param>\n    /// <param name=\"intro\">Intro timestamps.</param>\n    public IntroWithMetadata(string series, int season, string title, Segment intro)\n    {\n        Series = series;\n        Season = season;\n        Title = title;\n\n        ItemId = intro.ItemId;\n        Start = intro.Start;\n        End = intro.End;\n    }\n\n    /// <summary>\n    /// Gets or sets the series name of the TV episode associated with this intro.\n    /// </summary>\n    public string Series { get; set; }\n\n    /// <summary>\n    /// Gets or sets the season number of the TV episode associated with this intro.\n    /// </summary>\n    public int Season { get; set; }\n\n    /// <summary>\n    /// Gets or sets the title of the TV episode associated with this intro.\n    /// </summary>\n    public string Title { get; set; }\n}\n"
  },
  {
    "path": "Jellyfin.Plugin.MediaAnalyzer/Data/MediaSegmentsDb.cs",
    "content": "using System;\nusing System.Collections.Generic;\nusing System.Collections.ObjectModel;\nusing System.Linq;\nusing System.Threading.Tasks;\nusing Jellyfin.Data.Enums;\nusing MediaBrowser.Controller;\nusing MediaBrowser.Model.MediaSegments;\n\nnamespace Jellyfin.Plugin.MediaAnalyzer;\n\n/// <summary>\n/// Small abstraction over MediaSegmentsManager.\n/// </summary>\npublic class MediaSegmentsDb\n{\n    private readonly IMediaSegmentManager _segmentsManager;\n\n    /// <summary>\n    /// Initializes a new instance of the <see cref=\"MediaSegmentsDb\"/> class.\n    /// </summary>\n    /// <param name=\"segmentsManager\">MediaSegmentsManager.</param>\n    public MediaSegmentsDb(IMediaSegmentManager segmentsManager)\n    {\n        _segmentsManager = segmentsManager;\n    }\n\n    /// <summary>\n    /// Test if we can find segments.\n    /// </summary>\n    /// <param name=\"itemId\">ItemId.</param>\n    /// <param name=\"type\">Mode.</param>\n    /// <returns>A <see cref=\"Task{TResult}\"/> representing the result of the asynchronous operation.</returns>\n    public async Task<bool> HasSegments(Guid itemId, MediaSegmentType type)\n    {\n        var list = await _segmentsManager.GetSegmentsAsync(itemId, [type]).ConfigureAwait(false);\n        return list.Any();\n    }\n\n    /// <summary>\n    /// Create new media segment together with metadata. Can also handle additional metadata without segment.\n    /// </summary>\n    /// <param name=\"segments\">segments to add.</param>\n    /// <param name=\"metadata\">Metadata for a segment.</param>\n    /// <param name=\"mode\">Mode.</param>\n    /// <returns>A <see cref=\"Task{TResult}\"/> representing the result of the asynchronous operation.</returns>\n    public async Task CreateMediaSegments(ReadOnlyDictionary<Guid, Segment> segments, ReadOnlyDictionary<Guid, SegmentMetadata> metadata, MediaSegmentType mode)\n    {\n        var metaDBb = Plugin.Instance!.GetMetadataDb();\n        Dictionary<Guid, SegmentMetadata> metadataLocal = metadata.ToDictionary();\n\n        if (metaDBb == null)\n        {\n            throw new InvalidOperationException(\"Meta database was null\");\n        }\n\n        foreach (var (key, seg) in segments)\n        {\n            var newGuid = Guid.NewGuid();\n\n            var newSeg = new MediaSegmentDto()\n            {\n                Id = newGuid,\n                ItemId = seg.ItemId,\n                Type = mode,\n                StartTicks = Utils.SToTicks(seg.Start),\n                EndTicks = Utils.SToTicks(seg.End),\n            };\n\n            await _segmentsManager.CreateSegmentAsync(newSeg, Plugin.Instance!.Name).ConfigureAwait(false);\n\n            if (metadata.TryGetValue(key, out var meta))\n            {\n                metadataLocal.Remove(key);\n\n                meta.SegmentId = newGuid;\n                metaDBb.SaveSegment(meta);\n            }\n        }\n\n        // we may have more metadata\n        foreach (var meta in metadataLocal)\n        {\n            metaDBb.SaveSegment(meta.Value);\n        }\n    }\n\n    /// <summary>\n    /// Get segments from db by mode and id.\n    /// </summary>\n    /// <param name=\"itemId\">Item Id.</param>\n    /// <param name=\"mode\">Mode of analysis.</param>\n    /// <returns>Dictionary of guid,segments.</returns>\n    public async Task<Dictionary<Guid, Segment>> GetMediaSegmentsByIdAsync(Guid itemId, MediaSegmentType mode)\n    {\n        var segments = await _segmentsManager.GetSegmentsAsync(itemId, [mode]).ConfigureAwait(false);\n\n        var intros = new Dictionary<Guid, Segment>();\n\n        foreach (var item in segments)\n        {\n            intros.TryAdd(item.ItemId, new Segment()\n            {\n                ItemId = item.ItemId,\n                Start = Utils.TicksToS(item.StartTicks),\n                End = Utils.TicksToS(item.EndTicks),\n            });\n        }\n\n        return intros;\n    }\n}\n"
  },
  {
    "path": "Jellyfin.Plugin.MediaAnalyzer/Data/QueuedMedia.cs",
    "content": "using System;\n\nnamespace Jellyfin.Plugin.MediaAnalyzer;\n\n/// <summary>\n/// Media queued for analysis.\n/// </summary>\npublic class QueuedMedia\n{\n    /// <summary>\n    /// Gets or sets the Series name.\n    /// </summary>\n    public string SeriesName { get; set; } = string.Empty;\n\n    /// <summary>\n    /// Gets or sets the season number.\n    /// </summary>\n    public int SeasonNumber { get; set; }\n\n    /// <summary>\n    /// Gets or sets the media id.\n    /// </summary>\n    public Guid ItemId { get; set; }\n\n    /// <summary>\n    /// Gets or sets a value indicating whether this media has been already analyzed.\n    /// </summary>\n    public bool IsAnalyzed { get; set; }\n\n    /// <summary>\n    /// Gets or sets a value indicating whether this media should be skipped for blacklisting.\n    /// This will happen when a Season has just one episode, which can't be Chromaprint compared analyzed but maybe at a later run.\n    /// </summary>\n    public bool SkipPreventAnalyzing { get; set; }\n\n    /// <summary>\n    /// Gets or sets the full path to episode/movie.\n    /// </summary>\n    public string Path { get; set; } = string.Empty;\n\n    /// <summary>\n    /// Gets or sets the name of the media, episode or movie.\n    /// </summary>\n    public string Name { get; set; } = string.Empty;\n\n    /// <summary>\n    /// Gets or sets the name of the source like quality (1080p, 4k, ...).\n    /// </summary>\n    public string SourceName { get; set; } = string.Empty;\n\n    /// <summary>\n    /// Gets or sets the timestamp (in seconds) to stop searching for an introduction at.\n    /// </summary>\n    public int IntroFingerprintEnd { get; set; }\n\n    /// <summary>\n    /// Gets or sets the timestamp (in seconds) to start looking for end credits at.\n    /// </summary>\n    public int CreditsFingerprintStart { get; set; }\n\n    /// <summary>\n    /// Gets or sets the total duration of this media file (in seconds).\n    /// </summary>\n    public int Duration { get; set; }\n\n    /// <inheritdoc/>\n    public override bool Equals(object? obj)\n    {\n        return (obj as QueuedMedia)?.ItemId == this.ItemId;\n    }\n\n    /// <inheritdoc/>\n    public override int GetHashCode()\n    {\n        return ItemId.GetHashCode();\n    }\n\n    /// <summary>\n    /// Gets the full name of the media, episode or movie with source name/quality.\n    /// </summary>\n    /// <returns>The full name of the media.</returns>\n    public string GetFullName()\n    {\n        if (IsEpisode())\n        {\n            return $\"{SeriesName} S{SeasonNumber} - {Name}\";\n        }\n        else\n        {\n            return $\"{Name} ({SourceName})\";\n        }\n    }\n\n    /// <summary>\n    /// Gets or sets a value indicating whether this media is an episode, part of a tv show.\n    /// </summary>\n    /// <returns>Is an episode or not.</returns>\n    public bool IsEpisode()\n    {\n        return !string.IsNullOrEmpty(SeriesName);\n    }\n}\n"
  },
  {
    "path": "Jellyfin.Plugin.MediaAnalyzer/Data/Segment.cs",
    "content": "using System;\nusing System.Text.Json.Serialization;\n\nnamespace Jellyfin.Plugin.MediaAnalyzer;\n\n/// <summary>\n/// Result of fingerprinting and analyzing two episodes in a season.\n/// All times are measured in seconds relative to the beginning of the media file.\n/// </summary>\npublic class Segment\n{\n    /// <summary>\n    /// Initializes a new instance of the <see cref=\"Segment\"/> class.\n    /// </summary>\n    /// <param name=\"episode\">Episode.</param>\n    /// <param name=\"isEpisode\">is episode.</param>\n    /// <param name=\"intro\">Introduction time range.</param>\n    public Segment(Guid episode, bool isEpisode, TimeRange intro)\n    {\n        ItemId = episode;\n        Start = intro.Start;\n        End = intro.End;\n        IsEpisode = isEpisode;\n    }\n\n    /// <summary>\n    /// Initializes a new instance of the <see cref=\"Segment\"/> class.\n    /// </summary>\n    /// <param name=\"episode\">Episode.</param>\n    /// <param name=\"intro\">Introduction time range.</param>\n    public Segment(Guid episode, TimeRange intro)\n    {\n        ItemId = episode;\n        Start = intro.Start;\n        End = intro.End;\n    }\n\n    /// <summary>\n    /// Initializes a new instance of the <see cref=\"Segment\"/> class.\n    /// </summary>\n    /// <param name=\"episode\">Episode.</param>\n    public Segment(Guid episode)\n    {\n        ItemId = episode;\n        Start = 0;\n        End = 0;\n    }\n\n    /// <summary>\n    /// Initializes a new instance of the <see cref=\"Segment\"/> class.\n    /// </summary>\n    /// <param name=\"intro\">intro.</param>\n    public Segment(Segment intro)\n    {\n        ItemId = intro.ItemId;\n        Start = intro.Start;\n        End = intro.End;\n    }\n\n    /// <summary>\n    /// Initializes a new instance of the <see cref=\"Segment\"/> class.\n    /// </summary>\n    public Segment()\n    {\n    }\n\n    /// <summary>\n    /// Gets or sets the item ID of db.\n    /// </summary>\n    public Guid ItemId { get; set; }\n\n    /// <summary>\n    /// Gets a value indicating whether this introduction is valid or not.\n    /// Invalid results must not be returned through the API.\n    /// </summary>\n    public bool Valid => End > 0;\n\n    /// <summary>\n    /// Gets the duration of this intro.\n    /// </summary>\n    [JsonIgnore]\n    public double Duration => End - Start;\n\n    /// <summary>\n    /// Gets or sets the segment sequence start time.\n    /// </summary>\n    public double Start { get; set; }\n\n    /// <summary>\n    /// Gets or sets the segment sequence end time.\n    /// </summary>\n    public double End { get; set; }\n\n    /// <summary>\n    /// Gets or sets a value indicating whether this is an episode (not a movie).\n    /// </summary>\n    public bool IsEpisode { get; set; } = true;\n\n    /// <summary>\n    /// Gets or sets which analyzer created this segment.\n    /// </summary>\n    public AnalyzerType? AnalyzerType { get; set; }\n}\n"
  },
  {
    "path": "Jellyfin.Plugin.MediaAnalyzer/Data/TimeRange.cs",
    "content": "using System;\n\nnamespace Jellyfin.Plugin.MediaAnalyzer;\n\n#pragma warning disable CA1036 // Override methods on comparable types\n\n/// <summary>\n/// Range of contiguous time.\n/// </summary>\npublic class TimeRange : IComparable\n{\n    /// <summary>\n    /// Initializes a new instance of the <see cref=\"TimeRange\"/> class.\n    /// </summary>\n    public TimeRange()\n    {\n        Start = 0;\n        End = 0;\n    }\n\n    /// <summary>\n    /// Initializes a new instance of the <see cref=\"TimeRange\"/> class.\n    /// </summary>\n    /// <param name=\"start\">Time range start.</param>\n    /// <param name=\"end\">Time range end.</param>\n    public TimeRange(double start, double end)\n    {\n        Start = start;\n        End = end;\n    }\n\n    /// <summary>\n    /// Initializes a new instance of the <see cref=\"TimeRange\"/> class.\n    /// </summary>\n    /// <param name=\"original\">Original TimeRange.</param>\n    public TimeRange(TimeRange original)\n    {\n        Start = original.Start;\n        End = original.End;\n    }\n\n    /// <summary>\n    /// Gets or sets the time range start (in seconds).\n    /// </summary>\n    public double Start { get; set; }\n\n    /// <summary>\n    /// Gets or sets the time range end (in seconds).\n    /// </summary>\n    public double End { get; set; }\n\n    /// <summary>\n    /// Gets the duration of this time range (in seconds).\n    /// </summary>\n    public double Duration => End - Start;\n\n    /// <summary>\n    /// Compare TimeRange durations.\n    /// </summary>\n    /// <param name=\"obj\">Object to compare with.</param>\n    /// <returns>int.</returns>\n    public int CompareTo(object? obj)\n    {\n        if (!(obj is TimeRange tr))\n        {\n            throw new ArgumentException(\"obj must be a TimeRange\");\n        }\n\n        return tr.Duration.CompareTo(Duration);\n    }\n\n    /// <summary>\n    /// Tests if this TimeRange object intersects the provided TimeRange.\n    /// </summary>\n    /// <param name=\"tr\">Second TimeRange object to test.</param>\n    /// <returns>true if tr intersects the current TimeRange, false otherwise.</returns>\n    public bool Intersects(TimeRange tr)\n    {\n        return\n            (Start < tr.Start && tr.Start < End) ||\n            (Start < tr.End && tr.End < End);\n    }\n}\n"
  },
  {
    "path": "Jellyfin.Plugin.MediaAnalyzer/Data/TimeRangeHelpers.cs",
    "content": "using System;\nusing System.Collections.Generic;\n\nnamespace Jellyfin.Plugin.MediaAnalyzer;\n\n#pragma warning restore CA1036\n\n/// <summary>\n/// Time range helpers.\n/// </summary>\npublic static class TimeRangeHelpers\n{\n    /// <summary>\n    /// Finds the longest contiguous time range.\n    /// </summary>\n    /// <param name=\"times\">Sorted timestamps to search.</param>\n    /// <param name=\"maximumDistance\">Maximum distance permitted between contiguous timestamps.</param>\n    /// <returns>The longest contiguous time range (if one was found), or null (if none was found).</returns>\n    public static TimeRange? FindContiguous(double[] times, double maximumDistance)\n    {\n        if (times.Length == 0)\n        {\n            return null;\n        }\n\n        Array.Sort(times);\n\n        var ranges = new List<TimeRange>();\n        var currentRange = new TimeRange(times[0], times[0]);\n\n        // For all provided timestamps, check if it is contiguous with its neighbor.\n        for (var i = 0; i < times.Length - 1; i++)\n        {\n            var current = times[i];\n            var next = times[i + 1];\n\n            if (next - current <= maximumDistance)\n            {\n                currentRange.End = next;\n                continue;\n            }\n\n            ranges.Add(new TimeRange(currentRange));\n            currentRange = new TimeRange(next, next);\n        }\n\n        // Find and return the longest contiguous range.\n        ranges.Sort();\n\n        return (ranges.Count > 0) ? ranges[0] : null;\n    }\n}\n"
  },
  {
    "path": "Jellyfin.Plugin.MediaAnalyzer/Data/WarningManager.cs",
    "content": "namespace Jellyfin.Plugin.MediaAnalyzer;\n\nusing System;\n\n/// <summary>\n/// Support bundle warning.\n/// </summary>\n[Flags]\npublic enum PluginWarning\n{\n    /// <summary>\n    /// No warnings have been added.\n    /// </summary>\n    None = 0,\n\n    /// <summary>\n    /// Attempted to add skip button to web interface, but was unable to.\n    /// </summary>\n    UnableToAddSkipButton = 1,\n\n    /// <summary>\n    /// At least one media file on the server was unable to be fingerprinted by Chromaprint.\n    /// </summary>\n    InvalidChromaprintFingerprint = 2,\n\n    /// <summary>\n    /// The version of ffmpeg installed on the system is not compatible with the plugin.\n    /// </summary>\n    IncompatibleFFmpegBuild = 4,\n}\n\n/// <summary>\n/// Warning manager.\n/// </summary>\npublic static class WarningManager\n{\n    private static PluginWarning warnings;\n\n    /// <summary>\n    /// Set warning.\n    /// </summary>\n    /// <param name=\"warning\">Warning.</param>\n    public static void SetFlag(PluginWarning warning)\n    {\n        warnings |= warning;\n    }\n\n    /// <summary>\n    /// Clear warnings.\n    /// </summary>\n    public static void Clear()\n    {\n        warnings = PluginWarning.None;\n    }\n\n    /// <summary>\n    /// Get warnings.\n    /// </summary>\n    /// <returns>Warnings.</returns>\n    public static string GetWarnings()\n    {\n        return warnings.ToString();\n    }\n}\n"
  },
  {
    "path": "Jellyfin.Plugin.MediaAnalyzer/Db/MediaAnalyzerDbContext.cs",
    "content": "using System;\nusing Microsoft.EntityFrameworkCore;\n\nnamespace Jellyfin.Plugin.MediaAnalyzer;\n\n/// <summary>\n/// Plugin database.\n/// </summary>\npublic class MediaAnalyzerDbContext : DbContext\n{\n    private string dbPath;\n\n    /// <summary>\n    /// Initializes a new instance of the <see cref=\"MediaAnalyzerDbContext\"/> class.\n    /// </summary>\n    /// <param name=\"path\">Path to db.</param>\n    public MediaAnalyzerDbContext(string path)\n    {\n        dbPath = path;\n    }\n\n    /// <summary>\n    /// Initializes a new instance of the <see cref=\"MediaAnalyzerDbContext\"/> class.\n    /// </summary>\n    /// <param name=\"options\">The options.</param>\n    public MediaAnalyzerDbContext(DbContextOptions options) : base(options)\n    {\n        var folder = Environment.SpecialFolder.LocalApplicationData;\n        var path = Environment.GetFolderPath(folder);\n        dbPath = System.IO.Path.Join(path, \"mediaanalyzer.db\");\n    }\n\n    /// <summary>\n    /// Gets the <see cref=\"DbSet{TEntity}\"/> containing the blacklisted segments.\n    /// </summary>\n    public DbSet<SegmentMetadata> SegmentMetadata => Set<SegmentMetadata>();\n\n    /// <inheritdoc/>\n    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)\n        => optionsBuilder.UseSqlite($\"Data Source={dbPath}\");\n\n    /// <inheritdoc/>\n    protected override void OnModelCreating(ModelBuilder modelBuilder)\n    {\n        modelBuilder.Entity<SegmentMetadata>()\n            .HasKey(s => s.Id);\n\n        modelBuilder.Entity<SegmentMetadata>()\n            .Property(s => s.Id)\n            .ValueGeneratedOnAdd();\n\n        modelBuilder.Entity<SegmentMetadata>()\n            .HasIndex(s => s.ItemId);\n    }\n\n    /// <summary>\n    /// Apply migrations. Needs to be called before any actions are executed.\n    /// </summary>\n    public void ApplyMigrations()\n    {\n        this.Database.Migrate();\n    }\n}\n"
  },
  {
    "path": "Jellyfin.Plugin.MediaAnalyzer/Db/MediaAnalyzerDbFactory.cs",
    "content": "using Microsoft.EntityFrameworkCore;\nusing Microsoft.EntityFrameworkCore.Design;\n\nnamespace Jellyfin.Plugin.MediaAnalyzer;\n\n/// <summary>\n/// Plugin database factory.\n/// </summary>\npublic class MediaAnalyzerDbFactory : IDesignTimeDbContextFactory<MediaAnalyzerDbContext>\n{\n    /// <inheritdoc/>\n    public MediaAnalyzerDbContext CreateDbContext(string[] args)\n    {\n        var optionsBuilder = new DbContextOptionsBuilder<MediaAnalyzerDbContext>();\n        optionsBuilder.UseSqlite(\"Data Source=jfpmediaanalyzer.db\");\n\n        return new MediaAnalyzerDbContext(optionsBuilder.Options);\n    }\n}\n"
  },
  {
    "path": "Jellyfin.Plugin.MediaAnalyzer/Db/SegmentMetadata.cs",
    "content": "using System;\nusing Jellyfin.Data.Enums;\n\nnamespace Jellyfin.Plugin.MediaAnalyzer;\n\n/// <summary>\n/// Metadata for MediaSegments. Metadata is also created for non segments (e.g. analyze blocking).\n/// </summary>\npublic class SegmentMetadata\n{\n    /// <summary>\n    /// Initializes a new instance of the <see cref=\"SegmentMetadata\"/> class.\n    /// </summary>\n    /// <param name=\"media\">Media item.</param>\n    /// <param name=\"mode\">The mode it ran.</param>\n    /// <param name=\"analyzer\">Analyzer who created it.</param>\n    public SegmentMetadata(QueuedMedia media, MediaSegmentType mode, AnalyzerType analyzer)\n    {\n        ItemId = media.ItemId;\n        Type = mode;\n        AnalyzerType = analyzer;\n        Name = media.GetFullName();\n    }\n\n    /// <summary>\n    /// Initializes a new instance of the <see cref=\"SegmentMetadata\"/> class.\n    /// </summary>\n    public SegmentMetadata()\n    {\n    }\n\n    /// <summary>\n    /// Gets or sets the Id. Database generated.\n    /// </summary>\n    public Guid Id { get; set; }\n\n    /// <summary>\n    /// Gets or sets the \"full\" name for the Media (Series + Season + Episode or Movie + Source).\n    /// </summary>\n    public string Name { get; set; } = string.Empty;\n\n    /// <summary>\n    /// Gets or sets the segment Id of Jellyfin.\n    /// </summary>\n    public Guid SegmentId { get; set; }\n\n    /// <summary>\n    /// Gets or sets the item ID of Jellyfin.\n    /// </summary>\n    public Guid ItemId { get; set; }\n\n    /// <summary>\n    /// Gets or sets the segment type.\n    /// </summary>\n    public MediaSegmentType Type { get; set; }\n\n    /// <summary>\n    /// Gets or sets a value indicating whether the segment is blocked for future analysis.\n    /// </summary>\n    public bool PreventAnalyzing { get; set; }\n\n    /// <summary>\n    /// Gets or sets which analyzer created this segment.\n    /// </summary>\n    public AnalyzerType AnalyzerType { get; set; } = AnalyzerType.NotSet;\n\n    /// <summary>\n    /// Gets or sets the analyzer note. Data that an analyzer might provide as additional info.\n    /// </summary>\n    public string AnalyzerNote { get; set; } = string.Empty;\n}\n"
  },
  {
    "path": "Jellyfin.Plugin.MediaAnalyzer/Db/SegmentMetadataDb.cs",
    "content": "using System;\nusing System.Collections.Generic;\nusing System.Linq;\nusing System.Threading.Tasks;\nusing Jellyfin.Data.Enums;\nusing Jellyfin.Extensions;\nusing Microsoft.EntityFrameworkCore;\n\nnamespace Jellyfin.Plugin.MediaAnalyzer;\n\n/// <summary>\n/// Database api for segment metadata.\n/// </summary>\npublic class SegmentMetadataDb\n{\n    private string _pluginDbPath;\n\n    /// <summary>\n    /// Initializes a new instance of the <see cref=\"SegmentMetadataDb\"/> class.\n    /// </summary>\n    /// <param name=\"pluginDbPath\">Plugin db path.</param>\n    public SegmentMetadataDb(string pluginDbPath)\n    {\n        _pluginDbPath = pluginDbPath;\n    }\n\n    /// <summary>\n    /// Create or update a segment.\n    /// </summary>\n    /// <param name=\"seg\">Segment.</param>\n    public async void SaveSegment(SegmentMetadata seg)\n    {\n        using var db = GetPluginDb();\n        await CreateOrUpdate(db, seg).ConfigureAwait(false);\n        await db.SaveChangesAsync().ConfigureAwait(false);\n    }\n\n    /// <summary>\n    /// Create prevent analyze segments from QueuedMedia.\n    /// </summary>\n    /// <param name=\"media\">Queued Media.</param>\n    /// <param name=\"mode\">Mode.</param>\n    /// <returns>A <see cref=\"Task\"/> representing the asynchronous operation.</returns>\n    public async Task CreatePreventAnalyzeSegments(IReadOnlyCollection<QueuedMedia> media, MediaSegmentType mode)\n    {\n        using var db = GetPluginDb();\n\n        foreach (var seg in media)\n        {\n            var newseg = new SegmentMetadata\n            {\n                Name = seg.GetFullName(),\n                Type = mode,\n                PreventAnalyzing = true,\n                ItemId = seg.ItemId,\n            };\n\n            await CreateOrUpdate(db, newseg).ConfigureAwait(false);\n        }\n\n        await db.SaveChangesAsync().ConfigureAwait(false);\n    }\n\n    /// <summary>\n    /// Get metadata for segmentId.\n    /// </summary>\n    /// <param name=\"segmentId\">Media ItemId.</param>\n    /// <returns>A <see cref=\"Task\"/> representing the asynchronous operation.</returns>\n    public async Task<SegmentMetadata> GetSegment(Guid segmentId)\n    {\n        using var db = GetPluginDb();\n        return await db.SegmentMetadata.AsNoTracking().FirstAsync(s => s.SegmentId == segmentId).ConfigureAwait(false);\n    }\n\n    /// <summary>\n    /// Get metadata for itemId and type. We also store metadata for media that have no segmentId in jellyfin.\n    /// </summary>\n    /// <param name=\"itemId\">Media ItemId.</param>\n    /// <param name=\"type\">Segment Type.</param>\n    /// <param name=\"analyzer\">Optional: type of ananlyzer.</param>\n    /// <returns>A <see cref=\"Task\"/> representing the asynchronous operation.</returns>\n    public async Task<IEnumerable<SegmentMetadata>> GetSegments(Guid itemId, MediaSegmentType type, AnalyzerType? analyzer)\n    {\n        using var db = GetPluginDb();\n        var query = db.SegmentMetadata.Where(s => s.ItemId == itemId && s.Type == type);\n        if (analyzer is not null)\n        {\n            query = query.Where(s => s.AnalyzerType == analyzer);\n        }\n\n        return await query.AsNoTracking().ToListAsync().ConfigureAwait(false);\n    }\n\n    /// <summary>\n    /// Check if itemId and type should be prevented to analyze. AnalyzerType of these segments is NotSet.\n    /// </summary>\n    /// <param name=\"itemId\">Media ItemId.</param>\n    /// <param name=\"type\">Segment Type.</param>\n    /// <returns>A <see cref=\"Task\"/> representing the asynchronous operation.</returns>\n    public async Task<bool> PreventAnalyze(Guid itemId, MediaSegmentType type)\n    {\n        using var db = GetPluginDb();\n        var seg = await db.SegmentMetadata.FirstAsync(s => s.ItemId == itemId && s.Type == type && s.AnalyzerType == AnalyzerType.NotSet).ConfigureAwait(false);\n        // we may have multiple metadata for the same type+itemId. Search in all of them\n        return seg is not null && seg.PreventAnalyzing;\n    }\n\n    /// <summary>\n    /// Delete all metadata for itemId with prevent analyze set to true. Without itemId deletes them all.\n    /// </summary>\n    /// <param name=\"itemId\">Media ItemId.</param>\n    /// <returns>A <see cref=\"Task\"/> representing the asynchronous operation.</returns>\n    public async Task DeletePreventAnalyzeSegments(Guid? itemId)\n    {\n        using var db = GetPluginDb();\n        var query = db.SegmentMetadata.Where(s => s.PreventAnalyzing);\n\n        if (!itemId.IsNullOrEmpty())\n        {\n            query = db.SegmentMetadata.Where(s => s.ItemId == itemId);\n        }\n\n        await query.ExecuteDeleteAsync().ConfigureAwait(false);\n    }\n\n    /// <summary>\n    /// Delete all metadata for itemId and optional type.\n    /// </summary>\n    /// <param name=\"itemId\">Media ItemId.</param>\n    /// <param name=\"type\">Segment Type.</param>\n    /// <returns>A <see cref=\"Task\"/> representing the asynchronous operation.</returns>\n    public async Task DeleteSegments(Guid itemId, MediaSegmentType? type)\n    {\n        using var db = GetPluginDb();\n        var query = db.SegmentMetadata.Where(s => s.ItemId == itemId);\n\n        if (type is not null)\n        {\n            query = query.Where(s => s.Type == type);\n        }\n\n        await query.ExecuteDeleteAsync().ConfigureAwait(false);\n    }\n\n    /// <summary>\n    /// Create or update a segment.\n    /// </summary>\n    /// <param name=\"db\">Database.</param>\n    /// <param name=\"seg\">SegmentMetadata.</param>\n    /// <returns>Task.</returns>\n    private async Task CreateOrUpdate(MediaAnalyzerDbContext db, SegmentMetadata seg)\n    {\n        var found = await db.SegmentMetadata.FirstAsync(s => s.Id.Equals(seg.Id)).ConfigureAwait(false);\n\n        if (found is not null)\n        {\n            found.Name = seg.Name;\n            found.SegmentId = seg.SegmentId;\n            found.Type = seg.Type;\n            found.ItemId = seg.ItemId;\n            found.PreventAnalyzing = seg.PreventAnalyzing;\n            found.AnalyzerType = seg.AnalyzerType;\n            found.AnalyzerNote = seg.AnalyzerNote;\n        }\n        else\n        {\n            db.SegmentMetadata.Add(seg);\n        }\n    }\n\n    /// <summary>\n    /// Get context of plugin database.\n    /// </summary>\n    /// <returns>Instance of db.</returns>\n    public MediaAnalyzerDbContext GetPluginDb()\n    {\n        return new MediaAnalyzerDbContext(_pluginDbPath);\n    }\n}\n"
  },
  {
    "path": "Jellyfin.Plugin.MediaAnalyzer/Entrypoint/LibraryChangedEntrypoint.cs",
    "content": "using System;\nusing System.Threading;\nusing System.Threading.Tasks;\nusing Jellyfin.Data.Enums;\nusing MediaBrowser.Controller.Entities.Movies;\nusing MediaBrowser.Controller.Entities.TV;\nusing MediaBrowser.Controller.Library;\nusing MediaBrowser.Model.Entities;\nusing MediaBrowser.Model.Tasks;\nusing Microsoft.Extensions.Hosting;\nusing Microsoft.Extensions.Logging;\n\nnamespace Jellyfin.Plugin.MediaAnalyzer;\n\n/// <summary>\n/// Act on changes of the jellyfin library.\n/// </summary>\npublic sealed class LibraryChangedEntrypoint : IHostedService, IDisposable\n{\n    private readonly ILibraryManager _libraryManager;\n    private readonly ITaskManager _taskManager;\n    private readonly ILogger<LibraryChangedEntrypoint> _logger;\n    private readonly ILoggerFactory _loggerFactory;\n    private Timer _queueTimer;\n    private bool _analyzeAgain;\n\n    /// <summary>\n    /// Initializes a new instance of the <see cref=\"LibraryChangedEntrypoint\"/> class.\n    /// </summary>\n    /// <param name=\"libraryManager\">Library manager.</param>\n    /// <param name=\"taskManager\">Task manager.</param>\n    /// <param name=\"logger\">Logger.</param>\n    /// <param name=\"loggerFactory\">Logger factory.</param>\n    public LibraryChangedEntrypoint(\n        ILibraryManager libraryManager,\n        ITaskManager taskManager,\n        ILogger<LibraryChangedEntrypoint> logger,\n        ILoggerFactory loggerFactory)\n    {\n        _libraryManager = libraryManager;\n        _taskManager = taskManager;\n        _logger = logger;\n        _loggerFactory = loggerFactory;\n\n        _queueTimer = new Timer(\n         OnQueueTimerCallback,\n         null,\n         Timeout.InfiniteTimeSpan,\n         Timeout.InfiniteTimeSpan);\n    }\n\n    /// <inheritdoc/>\n    public Task StartAsync(CancellationToken cancellationToken)\n    {\n        _libraryManager.ItemAdded += LibraryManagerItemAdded;\n        _libraryManager.ItemUpdated += LibraryManagerItemUpdated;\n        _libraryManager.ItemRemoved += LibraryManagerItemRemoved;\n        FFmpegWrapper.Logger = _logger;\n\n        return Task.CompletedTask;\n    }\n\n    /// <inheritdoc/>\n    public Task StopAsync(CancellationToken cancellationToken)\n    {\n        _libraryManager.ItemAdded -= LibraryManagerItemAdded;\n        _libraryManager.ItemUpdated -= LibraryManagerItemUpdated;\n        _libraryManager.ItemRemoved -= LibraryManagerItemRemoved;\n        return Task.CompletedTask;\n    }\n\n    /// <summary>\n    /// Delete segments for itemid when library removed it.\n    /// </summary>\n    /// <param name=\"sender\">The sending entity.</param>\n    /// <param name=\"itemChangeEventArgs\">The <see cref=\"ItemChangeEventArgs\"/>.</param>\n    private void LibraryManagerItemRemoved(object? sender, ItemChangeEventArgs itemChangeEventArgs)\n    {\n        if (itemChangeEventArgs.Item is not Movie and not Episode)\n        {\n            return;\n        }\n\n        if (itemChangeEventArgs.Item.LocationType == LocationType.Virtual)\n        {\n            return;\n        }\n\n        _ = Plugin.Instance!.GetMetadataDb().DeleteSegments(itemChangeEventArgs.Item.Id, null);\n    }\n\n    /// <summary>\n    /// Library item was added.\n    /// </summary>\n    /// <param name=\"sender\">The sending entity.</param>\n    /// <param name=\"itemChangeEventArgs\">The <see cref=\"ItemChangeEventArgs\"/>.</param>\n    private void LibraryManagerItemAdded(object? sender, ItemChangeEventArgs itemChangeEventArgs)\n    {\n        if (!Plugin.Instance!.Configuration.RunAfterAddOrUpdateEvent)\n        {\n            return;\n        }\n\n        // Don't do anything if it's not a supported media type\n        if (itemChangeEventArgs.Item is not Movie and not Episode)\n        {\n            return;\n        }\n\n        if (itemChangeEventArgs.Item.LocationType == LocationType.Virtual)\n        {\n            return;\n        }\n\n        StartTimer();\n    }\n\n    /// <summary>\n    /// TaskManager task ended.\n    /// </summary>\n    /// <param name=\"sender\">The sending entity.</param>\n    /// <param name=\"eventArgs\">The <see cref=\"TaskCompletionEventArgs\"/>.</param>\n    private void TaskManagerTaskCompleted(object? sender, TaskCompletionEventArgs eventArgs)\n    {\n        var result = eventArgs.Result;\n\n        if (!Plugin.Instance!.Configuration.RunAfterLibraryScan)\n        {\n            return;\n        }\n\n        if (result.Key != \"RefreshLibrary\")\n        {\n            return;\n        }\n\n        if (result.Status != TaskCompletionStatus.Completed)\n        {\n            return;\n        }\n\n        StartTimer();\n    }\n\n    /// <summary>\n    /// Library item was updated.\n    /// </summary>\n    /// <param name=\"sender\">The sending entity.</param>\n    /// <param name=\"itemChangeEventArgs\">The <see cref=\"ItemChangeEventArgs\"/>.</param>\n    private void LibraryManagerItemUpdated(object? sender, ItemChangeEventArgs itemChangeEventArgs)\n    {\n        if (!Plugin.Instance!.Configuration.RunAfterAddOrUpdateEvent)\n        {\n            return;\n        }\n\n        // Don't do anything if it's not a supported media type\n        if (itemChangeEventArgs.Item is not Movie and not Episode)\n        {\n            return;\n        }\n\n        if (itemChangeEventArgs.Item.LocationType == LocationType.Virtual)\n        {\n            return;\n        }\n\n        StartTimer();\n    }\n\n    /// <summary>\n    /// Start or restart timer to debounce analyzing.\n    /// </summary>\n    private void StartTimer()\n    {\n        if (Plugin.Instance!.AnalysisRunning)\n        {\n            _analyzeAgain = true;\n        }\n        else\n        {\n            _logger.LogInformation(\"Media Library changed, analyzis will start soon!\");\n            _queueTimer.Change(TimeSpan.FromMilliseconds(15000), Timeout.InfiniteTimeSpan);\n        }\n    }\n\n    /// <summary>\n    /// Wait for timer callback to be completed.\n    /// </summary>\n    private void OnQueueTimerCallback(object? state)\n    {\n        try\n        {\n            OnQueueTimerCallbackInternal();\n        }\n        catch (Exception ex)\n        {\n            _logger.LogError(ex, \"Error in OnQueueTimerCallbackInternal\");\n        }\n    }\n\n    /// <summary>\n    /// Wait for timer to be completed.\n    /// </summary>\n    private void OnQueueTimerCallbackInternal()\n    {\n        _logger.LogInformation(\"Timer elapsed - start analyzing\");\n        Plugin.Instance!.AnalysisRunning = true;\n        var progress = new Progress<double>();\n        var cancellationToken = new CancellationToken(false);\n\n        // intro\n        var introBaseAnalyzer = new BaseItemAnalyzerTask(\n            MediaSegmentType.Intro,\n            _loggerFactory.CreateLogger<AnalyzeMedia>(),\n            _loggerFactory,\n            _libraryManager);\n\n        introBaseAnalyzer.AnalyzeItems(progress, cancellationToken);\n\n        // outro\n        var outroBaseAnalyzer = new BaseItemAnalyzerTask(\n            MediaSegmentType.Outro,\n            _loggerFactory.CreateLogger<AnalyzeMedia>(),\n            _loggerFactory,\n            _libraryManager);\n\n        outroBaseAnalyzer.AnalyzeItems(progress, cancellationToken);\n\n        Plugin.Instance!.AnalysisRunning = false;\n\n        // we might need to analyze again\n        if (_analyzeAgain)\n        {\n            _logger.LogInformation(\"Analyzing ended, but we need to analyze again!\");\n            _analyzeAgain = false;\n            StartTimer();\n        }\n    }\n\n    /// <inheritdoc/>\n    public void Dispose()\n    {\n        _queueTimer.Dispose();\n    }\n}\n"
  },
  {
    "path": "Jellyfin.Plugin.MediaAnalyzer/FFmpegWrapper.cs",
    "content": "using System;\nusing System.Collections.Generic;\nusing System.Diagnostics;\nusing System.Globalization;\nusing System.IO;\nusing System.Text;\nusing System.Text.RegularExpressions;\nusing Jellyfin.Data.Enums;\nusing Microsoft.Extensions.Logging;\n\nnamespace Jellyfin.Plugin.MediaAnalyzer;\n\n/// <summary>\n/// Wrapper for libchromaprint and the silencedetect filter.\n/// </summary>\npublic static class FFmpegWrapper\n{\n    private static readonly object InvertedIndexCacheLock = new();\n\n    /// <summary>\n    /// Used with FFmpeg's silencedetect filter to extract the start and end times of silence.\n    /// </summary>\n    private static readonly Regex SilenceDetectionExpression = new(\n        \"silence_(?<type>start|end): (?<time>[0-9\\\\.]+)\");\n\n    /// <summary>\n    /// Used with FFmpeg's blackframe filter to extract the time and percentage of black pixels.\n    /// </summary>\n    private static readonly Regex BlackFrameRegex = new(\"(pblack|t):[0-9.]+\");\n\n    /// <summary>\n    /// Gets or sets the logger.\n    /// </summary>\n    public static ILogger? Logger { get; set; }\n\n    private static Dictionary<string, string> ChromaprintLogs { get; set; } = new();\n\n    private static Dictionary<Guid, Dictionary<uint, int>> InvertedIndexCache { get; set; } = new();\n\n    /// <summary>\n    /// Check that the installed version of ffmpeg supports chromaprint.\n    /// </summary>\n    /// <returns>true if a compatible version of ffmpeg is installed, false on any error.</returns>\n    public static bool CheckFFmpegVersion()\n    {\n        try\n        {\n            // Always log ffmpeg's version information.\n            if (!CheckFFmpegRequirement(\n                \"-version\",\n                \"ffmpeg\",\n                \"version\",\n                \"Unknown error with FFmpeg version\"))\n            {\n                ChromaprintLogs[\"error\"] = \"unknown_error\";\n                WarningManager.SetFlag(PluginWarning.IncompatibleFFmpegBuild);\n                return false;\n            }\n\n            // First, validate that the installed version of ffmpeg supports chromaprint at all.\n            if (!CheckFFmpegRequirement(\n                \"-muxers\",\n                \"chromaprint\",\n                \"muxer list\",\n                \"The installed version of ffmpeg does not support chromaprint\"))\n            {\n                ChromaprintLogs[\"error\"] = \"chromaprint_not_supported\";\n                WarningManager.SetFlag(PluginWarning.IncompatibleFFmpegBuild);\n                return false;\n            }\n\n            // Second, validate that the Chromaprint muxer understands the \"-fp_format raw\" option.\n            if (!CheckFFmpegRequirement(\n                \"-h muxer=chromaprint\",\n                \"binary raw fingerprint\",\n                \"chromaprint options\",\n                \"The installed version of ffmpeg does not support raw binary fingerprints\"))\n            {\n                ChromaprintLogs[\"error\"] = \"fp_format_not_supported\";\n                WarningManager.SetFlag(PluginWarning.IncompatibleFFmpegBuild);\n                return false;\n            }\n\n            // Third, validate that ffmpeg supports of the all required silencedetect options.\n            if (!CheckFFmpegRequirement(\n                \"-h filter=silencedetect\",\n                \"noise tolerance\",\n                \"silencedetect options\",\n                \"The installed version of ffmpeg does not support the silencedetect filter\"))\n            {\n                ChromaprintLogs[\"error\"] = \"silencedetect_not_supported\";\n                WarningManager.SetFlag(PluginWarning.IncompatibleFFmpegBuild);\n                return false;\n            }\n\n            Logger?.LogDebug(\"Installed version of ffmpeg meets fingerprinting requirements\");\n            ChromaprintLogs[\"error\"] = \"okay\";\n            return true;\n        }\n        catch\n        {\n            ChromaprintLogs[\"error\"] = \"unknown_error\";\n            WarningManager.SetFlag(PluginWarning.IncompatibleFFmpegBuild);\n            return false;\n        }\n    }\n\n    /// <summary>\n    /// Fingerprint a queued episode.\n    /// </summary>\n    /// <param name=\"episode\">Queued episode to fingerprint.</param>\n    /// <param name=\"mode\">Portion of media file to fingerprint. Introduction = first 25% / 10 minutes and Credits = last 4 minutes.</param>\n    /// <returns>Numerical fingerprint points.</returns>\n    public static uint[] Fingerprint(QueuedMedia episode, MediaSegmentType mode)\n    {\n        int start, end;\n\n        if (mode == MediaSegmentType.Intro)\n        {\n            start = 0;\n            end = episode.IntroFingerprintEnd;\n        }\n        else if (mode == MediaSegmentType.Outro)\n        {\n            start = episode.CreditsFingerprintStart;\n            end = episode.Duration;\n        }\n        else\n        {\n            throw new ArgumentException(\"Unknown analysis mode \" + mode.ToString());\n        }\n\n        return Fingerprint(episode, mode, start, end);\n    }\n\n    /// <summary>\n    /// Transforms a Chromaprint into an inverted index of fingerprint points to the last index it appeared at.\n    /// </summary>\n    /// <param name=\"id\">Episode ID.</param>\n    /// <param name=\"fingerprint\">Chromaprint fingerprint.</param>\n    /// <returns>Inverted index.</returns>\n    public static Dictionary<uint, int> CreateInvertedIndex(Guid id, uint[] fingerprint)\n    {\n        lock (InvertedIndexCacheLock)\n        {\n            if (InvertedIndexCache.TryGetValue(id, out var cached))\n            {\n                return cached;\n            }\n        }\n\n        var invIndex = new Dictionary<uint, int>();\n\n        for (int i = 0; i < fingerprint.Length; i++)\n        {\n            // Get the current point.\n            var point = fingerprint[i];\n\n            // Append the current sample's timecode to the collection for this point.\n            invIndex[point] = i;\n        }\n\n        lock (InvertedIndexCacheLock)\n        {\n            InvertedIndexCache[id] = invIndex;\n        }\n\n        return invIndex;\n    }\n\n    /// <summary>\n    /// Detect ranges of silence in the provided episode.\n    /// </summary>\n    /// <param name=\"episode\">Queued episode.</param>\n    /// <param name=\"limit\">Maximum amount of audio (in seconds) to detect silence in.</param>\n    /// <returns>Array of TimeRange objects that are silent in the queued episode.</returns>\n    public static TimeRange[] DetectSilence(QueuedMedia episode, int limit)\n    {\n        Logger?.LogTrace(\n            \"Detecting silence in \\\"{File}\\\" (limit {Limit}, id {Id})\",\n            episode.Path,\n            limit,\n            episode.ItemId);\n\n        // -vn, -sn, -dn: ignore video, subtitle, and data tracks\n        var args = string.Format(\n            CultureInfo.InvariantCulture,\n            \"-vn -sn -dn \" +\n                \"-i \\\"{0}\\\" -to {1} -af \\\"silencedetect=noise={2}dB:duration=0.1\\\" -f null -\",\n            episode.Path,\n            limit,\n            Plugin.Instance?.Configuration.SilenceDetectionMaximumNoise ?? -50);\n\n        // Cache the output of this command to \"GUID-intro-silence-v1\"\n        var cacheKey = episode.ItemId.ToString(\"N\") + \"-intro-silence-v1\";\n\n        var currentRange = new TimeRange();\n        var silenceRanges = new List<TimeRange>();\n\n        /* Each match will have a type (either \"start\" or \"end\") and a timecode (a double).\n         *\n         * Sample output:\n         * [silencedetect @ 0x000000000000] silence_start: 12.34\n         * [silencedetect @ 0x000000000000] silence_end: 56.123 | silence_duration: 43.783\n        */\n        var raw = Encoding.UTF8.GetString(GetOutput(args, cacheKey, true));\n        foreach (Match match in SilenceDetectionExpression.Matches(raw))\n        {\n            var isStart = match.Groups[\"type\"].Value == \"start\";\n            var time = Convert.ToDouble(match.Groups[\"time\"].Value, CultureInfo.InvariantCulture);\n\n            if (isStart)\n            {\n                currentRange.Start = time;\n            }\n            else\n            {\n                currentRange.End = time;\n                silenceRanges.Add(new TimeRange(currentRange));\n            }\n        }\n\n        return silenceRanges.ToArray();\n    }\n\n    /// <summary>\n    /// Finds the location of all black frames in a media file within a time range.\n    /// </summary>\n    /// <param name=\"episode\">Media file to analyze.</param>\n    /// <param name=\"range\">Time range to search.</param>\n    /// <param name=\"minimum\">Percentage of the frame that must be black.</param>\n    /// <returns>Array of frames that are mostly black.</returns>\n    public static BlackFrame[] DetectBlackFrames(\n        QueuedMedia episode,\n        TimeRange range,\n        int minimum)\n    {\n        // Seek to the start of the time range and find frames that are at least 50% black.\n        var args = string.Format(\n            CultureInfo.InvariantCulture,\n            \"-ss {0} -i \\\"{1}\\\" -to {2} -an -dn -sn -vf \\\"blackframe=amount=50\\\" -f null -\",\n            range.Start,\n            episode.Path,\n            range.End - range.Start);\n\n        // Cache the results to GUID-blackframes-START-END-v1.\n        var cacheKey = string.Format(\n            CultureInfo.InvariantCulture,\n            \"{0}-blackframes-{1}-{2}-v1\",\n            episode.ItemId.ToString(\"N\"),\n            range.Start,\n            range.End);\n\n        var blackFrames = new List<BlackFrame>();\n\n        /* Run the blackframe filter.\n         *\n         * Sample output:\n         * [Parsed_blackframe_0 @ 0x0000000] frame:1 pblack:99 pts:43 t:0.043000 type:B last_keyframe:0\n         * [Parsed_blackframe_0 @ 0x0000000] frame:2 pblack:99 pts:85 t:0.085000 type:B last_keyframe:0\n         */\n        var raw = Encoding.UTF8.GetString(GetOutput(args, cacheKey, true));\n        foreach (var line in raw.Split('\\n'))\n        {\n            var matches = BlackFrameRegex.Matches(line);\n            if (matches.Count != 2)\n            {\n                continue;\n            }\n\n            var (strPercent, strTime) = (\n                matches[0].Value.Split(':')[1],\n                matches[1].Value.Split(':')[1]\n            );\n\n            var bf = new BlackFrame(\n                Convert.ToInt32(strPercent, CultureInfo.InvariantCulture),\n                Convert.ToDouble(strTime, CultureInfo.InvariantCulture));\n\n            if (bf.Percentage > minimum)\n            {\n                blackFrames.Add(bf);\n            }\n        }\n\n        return blackFrames.ToArray();\n    }\n\n    /// <summary>\n    /// Gets Chromaprint debugging logs.\n    /// </summary>\n    /// <returns>Markdown formatted logs.</returns>\n    public static string GetChromaprintLogs()\n    {\n        // Print the FFmpeg detection status at the top.\n        // Format: \"* FFmpeg: `error`\"\n        // Append two newlines to separate the bulleted list from the logs\n        var logs = string.Format(\n            CultureInfo.InvariantCulture,\n            \"* FFmpeg: `{0}`\\n\\n\",\n            ChromaprintLogs[\"error\"]);\n\n        // Always include ffmpeg version information\n        logs += FormatFFmpegLog(\"version\");\n\n        // Don't print feature detection logs if the plugin started up okay\n        if (ChromaprintLogs[\"error\"] == \"okay\")\n        {\n            return logs;\n        }\n\n        // Print all remaining logs\n        foreach (var kvp in ChromaprintLogs)\n        {\n            if (kvp.Key == \"error\" || kvp.Key == \"version\")\n            {\n                continue;\n            }\n\n            logs += FormatFFmpegLog(kvp.Key);\n        }\n\n        return logs;\n    }\n\n    /// <summary>\n    /// Run an FFmpeg command with the provided arguments and validate that the output contains\n    /// the provided string.\n    /// </summary>\n    /// <param name=\"arguments\">Arguments to pass to FFmpeg.</param>\n    /// <param name=\"mustContain\">String that the output must contain. Case insensitive.</param>\n    /// <param name=\"bundleName\">Support bundle key to store FFmpeg's output under.</param>\n    /// <param name=\"errorMessage\">Error message to log if this requirement is not met.</param>\n    /// <returns>true on success, false on error.</returns>\n    private static bool CheckFFmpegRequirement(\n        string arguments,\n        string mustContain,\n        string bundleName,\n        string errorMessage)\n    {\n        Logger?.LogDebug(\"Checking FFmpeg requirement {Arguments}\", arguments);\n\n        var output = Encoding.UTF8.GetString(GetOutput(arguments, string.Empty, false, 2000));\n        Logger?.LogTrace(\"Output of ffmpeg {Arguments}: {Output}\", arguments, output);\n        ChromaprintLogs[bundleName] = output;\n\n        if (!output.Contains(mustContain, StringComparison.OrdinalIgnoreCase))\n        {\n            Logger?.LogError(\"{ErrorMessage}\", errorMessage);\n            return false;\n        }\n\n        Logger?.LogDebug(\"FFmpeg requirement {Arguments} met\", arguments);\n\n        return true;\n    }\n\n    /// <summary>\n    /// Runs ffmpeg and returns standard output (or error).\n    /// If caching is enabled, will use cacheFilename to cache the output of this command.\n    /// </summary>\n    /// <param name=\"args\">Arguments to pass to ffmpeg.</param>\n    /// <param name=\"cacheFilename\">Filename to cache the output of this command to, or string.Empty if this command should not be cached.</param>\n    /// <param name=\"stderr\">If standard error should be returned.</param>\n    /// <param name=\"timeout\">Timeout (in miliseconds) to wait for ffmpeg to exit.</param>\n    private static ReadOnlySpan<byte> GetOutput(\n        string args,\n        string cacheFilename,\n        bool stderr = false,\n        int timeout = 60 * 1000)\n    {\n        var ffmpegPath = Plugin.Instance?.FFmpegPath ?? \"ffmpeg\";\n\n        // The silencedetect and blackframe filters output data at the info log level.\n        var useInfoLevel = args.Contains(\"silencedetect\", StringComparison.OrdinalIgnoreCase) ||\n            args.Contains(\"blackframe\", StringComparison.OrdinalIgnoreCase);\n\n        var logLevel = useInfoLevel ? \"info\" : \"warning\";\n\n        var cacheOutput =\n            (Plugin.Instance?.Configuration.CacheFingerprints ?? false) &&\n            !string.IsNullOrEmpty(cacheFilename);\n\n        // If caching is enabled, try to load the output of this command from the cached file.\n        if (cacheOutput)\n        {\n            // Calculate the absolute path to the cached file.\n            cacheFilename = Path.Join(Plugin.Instance!.FingerprintCachePath, cacheFilename);\n\n            // If the cached file exists, return whatever it holds.\n            if (File.Exists(cacheFilename))\n            {\n                Logger?.LogTrace(\"Returning contents of cache {Cache}\", cacheFilename);\n                return File.ReadAllBytes(cacheFilename);\n            }\n\n            Logger?.LogTrace(\"Not returning contents of cache {Cache} (not found)\", cacheFilename);\n        }\n\n        // Prepend some flags to prevent FFmpeg from logging it's banner and progress information\n        // for each file that is fingerprinted.\n        var prependArgument = string.Format(\n            CultureInfo.InvariantCulture,\n            \"-hide_banner -loglevel {0} \",\n            logLevel);\n\n        var info = new ProcessStartInfo(ffmpegPath, args.Insert(0, prependArgument))\n        {\n            WindowStyle = ProcessWindowStyle.Hidden,\n            CreateNoWindow = true,\n            UseShellExecute = false,\n            ErrorDialog = false,\n\n            RedirectStandardOutput = !stderr,\n            RedirectStandardError = stderr\n        };\n\n        var ffmpeg = new Process\n        {\n            StartInfo = info\n        };\n\n        Logger?.LogDebug(\n            \"Starting ffmpeg with the following arguments: {Arguments}\",\n            ffmpeg.StartInfo.Arguments);\n\n        ffmpeg.Start();\n\n        using (MemoryStream ms = new MemoryStream())\n        {\n            var buf = new byte[4096];\n            var bytesRead = 0;\n\n            do\n            {\n                var streamReader = stderr ? ffmpeg.StandardError : ffmpeg.StandardOutput;\n                bytesRead = streamReader.BaseStream.Read(buf, 0, buf.Length);\n                ms.Write(buf, 0, bytesRead);\n            }\n            while (bytesRead > 0);\n\n            ffmpeg.WaitForExit(timeout);\n\n            var output = ms.ToArray();\n\n            // If caching is enabled, cache the output of this command.\n            if (cacheOutput)\n            {\n                File.WriteAllBytes(cacheFilename, output);\n            }\n\n            return output;\n        }\n    }\n\n    /// <summary>\n    /// Fingerprint a queued episode.\n    /// </summary>\n    /// <param name=\"episode\">Queued episode to fingerprint.</param>\n    /// <param name=\"mode\">Portion of media file to fingerprint.</param>\n    /// <param name=\"start\">Time (in seconds) relative to the start of the file to start fingerprinting from.</param>\n    /// <param name=\"end\">Time (in seconds) relative to the start of the file to stop fingerprinting at.</param>\n    /// <returns>Numerical fingerprint points.</returns>\n    private static uint[] Fingerprint(QueuedMedia episode, MediaSegmentType mode, int start, int end)\n    {\n        // Try to load this episode from cache before running ffmpeg.\n        if (LoadCachedFingerprint(episode, mode, out uint[] cachedFingerprint))\n        {\n            Logger?.LogTrace(\"Fingerprint cache hit on {File}\", episode.Path);\n            return cachedFingerprint;\n        }\n\n        Logger?.LogDebug(\n            \"Fingerprinting [{Start}, {End}] from \\\"{File}\\\" (id {Id})\",\n            start,\n            end,\n            episode.Path,\n            episode.ItemId);\n\n        var args = string.Format(\n            CultureInfo.InvariantCulture,\n            \"-ss {0} -i \\\"{1}\\\" -to {2} -ac 2 -f chromaprint -fp_format raw -\",\n            start,\n            episode.Path,\n            end - start);\n\n        // Returns all fingerprint points as raw 32 bit unsigned integers (little endian).\n        var rawPoints = GetOutput(args, string.Empty);\n        if (rawPoints.Length == 0 || rawPoints.Length % 4 != 0)\n        {\n            Logger?.LogWarning(\"Chromaprint returned {Count} points for \\\"{Path}\\\"\", rawPoints.Length, episode.Path);\n            throw new FingerprintException(\"chromaprint output for \\\"\" + episode.Path + \"\\\" was malformed\");\n        }\n\n        var results = new List<uint>();\n        for (var i = 0; i < rawPoints.Length; i += 4)\n        {\n            var rawPoint = rawPoints.Slice(i, 4);\n            results.Add(BitConverter.ToUInt32(rawPoint));\n        }\n\n        // Try to cache this fingerprint.\n        CacheFingerprint(episode, mode, results);\n\n        return results.ToArray();\n    }\n\n    /// <summary>\n    /// Tries to load an episode's fingerprint from cache. If caching is not enabled, calling this function is a no-op.\n    /// This function was created before the unified caching mechanism was introduced (in v0.1.7).\n    /// </summary>\n    /// <param name=\"episode\">Episode to try to load from cache.</param>\n    /// <param name=\"mode\">Analysis mode.</param>\n    /// <param name=\"fingerprint\">Array to store the fingerprint in.</param>\n    /// <returns>true if the episode was successfully loaded from cache, false on any other error.</returns>\n    private static bool LoadCachedFingerprint(\n        QueuedMedia episode,\n        MediaSegmentType mode,\n        out uint[] fingerprint)\n    {\n        fingerprint = Array.Empty<uint>();\n\n        // If fingerprint caching isn't enabled, don't try to load anything.\n        if (!(Plugin.Instance?.Configuration.CacheFingerprints ?? false))\n        {\n            return false;\n        }\n\n        var path = GetFingerprintCachePath(episode, mode);\n\n        // If this episode isn't cached, bail out.\n        if (!File.Exists(path))\n        {\n            return false;\n        }\n\n        var raw = File.ReadAllLines(path, Encoding.UTF8);\n        var result = new List<uint>();\n\n        // Read each stringified uint.\n        result.EnsureCapacity(raw.Length);\n\n        try\n        {\n            foreach (var rawNumber in raw)\n            {\n                result.Add(Convert.ToUInt32(rawNumber, CultureInfo.InvariantCulture));\n            }\n        }\n        catch (FormatException)\n        {\n            // Occurs when the cached fingerprint is corrupt.\n            Logger?.LogDebug(\n                \"Cached fingerprint for {Path} ({Id}) is corrupt, ignoring cache\",\n                episode.Path,\n                episode.ItemId);\n\n            return false;\n        }\n\n        fingerprint = result.ToArray();\n        return true;\n    }\n\n    /// <summary>\n    /// Cache an episode's fingerprint to disk. If caching is not enabled, calling this function is a no-op.\n    /// This function was created before the unified caching mechanism was introduced (in v0.1.7).\n    /// </summary>\n    /// <param name=\"episode\">Episode to store in cache.</param>\n    /// <param name=\"mode\">Analysis mode.</param>\n    /// <param name=\"fingerprint\">Fingerprint of the episode to store.</param>\n    private static void CacheFingerprint(\n        QueuedMedia episode,\n        MediaSegmentType mode,\n        List<uint> fingerprint)\n    {\n        // Bail out if caching isn't enabled.\n        if (!(Plugin.Instance?.Configuration.CacheFingerprints ?? false))\n        {\n            return;\n        }\n\n        // Stringify each data point.\n        var lines = new List<string>();\n        foreach (var number in fingerprint)\n        {\n            lines.Add(number.ToString(CultureInfo.InvariantCulture));\n        }\n\n        // Cache the episode.\n        File.WriteAllLinesAsync(\n            GetFingerprintCachePath(episode, mode),\n            lines,\n            Encoding.UTF8).ConfigureAwait(false);\n    }\n\n    /// <summary>\n    /// Determines the path an episode should be cached at.\n    /// This function was created before the unified caching mechanism was introduced (in v0.1.7).\n    /// </summary>\n    /// <param name=\"episode\">Episode.</param>\n    /// <param name=\"mode\">Analysis mode.</param>\n    private static string GetFingerprintCachePath(QueuedMedia episode, MediaSegmentType mode)\n    {\n        var basePath = Path.Join(\n            Plugin.Instance!.FingerprintCachePath,\n            episode.ItemId.ToString(\"N\"));\n\n        if (mode == MediaSegmentType.Intro)\n        {\n            return basePath;\n        }\n        else if (mode == MediaSegmentType.Outro)\n        {\n            return basePath + \"-credits\";\n        }\n        else\n        {\n            throw new ArgumentException(\"Unknown analysis mode \" + mode.ToString());\n        }\n    }\n\n    private static string FormatFFmpegLog(string key)\n    {\n        /* Format:\n        * FFmpeg NAME:\n        * ```\n        * LOGS\n        * ```\n        */\n\n        var formatted = string.Format(CultureInfo.InvariantCulture, \"FFmpeg {0}:\\n```\\n\", key);\n        formatted += ChromaprintLogs[key];\n\n        // Ensure the closing triple backtick is on a separate line\n        if (!formatted.EndsWith('\\n'))\n        {\n            formatted += \"\\n\";\n        }\n\n        formatted += \"```\\n\\n\";\n\n        return formatted;\n    }\n}\n"
  },
  {
    "path": "Jellyfin.Plugin.MediaAnalyzer/Helper/Utils.cs",
    "content": "using System;\n\nnamespace Jellyfin.Plugin.MediaAnalyzer;\n\n/// <summary>\n/// Convert between Ticks and other time representations.\n/// </summary>\npublic static class Utils\n{\n    /// <summary>\n    /// Convert Seconds to Ticks.\n    /// </summary>\n    /// <param name=\"value\">seconds.</param>\n    /// <returns>Time in ticks.</returns>\n    public static long SToTicks(double value)\n    {\n        return TimeSpan.FromSeconds(value).Ticks;\n    }\n\n    /// <summary>\n    /// Convert Ticks to Seconds.\n    /// </summary>\n    /// <param name=\"value\">ticks.</param>\n    /// <returns>Time in seconds.</returns>\n    public static double TicksToS(long value)\n    {\n        return TimeSpan.FromTicks(value).Seconds;\n    }\n}\n"
  },
  {
    "path": "Jellyfin.Plugin.MediaAnalyzer/Jellyfin.Plugin.MediaAnalyzer.csproj",
    "content": "<Project Sdk=\"Microsoft.NET.Sdk\">\n\n  <PropertyGroup>\n    <TargetFramework>net8.0</TargetFramework>\n    <RootNamespace>Jellyfin.Plugin.MediaAnalyzer</RootNamespace>\n    <AssemblyVersion>0.4.0.0</AssemblyVersion>\n    <FileVersion>0.4.0.0</FileVersion>\n    <GenerateDocumentationFile>true</GenerateDocumentationFile>\n    <TreatWarningsAsErrors>true</TreatWarningsAsErrors>\n    <Nullable>enable</Nullable>\n    <MediaSegmentType>AllEnabledByDefault</MediaSegmentType>\n    <CodeAnalysisRuleSet>../jellyfin.ruleset</CodeAnalysisRuleSet>\n  </PropertyGroup>\n\n  <ItemGroup>\n    <!--\n    <PackageReference Include=\"Jellyfin.Controller\" Version=\"10.10.*\" />\n    <PackageReference Include=\"Jellyfin.Model\" Version=\"10.10.*\" />\n    -->\n    <PackageReference Include=\"Microsoft.EntityFrameworkCore.Design\" Version=\"8.0.3\">\n      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>\n      <PrivateAssets>all</PrivateAssets>\n    </PackageReference>\n    <PackageReference Include=\"Microsoft.EntityFrameworkCore.Sqlite\" Version=\"8.0.3\" />\n    <PackageReference Include=\"SerilogAnalyzer\" Version=\"0.15.0\" PrivateAssets=\"All\" />\n    <PackageReference Include=\"StyleCop.Analyzers\" Version=\"1.2.0-beta.556\" PrivateAssets=\"All\" />\n    <PackageReference Include=\"SmartAnalyzers.MultithreadingAnalyzer\" Version=\"1.1.31\" PrivateAssets=\"All\" />\n  </ItemGroup>\n  <!-- REMOVE AND USE NUGET PUBLISHED PACKAGES -->\n  <ItemGroup>\n    <ProjectReference Include=\"../../jellyfin/Jellyfin.Data/Jellyfin.Data.csproj\" />\n    <ProjectReference Include=\"../../jellyfin/MediaBrowser.Model/MediaBrowser.Model.csproj\" />\n    <ProjectReference Include=\"../../jellyfin/MediaBrowser.Controller/MediaBrowser.Controller.csproj\" />\n    <ProjectReference Include=\"../../jellyfin/MediaBrowser.Common/MediaBrowser.Common.csproj\" />\n    <ProjectReference Include=\"../../jellyfin/src/Jellyfin.Extensions/Jellyfin.Extensions.csproj\" />\n  </ItemGroup>\n\n  <ItemGroup>\n    <None Remove=\"Configuration\\configPage.html\" />\n    <EmbeddedResource Include=\"Configuration\\configPage.html\" />\n    <EmbeddedResource Include=\"Configuration\\visualizer.js\" />\n    <EmbeddedResource Include=\"Configuration\\version.txt\" />\n  </ItemGroup>\n\n</Project>\n"
  },
  {
    "path": "Jellyfin.Plugin.MediaAnalyzer/Migrations/20230525091047_CreateBlacklistSegment.Designer.cs",
    "content": "﻿// <auto-generated />\nusing System;\nusing Jellyfin.Plugin.MediaAnalyzer;\nusing Microsoft.EntityFrameworkCore;\nusing Microsoft.EntityFrameworkCore.Infrastructure;\nusing Microsoft.EntityFrameworkCore.Migrations;\nusing Microsoft.EntityFrameworkCore.Storage.ValueConversion;\n\n#nullable disable\n\nnamespace Jellyfin.Plugin.MediaAnalyzer.Migrations\n{\n    [DbContext(typeof(MediaAnalyzerDbContext))]\n    [Migration(\"20230525091047_CreateBlacklistSegment\")]\n    partial class CreateBlacklistSegment\n    {\n        /// <inheritdoc />\n        protected override void BuildTargetModel(ModelBuilder modelBuilder)\n        {\n#pragma warning disable 612, 618\n            modelBuilder.HasAnnotation(\"ProductVersion\", \"7.0.5\");\n\n            modelBuilder.Entity(\"Jellyfin.Plugin.MediaAnalyzer.BlacklistSegment\", b =>\n                {\n                    b.Property<Guid>(\"ItemId\")\n                        .HasColumnType(\"TEXT\");\n\n                    b.Property<int>(\"Type\")\n                        .HasColumnType(\"INTEGER\");\n\n                    b.Property<string>(\"Name\")\n                        .IsRequired()\n                        .HasColumnType(\"TEXT\");\n\n                    b.HasKey(\"ItemId\", \"Type\");\n\n                    b.ToTable(\"BlacklistSegment\");\n                });\n#pragma warning restore 612, 618\n        }\n    }\n}\n"
  },
  {
    "path": "Jellyfin.Plugin.MediaAnalyzer/Migrations/20230525091047_CreateBlacklistSegment.cs",
    "content": "﻿using System;\nusing Microsoft.EntityFrameworkCore.Migrations;\n\n#nullable disable\n\nnamespace Jellyfin.Plugin.MediaAnalyzer.Migrations\n{\n    /// <inheritdoc />\n    public partial class CreateBlacklistSegment : Migration\n    {\n        /// <inheritdoc />\n        protected override void Up(MigrationBuilder migrationBuilder)\n        {\n            migrationBuilder.CreateTable(\n                name: \"BlacklistSegment\",\n                columns: table => new\n                {\n                    ItemId = table.Column<Guid>(type: \"TEXT\", nullable: false),\n                    Type = table.Column<int>(type: \"INTEGER\", nullable: false),\n                    Name = table.Column<string>(type: \"TEXT\", nullable: false)\n                },\n                constraints: table =>\n                {\n                    table.PrimaryKey(\"PK_BlacklistSegment\", x => new { x.ItemId, x.Type });\n                });\n        }\n\n        /// <inheritdoc />\n        protected override void Down(MigrationBuilder migrationBuilder)\n        {\n            migrationBuilder.DropTable(\n                name: \"BlacklistSegment\");\n        }\n    }\n}\n"
  },
  {
    "path": "Jellyfin.Plugin.MediaAnalyzer/Migrations/20240903114429_CreateSegmentMetadata.Designer.cs",
    "content": "﻿// <auto-generated />\nusing System;\nusing Jellyfin.Plugin.MediaAnalyzer;\nusing Microsoft.EntityFrameworkCore;\nusing Microsoft.EntityFrameworkCore.Infrastructure;\nusing Microsoft.EntityFrameworkCore.Migrations;\nusing Microsoft.EntityFrameworkCore.Storage.ValueConversion;\n\n#nullable disable\n\nnamespace Jellyfin.Plugin.MediaAnalyzer.Migrations\n{\n    [DbContext(typeof(MediaAnalyzerDbContext))]\n    [Migration(\"20240903114429_CreateSegmentMetadata\")]\n    partial class CreateSegmentMetadata\n    {\n        /// <inheritdoc />\n        protected override void BuildTargetModel(ModelBuilder modelBuilder)\n        {\n#pragma warning disable 612, 618\n            modelBuilder.HasAnnotation(\"ProductVersion\", \"8.0.3\");\n\n            modelBuilder.Entity(\"Jellyfin.Plugin.MediaAnalyzer.SegmentMetadata\", b =>\n                {\n                    b.Property<Guid>(\"Id\")\n                        .ValueGeneratedOnAdd()\n                        .HasColumnType(\"TEXT\");\n\n                    b.Property<string>(\"AnalyzerNote\")\n                        .IsRequired()\n                        .HasColumnType(\"TEXT\");\n\n                    b.Property<int?>(\"AnalyzerType\")\n                        .HasColumnType(\"INTEGER\");\n\n                    b.Property<Guid>(\"ItemId\")\n                        .HasColumnType(\"TEXT\");\n\n                    b.Property<string>(\"Name\")\n                        .IsRequired()\n                        .HasColumnType(\"TEXT\");\n\n                    b.Property<bool>(\"PreventAnalyzing\")\n                        .HasColumnType(\"INTEGER\");\n\n                    b.Property<Guid>(\"SegmentId\")\n                        .HasColumnType(\"TEXT\");\n\n                    b.Property<string>(\"SeriesName\")\n                        .IsRequired()\n                        .HasColumnType(\"TEXT\");\n\n                    b.Property<int>(\"Type\")\n                        .HasColumnType(\"INTEGER\");\n\n                    b.HasKey(\"Id\");\n\n                    b.HasIndex(\"ItemId\");\n\n                    b.ToTable(\"SegmentMetadata\");\n                });\n#pragma warning restore 612, 618\n        }\n    }\n}\n"
  },
  {
    "path": "Jellyfin.Plugin.MediaAnalyzer/Migrations/20240903114429_CreateSegmentMetadata.cs",
    "content": "﻿using System;\nusing Microsoft.EntityFrameworkCore.Migrations;\n\n#nullable disable\n\nnamespace Jellyfin.Plugin.MediaAnalyzer.Migrations\n{\n    /// <inheritdoc />\n    public partial class CreateSegmentMetadata : Migration\n    {\n        /// <inheritdoc />\n        protected override void Up(MigrationBuilder migrationBuilder)\n        {\n            migrationBuilder.DropTable(\n                name: \"BlacklistSegment\");\n\n            migrationBuilder.CreateTable(\n                name: \"SegmentMetadata\",\n                columns: table => new\n                {\n                    Id = table.Column<Guid>(type: \"TEXT\", nullable: false),\n                    Name = table.Column<string>(type: \"TEXT\", nullable: false),\n                    SeriesName = table.Column<string>(type: \"TEXT\", nullable: false),\n                    SegmentId = table.Column<Guid>(type: \"TEXT\", nullable: false),\n                    ItemId = table.Column<Guid>(type: \"TEXT\", nullable: false),\n                    Type = table.Column<int>(type: \"INTEGER\", nullable: false),\n                    PreventAnalyzing = table.Column<bool>(type: \"INTEGER\", nullable: false),\n                    AnalyzerType = table.Column<int>(type: \"INTEGER\", nullable: true),\n                    AnalyzerNote = table.Column<string>(type: \"TEXT\", nullable: false)\n                },\n                constraints: table =>\n                {\n                    table.PrimaryKey(\"PK_SegmentMetadata\", x => x.Id);\n                });\n\n            migrationBuilder.CreateIndex(\n                name: \"IX_SegmentMetadata_ItemId\",\n                table: \"SegmentMetadata\",\n                column: \"ItemId\");\n        }\n\n        /// <inheritdoc />\n        protected override void Down(MigrationBuilder migrationBuilder)\n        {\n            migrationBuilder.DropTable(\n                name: \"SegmentMetadata\");\n\n            migrationBuilder.CreateTable(\n                name: \"BlacklistSegment\",\n                columns: table => new\n                {\n                    ItemId = table.Column<Guid>(type: \"TEXT\", nullable: false),\n                    Type = table.Column<int>(type: \"INTEGER\", nullable: false),\n                    Name = table.Column<string>(type: \"TEXT\", nullable: false)\n                },\n                constraints: table =>\n                {\n                    table.PrimaryKey(\"PK_BlacklistSegment\", x => new { x.ItemId, x.Type });\n                });\n        }\n    }\n}\n"
  },
  {
    "path": "Jellyfin.Plugin.MediaAnalyzer/Migrations/MediaAnalyzerDbContextModelSnapshot.cs",
    "content": "﻿// <auto-generated />\nusing System;\nusing Jellyfin.Plugin.MediaAnalyzer;\nusing Microsoft.EntityFrameworkCore;\nusing Microsoft.EntityFrameworkCore.Infrastructure;\nusing Microsoft.EntityFrameworkCore.Storage.ValueConversion;\n\n#nullable disable\n\nnamespace Jellyfin.Plugin.MediaAnalyzer.Migrations\n{\n    [DbContext(typeof(MediaAnalyzerDbContext))]\n    partial class MediaAnalyzerDbContextModelSnapshot : ModelSnapshot\n    {\n        protected override void BuildModel(ModelBuilder modelBuilder)\n        {\n#pragma warning disable 612, 618\n            modelBuilder.HasAnnotation(\"ProductVersion\", \"8.0.3\");\n\n            modelBuilder.Entity(\"Jellyfin.Plugin.MediaAnalyzer.SegmentMetadata\", b =>\n                {\n                    b.Property<Guid>(\"Id\")\n                        .ValueGeneratedOnAdd()\n                        .HasColumnType(\"TEXT\");\n\n                    b.Property<string>(\"AnalyzerNote\")\n                        .IsRequired()\n                        .HasColumnType(\"TEXT\");\n\n                    b.Property<int?>(\"AnalyzerType\")\n                        .HasColumnType(\"INTEGER\");\n\n                    b.Property<Guid>(\"ItemId\")\n                        .HasColumnType(\"TEXT\");\n\n                    b.Property<string>(\"Name\")\n                        .IsRequired()\n                        .HasColumnType(\"TEXT\");\n\n                    b.Property<bool>(\"PreventAnalyzing\")\n                        .HasColumnType(\"INTEGER\");\n\n                    b.Property<Guid>(\"SegmentId\")\n                        .HasColumnType(\"TEXT\");\n\n                    b.Property<string>(\"SeriesName\")\n                        .IsRequired()\n                        .HasColumnType(\"TEXT\");\n\n                    b.Property<int>(\"Type\")\n                        .HasColumnType(\"INTEGER\");\n\n                    b.HasKey(\"Id\");\n\n                    b.HasIndex(\"ItemId\");\n\n                    b.ToTable(\"SegmentMetadata\");\n                });\n#pragma warning restore 612, 618\n        }\n    }\n}\n"
  },
  {
    "path": "Jellyfin.Plugin.MediaAnalyzer/Plugin.cs",
    "content": "using System;\nusing System.Collections.Generic;\nusing System.IO;\nusing Jellyfin.Plugin.MediaAnalyzer.Configuration;\nusing MediaBrowser.Common.Configuration;\nusing MediaBrowser.Common.Plugins;\nusing MediaBrowser.Controller;\nusing MediaBrowser.Controller.Configuration;\nusing MediaBrowser.Controller.Entities;\nusing MediaBrowser.Controller.Library;\nusing MediaBrowser.Controller.Persistence;\nusing MediaBrowser.Model.Entities;\nusing MediaBrowser.Model.Plugins;\nusing MediaBrowser.Model.Serialization;\nusing Microsoft.Extensions.Logging;\n\nnamespace Jellyfin.Plugin.MediaAnalyzer;\n\n/// <summary>\n/// TV Show Intro Skip plugin. Uses audio analysis to find common sequences of audio shared between episodes.\n/// </summary>\npublic class Plugin : BasePlugin<PluginConfiguration>, IHasWebPages\n{\n    private IXmlSerializer _xmlSerializer;\n    private ILibraryManager _libraryManager;\n    private IItemRepository _itemRepository;\n    private IMediaSegmentManager _mediaSegmentsManager;\n    private ILogger<Plugin> _logger;\n    private string _pluginCachePath;\n    private string _pluginDbPath;\n    private SegmentMetadataDb _segmentMetadataDb;\n    private MediaSegmentsDb _mediasegmentsDb;\n\n    /// <summary>\n    /// Initializes a new instance of the <see cref=\"Plugin\"/> class.\n    /// </summary>\n    /// <param name=\"applicationPaths\">Instance of the <see cref=\"IApplicationPaths\"/> interface.</param>\n    /// <param name=\"xmlSerializer\">Instance of the <see cref=\"IXmlSerializer\"/> interface.</param>\n    /// <param name=\"serverConfiguration\">Server configuration manager.</param>\n    /// <param name=\"libraryManager\">Library manager.</param>\n    /// <param name=\"itemRepository\">Item repository.</param>\n    /// <param name=\"mediaSegmentsManager\">Segments manager.</param>\n    /// <param name=\"logger\">Logger.</param>\n    public Plugin(\n        IApplicationPaths applicationPaths,\n        IXmlSerializer xmlSerializer,\n        IServerConfigurationManager serverConfiguration,\n        ILibraryManager libraryManager,\n        IItemRepository itemRepository,\n        IMediaSegmentManager mediaSegmentsManager,\n        ILogger<Plugin> logger)\n        : base(applicationPaths, xmlSerializer)\n    {\n        Instance = this;\n\n        _xmlSerializer = xmlSerializer;\n        _libraryManager = libraryManager;\n        _itemRepository = itemRepository;\n        _mediaSegmentsManager = mediaSegmentsManager;\n        _logger = logger;\n\n        FFmpegPath = serverConfiguration.GetEncodingOptions().EncoderAppPathDisplay;\n\n        _pluginCachePath = Path.Join(applicationPaths.CachePath, \"JFPMediaAnalyzer\");\n        _pluginDbPath = Path.Join(applicationPaths.PluginConfigurationsPath, \"mediaanalyzer.db\");\n\n        FingerprintCachePath = Path.Join(_pluginCachePath, \"chromaprints\");\n\n        // Create the base & cache directories (if needed).\n        if (!Directory.Exists(FingerprintCachePath))\n        {\n            Directory.CreateDirectory(FingerprintCachePath);\n        }\n\n        // Create and migrate db\n        using (var context = new MediaAnalyzerDbContext(this._pluginDbPath))\n        {\n            context.ApplyMigrations();\n        }\n\n        // init db interfaces\n        _segmentMetadataDb = new SegmentMetadataDb(this._pluginDbPath);\n        _mediasegmentsDb = new MediaSegmentsDb(_mediaSegmentsManager);\n\n        ConfigurationChanged += OnConfigurationChanged;\n    }\n\n    /// <summary>\n    /// Gets or sets a value indicating whether analysis is running.\n    /// </summary>\n    public bool AnalysisRunning { get; set; } = false;\n\n    /// <summary>\n    /// Gets the directory to cache fingerprints in.\n    /// </summary>\n    public string FingerprintCachePath { get; private set; }\n\n    /// <summary>\n    /// Gets the full path to FFmpeg.\n    /// </summary>\n    public string FFmpegPath { get; private set; }\n\n    /// <inheritdoc />\n    public override string Name => \"Media Analyzer\";\n\n    /// <inheritdoc />\n    public override Guid Id => Guid.Parse(\"80885677-DACB-461B-AC97-EE7E971288AA\");\n\n    /// <summary>\n    /// Gets the plugin instance.\n    /// </summary>\n    public static Plugin? Instance { get; private set; }\n\n    /// <inheritdoc />\n    public IEnumerable<PluginPageInfo> GetPages()\n    {\n        return new[]\n        {\n            new PluginPageInfo\n            {\n                Name = this.Name,\n                EmbeddedResourcePath = GetType().Namespace + \".Configuration.configPage.html\"\n            },\n            new PluginPageInfo\n            {\n                Name = \"visualizer.js\",\n                EmbeddedResourcePath = GetType().Namespace + \".Configuration.visualizer.js\"\n            }\n        };\n    }\n\n    internal BaseItem? GetItem(Guid id)\n    {\n        return _libraryManager.GetItemById(id);\n    }\n\n    /// <summary>\n    /// Gets the full path for an item.\n    /// </summary>\n    /// <param name=\"id\">Item id.</param>\n    /// <returns>Full path to item.</returns>\n    internal string GetItemPath(Guid id)\n    {\n        var baseItem = GetItem(id);\n        if (baseItem is not null)\n        {\n            return baseItem.Path;\n        }\n        else\n        {\n            return string.Empty;\n        }\n    }\n\n    /// <summary>\n    /// Gets all chapters for this item.\n    /// </summary>\n    /// <param name=\"id\">Item id.</param>\n    /// <returns>List of chapters.</returns>\n    internal List<ChapterInfo> GetChapters(Guid id)\n    {\n        return _itemRepository.GetChapters(GetItem(id));\n    }\n\n    /// <summary>\n    /// Get metadata db.\n    /// </summary>\n    /// <returns>Instance of db.</returns>\n    public SegmentMetadataDb GetMetadataDb()\n    {\n        return this._segmentMetadataDb;\n    }\n\n    /// <summary>\n    /// Get segments db.\n    /// </summary>\n    /// <returns>Instance of db.</returns>\n    public MediaSegmentsDb GetMediaSegmentsDb()\n    {\n        return this._mediasegmentsDb;\n    }\n\n    private void OnConfigurationChanged(object? sender, BasePluginConfiguration e)\n    {\n        if (this.Configuration.ResetBlacklist == true)\n        {\n            _ = this.GetMetadataDb().DeletePreventAnalyzeSegments(null);\n            this.Configuration.ResetBlacklist = false;\n            this.SaveConfiguration(this.Configuration);\n        }\n    }\n\n    /// <summary>\n    /// Called just before the plugin is uninstalled from the server.\n    /// </summary>\n    public override void OnUninstalling()\n    {\n        // Delete cache data\n        if (Directory.Exists(_pluginCachePath))\n        {\n            Directory.Delete(_pluginCachePath, true);\n        }\n    }\n}\n"
  },
  {
    "path": "Jellyfin.Plugin.MediaAnalyzer/QueueManager.cs",
    "content": "namespace Jellyfin.Plugin.MediaAnalyzer;\n\nusing System;\nusing System.Collections.Generic;\nusing System.Collections.ObjectModel;\nusing System.Globalization;\nusing System.IO;\nusing System.Linq;\nusing System.Threading.Tasks;\nusing Jellyfin.Data.Enums;\nusing MediaBrowser.Controller.Entities;\nusing MediaBrowser.Controller.Entities.Movies;\nusing MediaBrowser.Controller.Entities.TV;\nusing MediaBrowser.Controller.Library;\nusing MediaBrowser.Model.Dto;\nusing Microsoft.Extensions.Logging;\n\n/// <summary>\n/// Manages enqueuing library items for analysis.\n/// </summary>\npublic class QueueManager\n{\n    private readonly MediaSegmentType _analyzingType;\n    private ILibraryManager _libraryManager;\n    private ILogger<QueueManager> _logger;\n    private double analysisPercent;\n    private List<string> selectedLibraries;\n    private Dictionary<string, List<int>> skippedTvShows;\n    private List<string> skippedMovies;\n    private Dictionary<Guid, List<QueuedMedia>> _queuedMedia;\n\n    /// <summary>\n    /// Initializes a new instance of the <see cref=\"QueueManager\"/> class.\n    /// </summary>\n    /// <param name=\"logger\">Logger.</param>\n    /// <param name=\"libraryManager\">Library manager.</param>\n    /// <param name=\"mode\">Analysis mode.</param>\n    public QueueManager(ILogger<QueueManager> logger, ILibraryManager libraryManager, MediaSegmentType mode)\n    {\n        _logger = logger;\n        _libraryManager = libraryManager;\n        _analyzingType = mode;\n\n        selectedLibraries = new();\n        _queuedMedia = new();\n        skippedTvShows = new();\n        skippedMovies = new();\n    }\n\n    /// <summary>\n    /// Gets all media items on the server.\n    /// </summary>\n    /// <returns>Queued media items.</returns>\n    public ReadOnlyDictionary<Guid, List<QueuedMedia>> GetMediaItems()\n    {\n        _queuedMedia.Clear();\n\n        // Assert that ffmpeg with chromaprint is installed\n        if (!FFmpegWrapper.CheckFFmpegVersion())\n        {\n            throw new FingerprintException(\n                \"ffmpeg with chromaprint is not installed on this system - episodes will not be analyzed. If Jellyfin is running natively, install jellyfin-ffmpeg5. If Jellyfin is running in a container, upgrade it to the latest version of 10.8.0.\");\n        }\n\n        LoadAnalysisSettings();\n\n        // For all selected libraries, enqueue all contained episodes.\n        foreach (var folder in _libraryManager.GetVirtualFolders())\n        {\n            // If libraries have been selected for analysis, ensure this library was selected.\n            if (selectedLibraries.Count > 0 && !selectedLibraries.Contains(folder.Name))\n            {\n                _logger.LogDebug(\"Not analyzing library \\\"{Name}\\\": not selected by user\", folder.Name);\n                continue;\n            }\n\n            _logger.LogInformation(\n                \"Running enqueue of items in library {Name} ({ItemId})\",\n                folder.Name,\n                folder.ItemId);\n\n            try\n            {\n                QueueLibraryContents(folder.ItemId);\n            }\n            catch (Exception ex)\n            {\n                _logger.LogError(\"Failed to enqueue items from library {Name}: {Exception}\", folder.Name, ex);\n            }\n        }\n\n        return new(_queuedMedia);\n    }\n\n    /// <summary>\n    /// Gets media items based on given itemId.\n    /// </summary>\n    /// <param name=\"itemIds\">All item ids to lookup.</param>\n    /// <returns>Queued media items.</returns>\n    public ReadOnlyDictionary<Guid, List<QueuedMedia>> GetMediaItemsById(Guid[] itemIds)\n    {\n        _queuedMedia.Clear();\n\n        foreach (var item in itemIds)\n        {\n            var bitem = _libraryManager.GetItemById(item);\n            if (bitem != null)\n            {\n                if (bitem is Episode episode)\n                {\n                    QueueEpisode(episode);\n                }\n\n                if (bitem is Movie movie)\n                {\n                    foreach (var source in movie.GetMediaSources(false))\n                    {\n                        QueueMovie(movie, source);\n                    }\n                }\n            }\n        }\n\n        return new(_queuedMedia);\n    }\n\n    /// <summary>\n    /// Loads the list of libraries which have been selected for analysis and the minimum intro duration.\n    /// Settings which have been modified from the defaults are logged.\n    /// </summary>\n    private void LoadAnalysisSettings()\n    {\n        var config = Plugin.Instance!.Configuration;\n\n        // Store the analysis percent\n        analysisPercent = Convert.ToDouble(config.AnalysisPercent) / 100;\n\n        // Get the list of library names which have been selected for analysis, ignoring whitespace and empty entries.\n        selectedLibraries = config.SelectedLibraries\n            .Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)\n            .ToList();\n\n        // Get the list movie names which should be skipped.\n        skippedMovies = config.SkippedMovies\n            .Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)\n            .ToList();\n\n        // Get the list of tvshow names and seasons which should be skipped for analysis.\n        var show = config.SkippedTvShows\n            .Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)\n            .ToList();\n\n        foreach (var s in show)\n        {\n            if (s.Contains(';', System.StringComparison.InvariantCulture))\n            {\n                var rseasons = s.Split(';', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);\n                var seasons = rseasons.Skip(1).ToArray();\n                var name = rseasons.ElementAt(0);\n                var seasonNumbers = new List<int>();\n\n                foreach (var season in seasons)\n                {\n                    var nr = season.Substring(1);\n\n                    try\n                    {\n                        seasonNumbers.Add(int.Parse(nr, CultureInfo.InvariantCulture));\n                    }\n                    catch (FormatException)\n                    {\n                        _logger.LogError(\"Skipping TV Shows: Failed to parse season number '{Nr}' for tv show: {Name}. Fix your config!\", nr, name);\n                    }\n                }\n\n                skippedTvShows.Add(name, seasonNumbers);\n            }\n            else\n            {\n                skippedTvShows.Add(s, new List<int>());\n            }\n        }\n\n        // If any libraries have been selected for analysis, log their names.\n        if (selectedLibraries.Count > 0)\n        {\n            _logger.LogInformation(\"Limiting analysis to the following libraries: {Selected}\", selectedLibraries);\n        }\n        else\n        {\n            _logger.LogDebug(\"Not limiting analysis by library name\");\n        }\n\n        // If analysis settings have been changed from the default, log the modified settings.\n        if (config.AnalysisLengthLimit != 15 || config.AnalysisPercent != 30 || config.MinimumIntroDuration != 15)\n        {\n            _logger.LogInformation(\n                \"Analysis settings have been changed to: {Percent}%/{Minutes}m and a minimum of {Minimum}s\",\n                config.AnalysisPercent,\n                config.AnalysisLengthLimit,\n                config.MinimumIntroDuration);\n        }\n    }\n\n    private void QueueLibraryContents(string rawId)\n    {\n        _logger.LogDebug(\"Constructing anonymous internal query\");\n\n        var includes = new BaseItemKind[] { BaseItemKind.Episode };\n\n        // When analyzing for credits also search for movies\n        if (_analyzingType == MediaSegmentType.Outro)\n        {\n            includes = includes.Concat(new BaseItemKind[] { BaseItemKind.Movie }).ToArray();\n        }\n\n        var query = new InternalItemsQuery()\n        {\n            // Order by series name, season, and then episode number so that status updates are logged in order\n            ParentId = Guid.Parse(rawId),\n            OrderBy = new[]\n            {\n                (ItemSortBy.SeriesSortName, SortOrder.Ascending),\n                (ItemSortBy.ParentIndexNumber, SortOrder.Ascending),\n                (ItemSortBy.IndexNumber, SortOrder.Ascending),\n            },\n            IncludeItemTypes = includes,\n            Recursive = true,\n            IsVirtualItem = false\n        };\n\n        _logger.LogDebug(\"Getting items\");\n\n        var items = _libraryManager.GetItemList(query, false);\n\n        if (items is null)\n        {\n            _logger.LogError(\"Library query result is null\");\n            return;\n        }\n\n        // Queue all media on the server for fingerprinting.\n        _logger.LogDebug(\"Iterating through library items\");\n\n        foreach (var item in items)\n        {\n            if (item is Episode episode)\n            {\n                if (SkipEpisode(episode))\n                {\n                    _logger.LogInformation(\"Skipping episode: '{EpisodeName}' of series: '{SeriesName} S{Season}'\", episode.Name, episode.SeriesName, episode.AiredSeasonNumber);\n                    continue;\n                }\n\n                QueueEpisode(episode);\n            }\n            else if (item is Movie movie)\n            {\n                if (skippedMovies.Contains(movie.Name))\n                {\n                    _logger.LogInformation(\"Skipping Movie: '{Name}'\", movie.Name);\n                    continue;\n                }\n\n                // Movie can have multiple MediaSources like 1080p and a 4k file, they have different ids\n                foreach (MediaSourceInfo source in movie.GetMediaSources(false))\n                {\n                    _logger.LogInformation(\"Adding movie: '{Name} ({Format})'\", movie.Name, source.Name);\n                    QueueMovie(movie, source);\n                }\n            }\n            else\n            {\n                _logger.LogDebug(\"Item {Name} is not an episode or movie\", item.Name);\n                continue;\n            }\n        }\n\n        _logger.LogDebug(\"Queued {Count} media items\", items.Count);\n    }\n\n    // Test if should skip the episode\n    private bool SkipEpisode(Episode episode)\n    {\n        if (skippedTvShows.TryGetValue(episode.SeriesName, out var seasons))\n        {\n            return (episode.AiredSeasonNumber != null && seasons.Contains(episode.AiredSeasonNumber.GetValueOrDefault())) ? true : false;\n        }\n\n        return false;\n    }\n\n    private void QueueEpisode(Episode episode)\n    {\n        if (Plugin.Instance is null)\n        {\n            throw new InvalidOperationException(\"plugin instance was null\");\n        }\n\n        if (string.IsNullOrEmpty(episode.Path))\n        {\n            _logger.LogWarning(\n                \"Not queuing episode \\\"{Name}\\\" from series \\\"{Series}\\\" ({Id}) as no path was provided by Jellyfin\",\n                episode.Name,\n                episode.SeriesName,\n                episode.Id);\n            return;\n        }\n\n        if (episode.RunTimeTicks is null)\n        {\n            _logger.LogWarning(\n                \"Not queuing episode \\\"{Name}\\\" from series \\\"{Series}\\\" ({Id}) as no duration was provided by Jellyfin\",\n                episode.Name,\n                episode.SeriesName,\n                episode.Id);\n            return;\n        }\n\n        // Limit analysis to the first X% of the episode and at most Y minutes.\n        // X and Y default to 30% and 15 minutes.\n        var duration = TimeSpan.FromTicks(episode.RunTimeTicks ?? 0).TotalSeconds;\n        var fingerprintDuration = duration;\n\n        if (fingerprintDuration >= 5 * 60)\n        {\n            fingerprintDuration *= analysisPercent;\n        }\n\n        fingerprintDuration = Math.Min(\n            fingerprintDuration,\n            60 * Plugin.Instance!.Configuration.AnalysisLengthLimit);\n\n        // Allocate a new list for each new season\n        _queuedMedia.TryAdd(episode.SeasonId, new List<QueuedMedia>());\n\n        // Queue the episode for analysis\n        var maxCreditsDuration = Plugin.Instance!.Configuration.MaximumEpisodeCreditsDuration;\n        _queuedMedia[episode.SeasonId].Add(new QueuedMedia()\n        {\n            SeriesName = episode.SeriesName,\n            SeasonNumber = episode.AiredSeasonNumber ?? 0,\n            ItemId = episode.Id,\n            Name = episode.Name,\n            Path = episode.Path,\n            Duration = Convert.ToInt32(duration),\n            IntroFingerprintEnd = Convert.ToInt32(fingerprintDuration),\n            CreditsFingerprintStart = Convert.ToInt32(duration - maxCreditsDuration),\n        });\n    }\n\n    private void QueueMovie(Movie movie, MediaSourceInfo source)\n    {\n        if (Plugin.Instance is null)\n        {\n            throw new InvalidOperationException(\"plugin instance was null\");\n        }\n\n        if (string.IsNullOrEmpty(source.Path))\n        {\n            _logger.LogWarning(\n                \"Not queuing movie '{Name} ({Source})' ({Id}) as no path was provided by Jellyfin\",\n                movie.Name,\n                source.Name,\n                source.Id);\n            return;\n        }\n\n        if (movie.RunTimeTicks is null)\n        {\n            _logger.LogWarning(\n                \"Not queuing movie '{Name} ({Source})' ({Id}) as no duration was provided by Jellyfin\",\n                movie.Name,\n                source.Name,\n                source.Id);\n            return;\n        }\n\n        // Limit analysis to the first X% of the episode and at most Y minutes.\n        // X and Y default to 30% and 15 minutes.\n        var duration = TimeSpan.FromTicks(movie.RunTimeTicks ?? 0).TotalSeconds;\n        var fingerprintDuration = duration;\n\n        if (fingerprintDuration >= 5 * 60)\n        {\n            fingerprintDuration *= analysisPercent;\n        }\n\n        fingerprintDuration = Math.Min(\n            fingerprintDuration,\n            60 * Plugin.Instance!.Configuration.AnalysisLengthLimit);\n\n        // Allocate a new list for each movie\n        _queuedMedia.TryAdd(Guid.Parse(source.Id), new List<QueuedMedia>());\n\n        // Queue the movie for analysis\n        var maxCreditsDuration = Plugin.Instance!.Configuration.MaximumMovieCreditsDuration;\n        _queuedMedia[Guid.Parse(source.Id)].Add(new QueuedMedia()\n        {\n            ItemId = Guid.Parse(source.Id),\n            Name = movie.Name,\n            SourceName = source.Name,\n            Path = source.Path,\n            Duration = Convert.ToInt32(duration),\n            IntroFingerprintEnd = Convert.ToInt32(fingerprintDuration),\n            CreditsFingerprintStart = Convert.ToInt32(duration - maxCreditsDuration),\n        });\n    }\n\n    /// <summary>\n    /// Verify that a collection of queued media items still exist in Jellyfin and in storage.\n    /// This is done to ensure that we don't analyze items that were deleted between the call to GetMediaItems() and popping them from the queue.\n    /// </summary>\n    /// <param name=\"candidates\">Queued media items.</param>\n    /// <param name=\"mode\">Analysis mode.</param>\n    /// <returns>Media items that have been verified to exist in Jellyfin and in storage.</returns>\n    public ReadOnlyCollection<QueuedMedia> VerifyQueue(ReadOnlyCollection<QueuedMedia> candidates, MediaSegmentType mode)\n    {\n        var verified = new List<QueuedMedia>();\n\n        foreach (var candidate in candidates)\n        {\n            try\n            {\n                var path = Plugin.Instance!.GetItemPath(candidate.ItemId);\n\n                if (File.Exists(path))\n                {\n                    verified.Add(candidate);\n                }\n            }\n            catch (Exception ex)\n            {\n                _logger.LogDebug(\n                    \"Skipping {Mode} analysis of {Name} ({Id}): {Exception}\",\n                    mode,\n                    candidate.Name,\n                    candidate.ItemId,\n                    ex);\n            }\n        }\n\n        return verified.AsReadOnly();\n    }\n\n    /// <summary>\n    /// Filter out all media that already have a segment of type in database.\n    /// </summary>\n    /// <param name=\"candidates\">Queued media items.</param>\n    /// <param name=\"mode\">Analysis mode.</param>\n    /// <returns>Media items that have no segment.</returns>\n    public async Task<(ReadOnlyCollection<QueuedMedia> FilteredItems, bool AnyUnanalyzed)>\n        FilterWithSegmentsAsync(ReadOnlyCollection<QueuedMedia> candidates, MediaSegmentType mode)\n    {\n        var unanalyzed = false;\n        var verified = new List<QueuedMedia>();\n\n        foreach (var candidate in candidates)\n        {\n            try\n            {\n                var timestamps = await Plugin.Instance!.GetMediaSegmentsDb().HasSegments(candidate.ItemId, mode).ConfigureAwait(false);\n\n                if (!timestamps)\n                {\n                    unanalyzed = true;\n                }\n                else\n                {\n                    candidate.IsAnalyzed = true;\n                }\n\n                verified.Add(candidate);\n            }\n            catch (Exception ex)\n            {\n                _logger.LogDebug(\n                    \"Skipping {Mode} analysis of {Name} ({Id}): {Exception}\",\n                    mode,\n                    candidate.Name,\n                    candidate.ItemId,\n                    ex);\n            }\n        }\n\n        return (verified.AsReadOnly(), unanalyzed);\n    }\n\n    /// <summary>\n    /// Filter out all media that is blacklisted.\n    /// </summary>\n    /// <param name=\"candidates\">Queued media items.</param>\n    /// <param name=\"mode\">Analysis mode.</param>\n    /// <returns>Media items that are not blacklisted.</returns>\n    public async Task<ReadOnlyCollection<QueuedMedia>> FilterWithBlacklistAsync(ReadOnlyCollection<QueuedMedia> candidates, MediaSegmentType mode)\n    {\n        var verified = new List<QueuedMedia>();\n\n        foreach (var candidate in candidates)\n        {\n            try\n            {\n                var prevent = await Plugin.Instance!.GetMetadataDb().PreventAnalyze(candidate.ItemId, mode == MediaSegmentType.Intro ? MediaSegmentType.Intro : MediaSegmentType.Outro).ConfigureAwait(false);\n                if (!prevent)\n                {\n                    verified.Add(candidate);\n                }\n            }\n            catch (Exception ex)\n            {\n                _logger.LogDebug(\n                    \"Skipping {Mode} analysis of {Name} ({Id}): {Exception}\",\n                    mode,\n                    candidate.Name,\n                    candidate.ItemId,\n                    ex);\n            }\n        }\n\n        return verified.AsReadOnly();\n    }\n}\n"
  },
  {
    "path": "Jellyfin.Plugin.MediaAnalyzer/ScheduledTasks/AnalyzeMedia.cs",
    "content": "using System;\nusing System.Collections.Generic;\nusing System.Threading;\nusing System.Threading.Tasks;\nusing Jellyfin.Data.Enums;\nusing MediaBrowser.Controller.Library;\nusing MediaBrowser.Model.Tasks;\nusing Microsoft.Extensions.Logging;\n\nnamespace Jellyfin.Plugin.MediaAnalyzer;\n\n/// <summary>\n/// Analyze all television episodes for introduction sequences.\n/// </summary>\npublic class AnalyzeMedia : IScheduledTask\n{\n    private readonly ILoggerFactory _loggerFactory;\n\n    private readonly ILibraryManager _libraryManager;\n\n    /// <summary>\n    /// Initializes a new instance of the <see cref=\"AnalyzeMedia\"/> class.\n    /// </summary>\n    /// <param name=\"loggerFactory\">Logger factory.</param>\n    /// <param name=\"libraryManager\">Library manager.</param>\n    public AnalyzeMedia(\n        ILoggerFactory loggerFactory,\n        ILibraryManager libraryManager)\n    {\n        _loggerFactory = loggerFactory;\n        _libraryManager = libraryManager;\n    }\n\n    /// <summary>\n    /// Gets the task name.\n    /// </summary>\n    public string Name => \"Analyze Media\";\n\n    /// <summary>\n    /// Gets the task category.\n    /// </summary>\n    public string Category => \"Media Analyzer\";\n\n    /// <summary>\n    /// Gets the task description.\n    /// </summary>\n    public string Description => \"Analyzes the audio of all television episodes to find introduction and credits sequences.\";\n\n    /// <summary>\n    /// Gets the task key.\n    /// </summary>\n    public string Key => \"JFPMediaAnalyzerAnalyzeMedia\";\n\n    /// <summary>\n    /// Analyze all episodes in the queue. Only one instance of this task should be run at a time.\n    /// </summary>\n    /// <param name=\"progress\">Task progress.</param>\n    /// <param name=\"cancellationToken\">Cancellation token.</param>\n    /// <returns>Task.</returns>\n    public Task ExecuteAsync(IProgress<double> progress, CancellationToken cancellationToken)\n    {\n        if (_libraryManager is null)\n        {\n            throw new InvalidOperationException(\"Library manager was null\");\n        }\n\n        if (Plugin.Instance!.AnalysisRunning)\n        {\n            return Task.CompletedTask;\n        }\n        else\n        {\n            Plugin.Instance!.AnalysisRunning = true;\n        }\n\n        // intro\n        var introBaseAnalyzer = new BaseItemAnalyzerTask(\n            MediaSegmentType.Intro,\n            _loggerFactory.CreateLogger<AnalyzeMedia>(),\n            _loggerFactory,\n            _libraryManager);\n\n        introBaseAnalyzer.AnalyzeItems(progress, cancellationToken);\n\n        // reset progress\n        progress.Report(0);\n\n        // outro\n        var outroBaseAnalyzer = new BaseItemAnalyzerTask(\n            MediaSegmentType.Outro,\n            _loggerFactory.CreateLogger<AnalyzeMedia>(),\n            _loggerFactory,\n            _libraryManager);\n\n        outroBaseAnalyzer.AnalyzeItems(progress, cancellationToken);\n\n        Plugin.Instance!.AnalysisRunning = false;\n\n        return Task.CompletedTask;\n    }\n\n    /// <summary>\n    /// Get task triggers.\n    /// </summary>\n    /// <returns>Task triggers.</returns>\n    public IEnumerable<TaskTriggerInfo> GetDefaultTriggers()\n    {\n        return Array.Empty<TaskTriggerInfo>();\n    }\n}\n"
  },
  {
    "path": "Jellyfin.Plugin.MediaAnalyzer/ScheduledTasks/BaseItemAnalyzerTask.cs",
    "content": "namespace Jellyfin.Plugin.MediaAnalyzer;\n\nusing System;\nusing System.Collections.ObjectModel;\nusing System.Linq;\nusing System.Threading;\nusing System.Threading.Tasks;\nusing Jellyfin.Data.Enums;\nusing MediaBrowser.Controller.Library;\nusing Microsoft.Extensions.Logging;\n\n/// <summary>\n/// Common code shared by all media item analyzer tasks.\n/// </summary>\npublic class BaseItemAnalyzerTask\n{\n    private readonly MediaSegmentType _analyzingType;\n\n    private readonly ILogger _logger;\n\n    private readonly ILoggerFactory _loggerFactory;\n\n    private readonly ILibraryManager _libraryManager;\n\n    /// <summary>\n    /// Initializes a new instance of the <see cref=\"BaseItemAnalyzerTask\"/> class.\n    /// </summary>\n    /// <param name=\"mode\">Analysis mode.</param>\n    /// <param name=\"logger\">Task logger.</param>\n    /// <param name=\"loggerFactory\">Logger factory.</param>\n    /// <param name=\"libraryManager\">Library manager.</param>\n    public BaseItemAnalyzerTask(\n        MediaSegmentType mode,\n        ILogger logger,\n        ILoggerFactory loggerFactory,\n        ILibraryManager libraryManager)\n    {\n        _analyzingType = mode;\n        _logger = logger;\n        _loggerFactory = loggerFactory;\n        _libraryManager = libraryManager;\n    }\n\n    /// <summary>\n    /// Analyze all media items on the server.\n    /// </summary>\n    /// <param name=\"progress\">Progress.</param>\n    /// <param name=\"cancellationToken\">Cancellation token.</param>\n    public void AnalyzeItems(\n        IProgress<double> progress,\n        CancellationToken cancellationToken)\n    {\n        var queueManager = new QueueManager(\n            _loggerFactory.CreateLogger<QueueManager>(),\n            _libraryManager,\n            _analyzingType);\n\n        var queue = queueManager.GetMediaItems();\n\n        var totalQueued = 0;\n        foreach (var kvp in queue)\n        {\n            totalQueued += kvp.Value.Count;\n        }\n\n        if (totalQueued == 0)\n        {\n            return;\n        }\n\n        var totalProcessed = 0;\n        var options = new ParallelOptions()\n        {\n            MaxDegreeOfParallelism = Plugin.Instance!.Configuration.MaxParallelism\n        };\n\n        Parallel.ForEach(queue, options, async (season) =>\n        {\n            // Since the first run of the task can run for multiple hours, ensure that none\n            // of the current media items were deleted from Jellyfin since the task was started.\n            var verifiedItems = queueManager.VerifyQueue(\n                season.Value.AsReadOnly(),\n                _analyzingType);\n\n            var notBlacklistedItems = await queueManager.FilterWithBlacklistAsync(verifiedItems, _analyzingType).ConfigureAwait(false);\n\n            var (episodes, unanalyzed) = await queueManager.FilterWithSegmentsAsync(notBlacklistedItems, _analyzingType).ConfigureAwait(false);\n\n            if (episodes.Count == 0)\n            {\n                return;\n            }\n\n            var first = episodes[0];\n\n            if (!unanalyzed)\n            {\n                if (first.IsEpisode())\n                {\n                    _logger.LogDebug(\n                        \"All episodes in {Name} season {Season} have already been analyzed for {AnalyzeType}\",\n                        first.SeriesName,\n                        first.SeasonNumber,\n                        _analyzingType);\n                }\n                else\n                {\n                    _logger.LogDebug(\n                        \"Movie {Name} have already been analyzed for {AnalyzeType}\",\n                        first.Name,\n                        _analyzingType);\n                }\n\n                return;\n            }\n\n            try\n            {\n                if (cancellationToken.IsCancellationRequested)\n                {\n                    return;\n                }\n\n                var analyzed = await AnalyzeItems(episodes, cancellationToken).ConfigureAwait(false);\n                Interlocked.Add(ref totalProcessed, analyzed);\n            }\n            catch (FingerprintException ex)\n            {\n                if (first.IsEpisode())\n                {\n                    _logger.LogWarning(\n                        \"Unable to analyze {Series} season {Season}: unable to fingerprint: {Ex}\",\n                        first.SeriesName,\n                        first.SeasonNumber,\n                        ex);\n                }\n                else\n                {\n                    _logger.LogDebug(\n                        \"Unable to analyze Movie {Name}: unable to fingerprint: {Ex}\",\n                        first.Name,\n                        ex);\n                }\n            }\n\n            progress.Report((totalProcessed * 100) / totalQueued);\n        });\n    }\n\n    /// <summary>\n    /// Analyze a group of media items for skippable segments.\n    /// </summary>\n    /// <param name=\"items\">Media items to analyze.</param>\n    /// <param name=\"cancellationToken\">Cancellation token.</param>\n    /// <returns>Number of items that were successfully analyzed.</returns>\n    private async Task<int> AnalyzeItems(\n        ReadOnlyCollection<QueuedMedia> items,\n        CancellationToken cancellationToken)\n    {\n        var totalItems = items.Count;\n        var first = items[0];\n\n        if (first.IsEpisode())\n        {\n            // Only analyze specials (season 0) if the user has opted in.\n            if (first.SeasonNumber == 0 && !Plugin.Instance!.Configuration.AnalyzeSeasonZero)\n            {\n                return 0;\n            }\n\n            _logger.LogInformation(\n                \"Analyzing {Count} files for {Type} from {Name} season {Season}\",\n                items.Count,\n                _analyzingType,\n                first.SeriesName,\n                first.SeasonNumber);\n        }\n        else\n        {\n            // we ignore movies intro run\n            if (_analyzingType == MediaSegmentType.Outro)\n            {\n                _logger.LogInformation(\"Analyzing Movie (Outro): {Name}\", first.Name);\n            }\n        }\n\n        var analyzers = new Collection<IMediaFileAnalyzer>\n        {\n            new ChapterAnalyzer(_loggerFactory.CreateLogger<ChapterAnalyzer>())\n        };\n\n        // Movies don't use chromparint analyzer\n        if (first.IsEpisode())\n        {\n            analyzers.Add(new ChromaprintAnalyzer(_loggerFactory.CreateLogger<ChromaprintAnalyzer>()));\n        }\n\n        if (_analyzingType == MediaSegmentType.Outro)\n        {\n            analyzers.Add(new BlackFrameAnalyzer(_loggerFactory.CreateLogger<BlackFrameAnalyzer>()));\n        }\n\n        // Use each analyzer to find skippable ranges in all media files, removing successfully\n        // analyzed items from the queue.\n        foreach (var analyzer in analyzers)\n        {\n            var (notAnalyzed, analyzed, metadata) = await analyzer.AnalyzeMediaFilesAsync(items, _analyzingType, cancellationToken);\n            items = notAnalyzed;\n\n            await Plugin.Instance!.GetMediaSegmentsDb().CreateMediaSegments(analyzed, metadata, _analyzingType).ConfigureAwait(false);\n        }\n\n        // Unanalyzed items should be blacklisted\n        var blacklisted = items.Where(i => !i.SkipPreventAnalyzing).ToList();\n\n        if (blacklisted.Count > 0 && Plugin.Instance!.Configuration.EnableBlacklist)\n        {\n            await Plugin.Instance!.GetMetadataDb().CreatePreventAnalyzeSegments(blacklisted, _analyzingType).ConfigureAwait(false);\n        }\n\n        return totalItems;\n    }\n}\n"
  },
  {
    "path": "Jellyfin.Plugin.MediaAnalyzer.Tests/Jellyfin.Plugin.MediaAnalyzer.Tests.csproj",
    "content": "<Project Sdk=\"Microsoft.NET.Sdk\">\n\n  <PropertyGroup>\n    <TargetFramework>net8.0</TargetFramework>\n    <Nullable>enable</Nullable>\n\n    <IsPackable>false</IsPackable>\n  </PropertyGroup>\n\n  <ItemGroup>\n    <PackageReference Include=\"Microsoft.NET.Test.Sdk\" Version=\"17.9.0\" />\n    <PackageReference Include=\"xunit\" Version=\"2.7.0\" />\n    <PackageReference Include=\"xunit.runner.visualstudio\" Version=\"2.5.7\">\n      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>\n      <PrivateAssets>all</PrivateAssets>\n    </PackageReference>\n    <PackageReference Include=\"coverlet.collector\" Version=\"6.0.2\">\n      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>\n      <PrivateAssets>all</PrivateAssets>\n    </PackageReference>\n  </ItemGroup>\n\n  <ItemGroup>\n    <ProjectReference Include=\"..\\Jellyfin.Plugin.MediaAnalyzer\\Jellyfin.Plugin.MediaAnalyzer.csproj\" />\n  </ItemGroup>\n\n</Project>\n"
  },
  {
    "path": "Jellyfin.Plugin.MediaAnalyzer.Tests/TestAudioFingerprinting.cs",
    "content": "/* These tests require that the host system has a version of FFmpeg installed\n * which supports both chromaprint and the \"-fp_format raw\" flag.\n */\n\nusing System;\nusing System.Collections.Generic;\nusing Xunit;\nusing Microsoft.Extensions.Logging;\nusing Jellyfin.Data.Enums;\n\nnamespace Jellyfin.Plugin.MediaAnalyzer.Tests;\n\npublic class TestAudioFingerprinting\n{\n    [FactSkipFFmpegTests]\n    public void TestInstallationCheck()\n    {\n        Assert.True(FFmpegWrapper.CheckFFmpegVersion());\n    }\n\n    [Theory]\n    [InlineData(0, 0)]\n    [InlineData(1, 1)]\n    [InlineData(5, 213)]\n    [InlineData(10, 56_021)]\n    [InlineData(16, 16_112_341)]\n    [InlineData(19, 2_465_585_877)]\n    public void TestBitCounting(int expectedBits, uint number)\n    {\n        var chromaprint = CreateChromaprintAnalyzer();\n        Assert.Equal(expectedBits, chromaprint.CountBits(number));\n    }\n\n    [FactSkipFFmpegTests]\n    public void TestFingerprinting()\n    {\n        // Generated with `fpcalc -raw audio/big_buck_bunny_intro.mp3`\n        var expected = new uint[]{\n            3269995649, 3261610160, 3257403872, 1109989680, 1109993760, 1110010656, 1110142768, 1110175504,\n            1110109952, 1126874880, 2788611, 2787586, 6981634, 15304754, 28891170, 43579426, 43542561,\n            47737888, 41608640, 40559296, 36352644, 53117572, 2851460, 1076465548, 1080662428, 1080662492,\n            1089182044, 1148041501, 1148037422, 3291343918, 3290980398, 3429367854, 3437756714, 3433698090,\n            3433706282, 3366600490, 3366464314, 2296916250, 3362269210, 3362265115, 3362266441, 3370784472,\n            3366605480, 1218990776, 1223217816, 1231602328, 1260950200, 1245491640, 169845176, 1510908120,\n            1510911000, 2114365528, 2114370008, 1996929688, 1996921480, 1897171592, 1884588680, 1347470984,\n            1343427226, 1345467054, 1349657318, 1348673570, 1356869666, 1356865570, 295837698, 60957698,\n            44194818, 48416770, 40011778, 36944210, 303147954, 369146786, 1463847842, 1434488738, 1417709474,\n            1417713570, 3699441634, 3712167202, 3741460534, 2585144342, 2597725238, 2596200487, 2595926077,\n            2595984141, 2594734600, 2594736648, 2598931176, 2586348264, 2586348264, 2586561257, 2586451659,\n            2603225802, 2603225930, 2573860970, 2561151018, 3634901034, 3634896954, 3651674122, 3416793162,\n            3416816715, 3404331257, 3395844345, 3395836155, 3408464089, 3374975369, 1282036360, 1290457736,\n            1290400440, 1290314408, 1281925800, 1277727404, 1277792932, 1278785460, 1561962388, 1426698196,\n            3607924711, 4131892839, 4140215815, 4292259591, 3218515717, 3209938229, 3171964197, 3171956013,\n            4229117295, 4229312879, 4242407935, 4240114111, 4239987133, 4239990013, 3703060732, 1547188252,\n            1278748677, 1278748935, 1144662786, 1148854786, 1090388802, 1090388962, 1086260130, 1085940098,\n            1102709122, 45811586, 44634002, 44596656, 44592544, 1122527648, 1109944736, 1109977504, 1111030243,\n            1111017762, 1109969186, 1126721826, 1101556002, 1084844322, 1084979506, 1084914450, 1084914449,\n            1084873520, 3228093296, 3224996817, 3225062275, 3241840002, 3346701698, 3349843394, 3349782306,\n            3349719842, 3353914146, 3328748322, 3328747810, 3328809266, 3471476754, 3472530451, 3472473123,\n            3472417825, 3395841056, 3458735136, 3341420624, 1076496560, 1076501168, 1076501136, 1076497024\n        };\n\n        var actual = FFmpegWrapper.Fingerprint(\n            queueEpisode(\"audio/big_buck_bunny_intro.mp3\"),\n            MediaSegmentType.Intro);\n\n        Assert.Equal(expected, actual);\n    }\n\n    [Fact]\n    public void TestIndexGeneration()\n    {\n        //                     0  1  2  3  4  5   6   7\n        var fpr = new uint[] { 1, 2, 3, 1, 5, 77, 42, 2 };\n        var expected = new Dictionary<uint, int>()\n        {\n            {1, 3},\n            {2, 7},\n            {3, 2},\n            {5, 4},\n            {42, 6},\n            {77, 5},\n        };\n\n        var actual = FFmpegWrapper.CreateInvertedIndex(Guid.NewGuid(), fpr);\n\n        Assert.Equal(expected, actual);\n    }\n\n    [FactSkipFFmpegTests]\n    public void TestIntroDetection()\n    {\n        var chromaprint = CreateChromaprintAnalyzer();\n\n        var lhsEpisode = queueEpisode(\"audio/big_buck_bunny_intro.mp3\");\n        var rhsEpisode = queueEpisode(\"audio/big_buck_bunny_clip.mp3\");\n        var lhsFingerprint = FFmpegWrapper.Fingerprint(lhsEpisode, MediaSegmentType.Intro);\n        var rhsFingerprint = FFmpegWrapper.Fingerprint(rhsEpisode, MediaSegmentType.Intro);\n\n        var (lhs, rhs) = chromaprint.CompareEpisodes(\n            lhsEpisode.ItemId,\n            lhsFingerprint,\n            rhsEpisode.ItemId,\n            rhsFingerprint);\n\n        Assert.True(lhs.Valid);\n        Assert.Equal(0, lhs.Start);\n        Assert.Equal(17.792, lhs.End);\n\n        Assert.True(rhs.Valid);\n        Assert.Equal(5.12, rhs.Start);\n        Assert.Equal(22.912, rhs.End);\n    }\n\n    /// <summary>\n    /// Test that the silencedetect wrapper is working.\n    /// </summary>\n    [FactSkipFFmpegTests]\n    public void TestSilenceDetection()\n    {\n        var clip = queueEpisode(\"audio/big_buck_bunny_clip.mp3\");\n\n        var expected = new TimeRange[]\n        {\n            new TimeRange(44.6310, 44.8072),\n            new TimeRange(53.5905, 53.8070),\n            new TimeRange(53.8458, 54.2024),\n            new TimeRange(54.2611, 54.5935),\n            new TimeRange(54.7098, 54.9293),\n            new TimeRange(54.9294, 55.2590),\n        };\n\n        var actual = FFmpegWrapper.DetectSilence(clip, 60);\n\n        Assert.Equal(expected, actual);\n    }\n\n    private QueuedMedia queueEpisode(string path)\n    {\n        return new QueuedMedia()\n        {\n            ItemId = Guid.NewGuid(),\n            Path = \"../../../\" + path,\n            IntroFingerprintEnd = 60\n        };\n    }\n\n    private ChromaprintAnalyzer CreateChromaprintAnalyzer()\n    {\n        var logger = new LoggerFactory().CreateLogger<ChromaprintAnalyzer>();\n        return new(logger);\n    }\n}\n\npublic class FactSkipFFmpegTests : FactAttribute\n{\n#if SKIP_FFMPEG_TESTS\n    public FactSkipFFmpegTests() {\n        Skip = \"SKIP_FFMPEG_TESTS defined, skipping unit tests that require FFmpeg to be installed\";\n    }\n#endif\n}\n"
  },
  {
    "path": "Jellyfin.Plugin.MediaAnalyzer.Tests/TestBlackFrames.cs",
    "content": "namespace Jellyfin.Plugin.MediaAnalyzer.Tests;\n\nusing System;\nusing System.Collections.Generic;\nusing Jellyfin.Data.Enums;\nusing Microsoft.Extensions.Logging;\nusing Xunit;\n\npublic class TestBlackFrames\n{\n    [FactSkipFFmpegTests]\n    public void TestBlackFrameDetection()\n    {\n        var range = 1e-5;\n\n        var expected = new List<BlackFrame>();\n        expected.AddRange(CreateFrameSequence(2, 3));\n        expected.AddRange(CreateFrameSequence(5, 6));\n        expected.AddRange(CreateFrameSequence(8, 9.96));\n\n        var actual = FFmpegWrapper.DetectBlackFrames(queueFile(\"rainbow.mp4\"), new(0, 10), 85);\n\n        for (var i = 0; i < expected.Count; i++)\n        {\n            var (e, a) = (expected[i], actual[i]);\n            Assert.Equal(e.Percentage, a.Percentage);\n            Assert.InRange(a.Time, e.Time - range, e.Time + range);\n        }\n    }\n\n    [FactSkipFFmpegTests]\n    public void TestEndCreditDetection()\n    {\n        var range = 1;\n\n        var analyzer = CreateBlackFrameAnalyzer();\n\n        var episode = queueFile(\"credits.mp4\");\n        episode.Duration = (int)new TimeSpan(0, 5, 30).TotalSeconds;\n\n        var result = analyzer.AnalyzeMediaFile(episode, MediaSegmentType.Outro, 85);\n        Assert.NotNull(result);\n        Assert.InRange(result.Start, 300 - range, 300 + range);\n    }\n\n    private QueuedMedia queueFile(string path)\n    {\n        return new()\n        {\n            ItemId = Guid.NewGuid(),\n            Name = path,\n            Path = \"../../../video/\" + path\n        };\n    }\n\n    private BlackFrame[] CreateFrameSequence(double start, double end)\n    {\n        var frames = new List<BlackFrame>();\n\n        for (var i = start; i < end; i += 0.04)\n        {\n            frames.Add(new(100, i));\n        }\n\n        return frames.ToArray();\n    }\n\n    private BlackFrameAnalyzer CreateBlackFrameAnalyzer()\n    {\n        var logger = new LoggerFactory().CreateLogger<BlackFrameAnalyzer>();\n        return new(logger);\n    }\n}\n"
  },
  {
    "path": "Jellyfin.Plugin.MediaAnalyzer.Tests/TestChapterAnalyzer.cs",
    "content": "namespace Jellyfin.Plugin.MediaAnalyzer.Tests;\n\nusing System;\nusing System.Collections.Generic;\nusing System.Collections.ObjectModel;\nusing Jellyfin.Data.Enums;\nusing MediaBrowser.Model.Entities;\nusing Microsoft.Extensions.Logging;\nusing Xunit;\n\npublic class TestChapterAnalyzer\n{\n    [Theory]\n    [InlineData(\"Opening\")]\n    [InlineData(\"OP\")]\n    [InlineData(\"Intro\")]\n    [InlineData(\"Intro Start\")]\n    [InlineData(\"Introduction\")]\n    public void TestIntroductionExpression(string chapterName)\n    {\n        var chapters = CreateChapters(chapterName, MediaSegmentType.Intro);\n        var introChapter = FindChapter(chapters, MediaSegmentType.Intro);\n\n        Assert.NotNull(introChapter);\n        Assert.Equal(60, introChapter.Start);\n        Assert.Equal(90, introChapter.End);\n    }\n\n    [Theory]\n    [InlineData(\"End Credits\")]\n    [InlineData(\"Ending\")]\n    [InlineData(\"Credit start\")]\n    [InlineData(\"Closing Credits\")]\n    [InlineData(\"Credits\")]\n    public void TestEndCreditsExpression(string chapterName)\n    {\n        var chapters = CreateChapters(chapterName, MediaSegmentType.Outro);\n        var creditsChapter = FindChapter(chapters, MediaSegmentType.Outro);\n\n        Assert.NotNull(creditsChapter);\n        Assert.Equal(1890, creditsChapter.Start);\n        Assert.Equal(2000, creditsChapter.End);\n    }\n\n    private Segment? FindChapter(Collection<ChapterInfo> chapters, MediaSegmentType mode)\n    {\n        var logger = new LoggerFactory().CreateLogger<ChapterAnalyzer>();\n        var analyzer = new ChapterAnalyzer(logger);\n\n        var config = new Configuration.PluginConfiguration();\n        var expression = mode == MediaSegmentType.Intro ?\n            config.ChapterAnalyzerIntroductionPattern :\n            config.ChapterAnalyzerEndCreditsPattern;\n\n        return analyzer.FindMatchingChapter(new() { Duration = 2000 }, chapters, expression, mode);\n    }\n\n    private Collection<ChapterInfo> CreateChapters(string name, MediaSegmentType mode)\n    {\n        var chapters = new[]{\n            CreateChapter(\"Cold Open\", 0),\n            CreateChapter(mode == MediaSegmentType.Intro ? name : \"Introduction\", 60),\n            CreateChapter(\"Main Episode\", 90),\n            CreateChapter(mode == MediaSegmentType.Outro ? name : \"Credits\", 1890)\n        };\n\n        return new(new List<ChapterInfo>(chapters));\n    }\n\n    /// <summary>\n    /// Create a ChapterInfo object.\n    /// </summary>\n    /// <param name=\"name\">Chapter name.</param>\n    /// <param name=\"position\">Chapter position (in seconds).</param>\n    /// <returns>ChapterInfo.</returns>\n    private ChapterInfo CreateChapter(string name, int position)\n    {\n        return new()\n        {\n            Name = name,\n            StartPositionTicks = TimeSpan.FromSeconds(position).Ticks\n        };\n    }\n}\n"
  },
  {
    "path": "Jellyfin.Plugin.MediaAnalyzer.Tests/TestContiguous.cs",
    "content": "using Xunit;\n\nnamespace Jellyfin.Plugin.MediaAnalyzer.Tests;\n\npublic class TestTimeRanges\n{\n    [Fact]\n    public void TestSmallRange()\n    {\n        var times = new double[]{\n            1, 1.5, 2, 2.5, 3, 3.5, 4,\n            100, 100.5, 101, 101.5\n        };\n\n        var expected = new TimeRange(1, 4);\n        var actual = TimeRangeHelpers.FindContiguous(times, 2);\n\n        Assert.Equal(expected, actual);\n    }\n\n    [Fact]\n    public void TestLargeRange()\n    {\n        var times = new double[]{\n            1, 1.5, 2,\n            2.8, 2.9, 2.995, 3.0, 3.01, 3.02, 3.4, 3.45, 3.48, 3.7, 3.77, 3.78, 3.781, 3.782, 3.789, 3.85,\n            4.5, 5.3122, 5.3123, 5.3124, 5.3125, 5.3126, 5.3127, 5.3128,\n            55, 55.5, 55.6, 55.7\n        };\n\n        var expected = new TimeRange(1, 5.3128);\n        var actual = TimeRangeHelpers.FindContiguous(times, 2);\n\n        Assert.Equal(expected, actual);\n    }\n\n    [Fact]\n    public void TestFuturama()\n    {\n        // These timestamps were manually extracted from Futurama S01E04 and S01E05.\n        var times = new double[]{\n            2.176, 8.32, 10.112, 11.264, 13.696, 16, 16.128, 16.64, 16.768, 16.896, 17.024, 17.152, 17.28,\n            17.408, 17.536, 17.664, 17.792, 17.92, 18.048, 18.176, 18.304, 18.432, 18.56, 18.688, 18.816,\n            18.944, 19.072, 19.2, 19.328, 19.456, 19.584, 19.712, 19.84, 19.968, 20.096, 20.224, 20.352,\n            20.48, 20.608, 20.736, 20.864, 20.992, 21.12, 21.248, 21.376, 21.504, 21.632, 21.76, 21.888,\n            22.016, 22.144, 22.272, 22.4, 22.528, 22.656, 22.784, 22.912, 23.04, 23.168, 23.296, 23.424,\n            23.552, 23.68, 23.808, 23.936, 24.064, 24.192, 24.32, 24.448, 24.576, 24.704, 24.832, 24.96,\n            25.088, 25.216, 25.344, 25.472, 25.6, 25.728, 25.856, 25.984, 26.112, 26.24, 26.368, 26.496,\n            26.624, 26.752, 26.88, 27.008, 27.136, 27.264, 27.392, 27.52, 27.648, 27.776, 27.904, 28.032,\n            28.16, 28.288, 28.416, 28.544, 28.672, 28.8, 28.928, 29.056, 29.184, 29.312, 29.44, 29.568,\n            29.696, 29.824, 29.952, 30.08, 30.208, 30.336, 30.464, 30.592, 30.72, 30.848, 30.976, 31.104,\n            31.232, 31.36, 31.488, 31.616, 31.744, 31.872, 32, 32.128, 32.256, 32.384, 32.512, 32.64,\n            32.768, 32.896, 33.024, 33.152, 33.28, 33.408, 33.536, 33.664, 33.792, 33.92, 34.048, 34.176,\n            34.304, 34.432, 34.56, 34.688, 34.816, 34.944, 35.072, 35.2, 35.328, 35.456, 35.584, 35.712,\n            35.84, 35.968, 36.096, 36.224, 36.352, 36.48, 36.608, 36.736, 36.864, 36.992, 37.12, 37.248,\n            37.376, 37.504, 37.632, 37.76, 37.888, 38.016, 38.144, 38.272, 38.4, 38.528, 38.656, 38.784,\n            38.912, 39.04, 39.168, 39.296, 39.424, 39.552, 39.68, 39.808, 39.936, 40.064, 40.192, 40.32,\n            40.448, 40.576, 40.704, 40.832, 40.96, 41.088, 41.216, 41.344, 41.472, 41.6, 41.728, 41.856,\n            41.984, 42.112, 42.24, 42.368, 42.496, 42.624, 42.752, 42.88, 43.008, 43.136, 43.264, 43.392,\n            43.52, 43.648, 43.776, 43.904, 44.032, 44.16, 44.288, 44.416, 44.544, 44.672, 44.8, 44.928,\n            45.056, 45.184, 57.344, 62.976, 68.864, 74.368, 81.92, 82.048, 86.528, 100.864, 102.656,\n            102.784, 102.912, 103.808, 110.976, 116.864, 125.696, 128.384, 133.248, 133.376, 136.064,\n            136.704, 142.976, 150.272, 152.064, 164.864, 164.992, 166.144, 166.272, 175.488, 190.08,\n            191.872, 192, 193.28, 193.536, 213.376, 213.504, 225.664, 225.792, 243.2, 243.84, 256,\n            264.448, 264.576, 264.704, 269.568, 274.816, 274.944, 276.096, 283.264, 294.784, 294.912,\n            295.04, 295.168, 313.984, 325.504, 333.568, 335.872, 336.384\n        };\n\n        var expected = new TimeRange(16, 45.184);\n        var actual = TimeRangeHelpers.FindContiguous(times, 2);\n\n        Assert.Equal(expected, actual);\n    }\n\n    /// <summary>\n    /// Tests that TimeRange intersections are detected correctly.\n    /// Tests each time range against a range of 5 to 10 seconds.\n    /// </summary>\n    [Theory]\n    [InlineData(1, 4, false)]   // too early\n    [InlineData(4, 6, true)]    // intersects on the left\n    [InlineData(7, 8, true)]    // in the middle\n    [InlineData(9, 12, true)]   // intersects on the right\n    [InlineData(13, 15, false)] // too late\n    public void TestTimeRangeIntersection(int start, int end, bool expected)\n    {\n        var large = new TimeRange(5, 10);\n        var testRange = new TimeRange(start, end);\n\n        Assert.Equal(expected, large.Intersects(testRange));\n    }\n}\n"
  },
  {
    "path": "Jellyfin.Plugin.MediaAnalyzer.Tests/TestWarnings.cs",
    "content": "namespace Jellyfin.Plugin.MediaAnalyzer.Tests;\n\nusing Xunit;\n\npublic class TestFlags\n{\n    [Fact]\n    public void TestEmptyFlagSerialization()\n    {\n        WarningManager.Clear();\n        Assert.Equal(\"None\", WarningManager.GetWarnings());\n    }\n\n    [Fact]\n    public void TestSingleFlagSerialization()\n    {\n        WarningManager.Clear();\n        WarningManager.SetFlag(PluginWarning.UnableToAddSkipButton);\n        Assert.Equal(\"UnableToAddSkipButton\", WarningManager.GetWarnings());\n    }\n\n    [Fact]\n    public void TestDoubleFlagSerialization()\n    {\n        WarningManager.Clear();\n        WarningManager.SetFlag(PluginWarning.UnableToAddSkipButton);\n        WarningManager.SetFlag(PluginWarning.InvalidChromaprintFingerprint);\n        WarningManager.SetFlag(PluginWarning.InvalidChromaprintFingerprint);\n\n        Assert.Equal(\n            \"UnableToAddSkipButton, InvalidChromaprintFingerprint\",\n            WarningManager.GetWarnings());\n    }\n}\n"
  },
  {
    "path": "Jellyfin.Plugin.MediaAnalyzer.Tests/audio/README.txt",
    "content": "The audio used in the fingerprinting unit tests is from Big Buck Bunny, attributed below.\n\nBoth big_buck_bunny_intro.mp3 and big_buck_bunny_clip.mp3 are derived from Big Buck Bunny, (c) copyright 2008, Blender Foundation / www.bigbuckbunny.org. They are used under the Creative Commons Attribution 3.0 and the original source can be found at https://www.youtube.com/watch?v=YE7VzlLtp-4.\n\nBoth files have been downmixed to two audio channels.\nbig_buck_bunny_intro.mp3 is from 5 to 30 seconds.\nbig_buck_bunny_clip.mp3 is from 0 to 60 seconds.\n"
  },
  {
    "path": "Jellyfin.Plugin.MediaAnalyzer.Tests/e2e_tests/.gitignore",
    "content": "# Binaries\n/verifier/verifier\n/run_tests\n/plugin_binaries/\n\n# Wrapper configuration and base configuration files\nconfig.json\n/config/\n\n# Timestamp reports\n/reports/\n\n# Selenium screenshots\nselenium/screenshots/\n"
  },
  {
    "path": "Jellyfin.Plugin.MediaAnalyzer.Tests/e2e_tests/README.md",
    "content": "# End to end testing framework\n\n## wrapper\n\nThe wrapper script (compiled as `run_tests`) runs multiple tests on Jellyfin servers to verify that the plugin works as intended. It tests:\n\n- Introduction timestamp accuracy (using `verifier`)\n- Web interface functionality (using `selenium/main.py`)\n\n## verifier\n\n### Description\n\nThis program is responsible for:\n* Saving all discovered introduction timestamps into a report\n* Comparing two reports against each other to find episodes that:\n    * Are missing introductions in both reports\n    * Have introductions in both reports, but with different timestamps\n    * Newly discovered introductions\n    * Introductions that were discovered previously, but not anymore\n* Validating the schema of returned `Intro` objects from the `/IntroTimestamps` API endpoint\n\n### Usage examples\n* Generate intro timestamp report from a local server:\n    * `./verifier -address http://127.0.0.1:8096 -key api_key`\n* Generate intro timestamp report from a remote server, polling for task completion every 20 seconds:\n    * `./verifier -address https://example.com -key api_key -poll 20s -o example.json`\n* Compare two previously generated reports:\n    * `./verifier -r1 v0.1.5.json -r2 v0.1.6.json`\n* Validate the API schema for three episodes:\n    * `./verifier -address http://127.0.0.1:8096 -key api_key -validate id1,id2,id3`\n\n## Selenium web interface tests\n\nSelenium is used to verify that the plugin's web interface works as expected. It simulates a user:\n\n* Clicking the skip intro button\n    * Checks that clicking the button skips the intro and keeps playing the video\n* Changing settings (will be added in the future)\n    * Maximum degree of parallelism\n    * Selecting libraries for analysis\n    * EDL settings\n    * Introduction requirements\n    * Auto skip\n    * Show/hide skip prompt\n* Timestamp editor (will be added in the future)\n    * Displays timestamps\n    * Modifies timestamps\n    * Erases season timestamps\n* Fingerprint visualizer (will be added in the future)\n    * Suggests shifts\n    * Visualizer canvas is drawn on\n"
  },
  {
    "path": "Jellyfin.Plugin.MediaAnalyzer.Tests/e2e_tests/build.sh",
    "content": "#!/bin/bash\n\necho \"[+] Building timestamp verifier\"\n(cd verifier && go build -o verifier) || exit 1\n\necho \"[+] Building test wrapper\"\n(cd wrapper && go test ./... && go build -o ../run_tests) || exit 1\n\necho\necho \"[+] All programs built successfully\"\n"
  },
  {
    "path": "Jellyfin.Plugin.MediaAnalyzer.Tests/e2e_tests/config_sample.jsonc",
    "content": "{\n    \"common\": {\n        \"library\": \"/full/path/to/test/library/on/host/TV\",\n        \"episode\": \"Episode title to search for\"\n    },\n    \"servers\": [\n        {\n            \"comment\": \"Optional comment to identify this server\",\n            \"image\": \"ghcr.io/confusedpolarbear/jellyfin-intro-skipper:latest\",\n            \"username\": \"admin\",\n            \"password\": \"hunter2\",\n            \"browsers\": [\n                \"chrome\",\n                \"firefox\"\n            ], // supported values are \"chrome\" and \"firefox\".\n            \"tests\": [\n                \"skip_button\", // test skip intro button\n                \"settings\" // test plugin administration page\n            ]\n        }\n    ]\n}\n"
  },
  {
    "path": "Jellyfin.Plugin.MediaAnalyzer.Tests/e2e_tests/docker-compose.yml",
    "content": "version: \"3\"\nservices:\n  chrome:\n    image: selenium/standalone-chrome:106.0\n    shm_size: 2gb\n    ports:\n      - 4444:4444\n    environment:\n      - SE_NODE_SESSION_TIMEOUT=10\n\n  firefox:\n    image: selenium/standalone-firefox:105.0\n    shm_size: 2gb\n    ports:\n      - 4445:4444\n    environment:\n      - SE_NODE_SESSION_TIMEOUT=10\n\n  chrome_video:\n    image: selenium/video\n    environment:\n      - DISPLAY_CONTAINER_NAME=chrome\n      - FILE_NAME=chrome_video.mp4\n    volumes:\n      - /tmp/selenium/videos:/videos\n\n  firefox_video:\n    image: selenium/video\n    environment:\n      - DISPLAY_CONTAINER_NAME=firefox\n      - FILE_NAME=firefox_video.mp4\n    volumes:\n      - /tmp/selenium/videos:/videos\n"
  },
  {
    "path": "Jellyfin.Plugin.MediaAnalyzer.Tests/e2e_tests/selenium/main.py",
    "content": "import argparse, os, time\n\nfrom selenium import webdriver\nfrom selenium.webdriver.common.by import By\nfrom selenium.webdriver.common.keys import Keys\n\n\n# Driver function\ndef main():\n    # Parse CLI arguments and store in a dictionary\n    parser = argparse.ArgumentParser()\n    parser.add_argument(\"-host\", help=\"Jellyfin server address with protocol and port.\")\n    parser.add_argument(\"-username\", help=\"Username.\")\n    parser.add_argument(\"-password\", help=\"Password.\")\n    parser.add_argument(\"-name\", help=\"Name of episode to search for.\")\n    parser.add_argument(\n        \"--tests\", help=\"Space separated list of Selenium tests to run.\", type=str, nargs=\"+\"\n    )\n    parser.add_argument(\n        \"--browsers\",\n        help=\"Space separated list of browsers to run tests with.\",\n        type=str,\n        nargs=\"+\",\n        choices=[\"chrome\", \"firefox\"],\n    )\n    args = parser.parse_args()\n\n    server = {\n        \"host\": args.host,\n        \"username\": args.username,\n        \"password\": args.password,\n        \"episode\": args.name,\n        \"browsers\": args.browsers,\n        \"tests\": args.tests,\n    }\n\n    # Print the server info for debugging and run the test\n    print()\n    print(f\"Browsers: {server['browsers']}\")\n    print(f\"Address:  {server['host']}\")\n    print(f\"Username: {server['username']}\")\n    print(f\"Episode:  \\\"{server['episode']}\\\"\")\n    print(f\"Tests:    {server['tests']}\")\n    print()\n\n    # Setup the list of drivers to run tests with\n    if server[\"browsers\"] is None:\n        print(\"[!] --browsers is required\")\n        exit(1)\n\n    drivers = []\n    if \"chrome\" in server[\"browsers\"]:\n        drivers = [(\"http://127.0.0.1:4444\", \"Chrome\")]\n    if \"firefox\" in server[\"browsers\"]:\n        drivers.append((\"http://127.0.0.1:4445\", \"Firefox\"))\n\n    # Test with all selected drivers\n    for driver in drivers:\n        print(f\"[!] Starting new test run using {driver[1]}\")\n        test_server(server, driver[0], driver[1])\n        print()\n\n\n# Main server test function\ndef test_server(server, executor, driver_type):\n    # Configure Selenium to use a remote driver\n    print(f\"[+] Configuring Selenium to use executor {executor} of type {driver_type}\")\n\n    opts = None\n    if driver_type == \"Chrome\":\n        opts = webdriver.ChromeOptions()\n    elif driver_type == \"Firefox\":\n        opts = webdriver.FirefoxOptions()\n    else:\n        raise ValueError(f\"Unknown driver type {driver_type}\")\n\n    driver = webdriver.Remote(command_executor=executor, options=opts)\n\n    try:\n        # Wait up to two seconds when finding an element before reporting failure\n        driver.implicitly_wait(2)\n\n        # Login to Jellyfin\n        driver.get(make_url(server, \"/\"))\n\n        print(f\"[+] Authenticating as {server['username']}\")\n        login(driver, server)\n\n        if \"skip_button\" in server[\"tests\"]:\n            # Play the user specified episode and verify skip intro button functionality. This episode is expected to:\n            #   * already have been analyzed for an introduction\n            #   * have an introduction at the beginning of the episode\n            print(\"[+] Testing skip intro button\")\n            test_skip_button(driver, server)\n\n        print(\"[+] All tests completed successfully\")\n    finally:\n        # Unconditionally end the Selenium session\n        driver.quit()\n\n\ndef login(driver, server):\n    # Append the Enter key to the password to submit the form\n    us = server[\"username\"]\n    pw = server[\"password\"] + Keys.ENTER\n\n    # Fill out and submit the login form\n    driver.find_element(By.ID, \"txtManualName\").send_keys(us)\n    driver.find_element(By.ID, \"txtManualPassword\").send_keys(pw)\n\n\ndef test_skip_button(driver, server):\n    print(f\"  [+] Searching for episode \\\"{server['episode']}\\\"\")\n\n    search = driver.find_element(By.CSS_SELECTOR, \".headerSearchButton span.search\")\n\n    if driver.capabilities[\"browserName\"] == \"firefox\":\n        # Work around a FF bug where the search element isn't considered clickable right away\n        time.sleep(1)\n\n    # Click the search button\n    search.click()\n\n    # Type the episode name\n    driver.find_element(By.CSS_SELECTOR, \".searchfields-txtSearch\").send_keys(\n        server[\"episode\"]\n    )\n\n    # Click the first episode in the search results\n    driver.find_element(\n        By.CSS_SELECTOR, \".searchResults button[data-type='Episode']\"\n    ).click()\n\n    # Wait for the episode page to finish loading by searching for the episode description (overview)\n    driver.find_element(By.CSS_SELECTOR, \".overview\")\n\n    print(f\"  [+] Waiting for playback to start\")\n\n    # Click the play button in the toolbar\n    driver.find_element(\n        By.CSS_SELECTOR, \"div.mainDetailButtons span.play_arrow\"\n    ).click()\n\n    # Wait for playback to start by searching for the lower OSD control bar\n    driver.find_element(By.CSS_SELECTOR, \".osdControls\")\n\n    # Let the video play a little bit so the position before clicking the button can be logged\n    print(\"  [+] Playing video\")\n    time.sleep(2)\n    screenshot(driver, \"skip_button_pre_skip\")\n    assert_video_playing(driver)\n\n    # Find the skip intro button and click it, logging the new video position after the seek is preformed\n    print(\"  [+] Clicking skip intro button\")\n    driver.find_element(By.CSS_SELECTOR, \"div#skipIntro\").click()\n    time.sleep(1)\n    screenshot(driver, \"skip_button_post_skip\")\n    assert_video_playing(driver)\n\n    # Keep playing the video for a few seconds to ensure that:\n    #   * the intro was successfully skipped\n    #   * video playback continued automatically post button click\n    print(\"  [+] Verifying post skip position\")\n    time.sleep(4)\n\n    screenshot(driver, \"skip_button_post_play\")\n    assert_video_playing(driver)\n\n\n# Utility functions\ndef make_url(server, url):\n    final = server[\"host\"] + url\n    print(f\"[+] Navigating to {final}\")\n    return final\n\n\ndef screenshot(driver, filename):\n    dest = f\"screenshots/{filename}.png\"\n    driver.save_screenshot(dest)\n\n\n# Returns the current video playback position and if the video is paused.\n# Will raise an exception if playback is paused as the video shouldn't ever pause when using this plugin.\ndef assert_video_playing(driver):\n    ret = driver.execute_script(\n        \"\"\"\n        const video = document.querySelector(\"video\");\n        return {\n            \"position\": video.currentTime,\n            \"paused\": video.paused\n        };\n        \"\"\"\n    )\n\n    if ret[\"paused\"]:\n        raise Exception(\"Video should not be paused\")\n\n    print(f\"  [+] Video playback position: {ret['position']}\")\n\n    return ret\n\n\nmain()\n"
  },
  {
    "path": "Jellyfin.Plugin.MediaAnalyzer.Tests/e2e_tests/selenium/requirements.txt",
    "content": "selenium >= 4.3.0\n"
  },
  {
    "path": "Jellyfin.Plugin.MediaAnalyzer.Tests/e2e_tests/verifier/go.mod",
    "content": "module github.com/confusedpolarbear/intro_skipper_verifier\n\ngo 1.17\n"
  },
  {
    "path": "Jellyfin.Plugin.MediaAnalyzer.Tests/e2e_tests/verifier/http.go",
    "content": "package main\n\nimport (\n\t\"encoding/json\"\n\t\"fmt\"\n\t\"io\"\n\t\"net/http\"\n\t\"strings\"\n\t\"time\"\n\n\t\"github.com/confusedpolarbear/intro_skipper_verifier/structs\"\n)\n\n// Gets the contents of the provided URL or panics.\nfunc SendRequest(method, url, apiKey string) []byte {\n\thttp.DefaultClient.Timeout = 10 * time.Second\n\n\t// Construct the request\n\treq, err := http.NewRequest(method, url, nil)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\t// Include the authorization token\n\treq.Header.Set(\"Authorization\", fmt.Sprintf(`MediaBrowser Token=\"%s\"`, apiKey))\n\n\t// Send the request\n\tres, err := http.DefaultClient.Do(req)\n\n\tif !strings.Contains(url, \"hideUrl\") {\n\t\tfmt.Printf(\"[+] %s %s: %d\\n\", method, url, res.StatusCode)\n\t}\n\n\t// Panic if any error occurred\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\t// Check for API key validity\n\tif res.StatusCode == http.StatusUnauthorized {\n\t\tpanic(\"Server returned 401 (Unauthorized). Check API key validity and try again.\")\n\t}\n\n\t// Read and return the entire body\n\tdefer res.Body.Close()\n\tbody, err := io.ReadAll(res.Body)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\treturn body\n}\n\nfunc GetServerInfo(hostAddress, apiKey string) structs.PublicInfo {\n\tvar info structs.PublicInfo\n\n\tfmt.Println(\"[+] Getting server information\")\n\trawInfo := SendRequest(\"GET\", hostAddress+\"/System/Info/Public\", apiKey)\n\n\tif err := json.Unmarshal(rawInfo, &info); err != nil {\n\t\tpanic(err)\n\t}\n\n\treturn info\n}\n\nfunc GetPluginConfiguration(hostAddress, apiKey string) structs.PluginConfiguration {\n\tvar config structs.PluginConfiguration\n\n\tfmt.Println(\"[+] Getting plugin configuration\")\n\trawConfig := SendRequest(\"GET\", hostAddress+\"/Plugins/c83d86bb-a1e0-4c35-a113-e2101cf4ee6b/Configuration\", apiKey)\n\n\tif err := json.Unmarshal(rawConfig, &config); err != nil {\n\t\tpanic(err)\n\t}\n\n\treturn config\n}\n"
  },
  {
    "path": "Jellyfin.Plugin.MediaAnalyzer.Tests/e2e_tests/verifier/main.go",
    "content": "package main\n\nimport (\n\t\"flag\"\n\t\"time\"\n)\n\nfunc flags() {\n\t// Report generation\n\thostAddress := flag.String(\"address\", \"\", \"Address of Jellyfin server to extract intro information from.\")\n\tapiKey := flag.String(\"key\", \"\", \"Administrator API key to authenticate with.\")\n\tkeepTimestamps := flag.Bool(\"keep\", false, \"Keep the current timestamps instead of erasing and reanalyzing.\")\n\tpollInterval := flag.Duration(\"poll\", 10*time.Second, \"Interval to poll task completion at.\")\n\treportDestination := flag.String(\"o\", \"\", \"Report destination filename. Defaults to intros-ADDRESS-TIMESTAMP.json.\")\n\n\t// Report comparison\n\treport1 := flag.String(\"r1\", \"\", \"First report.\")\n\treport2 := flag.String(\"r2\", \"\", \"Second report.\")\n\n\t// API schema validator\n\tids := flag.String(\"validate\", \"\", \"Comma separated item ids to validate the API schema for.\")\n\n\t// Print usage examples\n\tflag.CommandLine.Usage = func() {\n\t\tflag.CommandLine.Output().Write([]byte(\"Flags:\\n\"))\n\t\tflag.PrintDefaults()\n\n\t\tusage := \"\\nUsage:\\n\" +\n\t\t\t\"Generate intro timestamp report from a local server:\\n\" +\n\t\t\t\"./verifier -address http://127.0.0.1:8096 -key api_key\\n\\n\" +\n\n\t\t\t\"Generate intro timestamp report from a remote server, polling for task completion every 20 seconds:\\n\" +\n\t\t\t\"./verifier -address https://example.com -key api_key -poll 20s -o example.json\\n\\n\" +\n\n\t\t\t\"Compare two previously generated reports:\\n\" +\n\t\t\t\"./verifier -r1 v0.1.5.json -r2 v0.1.6.json\\n\\n\" +\n\n\t\t\t\"Validate the API schema for some item ids:\\n\" +\n\t\t\t\"./verifier -address http://127.0.0.1:8096 -key api_key -validate id1,id2,id3\\n\"\n\n\t\tflag.CommandLine.Output().Write([]byte(usage))\n\t}\n\n\tflag.Parse()\n\n\tif *hostAddress != \"\" && *apiKey != \"\" {\n\t\tif *ids == \"\" {\n\t\t\tgenerateReport(*hostAddress, *apiKey, *reportDestination, *keepTimestamps, *pollInterval)\n\t\t} else {\n\t\t\tvalidateApiSchema(*hostAddress, *apiKey, *ids)\n\t\t}\n\n\t} else if *report1 != \"\" && *report2 != \"\" {\n\t\tcompareReports(*report1, *report2, *reportDestination)\n\n\t} else {\n\t\tpanic(\"Either (-address and -key) or (-r1 and -r2) are required.\")\n\t}\n}\n\nfunc main() {\n\tflags()\n}\n"
  },
  {
    "path": "Jellyfin.Plugin.MediaAnalyzer.Tests/e2e_tests/verifier/report.html",
    "content": "<!DOCTYPE html>\n<html>\n\n<!-- TODO: when templating this, pre-populate the ignored shows value with something pulled from a config file -->\n\n<head>\n    <style>\n        /* dark mode */\n        body {\n            background-color: #1e1e1e;\n            color: white;\n        }\n\n        /* enable borders on the table row */\n        table {\n            border-collapse: collapse;\n        }\n\n        table td {\n            padding-right: 5px;\n        }\n\n        /* remove top & bottom margins */\n        .report-info *,\n        .episode * {\n            margin-bottom: 0;\n            margin-top: 0;\n        }\n\n        /* visually separate the report header from the contents */\n        .report-info .report {\n            background-color: #0c3c55;\n            border-radius: 7px;\n            display: inline-block;\n        }\n\n        .report h3 {\n            display: inline;\n        }\n\n        .report.stats {\n            margin-top: 4px;\n        }\n\n        details {\n            margin-bottom: 10px;\n        }\n\n        summary {\n            cursor: pointer;\n        }\n\n        /* prevent the details from taking up the entire width of the screen */\n        .show>details {\n            max-width: 50%;\n        }\n\n        /* indent season headers some */\n        .season {\n            margin-left: 1em;\n        }\n\n        /* indent individual episode timestamps some more */\n        .episode {\n            margin-left: 1em;\n        }\n\n        /* if an intro was not found previously but is now, that's good */\n        .episode[data-warning=\"improvement\"] {\n            background-color: #044b04;\n        }\n\n        /* if an intro was found previously but isn't now, that's bad */\n        .episode[data-warning=\"only_previous\"],\n        .episode[data-warning=\"missing\"] {\n            background-color: firebrick;\n        }\n\n        /* if an intro was found on both runs but the timestamps are pretty different, that's interesting */\n        .episode[data-warning=\"different\"] {\n            background-color: #b77600;\n        }\n\n        #stats.warning {\n            border: 2px solid firebrick;\n            font-weight: bolder;\n        }\n    </style>\n</head>\n\n<body>\n    <div class=\"report-info\">\n        <h2 class=\"margin-bottom:1em\">Intro Timestamp Differential</h2>\n\n        <div class=\"report old\">\n            <h3 style=\"margin-top:0.5em\">First report</h3>\n\n            {{ block \"ReportInfo\" .OldReport }}\n            <table>\n                <tbody>\n                    <tr style=\"border-bottom: 1px solid black\">\n                        <td>Path</td>\n                        <td><code>{{ .Path }}</code></td>\n                    </tr>\n\n                    <tr>\n                        <td>Jellyfin</td>\n                        <td>{{ .ServerInfo.Version }} on {{ .ServerInfo.OperatingSystem }}</td>\n                    </tr>\n                    <tr>\n                        <td>Analysis Settings</td>\n                        <td>{{ printAnalysisSettings .PluginConfig }}</td>\n                    </tr>\n                    <tr style=\"border-bottom: 1px solid black\">\n                        <td>Introduction Requirements</td>\n                        <td>{{ printIntroductionReqs .PluginConfig }}</td>\n                    </tr>\n\n                    <tr>\n                        <td>Start time</td>\n                        <td>{{ printTime .StartedAt }}</td>\n                    </tr>\n                    <tr>\n                        <td>End time</td>\n                        <td>{{ printTime .FinishedAt }}</td>\n                    </tr>\n                    <tr>\n                        <td>Duration</td>\n                        <td>{{ printDuration .Runtime }}</td>\n                    </tr>\n                </tbody>\n            </table>\n            {{ end }}\n        </div>\n\n        <div class=\"report new\">\n            <h3 style=\"padding-top:0.5em\">Second report</h3>\n\n            {{ template \"ReportInfo\" .NewReport }}\n        </div>\n        <br />\n\n        <div class=\"report stats\">\n            <h3 style=\"padding-top:0.5em\">Statistics</h3>\n\n            <table>\n                <tbody>\n                    <tr>\n                        <td>Total episodes</td>\n                        <td id=\"statTotal\"></td>\n                    </tr>\n                    <tr>\n                        <td>Never found</td>\n                        <td id=\"statMissing\"></td>\n                    </tr>\n                    <tr>\n                        <td>Changed</td>\n                        <td id=\"statChanged\"></td>\n                    </tr>\n                    <tr>\n                        <td>Gains</td>\n                        <td id=\"statGain\"></td>\n                    </tr>\n                    <tr>\n                        <td>Losses</td>\n                        <td id=\"statLoss\"></td>\n                    </tr>\n                </tbody>\n            </table>\n        </div>\n\n        <div class=\"report settings\">\n            <h3 style=\"padding-top:0.5em\">Settings</h3>\n\n            <form style=\"display:table\">\n                <label for=\"minimumPercentage\">Minimum percentage</label>\n                <input id=\"minimumPercentage\" type=\"number\" value=\"85\" min=\"0\" max=\"100\"\n                    style=\"margin-left: 5px; max-width: 100px\" /> <br />\n\n                <label for=\"ignoreShows\">Ignored shows</label>\n                <input id=\"ignoredShows\" type=\"text\" /> <br />\n\n                <button id=\"btnUpdate\" type=\"button\">Update</button>\n            </form>\n        </div>\n    </div>\n\n    {{/* store a reference to the data before the range query */}}\n    {{ $p := . }}\n\n    {{/* sort the show names and iterate over them */}}\n    {{ range $name := sortShows .OldReport.Shows }}\n    <div class=\"show\" id=\"{{ $name }}\">\n        <details>\n            {{/* get the unsorted seasons for this show */}}\n            {{ $seasons := index $p.OldReport.Shows $name }}\n\n            {{/* log the show name and number of seasons */}}\n            <summary>\n                <span class=\"showTitle\">\n                    <strong>{{ $name }}</strong>\n                    <span id=\"stats\"></span>\n                </span>\n            </summary>\n\n            <div class=\"seasons\">\n                {{/* sort the seasons to ensure they display in numerical order */}}\n                {{ range $seasonNumber := (sortSeasons $seasons) }}\n                <div class=\"season\" id=\"{{ $name }}-{{ $seasonNumber }}\">\n                    <details>\n                        <summary>\n                            <span>\n                                <strong>Season {{ $seasonNumber }}</strong>\n                                <span id=\"stats\"></span>\n                            </span>\n                        </summary>\n\n                        {{/* compare each episode in the old report to the same episode in the new report */}}\n                        {{ range $episode := index $seasons $seasonNumber }}\n\n                        {{/* lookup and compare both episodes */}}\n                        {{ $comparison := compareEpisodes $episode.EpisodeId $p }}\n                        {{ $old := $comparison.Old }}\n                        {{ $new := $comparison.New }}\n\n                        {{/* set attributes indicating if an intro was found in the old and new reports */}}\n                        <div class=\"episode\" data-warning=\"{{ $comparison.WarningShort }}\">\n                            <p>{{ $episode.Title }}</p>\n\n                            <p>\n                                Old: {{ $old.FormattedStart }} - {{ $old.FormattedEnd }}\n                                (<span class=\"duration old\">{{ $old.Duration }}</span>)\n                                (valid: {{ $old.Valid }}) <br />\n\n                                New: {{ $new.FormattedStart }} - {{ $new.FormattedEnd }}\n                                (<span class=\"duration new\">{{ $new.Duration }}</span>)\n                                (valid: {{ $new.Valid }}) <br />\n\n                                {{ if ne $comparison.WarningShort \"okay\" }}\n                                Warning: {{ $comparison.Warning }}\n                                {{ end }}\n                            </p>\n\n                            <br />\n                        </div>\n                        {{ end }}\n                    </details>\n                </div>\n                {{ end }}\n            </div>\n        </details>\n    </div>\n    {{ end }}\n\n    <script>\n        function count(parent, warning) {\n            const sel = `div.episode[data-warning='${warning}']`\n\n            // Don't include hidden elements in the count\n            let count = 0;\n            for (const elem of parent.querySelectorAll(sel)) {\n                // offsetParent is defined when the element is not hidden\n                if (elem.offsetParent) {\n                    count++;\n                }\n            }\n\n            return count;\n        }\n\n        function getPercent(part, whole) {\n            const percent = Math.round((part * 10_000) / whole) / 100;\n            return `${part} (${percent}%)`;\n        }\n\n        function setText(selector, text) {\n            document.querySelector(selector).textContent = text;\n        }\n\n        // Gets the minimum percentage of episodes in a group (a series or season)\n        // that must have a detected introduction.\n        function getMinimumPercentage() {\n            const value = document.querySelector(\"#minimumPercentage\").value;\n            return Number(value);\n        }\n\n        // Gets the average duration for all episodes in a parent group.\n        // durationClass must be either \"old\" or \"new\".\n        function getAverageDuration(parent, durationClass) {\n            // Get all durations in the parent\n            const elems = parent.querySelectorAll(\".duration.\" + durationClass);\n\n            // Calculate the average duration, ignoring any episode without an intro\n            let totalDuration = 0;\n            let totalEpisodes = 0;\n            for (const e of elems) {\n                const dur = Number(e.textContent);\n                if (dur === 0) {\n                    continue;\n                }\n\n                totalDuration += dur;\n                totalEpisodes++;\n            }\n\n            if (totalEpisodes === 0) {\n                return 0;\n            }\n\n            return Math.round(totalDuration / totalEpisodes);\n        }\n\n        // Calculate statistics for all episodes in a parent element (a series or a season).\n        function setGroupStatistics(parent) {\n            // Count the total number of episodes.\n            const total = parent.querySelectorAll(\"div.episode\").length;\n\n            // Count how many episodes have no warnings.\n            const okayCount = count(parent, \"okay\") + count(parent, \"improvement\");\n            const okayPercent = Math.round((okayCount * 100) / total);\n            const isOkay = okayPercent >= getMinimumPercentage();\n\n            // Calculate the previous and current average durations\n            const oldDuration = getAverageDuration(parent, \"old\");\n            const newDuration = getAverageDuration(parent, \"new\");\n\n            // Display the statistics\n            const stats = parent.querySelector(\"#stats\");\n            stats.textContent = `${okayCount} / ${total} (${okayPercent}%) okay. r1 ${oldDuration} r2 ${newDuration}`;\n\n            if (!isOkay) {\n                stats.classList.add(\"warning\");\n            } else {\n                stats.classList.remove(\"warning\");\n            }\n        }\n\n        function updateGlobalStatistics() {\n            // Display all shows\n            for (const show of document.querySelectorAll(\"div.show\")) {\n                show.style.display = \"unset\";\n            }\n\n            // Hide any shows that are ignored\n            for (let ignored of document.querySelector(\"#ignoredShows\").value.split(\",\")) {\n                const elem = document.querySelector(`div.show[id='${ignored}']`);\n                if (!elem) {\n                    console.warn(\"unable to find show\", ignored);\n                    continue;\n                }\n\n                elem.style.display = \"none\";\n            }\n\n            const total = document.querySelectorAll(\"div.episode\").length;\n            const missing = count(document, \"missing\");\n            const different = count(document, \"different\")\n            const gain = count(document, \"improvement\");\n            const loss = count(document, \"only_previous\");\n            const okay = total - missing - different - loss;\n\n            setText(\"#statTotal\", getPercent(okay, total));\n            setText(\"#statMissing\", getPercent(missing, total));\n            setText(\"#statChanged\", getPercent(different, total));\n            setText(\"#statGain\", getPercent(gain, total));\n            setText(\"#statLoss\", getPercent(loss, total));\n        }\n\n        function updateStatistics() {\n            for (const series of document.querySelectorAll(\"div.show\")) {\n                setGroupStatistics(series);\n\n                for (const season of series.querySelectorAll(\"div.season\")) {\n                    setGroupStatistics(season);\n                }\n            }\n        }\n\n        // Display statistics for all episodes and by groups\n        updateGlobalStatistics();\n        updateStatistics();\n\n        // Add event handlers\n        document.querySelector(\"#minimumPercentage\").addEventListener(\"input\", updateStatistics);\n        document.querySelector(\"#btnUpdate\").addEventListener(\"click\", updateGlobalStatistics);\n    </script>\n</body>\n\n</html>\n"
  },
  {
    "path": "Jellyfin.Plugin.MediaAnalyzer.Tests/e2e_tests/verifier/report_comparison.go",
    "content": "package main\n\nimport (\n\t_ \"embed\"\n\t\"encoding/json\"\n\t\"fmt\"\n\t\"html/template\"\n\t\"math\"\n\t\"os\"\n\t\"time\"\n\n\t\"github.com/confusedpolarbear/intro_skipper_verifier/structs\"\n)\n\n//go:embed report.html\nvar reportTemplate []byte\n\nfunc compareReports(oldReportPath, newReportPath, destination string) {\n\tstart := time.Now()\n\n\t// Populate the destination filename if none was provided\n\tif destination == \"\" {\n\t\tdestination = fmt.Sprintf(\"report-%d.html\", start.Unix())\n\t}\n\n\t// Open the report for writing\n\tf, err := os.OpenFile(destination, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)\n\tif err != nil {\n\t\tpanic(err)\n\t} else {\n\t\tdefer f.Close()\n\t}\n\n\tfmt.Printf(\"Started at:    %s\\n\", start.Format(time.RFC1123))\n\tfmt.Printf(\"First report:  %s\\n\", oldReportPath)\n\tfmt.Printf(\"Second report: %s\\n\", newReportPath)\n\tfmt.Printf(\"Destination:   %s\\n\\n\", destination)\n\n\t// Unmarshal both reports\n\toldReport, newReport := unmarshalReport(oldReportPath), unmarshalReport(newReportPath)\n\n\tfmt.Println(\"[+] Comparing reports\")\n\n\t// Setup a function map with helper functions to use in the template\n\ttmp := template.New(\"report\")\n\n\tfuncs := make(template.FuncMap)\n\n\tfuncs[\"printTime\"] = func(t time.Time) string {\n\t\treturn t.Format(time.RFC1123)\n\t}\n\n\tfuncs[\"printDuration\"] = func(d time.Duration) string {\n\t\treturn d.Round(time.Second).String()\n\t}\n\n\tfuncs[\"printAnalysisSettings\"] = func(pc structs.PluginConfiguration) string {\n\t\treturn pc.AnalysisSettings()\n\t}\n\n\tfuncs[\"printIntroductionReqs\"] = func(pc structs.PluginConfiguration) string {\n\t\treturn pc.IntroductionRequirements()\n\t}\n\n\tfuncs[\"sortShows\"] = templateSortShows\n\tfuncs[\"sortSeasons\"] = templateSortSeason\n\tfuncs[\"compareEpisodes\"] = templateCompareEpisodes\n\ttmp.Funcs(funcs)\n\n\t// Load the template or panic\n\treport := template.Must(tmp.Parse(string(reportTemplate)))\n\n\terr = report.Execute(f,\n\t\tstructs.TemplateReportData{\n\t\t\tOldReport: oldReport,\n\t\t\tNewReport: newReport,\n\t\t})\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\t// Log success\n\tfmt.Printf(\"[+] Reports successfully compared in %s\\n\", time.Since(start).Round(time.Millisecond))\n}\n\nfunc unmarshalReport(path string) structs.Report {\n\t// Read the provided report\n\tcontents, err := os.ReadFile(path)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\t// Unmarshal\n\tvar report structs.Report\n\tif err := json.Unmarshal(contents, &report); err != nil {\n\t\tpanic(err)\n\t}\n\n\t// Setup maps and template data for later use\n\treport.Path = path\n\treport.Shows = make(map[string]structs.Seasons)\n\treport.IntroMap = make(map[string]structs.Intro)\n\n\t// Sort episodes by show and season\n\tfor _, intro := range report.Intros {\n\t\t// Round the duration to the nearest second to avoid showing 8 decimal places in the report\n\t\tintro.Duration = float32(math.Round(float64(intro.Duration)))\n\n\t\t// Pretty print the intro start and end times\n\t\tintro.FormattedStart = (time.Duration(intro.IntroStart) * time.Second).String()\n\t\tintro.FormattedEnd = (time.Duration(intro.IntroEnd) * time.Second).String()\n\n\t\tshow, season := intro.Series, intro.Season\n\n\t\t// If this show hasn't been seen before, allocate space for it\n\t\tif _, ok := report.Shows[show]; !ok {\n\t\t\treport.Shows[show] = make(structs.Seasons)\n\t\t}\n\n\t\t// Store this intro in the season of this show\n\t\tepisodes := report.Shows[show][season]\n\t\tepisodes = append(episodes, intro)\n\t\treport.Shows[show][season] = episodes\n\n\t\t// Store a reference to this intro in a lookup table\n\t\treport.IntroMap[intro.EpisodeId] = intro\n\t}\n\n\t// Print report info\n\tfmt.Printf(\"Report %s:\\n\", path)\n\tfmt.Printf(\"Generated with Jellyfin %s running on %s\\n\", report.ServerInfo.Version, report.ServerInfo.OperatingSystem)\n\tfmt.Printf(\"Analysis settings: %s\\n\", report.PluginConfig.AnalysisSettings())\n\tfmt.Printf(\"Introduction reqs: %s\\n\", report.PluginConfig.IntroductionRequirements())\n\tfmt.Printf(\"Episodes analyzed: %d\\n\", len(report.Intros))\n\tfmt.Println()\n\n\treturn report\n}\n"
  },
  {
    "path": "Jellyfin.Plugin.MediaAnalyzer.Tests/e2e_tests/verifier/report_comparison_util.go",
    "content": "package main\n\nimport (\n\t\"fmt\"\n\t\"math\"\n\t\"sort\"\n\n\t\"github.com/confusedpolarbear/intro_skipper_verifier/structs\"\n)\n\n// report template helper functions\n\n// Sort show names alphabetically\nfunc templateSortShows(shows map[string]structs.Seasons) []string {\n\tvar showNames []string\n\n\tfor show := range shows {\n\t\tshowNames = append(showNames, show)\n\t}\n\n\tsort.Strings(showNames)\n\n\treturn showNames\n}\n\n// Sort season numbers\nfunc templateSortSeason(show structs.Seasons) []int {\n\tvar keys []int\n\n\tfor season := range show {\n\t\tkeys = append(keys, season)\n\t}\n\n\tsort.Ints(keys)\n\n\treturn keys\n}\n\n// Compare the episode with the provided ID in the old report to the episode in the new report.\nfunc templateCompareEpisodes(id string, reports structs.TemplateReportData) structs.IntroPair {\n\tvar pair structs.IntroPair\n\tvar tolerance int = 5\n\n\t// Locate both episodes\n\tpair.Old = reports.OldReport.IntroMap[id]\n\tpair.New = reports.NewReport.IntroMap[id]\n\n\t// Mark the timestamps as similar if they are within a few seconds of each other\n\tsimilar := func(oldTime, newTime float32) bool {\n\t\tdiff := math.Abs(float64(newTime) - float64(oldTime))\n\t\treturn diff <= float64(tolerance)\n\t}\n\n\tif pair.Old.Valid && !pair.New.Valid {\n\t\t// If an intro was found previously, but not now, flag it\n\t\tpair.WarningShort = \"only_previous\"\n\t\tpair.Warning = \"Introduction found in previous report, but not the current one\"\n\n\t} else if !pair.Old.Valid && pair.New.Valid {\n\t\t// If an intro was not found previously, but found now, flag it\n\t\tpair.WarningShort = \"improvement\"\n\t\tpair.Warning = \"New introduction discovered\"\n\n\t} else if !pair.Old.Valid && !pair.New.Valid {\n\t\t// If an intro has never been found for this episode\n\t\tpair.WarningShort = \"missing\"\n\t\tpair.Warning = \"No introduction has ever been found for this episode\"\n\n\t} else if !similar(pair.Old.IntroStart, pair.New.IntroStart) || !similar(pair.Old.IntroEnd, pair.New.IntroEnd) {\n\t\t// If the intro timestamps are too different, flag it\n\t\tpair.WarningShort = \"different\"\n\t\tpair.Warning = fmt.Sprintf(\"Timestamps differ by more than %d seconds\", tolerance)\n\n\t} else {\n\t\t// No warning was generated\n\t\tpair.WarningShort = \"okay\"\n\t\tpair.Warning = \"Okay\"\n\t}\n\n\treturn pair\n}\n"
  },
  {
    "path": "Jellyfin.Plugin.MediaAnalyzer.Tests/e2e_tests/verifier/report_generator.go",
    "content": "package main\n\nimport (\n\t\"encoding/json\"\n\t\"fmt\"\n\t\"os\"\n\t\"os/exec\"\n\t\"strings\"\n\t\"time\"\n\n\t\"github.com/confusedpolarbear/intro_skipper_verifier/structs\"\n)\n\nvar spinners []string\nvar spinnerIndex int\n\nfunc generateReport(hostAddress, apiKey, reportDestination string, keepTimestamps bool, pollInterval time.Duration) {\n\tstart := time.Now()\n\n\t// Setup the spinner\n\tspinners = strings.Split(\"⣷⣯⣟⡿⢿⣻⣽⣾\", \"\")\n\tspinnerIndex = -1 // start the spinner on the first graphic\n\n\t// Setup the filename to save intros to\n\tif reportDestination == \"\" {\n\t\treportDestination = fmt.Sprintf(\"intros-%s-%d.json\", hostAddress, time.Now().Unix())\n\t\treportDestination = strings.ReplaceAll(reportDestination, \"http://\", \"\")\n\t\treportDestination = strings.ReplaceAll(reportDestination, \"https://\", \"\")\n\t}\n\n\t// Ensure the file is writable\n\tif err := os.WriteFile(reportDestination, nil, 0600); err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Printf(\"Started at:  %s\\n\", start.Format(time.RFC1123))\n\tfmt.Printf(\"Address:     %s\\n\", hostAddress)\n\tfmt.Printf(\"Destination: %s\\n\", reportDestination)\n\tfmt.Println()\n\n\t// Get Jellyfin server information and plugin configuration\n\tinfo := GetServerInfo(hostAddress, apiKey)\n\tconfig := GetPluginConfiguration(hostAddress, apiKey)\n\tfmt.Println()\n\n\tfmt.Printf(\"Jellyfin OS:       %s\\n\", info.OperatingSystem)\n\tfmt.Printf(\"Jellyfin version:  %s\\n\", info.Version)\n\tfmt.Printf(\"Analysis settings: %s\\n\", config.AnalysisSettings())\n\tfmt.Printf(\"Introduction reqs: %s\\n\", config.IntroductionRequirements())\n\tfmt.Printf(\"Erase timestamps:  %t\\n\", !keepTimestamps)\n\tfmt.Println()\n\n\t// If not keeping timestamps, run the fingerprint task.\n\t// Otherwise, log that the task isn't being run\n\tif !keepTimestamps {\n\t\trunAnalysisAndWait(hostAddress, apiKey, pollInterval)\n\t} else {\n\t\tfmt.Println(\"[+] Using previously discovered intros\")\n\t}\n\tfmt.Println()\n\n\t// Save all intros from the server\n\tfmt.Println(\"[+] Saving intros\")\n\n\tvar report structs.Report\n\trawIntros := SendRequest(\"GET\", hostAddress+\"/Intros/All\", apiKey)\n\tif err := json.Unmarshal(rawIntros, &report.Intros); err != nil {\n\t\tpanic(err)\n\t}\n\n\t// Calculate the durations of all intros\n\tfor i := range report.Intros {\n\t\tintro := report.Intros[i]\n\t\tintro.Duration = intro.IntroEnd - intro.IntroStart\n\t\treport.Intros[i] = intro\n\t}\n\n\tfmt.Println()\n\tfmt.Println(\"[+] Saving report\")\n\n\t// Store timing data, server information, and plugin configuration\n\treport.StartedAt = start\n\treport.FinishedAt = time.Now()\n\treport.Runtime = report.FinishedAt.Sub(report.StartedAt)\n\treport.ServerInfo = info\n\treport.PluginConfig = config\n\n\t// Marshal the report\n\tmarshalled, err := json.Marshal(report)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tif err := os.WriteFile(reportDestination, marshalled, 0600); err != nil {\n\t\tpanic(err)\n\t}\n\n\t// Change report permissions\n\texec.Command(\"chown\", \"1000:1000\", reportDestination).Run()\n\n\tfmt.Println(\"[+] Done\")\n}\n\nfunc runAnalysisAndWait(hostAddress, apiKey string, pollInterval time.Duration) {\n\tvar taskId string = \"\"\n\n\ttype taskInfo struct {\n\t\tState                     string\n\t\tCurrentProgressPercentage int\n\t}\n\n\tfmt.Println(\"[+] Erasing previously discovered intros\")\n\tSendRequest(\"POST\", hostAddress+\"/Intros/EraseTimestamps\", apiKey)\n\tfmt.Println()\n\n\tvar taskIds = []string{\n\t\t\"f64d8ad58e3d7b98548e1a07697eb100\", // v0.1.8\n\t\t\"8863329048cc357f7dfebf080f2fe204\",\n\t\t\"6adda26c5261c40e8fa4a7e7df568be2\"}\n\n\tfmt.Println(\"[+] Starting analysis task\")\n\tfor _, id := range taskIds {\n\t\tbody := SendRequest(\"POST\", hostAddress+\"/ScheduledTasks/Running/\"+id, apiKey)\n\t\tfmt.Println()\n\n\t\t// If the scheduled task was found, store the task ID for later\n\t\tif !strings.Contains(string(body), \"Not Found\") {\n\t\t\ttaskId = id\n\t\t\tbreak\n\t\t}\n\t}\n\n\tif taskId == \"\" {\n\t\tpanic(\"unable to find scheduled task\")\n\t}\n\n\tfmt.Println(\"[+] Waiting for analysis task to complete\")\n\tfmt.Print(\"[+] Episodes analyzed: 0%\")\n\n\tvar info taskInfo       // Last known scheduled task state\n\tvar lastQuery time.Time // Time the task info was last updated\n\n\tfor {\n\t\ttime.Sleep(500 * time.Millisecond)\n\n\t\t// Update the spinner\n\t\tif spinnerIndex++; spinnerIndex >= len(spinners) {\n\t\t\tspinnerIndex = 0\n\t\t}\n\n\t\tfmt.Printf(\"\\r[%s] Episodes analyzed: %d%%\", spinners[spinnerIndex], info.CurrentProgressPercentage)\n\n\t\tif info.CurrentProgressPercentage == 100 {\n\t\t\tfmt.Printf(\"\\r[+]\") // reset the spinner\n\t\t\tfmt.Println()\n\t\t\tbreak\n\t\t}\n\n\t\t// Get the latest task state & unmarshal (only if enough time has passed since the last update)\n\t\tif time.Since(lastQuery) <= pollInterval {\n\t\t\tcontinue\n\t\t}\n\n\t\tlastQuery = time.Now()\n\n\t\traw := SendRequest(\"GET\", hostAddress+\"/ScheduledTasks/\"+taskId+\"?hideUrl=1\", apiKey)\n\n\t\tif err := json.Unmarshal(raw, &info); err != nil {\n\t\t\tfmt.Printf(\"[!] Unable to unmarshal response into taskInfo struct: %s\\n\", err)\n\t\t\tfmt.Printf(\"%s\\n\", raw)\n\t\t\tcontinue\n\t\t}\n\n\t\t// Print the latest task state\n\t\tswitch info.State {\n\t\tcase \"Idle\":\n\t\t\tinfo.CurrentProgressPercentage = 100\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "Jellyfin.Plugin.MediaAnalyzer.Tests/e2e_tests/verifier/schema_validation.go",
    "content": "package main\n\nimport (\n\t\"encoding/json\"\n\t\"fmt\"\n\t\"strings\"\n\t\"time\"\n\n\t\"github.com/confusedpolarbear/intro_skipper_verifier/structs\"\n)\n\n// Given a comma separated list of item IDs, validate the returned API schema.\nfunc validateApiSchema(hostAddress, apiKey, rawIds string) {\n\t// Iterate over the raw item IDs and validate the schema of API responses\n\tids := strings.Split(rawIds, \",\")\n\n\tstart := time.Now()\n\n\tfmt.Printf(\"Started at:  %s\\n\", start.Format(time.RFC1123))\n\tfmt.Printf(\"Address:     %s\\n\", hostAddress)\n\tfmt.Println()\n\n\t// Get Jellyfin server information\n\tinfo := GetServerInfo(hostAddress, apiKey)\n\tfmt.Println()\n\n\tfmt.Printf(\"Jellyfin OS:      %s\\n\", info.OperatingSystem)\n\tfmt.Printf(\"Jellyfin version: %s\\n\", info.Version)\n\tfmt.Println()\n\n\tfor _, id := range ids {\n\t\tfmt.Printf(\"[+] Validating item %s\\n\", id)\n\n\t\tfmt.Println(\"  [+] Validating API v1 (implicitly versioned)\")\n\t\tintro, schema := getTimestampsV1(hostAddress, apiKey, id, \"\")\n\t\tvalidateV1Intro(id, intro, schema)\n\n\t\tfmt.Println(\"  [+] Validating API v1 (explicitly versioned)\")\n\t\tintro, schema = getTimestampsV1(hostAddress, apiKey, id, \"v1\")\n\t\tvalidateV1Intro(id, intro, schema)\n\n\t\tfmt.Println()\n\t}\n\n\tfmt.Printf(\"Validated %d items in %s\\n\", len(ids), time.Since(start).Round(time.Millisecond))\n}\n\n// Validates the returned intro object, panicking on any error.\nfunc validateV1Intro(id string, intro structs.Intro, schema map[string]interface{}) {\n\t// Validate the item ID\n\tif intro.EpisodeId != id {\n\t\tpanic(fmt.Sprintf(\"Intro struct has incorrect item ID. Expected '%s', found '%s'\", id, intro.EpisodeId))\n\t}\n\n\t// Validate the intro start and end times\n\tif intro.IntroStart < 0 || intro.IntroEnd < 0 {\n\t\tpanic(\"Intro struct has a negative intro start or end time\")\n\t}\n\n\t// Validate the intro duration\n\tif duration := intro.IntroEnd - intro.IntroStart; duration < 15 {\n\t\tpanic(fmt.Sprintf(\"Intro struct has duration %0.2f but the minimum allowed is 15\", duration))\n\t}\n\n\t// Ensure the intro is marked as valid.\n\tif !intro.Valid {\n\t\tpanic(\"Intro struct is not marked as valid\")\n\t}\n\n\t// Check for any extraneous properties\n\tallowedProperties := []string{\"EpisodeId\", \"Valid\", \"IntroStart\", \"IntroEnd\"}\n\n\tfor schemaKey := range schema {\n\t\tokay := false\n\n\t\tfor _, allowed := range allowedProperties {\n\t\t\tif allowed == schemaKey {\n\t\t\t\tokay = true\n\t\t\t\tbreak\n\t\t\t}\n\t\t}\n\n\t\tif !okay {\n\t\t\tpanic(fmt.Sprintf(\"Intro object contains unknown key '%s'\", schemaKey))\n\t\t}\n\t}\n}\n\n// Gets the timestamps for the provided item or panics.\nfunc getTimestampsV1(hostAddress, apiKey, id, version string) (structs.Intro, map[string]interface{}) {\n\tvar rawResponse map[string]interface{}\n\tvar intro structs.Intro\n\n\t// Make an authenticated GET request to {Host}/Episode/{ItemId}/IntroTimestamps/{Version}\n\traw := SendRequest(\"GET\", fmt.Sprintf(\"%s/Episode/%s/IntroTimestamps/%s?hideUrl=1\", hostAddress, id, version), apiKey)\n\n\t// Unmarshal the response as a version 1 API response, ignoring any unknown fields.\n\tif err := json.Unmarshal(raw, &intro); err != nil {\n\t\tpanic(err)\n\t}\n\n\t// Second, unmarshal the response into a map so that any unknown fields can be detected and alerted on.\n\tif err := json.Unmarshal(raw, &rawResponse); err != nil {\n\t\tpanic(err)\n\t}\n\n\treturn intro, rawResponse\n}\n"
  },
  {
    "path": "Jellyfin.Plugin.MediaAnalyzer.Tests/e2e_tests/verifier/structs/intro.go",
    "content": "package structs\n\ntype Intro struct {\n\tEpisodeId string\n\n\tSeries string\n\tSeason int\n\tTitle  string\n\n\tIntroStart float32\n\tIntroEnd   float32\n\tDuration   float32\n\tValid      bool\n\n\tFormattedStart string\n\tFormattedEnd   string\n}\n"
  },
  {
    "path": "Jellyfin.Plugin.MediaAnalyzer.Tests/e2e_tests/verifier/structs/plugin_configuration.go",
    "content": "package structs\n\nimport (\n\t\"fmt\"\n\t\"strings\"\n)\n\ntype PluginConfiguration struct {\n\tCacheFingerprints bool\n\tMaxParallelism    int\n\tSelectedLibraries string\n\n\tAnalysisPercent      int\n\tAnalysisLengthLimit  int\n\tMinimumIntroDuration int\n}\n\nfunc (c PluginConfiguration) AnalysisSettings() string {\n\t// If no libraries have been selected, display a star.\n\t// Otherwise, quote each library before displaying the slice.\n\tvar libs []string\n\tif c.SelectedLibraries == \"\" {\n\t\tlibs = []string{\"*\"}\n\t} else {\n\t\tfor _, tmp := range strings.Split(c.SelectedLibraries, \",\") {\n\t\t\ttmp = `\"` + strings.TrimSpace(tmp) + `\"`\n\t\t\tlibs = append(libs, tmp)\n\t\t}\n\t}\n\n\treturn fmt.Sprintf(\n\t\t\"cfp=%t thr=%d lbs=%v\",\n\t\tc.CacheFingerprints,\n\t\tc.MaxParallelism,\n\t\tlibs)\n}\n\nfunc (c PluginConfiguration) IntroductionRequirements() string {\n\treturn fmt.Sprintf(\n\t\t\"per=%d%% max=%dm min=%ds\",\n\t\tc.AnalysisPercent,\n\t\tc.AnalysisLengthLimit,\n\t\tc.MinimumIntroDuration)\n}\n"
  },
  {
    "path": "Jellyfin.Plugin.MediaAnalyzer.Tests/e2e_tests/verifier/structs/public_info.go",
    "content": "package structs\n\ntype PublicInfo struct {\n\tVersion         string\n\tOperatingSystem string\n}\n"
  },
  {
    "path": "Jellyfin.Plugin.MediaAnalyzer.Tests/e2e_tests/verifier/structs/report.go",
    "content": "package structs\n\nimport \"time\"\n\ntype Seasons map[int][]Intro\n\ntype Report struct {\n\tPath string `json:\"-\"`\n\n\tStartedAt  time.Time\n\tFinishedAt time.Time\n\tRuntime    time.Duration\n\n\tServerInfo   PublicInfo\n\tPluginConfig PluginConfiguration\n\n\tIntros []Intro\n\n\t// Intro lookup table. Only populated when loading a report.\n\tIntroMap map[string]Intro `json:\"-\"`\n\n\t// Intros which have been sorted by show and season number. Only populated when loading a report.\n\tShows map[string]Seasons `json:\"-\"`\n}\n\n// Data passed to the report template.\ntype TemplateReportData struct {\n\t// First report.\n\tOldReport Report\n\n\t// Second report.\n\tNewReport Report\n}\n\n// A pair of introductions from an old and new reports.\ntype IntroPair struct {\n\tOld Intro\n\tNew Intro\n\n\t// Recognized warning types:\n\t//   * okay:          no warning\n\t//   * different:     timestamps are too dissimilar\n\t//   * only_previous: introduction found in old report but not new one\n\tWarningShort string\n\n\t// If this pair of intros is not okay, a short description about the cause\n\tWarning string\n}\n"
  },
  {
    "path": "Jellyfin.Plugin.MediaAnalyzer.Tests/e2e_tests/wrapper/exec.go",
    "content": "package main\n\nimport (\n\t\"bufio\"\n\t\"context\"\n\t\"fmt\"\n\t\"io\"\n\t\"os/exec\"\n\t\"regexp\"\n\t\"strings\"\n\t\"time\"\n)\n\n// Run an external program\nfunc RunProgram(program string, args []string, timeout time.Duration) {\n\t// Flag if we are starting or stopping a container\n\tmanagingContainer := program == \"docker\"\n\n\t// Create context and command\n\tctx, cancel := context.WithTimeout(context.Background(), timeout)\n\tdefer cancel()\n\tcmd := exec.CommandContext(ctx, program, args...)\n\n\t// Stringify and censor the program's arguments\n\tstrArgs := redactString(strings.Join(args, \" \"))\n\tfmt.Printf(\"  [+] Running %s %s\\n\", program, strArgs)\n\n\t// Setup pipes\n\tstdout, err := cmd.StdoutPipe()\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tstderr, err := cmd.StderrPipe()\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\t// Start the command\n\tif err := cmd.Start(); err != nil {\n\t\tpanic(err)\n\t}\n\n\t// Stream any messages to the terminal\n\tfor _, r := range []io.Reader{stdout, stderr} {\n\t\t// Don't log stdout from the container\n\t\tif managingContainer && r == stdout {\n\t\t\tcontinue\n\t\t}\n\n\t\tscanner := bufio.NewScanner(r)\n\t\tscanner.Split(bufio.ScanRunes)\n\n\t\tfor scanner.Scan() {\n\t\t\tfmt.Print(scanner.Text())\n\t\t}\n\t}\n}\n\n// Redacts sensitive command line arguments.\nfunc redactString(raw string) string {\n\tredactionRegex := regexp.MustCompilePOSIX(`-(user|pass|key) [^ ]+`)\n\treturn redactionRegex.ReplaceAllString(raw, \"-$1 REDACTED\")\n}\n"
  },
  {
    "path": "Jellyfin.Plugin.MediaAnalyzer.Tests/e2e_tests/wrapper/exec_test.go",
    "content": "package main\n\nimport \"testing\"\n\nfunc TestStringRedaction(t *testing.T) {\n\traw := \"-key deadbeef -first second -user admin -third fourth -pass hunter2\"\n\texpected := \"-key REDACTED -first second -user REDACTED -third fourth -pass REDACTED\"\n\tactual := redactString(raw)\n\n\tif expected != actual {\n\t\tt.Errorf(`String was redacted incorrectly: \"%s\"`, actual)\n\t}\n}\n"
  },
  {
    "path": "Jellyfin.Plugin.MediaAnalyzer.Tests/e2e_tests/wrapper/go.mod",
    "content": "module github.com/confusedpolarbear/intro_skipper_wrapper\n\ngo 1.17\n"
  },
  {
    "path": "Jellyfin.Plugin.MediaAnalyzer.Tests/e2e_tests/wrapper/library.json",
    "content": "{\n  \"LibraryOptions\": {\n    \"EnableArchiveMediaFiles\": false,\n    \"EnablePhotos\": false,\n    \"EnableRealtimeMonitor\": false,\n    \"ExtractChapterImagesDuringLibraryScan\": false,\n    \"EnableChapterImageExtraction\": false,\n    \"EnableInternetProviders\": false,\n    \"SaveLocalMetadata\": false,\n    \"EnableAutomaticSeriesGrouping\": false,\n    \"PreferredMetadataLanguage\": \"\",\n    \"MetadataCountryCode\": \"\",\n    \"SeasonZeroDisplayName\": \"Specials\",\n    \"AutomaticRefreshIntervalDays\": 0,\n    \"EnableEmbeddedTitles\": false,\n    \"EnableEmbeddedEpisodeInfos\": false,\n    \"AllowEmbeddedSubtitles\": \"AllowAll\",\n    \"SkipSubtitlesIfEmbeddedSubtitlesPresent\": false,\n    \"SkipSubtitlesIfAudioTrackMatches\": false,\n    \"SaveSubtitlesWithMedia\": true,\n    \"RequirePerfectSubtitleMatch\": true,\n    \"AutomaticallyAddToCollection\": false,\n    \"MetadataSavers\": [],\n    \"TypeOptions\": [\n      {\n        \"Type\": \"Series\",\n        \"MetadataFetchers\": [\n          \"TheMovieDb\",\n          \"The Open Movie Database\"\n        ],\n        \"MetadataFetcherOrder\": [\n          \"TheMovieDb\",\n          \"The Open Movie Database\"\n        ],\n        \"ImageFetchers\": [\n          \"TheMovieDb\"\n        ],\n        \"ImageFetcherOrder\": [\n          \"TheMovieDb\"\n        ]\n      },\n      {\n        \"Type\": \"Season\",\n        \"MetadataFetchers\": [\n          \"TheMovieDb\"\n        ],\n        \"MetadataFetcherOrder\": [\n          \"TheMovieDb\"\n        ],\n        \"ImageFetchers\": [\n          \"TheMovieDb\"\n        ],\n        \"ImageFetcherOrder\": [\n          \"TheMovieDb\"\n        ]\n      },\n      {\n        \"Type\": \"Episode\",\n        \"MetadataFetchers\": [\n          \"TheMovieDb\",\n          \"The Open Movie Database\"\n        ],\n        \"MetadataFetcherOrder\": [\n          \"TheMovieDb\",\n          \"The Open Movie Database\"\n        ],\n        \"ImageFetchers\": [\n          \"TheMovieDb\",\n          \"The Open Movie Database\",\n          \"Embedded Image Extractor\",\n          \"Screen Grabber\"\n        ],\n        \"ImageFetcherOrder\": [\n          \"TheMovieDb\",\n          \"The Open Movie Database\",\n          \"Embedded Image Extractor\",\n          \"Screen Grabber\"\n        ]\n      }\n    ],\n    \"LocalMetadataReaderOrder\": [\n      \"Nfo\"\n    ],\n    \"SubtitleDownloadLanguages\": [],\n    \"DisabledSubtitleFetchers\": [],\n    \"SubtitleFetcherOrder\": [],\n    \"PathInfos\": [\n      {\n        \"Path\": \"/media/TV\"\n      }\n    ]\n  }\n}\n"
  },
  {
    "path": "Jellyfin.Plugin.MediaAnalyzer.Tests/e2e_tests/wrapper/main.go",
    "content": "package main\n\nimport (\n\t\"bufio\"\n\t\"bytes\"\n\t\"crypto/rand\"\n\t\"encoding/hex\"\n\t\"encoding/json\"\n\t\"flag\"\n\t\"fmt\"\n\t\"io\"\n\t\"net/http\"\n\t\"os\"\n\t\"path\"\n\t\"strings\"\n\t\"time\"\n)\n\n// IP address to use when connecting to local containers.\nvar containerAddress string\n\n// Path to compiled plugin DLL to install in local containers.\nvar pluginPath string\n\n// Randomly generated password used to setup container with.\nvar containerPassword string\n\nfunc flags() {\n\tflag.StringVar(&pluginPath, \"dll\", \"\", \"Path to plugin DLL to install in container images.\")\n\tflag.StringVar(&containerAddress, \"caddr\", \"\", \"IP address to use when connecting to local containers.\")\n\tflag.Parse()\n\n\t// Randomize the container's password\n\trawPassword := make([]byte, 32)\n\tif _, err := rand.Read(rawPassword); err != nil {\n\t\tpanic(err)\n\t}\n\n\tcontainerPassword = hex.EncodeToString(rawPassword)\n}\n\nfunc main() {\n\tflags()\n\n\tstart := time.Now()\n\n\tfmt.Printf(\"[+] Start time: %s\\n\", start)\n\n\t// Load list of servers\n\tfmt.Println(\"[+] Loading configuration\")\n\tconfig := loadConfiguration()\n\tfmt.Println()\n\n\t// Start Selenium by bringing up the compose file in detatched mode\n\tfmt.Println(\"[+] Starting Selenium\")\n\tRunProgram(\"docker-compose\", []string{\"up\", \"-d\"}, 10*time.Second)\n\n\t// If any error occurs, bring Selenium down before exiting\n\tdefer func() {\n\t\tfmt.Println(\"[+] Stopping Selenium\")\n\t\tRunProgram(\"docker-compose\", []string{\"down\"}, 15*time.Second)\n\t}()\n\n\t// Test all provided Jellyfin servers\n\tfor _, server := range config.Servers {\n\t\tif server.Skip {\n\t\t\tcontinue\n\t\t}\n\n\t\tvar configurationDirectory string\n\t\tvar apiKey string\n\t\tvar seleniumArgs []string\n\n\t\t// LSIO containers use some slighly different paths & permissions\n\t\tlsioImage := strings.Contains(server.Image, \"linuxserver\")\n\n\t\tfmt.Println()\n\t\tfmt.Printf(\"[+] Testing %s\\n\", server.Comment)\n\n\t\tif server.Docker {\n\t\t\tvar err error\n\n\t\t\t// Setup a temporary folder for the container's configuration\n\t\t\tconfigurationDirectory, err = os.MkdirTemp(\"/dev/shm\", \"jf-e2e-*\")\n\t\t\tif err != nil {\n\t\t\t\tpanic(err)\n\t\t\t}\n\n\t\t\t// Create a folder to install the plugin into\n\t\t\tpluginDirectory := path.Join(configurationDirectory, \"plugins\", \"intro-skipper\")\n\t\t\tif lsioImage {\n\t\t\t\tpluginDirectory = path.Join(configurationDirectory, \"data\", \"plugins\", \"intro-skipper\")\n\t\t\t}\n\n\t\t\tfmt.Println(\"  [+] Creating plugin directory\")\n\t\t\tif err := os.MkdirAll(pluginDirectory, 0700); err != nil {\n\t\t\t\tfmt.Printf(\"  [!] Failed to create plugin directory: %s\\n\", err)\n\t\t\t\tgoto cleanup\n\t\t\t}\n\n\t\t\t// If this is an LSIO container, adjust the permissions on the plugin directory\n\t\t\tif lsioImage {\n\t\t\t\tRunProgram(\n\t\t\t\t\t\"chown\",\n\t\t\t\t\t[]string{\n\t\t\t\t\t\t\"911:911\",\n\t\t\t\t\t\t\"-R\",\n\t\t\t\t\t\tpath.Join(configurationDirectory, \"data\", \"plugins\")},\n\t\t\t\t\t2*time.Second)\n\t\t\t}\n\n\t\t\t// Install the plugin\n\t\t\tfmt.Printf(\"  [+] Copying plugin %s to %s\\n\", pluginPath, pluginDirectory)\n\t\t\tRunProgram(\"cp\", []string{pluginPath, pluginDirectory}, 2*time.Second)\n\t\t\tfmt.Println()\n\n\t\t\t/* Start the container with the following settings:\n\t\t\t *    Name:  jf-e2e\n\t\t\t *    Port:  8097\n\t\t\t *    Media: Mounted to /media, read only\n\t\t\t */\n\t\t\tcontainerArgs := []string{\"run\", \"--name\", \"jf-e2e\", \"--rm\", \"-p\", \"8097:8096\",\n\t\t\t\t\"-v\", fmt.Sprintf(\"%s:%s:rw\", configurationDirectory, \"/config\"),\n\t\t\t\t\"-v\", fmt.Sprintf(\"%s:%s:ro\", config.Common.Library, \"/media\"),\n\t\t\t\tserver.Image}\n\n\t\t\tfmt.Printf(\"  [+] Starting container %s\\n\", server.Image)\n\t\t\tgo RunProgram(\"docker\", containerArgs, 60*time.Second)\n\n\t\t\t// Wait for the container to fully start\n\t\t\twaitForServerStartup(server.Address)\n\t\t\tfmt.Println()\n\n\t\t\tfmt.Println(\"  [+] Setting up container\")\n\n\t\t\t// Set up the container\n\t\t\tSetupServer(server.Address, containerPassword)\n\n\t\t\t// Restart the container and wait for it to come back up\n\t\t\tRunProgram(\"docker\", []string{\"restart\", \"jf-e2e\"}, 10*time.Second)\n\t\t\ttime.Sleep(time.Second)\n\t\t\twaitForServerStartup(server.Address)\n\t\t\tfmt.Println()\n\t\t} else {\n\t\t\tfmt.Println(\"[+] Remote instance, assuming plugin is already installed\")\n\t\t}\n\n\t\t// Get an API key\n\t\tapiKey = login(server)\n\n\t\t// Rescan the library if this is a server that we just setup\n\t\tif server.Docker {\n\t\t\tfmt.Println(\"  [+] Rescanning library\")\n\n\t\t\tsendRequest(\n\t\t\t\tserver.Address+\"/ScheduledTasks/Running/7738148ffcd07979c7ceb148e06b3aed?api_key=\"+apiKey,\n\t\t\t\t\"POST\",\n\t\t\t\t\"\")\n\n\t\t\t// TODO: poll for task completion\n\t\t\ttime.Sleep(10 * time.Second)\n\n\t\t\tfmt.Println()\n\t\t}\n\n\t\t// Analyze episodes and save report\n\t\tfmt.Println(\"  [+] Analyzing episodes\")\n\t\tfmt.Print(\"\\033[37;1m\") // change the color of the verifier's text\n\t\tRunProgram(\n\t\t\t\"./verifier/verifier\",\n\t\t\t[]string{\n\t\t\t\t\"-address\", server.Address,\n\t\t\t\t\"-key\", apiKey, \"-o\",\n\t\t\t\tfmt.Sprintf(\"reports/%s-%d.json\", server.Comment, start.Unix())},\n\t\t\t5*time.Minute)\n\t\tfmt.Print(\"\\033[39;0m\") // reset terminal text color\n\n\t\t// Pause for any manual tests\n\t\tif server.ManualTests {\n\t\t\tfmt.Println(\"  [!] Pausing for manual tests\")\n\t\t\treader := bufio.NewReader(os.Stdin)\n\t\t\treader.ReadString('\\n')\n\t\t}\n\n\t\t// Setup base Selenium arguments\n\t\tseleniumArgs = []string{\n\t\t\t\"-u\", // force stdout to be unbuffered\n\t\t\t\"main.py\",\n\t\t\t\"-host\", server.Address,\n\t\t\t\"-user\", server.Username,\n\t\t\t\"-pass\", server.Password,\n\t\t\t\"-name\", config.Common.Episode}\n\n\t\t// Append all requested Selenium tests\n\t\tseleniumArgs = append(seleniumArgs, \"--tests\")\n\t\tseleniumArgs = append(seleniumArgs, server.Tests...)\n\n\t\t// Append all requested browsers\n\t\tseleniumArgs = append(seleniumArgs, \"--browsers\")\n\t\tseleniumArgs = append(seleniumArgs, server.Browsers...)\n\n\t\t// Run Selenium\n\t\tos.Chdir(\"selenium\")\n\t\tRunProgram(\"python3\", seleniumArgs, time.Minute)\n\t\tos.Chdir(\"..\")\n\n\tcleanup:\n\t\tif server.Docker {\n\t\t\t// Stop the container\n\t\t\tfmt.Println(\"  [+] Stopping and removing container\")\n\t\t\tRunProgram(\"docker\", []string{\"stop\", \"jf-e2e\"}, 10*time.Second)\n\n\t\t\t// Cleanup the container's configuration\n\t\t\tfmt.Printf(\"  [+] Deleting %s\\n\", configurationDirectory)\n\n\t\t\tif err := os.RemoveAll(configurationDirectory); err != nil {\n\t\t\t\tpanic(err)\n\t\t\t}\n\t\t}\n\t}\n}\n\n// Login to the specified Jellyfin server and return an API key\nfunc login(server Server) string {\n\ttype AuthenticateUserByName struct {\n\t\tAccessToken string\n\t}\n\n\tfmt.Println(\"  [+] Sending authentication request\")\n\n\t// Create request body\n\trawBody := fmt.Sprintf(`{\"Username\":\"%s\",\"Pw\":\"%s\"}`, server.Username, server.Password)\n\tbody := bytes.NewBufferString(rawBody)\n\n\t// Create the request\n\treq, err := http.NewRequest(\n\t\t\"POST\",\n\t\tfmt.Sprintf(\"%s/Users/AuthenticateByName\", server.Address),\n\t\tbody)\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\t// Set headers\n\treq.Header.Set(\"Content-Type\", \"application/json\")\n\treq.Header.Set(\n\t\t\"X-Emby-Authorization\",\n\t\t`MediaBrowser Client=\"JF E2E Tests\", Version=\"0.0.1\", DeviceId=\"E2E\", Device=\"E2E\"`)\n\n\t// Authenticate\n\tres, err := http.DefaultClient.Do(req)\n\tif err != nil {\n\t\tpanic(err)\n\t} else if res.StatusCode != http.StatusOK {\n\t\tpanic(fmt.Sprintf(\"authentication returned code %d\", res.StatusCode))\n\t}\n\n\tdefer res.Body.Close()\n\n\t// Read body\n\tfullBody, err := io.ReadAll(res.Body)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\t// Unmarshal body and return token\n\tvar token AuthenticateUserByName\n\tif err := json.Unmarshal(fullBody, &token); err != nil {\n\t\tpanic(err)\n\t}\n\n\treturn token.AccessToken\n}\n\n// Wait up to ten seconds for the provided Jellyfin server to fully startup\nfunc waitForServerStartup(address string) {\n\tattempts := 10\n\tfmt.Println(\"  [+] Waiting for server to finish starting\")\n\n\tfor {\n\t\t// Sleep in between requests\n\t\ttime.Sleep(time.Second)\n\n\t\t// Ping the /System/Info/Public endpoint\n\t\tres, err := http.Get(fmt.Sprintf(\"%s/System/Info/Public\", address))\n\n\t\t// If the server didn't return 200 OK, loop\n\t\tif err != nil || res.StatusCode != http.StatusOK {\n\t\t\tif attempts--; attempts <= 0 {\n\t\t\t\tpanic(\"server is taking too long to startup\")\n\t\t\t}\n\n\t\t\tcontinue\n\t\t}\n\n\t\t// Assume startup has finished, break\n\t\tbreak\n\t}\n}\n\n// Read configuration from config.json\nfunc loadConfiguration() Configuration {\n\tvar config Configuration\n\n\t// Load the contents of the configuration file\n\traw, err := os.ReadFile(\"config.json\")\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\t// Unmarshal\n\tif err := json.Unmarshal(raw, &config); err != nil {\n\t\tpanic(err)\n\t}\n\n\t// Print debugging info\n\tfmt.Printf(\"Library:  %s\\n\", config.Common.Library)\n\tfmt.Printf(\"Episode:  \\\"%s\\\"\\n\", config.Common.Episode)\n\tfmt.Printf(\"Password: %s\\n\", containerPassword)\n\tfmt.Println()\n\n\t// Check the validity of all entries\n\tfor i, server := range config.Servers {\n\t\t// If this is an entry for a local container, ensure the server address is correct\n\t\tif server.Image != \"\" {\n\t\t\t// Ensure that values were provided for the host's IP address, base configuration directory,\n\t\t\t// and a path to the compiled plugin DLL to install.\n\t\t\tif containerAddress == \"\" {\n\t\t\t\tpanic(\"The -caddr argument is required.\")\n\t\t\t}\n\n\t\t\tif pluginPath == \"\" {\n\t\t\t\tpanic(\"The -dll argument is required.\")\n\t\t\t}\n\n\t\t\tserver.Username = \"admin\"\n\t\t\tserver.Password = containerPassword\n\t\t\tserver.Address = fmt.Sprintf(\"http://%s:8097\", containerAddress)\n\t\t\tserver.Docker = true\n\t\t}\n\n\t\t// If no browsers were specified, default to Chrome (for speed)\n\t\tif len(server.Browsers) == 0 {\n\t\t\tserver.Browsers = []string{\"chrome\"}\n\t\t}\n\n\t\t// If no tests were specified, only test that the plugin settings page works\n\t\tif len(server.Tests) == 0 {\n\t\t\tserver.Tests = []string{\"settings\"}\n\t\t}\n\n\t\t// Verify that an address was provided\n\t\tif len(server.Address) == 0 {\n\t\t\tpanic(\"Server address is required\")\n\t\t}\n\n\t\tfmt.Printf(\"===== Server: %s =====\\n\", server.Comment)\n\n\t\tif server.Skip {\n\t\t\tfmt.Println(\"Skip:     true\")\n\t\t}\n\n\t\tfmt.Printf(\"Docker:   %t\\n\", server.Docker)\n\t\tif server.Docker {\n\t\t\tfmt.Printf(\"Image:    %s\\n\", server.Image)\n\t\t}\n\n\t\tfmt.Printf(\"Address:  %s\\n\", server.Address)\n\t\tfmt.Printf(\"Browsers: %v\\n\", server.Browsers)\n\t\tfmt.Printf(\"Tests:    %v\\n\", server.Tests)\n\t\tfmt.Println()\n\n\t\tconfig.Servers[i] = server\n\t}\n\n\tfmt.Println(\"=================\")\n\n\treturn config\n}\n"
  },
  {
    "path": "Jellyfin.Plugin.MediaAnalyzer.Tests/e2e_tests/wrapper/setup.go",
    "content": "package main\n\nimport (\n\t\"bytes\"\n\t_ \"embed\"\n\t\"fmt\"\n\t\"net/http\"\n)\n\n//go:embed library.json\nvar librarySetupPayload string\n\nfunc SetupServer(server, password string) {\n\tmakeUrl := func(u string) string {\n\t\treturn fmt.Sprintf(\"%s/%s\", server, u)\n\t}\n\n\t// Set the server language to English\n\tsendRequest(\n\t\tmakeUrl(\"Startup/Configuration\"),\n\t\t\"POST\",\n\t\t`{\"UICulture\":\"en-US\",\"MetadataCountryCode\":\"US\",\"PreferredMetadataLanguage\":\"en\"}`)\n\n\t// Get the first user\n\tsendRequest(makeUrl(\"Startup/User\"), \"GET\", \"\")\n\n\t// Create the first user\n\tsendRequest(\n\t\tmakeUrl(\"Startup/User\"),\n\t\t\"POST\",\n\t\tfmt.Sprintf(`{\"Name\":\"admin\",\"Password\":\"%s\"}`, password))\n\n\t// Create a TV library from the media at /media/TV.\n\tsendRequest(\n\t\tmakeUrl(\"Library/VirtualFolders?collectionType=tvshows&refreshLibrary=false&name=Shows\"),\n\t\t\"POST\",\n\t\tlibrarySetupPayload)\n\n\t// Setup remote access\n\tsendRequest(\n\t\tmakeUrl(\"Startup/RemoteAccess\"),\n\t\t\"POST\",\n\t\t`{\"EnableRemoteAccess\":true,\"EnableAutomaticPortMapping\":false}`)\n\n\t// Mark the wizard as complete\n\tsendRequest(\n\t\tmakeUrl(\"Startup/Complete\"),\n\t\t\"POST\",\n\t\t``)\n}\n\nfunc sendRequest(url string, method string, body string) {\n\t// Create the request\n\treq, err := http.NewRequest(method, url, bytes.NewBuffer([]byte(body)))\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\t// Set required headers\n\treq.Header.Set(\"Content-Type\", \"application/json\")\n\treq.Header.Set(\n\t\t\"X-Emby-Authorization\",\n\t\t`MediaBrowser Client=\"JF E2E Tests\", Version=\"0.0.1\", DeviceId=\"E2E\", Device=\"E2E\"`)\n\n\t// Send it\n\tfmt.Printf(\"  [+] %s %s\", method, url)\n\tres, err := http.DefaultClient.Do(req)\n\n\tif err != nil {\n\t\tfmt.Println()\n\t\tpanic(err)\n\t}\n\n\tfmt.Printf(\" %d\\n\", res.StatusCode)\n\n\tif res.StatusCode != http.StatusNoContent && res.StatusCode != http.StatusOK {\n\t\tpanic(\"invalid status code received during setup\")\n\t}\n}\n"
  },
  {
    "path": "Jellyfin.Plugin.MediaAnalyzer.Tests/e2e_tests/wrapper/structs.go",
    "content": "package main\n\ntype Configuration struct {\n\tCommon  Common   `json:\"common\"`\n\tServers []Server `json:\"servers\"`\n}\n\ntype Common struct {\n\tLibrary string `json:\"library\"`\n\tEpisode string `json:\"episode\"`\n}\n\ntype Server struct {\n\tSkip        bool     `json:\"skip\"`\n\tComment     string   `json:\"comment\"`\n\tAddress     string   `json:\"address\"`\n\tImage       string   `json:\"image\"`\n\tUsername    string   `json:\"username\"`\n\tPassword    string   `json:\"password\"`\n\tBrowsers    []string `json:\"browsers\"`\n\tTests       []string `json:\"tests\"`\n\tManualTests bool     `json:\"manual_tests\"`\n\n\t// These properties are set at runtime\n\tDocker bool `json:\"-\"`\n}\n"
  },
  {
    "path": "Jellyfin.Plugin.MediaAnalyzer.sln",
    "content": "﻿Microsoft Visual Studio Solution File, Format Version 12.00\n#\nProject(\"{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}\") = \"Jellyfin.Plugin.MediaAnalyzer\", \"Jellyfin.Plugin.MediaAnalyzer\\Jellyfin.Plugin.MediaAnalyzer.csproj\", \"{D921B930-CF91-406F-ACBC-08914DCD0D34}\"\nEndProject\nProject(\"{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}\") = \"Jellyfin.Plugin.MediaAnalyzer.Tests\", \"Jellyfin.Plugin.MediaAnalyzer.Tests\\Jellyfin.Plugin.MediaAnalyzer.Tests.csproj\", \"{9E30DA42-983E-46E0-A3BF-A2BA56FE9718}\"\nEndProject\nGlobal\n\tGlobalSection(SolutionConfigurationPlatforms) = preSolution\n\t\tDebug|Any CPU = Debug|Any CPU\n\t\tRelease|Any CPU = Release|Any CPU\n\tEndGlobalSection\n\tGlobalSection(ProjectConfigurationPlatforms) = postSolution\n\t\t{D921B930-CF91-406F-ACBC-08914DCD0D34}.Debug|Any CPU.ActiveCfg = Debug|Any CPU\n\t\t{D921B930-CF91-406F-ACBC-08914DCD0D34}.Debug|Any CPU.Build.0 = Debug|Any CPU\n\t\t{D921B930-CF91-406F-ACBC-08914DCD0D34}.Release|Any CPU.ActiveCfg = Release|Any CPU\n\t\t{D921B930-CF91-406F-ACBC-08914DCD0D34}.Release|Any CPU.Build.0 = Release|Any CPU\n\t\t{9E30DA42-983E-46E0-A3BF-A2BA56FE9718}.Debug|Any CPU.ActiveCfg = Debug|Any CPU\n\t\t{9E30DA42-983E-46E0-A3BF-A2BA56FE9718}.Debug|Any CPU.Build.0 = Debug|Any CPU\n\t\t{9E30DA42-983E-46E0-A3BF-A2BA56FE9718}.Release|Any CPU.ActiveCfg = Release|Any CPU\n\t\t{9E30DA42-983E-46E0-A3BF-A2BA56FE9718}.Release|Any CPU.Build.0 = Release|Any CPU\n\tEndGlobalSection\nEndGlobal\n"
  },
  {
    "path": "LICENSE",
    "content": "                    GNU GENERAL PUBLIC LICENSE\n                       Version 3, 29 June 2007\n\n Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>\n Everyone is permitted to copy and distribute verbatim copies\n of this license document, but changing it is not allowed.\n\n                            Preamble\n\n  The GNU General Public License is a free, copyleft license for\nsoftware and other kinds of works.\n\n  The licenses for most software and other practical works are designed\nto take away your freedom to share and change the works.  By contrast,\nthe GNU General Public License is intended to guarantee your freedom to\nshare and change all versions of a program--to make sure it remains free\nsoftware for all its users.  We, the Free Software Foundation, use the\nGNU General Public License for most of our software; it applies also to\nany other work released this way by its authors.  You can apply it to\nyour programs, too.\n\n  When we speak of free software, we are referring to freedom, not\nprice.  Our General Public Licenses are designed to make sure that you\nhave the freedom to distribute copies of free software (and charge for\nthem if you wish), that you receive source code or can get it if you\nwant it, that you can change the software or use pieces of it in new\nfree programs, and that you know you can do these things.\n\n  To protect your rights, we need to prevent others from denying you\nthese rights or asking you to surrender the rights.  Therefore, you have\ncertain responsibilities if you distribute copies of the software, or if\nyou modify it: responsibilities to respect the freedom of others.\n\n  For example, if you distribute copies of such a program, whether\ngratis or for a fee, you must pass on to the recipients the same\nfreedoms that you received.  You must make sure that they, too, receive\nor can get the source code.  And you must show them these terms so they\nknow their rights.\n\n  Developers that use the GNU GPL protect your rights with two steps:\n(1) assert copyright on the software, and (2) offer you this License\ngiving you legal permission to copy, distribute and/or modify it.\n\n  For the developers' and authors' protection, the GPL clearly explains\nthat there is no warranty for this free software.  For both users' and\nauthors' sake, the GPL requires that modified versions be marked as\nchanged, so that their problems will not be attributed erroneously to\nauthors of previous versions.\n\n  Some devices are designed to deny users access to install or run\nmodified versions of the software inside them, although the manufacturer\ncan do so.  This is fundamentally incompatible with the aim of\nprotecting users' freedom to change the software.  The systematic\npattern of such abuse occurs in the area of products for individuals to\nuse, which is precisely where it is most unacceptable.  Therefore, we\nhave designed this version of the GPL to prohibit the practice for those\nproducts.  If such problems arise substantially in other domains, we\nstand ready to extend this provision to those domains in future versions\nof the GPL, as needed to protect the freedom of users.\n\n  Finally, every program is threatened constantly by software patents.\nStates should not allow patents to restrict development and use of\nsoftware on general-purpose computers, but in those that do, we wish to\navoid the special danger that patents applied to a free program could\nmake it effectively proprietary.  To prevent this, the GPL assures that\npatents cannot be used to render the program non-free.\n\n  The precise terms and conditions for copying, distribution and\nmodification follow.\n\n                       TERMS AND CONDITIONS\n\n  0. Definitions.\n\n  \"This License\" refers to version 3 of the GNU General Public License.\n\n  \"Copyright\" also means copyright-like laws that apply to other kinds of\nworks, such as semiconductor masks.\n\n  \"The Program\" refers to any copyrightable work licensed under this\nLicense.  Each licensee is addressed as \"you\".  \"Licensees\" and\n\"recipients\" may be individuals or organizations.\n\n  To \"modify\" a work means to copy from or adapt all or part of the work\nin a fashion requiring copyright permission, other than the making of an\nexact copy.  The resulting work is called a \"modified version\" of the\nearlier work or a work \"based on\" the earlier work.\n\n  A \"covered work\" means either the unmodified Program or a work based\non the Program.\n\n  To \"propagate\" a work means to do anything with it that, without\npermission, would make you directly or secondarily liable for\ninfringement under applicable copyright law, except executing it on a\ncomputer or modifying a private copy.  Propagation includes copying,\ndistribution (with or without modification), making available to the\npublic, and in some countries other activities as well.\n\n  To \"convey\" a work means any kind of propagation that enables other\nparties to make or receive copies.  Mere interaction with a user through\na computer network, with no transfer of a copy, is not conveying.\n\n  An interactive user interface displays \"Appropriate Legal Notices\"\nto the extent that it includes a convenient and prominently visible\nfeature that (1) displays an appropriate copyright notice, and (2)\ntells the user that there is no warranty for the work (except to the\nextent that warranties are provided), that licensees may convey the\nwork under this License, and how to view a copy of this License.  If\nthe interface presents a list of user commands or options, such as a\nmenu, a prominent item in the list meets this criterion.\n\n  1. Source Code.\n\n  The \"source code\" for a work means the preferred form of the work\nfor making modifications to it.  \"Object code\" means any non-source\nform of a work.\n\n  A \"Standard Interface\" means an interface that either is an official\nstandard defined by a recognized standards body, or, in the case of\ninterfaces specified for a particular programming language, one that\nis widely used among developers working in that language.\n\n  The \"System Libraries\" of an executable work include anything, other\nthan the work as a whole, that (a) is included in the normal form of\npackaging a Major Component, but which is not part of that Major\nComponent, and (b) serves only to enable use of the work with that\nMajor Component, or to implement a Standard Interface for which an\nimplementation is available to the public in source code form.  A\n\"Major Component\", in this context, means a major essential component\n(kernel, window system, and so on) of the specific operating system\n(if any) on which the executable work runs, or a compiler used to\nproduce the work, or an object code interpreter used to run it.\n\n  The \"Corresponding Source\" for a work in object code form means all\nthe source code needed to generate, install, and (for an executable\nwork) run the object code and to modify the work, including scripts to\ncontrol those activities.  However, it does not include the work's\nSystem Libraries, or general-purpose tools or generally available free\nprograms which are used unmodified in performing those activities but\nwhich are not part of the work.  For example, Corresponding Source\nincludes interface definition files associated with source files for\nthe work, and the source code for shared libraries and dynamically\nlinked subprograms that the work is specifically designed to require,\nsuch as by intimate data communication or control flow between those\nsubprograms and other parts of the work.\n\n  The Corresponding Source need not include anything that users\ncan regenerate automatically from other parts of the Corresponding\nSource.\n\n  The Corresponding Source for a work in source code form is that\nsame work.\n\n  2. Basic Permissions.\n\n  All rights granted under this License are granted for the term of\ncopyright on the Program, and are irrevocable provided the stated\nconditions are met.  This License explicitly affirms your unlimited\npermission to run the unmodified Program.  The output from running a\ncovered work is covered by this License only if the output, given its\ncontent, constitutes a covered work.  This License acknowledges your\nrights of fair use or other equivalent, as provided by copyright law.\n\n  You may make, run and propagate covered works that you do not\nconvey, without conditions so long as your license otherwise remains\nin force.  You may convey covered works to others for the sole purpose\nof having them make modifications exclusively for you, or provide you\nwith facilities for running those works, provided that you comply with\nthe terms of this License in conveying all material for which you do\nnot control copyright.  Those thus making or running the covered works\nfor you must do so exclusively on your behalf, under your direction\nand control, on terms that prohibit them from making any copies of\nyour copyrighted material outside their relationship with you.\n\n  Conveying under any other circumstances is permitted solely under\nthe conditions stated below.  Sublicensing is not allowed; section 10\nmakes it unnecessary.\n\n  3. Protecting Users' Legal Rights From Anti-Circumvention Law.\n\n  No covered work shall be deemed part of an effective technological\nmeasure under any applicable law fulfilling obligations under article\n11 of the WIPO copyright treaty adopted on 20 December 1996, or\nsimilar laws prohibiting or restricting circumvention of such\nmeasures.\n\n  When you convey a covered work, you waive any legal power to forbid\ncircumvention of technological measures to the extent such circumvention\nis effected by exercising rights under this License with respect to\nthe covered work, and you disclaim any intention to limit operation or\nmodification of the work as a means of enforcing, against the work's\nusers, your or third parties' legal rights to forbid circumvention of\ntechnological measures.\n\n  4. Conveying Verbatim Copies.\n\n  You may convey verbatim copies of the Program's source code as you\nreceive it, in any medium, provided that you conspicuously and\nappropriately publish on each copy an appropriate copyright notice;\nkeep intact all notices stating that this License and any\nnon-permissive terms added in accord with section 7 apply to the code;\nkeep intact all notices of the absence of any warranty; and give all\nrecipients a copy of this License along with the Program.\n\n  You may charge any price or no price for each copy that you convey,\nand you may offer support or warranty protection for a fee.\n\n  5. Conveying Modified Source Versions.\n\n  You may convey a work based on the Program, or the modifications to\nproduce it from the Program, in the form of source code under the\nterms of section 4, provided that you also meet all of these conditions:\n\n    a) The work must carry prominent notices stating that you modified\n    it, and giving a relevant date.\n\n    b) The work must carry prominent notices stating that it is\n    released under this License and any conditions added under section\n    7.  This requirement modifies the requirement in section 4 to\n    \"keep intact all notices\".\n\n    c) You must license the entire work, as a whole, under this\n    License to anyone who comes into possession of a copy.  This\n    License will therefore apply, along with any applicable section 7\n    additional terms, to the whole of the work, and all its parts,\n    regardless of how they are packaged.  This License gives no\n    permission to license the work in any other way, but it does not\n    invalidate such permission if you have separately received it.\n\n    d) If the work has interactive user interfaces, each must display\n    Appropriate Legal Notices; however, if the Program has interactive\n    interfaces that do not display Appropriate Legal Notices, your\n    work need not make them do so.\n\n  A compilation of a covered work with other separate and independent\nworks, which are not by their nature extensions of the covered work,\nand which are not combined with it such as to form a larger program,\nin or on a volume of a storage or distribution medium, is called an\n\"aggregate\" if the compilation and its resulting copyright are not\nused to limit the access or legal rights of the compilation's users\nbeyond what the individual works permit.  Inclusion of a covered work\nin an aggregate does not cause this License to apply to the other\nparts of the aggregate.\n\n  6. Conveying Non-Source Forms.\n\n  You may convey a covered work in object code form under the terms\nof sections 4 and 5, provided that you also convey the\nmachine-readable Corresponding Source under the terms of this License,\nin one of these ways:\n\n    a) Convey the object code in, or embodied in, a physical product\n    (including a physical distribution medium), accompanied by the\n    Corresponding Source fixed on a durable physical medium\n    customarily used for software interchange.\n\n    b) Convey the object code in, or embodied in, a physical product\n    (including a physical distribution medium), accompanied by a\n    written offer, valid for at least three years and valid for as\n    long as you offer spare parts or customer support for that product\n    model, to give anyone who possesses the object code either (1) a\n    copy of the Corresponding Source for all the software in the\n    product that is covered by this License, on a durable physical\n    medium customarily used for software interchange, for a price no\n    more than your reasonable cost of physically performing this\n    conveying of source, or (2) access to copy the\n    Corresponding Source from a network server at no charge.\n\n    c) Convey individual copies of the object code with a copy of the\n    written offer to provide the Corresponding Source.  This\n    alternative is allowed only occasionally and noncommercially, and\n    only if you received the object code with such an offer, in accord\n    with subsection 6b.\n\n    d) Convey the object code by offering access from a designated\n    place (gratis or for a charge), and offer equivalent access to the\n    Corresponding Source in the same way through the same place at no\n    further charge.  You need not require recipients to copy the\n    Corresponding Source along with the object code.  If the place to\n    copy the object code is a network server, the Corresponding Source\n    may be on a different server (operated by you or a third party)\n    that supports equivalent copying facilities, provided you maintain\n    clear directions next to the object code saying where to find the\n    Corresponding Source.  Regardless of what server hosts the\n    Corresponding Source, you remain obligated to ensure that it is\n    available for as long as needed to satisfy these requirements.\n\n    e) Convey the object code using peer-to-peer transmission, provided\n    you inform other peers where the object code and Corresponding\n    Source of the work are being offered to the general public at no\n    charge under subsection 6d.\n\n  A separable portion of the object code, whose source code is excluded\nfrom the Corresponding Source as a System Library, need not be\nincluded in conveying the object code work.\n\n  A \"User Product\" is either (1) a \"consumer product\", which means any\ntangible personal property which is normally used for personal, family,\nor household purposes, or (2) anything designed or sold for incorporation\ninto a dwelling.  In determining whether a product is a consumer product,\ndoubtful cases shall be resolved in favor of coverage.  For a particular\nproduct received by a particular user, \"normally used\" refers to a\ntypical or common use of that class of product, regardless of the status\nof the particular user or of the way in which the particular user\nactually uses, or expects or is expected to use, the product.  A product\nis a consumer product regardless of whether the product has substantial\ncommercial, industrial or non-consumer uses, unless such uses represent\nthe only significant mode of use of the product.\n\n  \"Installation Information\" for a User Product means any methods,\nprocedures, authorization keys, or other information required to install\nand execute modified versions of a covered work in that User Product from\na modified version of its Corresponding Source.  The information must\nsuffice to ensure that the continued functioning of the modified object\ncode is in no case prevented or interfered with solely because\nmodification has been made.\n\n  If you convey an object code work under this section in, or with, or\nspecifically for use in, a User Product, and the conveying occurs as\npart of a transaction in which the right of possession and use of the\nUser Product is transferred to the recipient in perpetuity or for a\nfixed term (regardless of how the transaction is characterized), the\nCorresponding Source conveyed under this section must be accompanied\nby the Installation Information.  But this requirement does not apply\nif neither you nor any third party retains the ability to install\nmodified object code on the User Product (for example, the work has\nbeen installed in ROM).\n\n  The requirement to provide Installation Information does not include a\nrequirement to continue to provide support service, warranty, or updates\nfor a work that has been modified or installed by the recipient, or for\nthe User Product in which it has been modified or installed.  Access to a\nnetwork may be denied when the modification itself materially and\nadversely affects the operation of the network or violates the rules and\nprotocols for communication across the network.\n\n  Corresponding Source conveyed, and Installation Information provided,\nin accord with this section must be in a format that is publicly\ndocumented (and with an implementation available to the public in\nsource code form), and must require no special password or key for\nunpacking, reading or copying.\n\n  7. Additional Terms.\n\n  \"Additional permissions\" are terms that supplement the terms of this\nLicense by making exceptions from one or more of its conditions.\nAdditional permissions that are applicable to the entire Program shall\nbe treated as though they were included in this License, to the extent\nthat they are valid under applicable law.  If additional permissions\napply only to part of the Program, that part may be used separately\nunder those permissions, but the entire Program remains governed by\nthis License without regard to the additional permissions.\n\n  When you convey a copy of a covered work, you may at your option\nremove any additional permissions from that copy, or from any part of\nit.  (Additional permissions may be written to require their own\nremoval in certain cases when you modify the work.)  You may place\nadditional permissions on material, added by you to a covered work,\nfor which you have or can give appropriate copyright permission.\n\n  Notwithstanding any other provision of this License, for material you\nadd to a covered work, you may (if authorized by the copyright holders of\nthat material) supplement the terms of this License with terms:\n\n    a) Disclaiming warranty or limiting liability differently from the\n    terms of sections 15 and 16 of this License; or\n\n    b) Requiring preservation of specified reasonable legal notices or\n    author attributions in that material or in the Appropriate Legal\n    Notices displayed by works containing it; or\n\n    c) Prohibiting misrepresentation of the origin of that material, or\n    requiring that modified versions of such material be marked in\n    reasonable ways as different from the original version; or\n\n    d) Limiting the use for publicity purposes of names of licensors or\n    authors of the material; or\n\n    e) Declining to grant rights under trademark law for use of some\n    trade names, trademarks, or service marks; or\n\n    f) Requiring indemnification of licensors and authors of that\n    material by anyone who conveys the material (or modified versions of\n    it) with contractual assumptions of liability to the recipient, for\n    any liability that these contractual assumptions directly impose on\n    those licensors and authors.\n\n  All other non-permissive additional terms are considered \"further\nrestrictions\" within the meaning of section 10.  If the Program as you\nreceived it, or any part of it, contains a notice stating that it is\ngoverned by this License along with a term that is a further\nrestriction, you may remove that term.  If a license document contains\na further restriction but permits relicensing or conveying under this\nLicense, you may add to a covered work material governed by the terms\nof that license document, provided that the further restriction does\nnot survive such relicensing or conveying.\n\n  If you add terms to a covered work in accord with this section, you\nmust place, in the relevant source files, a statement of the\nadditional terms that apply to those files, or a notice indicating\nwhere to find the applicable terms.\n\n  Additional terms, permissive or non-permissive, may be stated in the\nform of a separately written license, or stated as exceptions;\nthe above requirements apply either way.\n\n  8. Termination.\n\n  You may not propagate or modify a covered work except as expressly\nprovided under this License.  Any attempt otherwise to propagate or\nmodify it is void, and will automatically terminate your rights under\nthis License (including any patent licenses granted under the third\nparagraph of section 11).\n\n  However, if you cease all violation of this License, then your\nlicense from a particular copyright holder is reinstated (a)\nprovisionally, unless and until the copyright holder explicitly and\nfinally terminates your license, and (b) permanently, if the copyright\nholder fails to notify you of the violation by some reasonable means\nprior to 60 days after the cessation.\n\n  Moreover, your license from a particular copyright holder is\nreinstated permanently if the copyright holder notifies you of the\nviolation by some reasonable means, this is the first time you have\nreceived notice of violation of this License (for any work) from that\ncopyright holder, and you cure the violation prior to 30 days after\nyour receipt of the notice.\n\n  Termination of your rights under this section does not terminate the\nlicenses of parties who have received copies or rights from you under\nthis License.  If your rights have been terminated and not permanently\nreinstated, you do not qualify to receive new licenses for the same\nmaterial under section 10.\n\n  9. Acceptance Not Required for Having Copies.\n\n  You are not required to accept this License in order to receive or\nrun a copy of the Program.  Ancillary propagation of a covered work\noccurring solely as a consequence of using peer-to-peer transmission\nto receive a copy likewise does not require acceptance.  However,\nnothing other than this License grants you permission to propagate or\nmodify any covered work.  These actions infringe copyright if you do\nnot accept this License.  Therefore, by modifying or propagating a\ncovered work, you indicate your acceptance of this License to do so.\n\n  10. Automatic Licensing of Downstream Recipients.\n\n  Each time you convey a covered work, the recipient automatically\nreceives a license from the original licensors, to run, modify and\npropagate that work, subject to this License.  You are not responsible\nfor enforcing compliance by third parties with this License.\n\n  An \"entity transaction\" is a transaction transferring control of an\norganization, or substantially all assets of one, or subdividing an\norganization, or merging organizations.  If propagation of a covered\nwork results from an entity transaction, each party to that\ntransaction who receives a copy of the work also receives whatever\nlicenses to the work the party's predecessor in interest had or could\ngive under the previous paragraph, plus a right to possession of the\nCorresponding Source of the work from the predecessor in interest, if\nthe predecessor has it or can get it with reasonable efforts.\n\n  You may not impose any further restrictions on the exercise of the\nrights granted or affirmed under this License.  For example, you may\nnot impose a license fee, royalty, or other charge for exercise of\nrights granted under this License, and you may not initiate litigation\n(including a cross-claim or counterclaim in a lawsuit) alleging that\nany patent claim is infringed by making, using, selling, offering for\nsale, or importing the Program or any portion of it.\n\n  11. Patents.\n\n  A \"contributor\" is a copyright holder who authorizes use under this\nLicense of the Program or a work on which the Program is based.  The\nwork thus licensed is called the contributor's \"contributor version\".\n\n  A contributor's \"essential patent claims\" are all patent claims\nowned or controlled by the contributor, whether already acquired or\nhereafter acquired, that would be infringed by some manner, permitted\nby this License, of making, using, or selling its contributor version,\nbut do not include claims that would be infringed only as a\nconsequence of further modification of the contributor version.  For\npurposes of this definition, \"control\" includes the right to grant\npatent sublicenses in a manner consistent with the requirements of\nthis License.\n\n  Each contributor grants you a non-exclusive, worldwide, royalty-free\npatent license under the contributor's essential patent claims, to\nmake, use, sell, offer for sale, import and otherwise run, modify and\npropagate the contents of its contributor version.\n\n  In the following three paragraphs, a \"patent license\" is any express\nagreement or commitment, however denominated, not to enforce a patent\n(such as an express permission to practice a patent or covenant not to\nsue for patent infringement).  To \"grant\" such a patent license to a\nparty means to make such an agreement or commitment not to enforce a\npatent against the party.\n\n  If you convey a covered work, knowingly relying on a patent license,\nand the Corresponding Source of the work is not available for anyone\nto copy, free of charge and under the terms of this License, through a\npublicly available network server or other readily accessible means,\nthen you must either (1) cause the Corresponding Source to be so\navailable, or (2) arrange to deprive yourself of the benefit of the\npatent license for this particular work, or (3) arrange, in a manner\nconsistent with the requirements of this License, to extend the patent\nlicense to downstream recipients.  \"Knowingly relying\" means you have\nactual knowledge that, but for the patent license, your conveying the\ncovered work in a country, or your recipient's use of the covered work\nin a country, would infringe one or more identifiable patents in that\ncountry that you have reason to believe are valid.\n\n  If, pursuant to or in connection with a single transaction or\narrangement, you convey, or propagate by procuring conveyance of, a\ncovered work, and grant a patent license to some of the parties\nreceiving the covered work authorizing them to use, propagate, modify\nor convey a specific copy of the covered work, then the patent license\nyou grant is automatically extended to all recipients of the covered\nwork and works based on it.\n\n  A patent license is \"discriminatory\" if it does not include within\nthe scope of its coverage, prohibits the exercise of, or is\nconditioned on the non-exercise of one or more of the rights that are\nspecifically granted under this License.  You may not convey a covered\nwork if you are a party to an arrangement with a third party that is\nin the business of distributing software, under which you make payment\nto the third party based on the extent of your activity of conveying\nthe work, and under which the third party grants, to any of the\nparties who would receive the covered work from you, a discriminatory\npatent license (a) in connection with copies of the covered work\nconveyed by you (or copies made from those copies), or (b) primarily\nfor and in connection with specific products or compilations that\ncontain the covered work, unless you entered into that arrangement,\nor that patent license was granted, prior to 28 March 2007.\n\n  Nothing in this License shall be construed as excluding or limiting\nany implied license or other defenses to infringement that may\notherwise be available to you under applicable patent law.\n\n  12. No Surrender of Others' Freedom.\n\n  If conditions are imposed on you (whether by court order, agreement or\notherwise) that contradict the conditions of this License, they do not\nexcuse you from the conditions of this License.  If you cannot convey a\ncovered work so as to satisfy simultaneously your obligations under this\nLicense and any other pertinent obligations, then as a consequence you may\nnot convey it at all.  For example, if you agree to terms that obligate you\nto collect a royalty for further conveying from those to whom you convey\nthe Program, the only way you could satisfy both those terms and this\nLicense would be to refrain entirely from conveying the Program.\n\n  13. Use with the GNU Affero General Public License.\n\n  Notwithstanding any other provision of this License, you have\npermission to link or combine any covered work with a work licensed\nunder version 3 of the GNU Affero General Public License into a single\ncombined work, and to convey the resulting work.  The terms of this\nLicense will continue to apply to the part which is the covered work,\nbut the special requirements of the GNU Affero General Public License,\nsection 13, concerning interaction through a network will apply to the\ncombination as such.\n\n  14. Revised Versions of this License.\n\n  The Free Software Foundation may publish revised and/or new versions of\nthe GNU General Public License from time to time.  Such new versions will\nbe similar in spirit to the present version, but may differ in detail to\naddress new problems or concerns.\n\n  Each version is given a distinguishing version number.  If the\nProgram specifies that a certain numbered version of the GNU General\nPublic License \"or any later version\" applies to it, you have the\noption of following the terms and conditions either of that numbered\nversion or of any later version published by the Free Software\nFoundation.  If the Program does not specify a version number of the\nGNU General Public License, you may choose any version ever published\nby the Free Software Foundation.\n\n  If the Program specifies that a proxy can decide which future\nversions of the GNU General Public License can be used, that proxy's\npublic statement of acceptance of a version permanently authorizes you\nto choose that version for the Program.\n\n  Later license versions may give you additional or different\npermissions.  However, no additional obligations are imposed on any\nauthor or copyright holder as a result of your choosing to follow a\nlater version.\n\n  15. Disclaimer of Warranty.\n\n  THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY\nAPPLICABLE LAW.  EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT\nHOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM \"AS IS\" WITHOUT WARRANTY\nOF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,\nTHE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR\nPURPOSE.  THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM\nIS WITH YOU.  SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF\nALL NECESSARY SERVICING, REPAIR OR CORRECTION.\n\n  16. Limitation of Liability.\n\n  IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING\nWILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS\nTHE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY\nGENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE\nUSE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF\nDATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD\nPARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),\nEVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF\nSUCH DAMAGES.\n\n  17. Interpretation of Sections 15 and 16.\n\n  If the disclaimer of warranty and limitation of liability provided\nabove cannot be given local legal effect according to their terms,\nreviewing courts shall apply local law that most closely approximates\nan absolute waiver of all civil liability in connection with the\nProgram, unless a warranty or assumption of liability accompanies a\ncopy of the Program in return for a fee.\n\n                     END OF TERMS AND CONDITIONS\n\n            How to Apply These Terms to Your New Programs\n\n  If you develop a new program, and you want it to be of the greatest\npossible use to the public, the best way to achieve this is to make it\nfree software which everyone can redistribute and change under these terms.\n\n  To do so, attach the following notices to the program.  It is safest\nto attach them to the start of each source file to most effectively\nstate the exclusion of warranty; and each file should have at least\nthe \"copyright\" line and a pointer to where the full notice is found.\n\n    <one line to give the program's name and a brief idea of what it does.>\n    Copyright (C) <year>  <name of author>\n\n    This program is free software: you can redistribute it and/or modify\n    it under the terms of the GNU General Public License as published by\n    the Free Software Foundation, either version 3 of the License, or\n    (at your option) any later version.\n\n    This program is distributed in the hope that it will be useful,\n    but WITHOUT ANY WARRANTY; without even the implied warranty of\n    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n    GNU General Public License for more details.\n\n    You should have received a copy of the GNU General Public License\n    along with this program.  If not, see <https://www.gnu.org/licenses/>.\n\nAlso add information on how to contact you by electronic and paper mail.\n\n  If the program does terminal interaction, make it output a short\nnotice like this when it starts in an interactive mode:\n\n    <program>  Copyright (C) <year>  <name of author>\n    This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.\n    This is free software, and you are welcome to redistribute it\n    under certain conditions; type `show c' for details.\n\nThe hypothetical commands `show w' and `show c' should show the appropriate\nparts of the General Public License.  Of course, your program's commands\nmight be different; for a GUI interface, you would use an \"about box\".\n\n  You should also get your employer (if you work as a programmer) or school,\nif any, to sign a \"copyright disclaimer\" for the program, if necessary.\nFor more information on this, and how to apply and follow the GNU GPL, see\n<https://www.gnu.org/licenses/>.\n\n  The GNU General Public License does not permit incorporating your program\ninto proprietary programs.  If your program is a subroutine library, you\nmay consider it more useful to permit linking proprietary applications with\nthe library.  If this is what you want to do, use the GNU Lesser General\nPublic License instead of this License.  But first, please read\n<https://www.gnu.org/licenses/why-not-lgpl.html>.\n"
  },
  {
    "path": "README.md",
    "content": "# Jellyfin Media Analyzer\n\n## Archived\n\n⚠️\nThis project is no longer in development. Please move on to [intro-skipper](https://github.com/intro-skipper/intro-skipper?tab=readme-ov-file#intro-skipper)\n⚠️\n\n## Features\n\nAnalyzes Movies and TV Shows to detect Intros and Outros. Uses the new official Jellyfin API.\n\n* Detect Intro segments in tv shows\n* Detect Outro segments in tv shows and movies\n* Multiple Detection Types\n  * Chapter Analyzer (Intro/Outro): Scan chapter names for trigger words like 'Intro' 'End'\n  * Chromaprint Analyzer (Intro/Outro - tv shows): Compare audio fingerprints of two media files and find matches\n  * BlackFrame Analyzer (Outro): Scan for continous mostly black content\n* [Jellyfin Segment Editor](https://github.com/endrl/segment-editor?tab=readme-ov-file#jellyfin-segment-editor) support\n\n## Requirements\n\n* Jellyfin 10.10\n\n## Installation instructions\n\n1. Add plugin repository to your server: `https://raw.githubusercontent.com/endrl/jellyfin-plugin-repo/master/manifest.json`\n2. Install the Media Analyzer plugin from the General section\n3. Restart Jellyfin\n4. Go to Dashboard -> Scheduled Tasks -> Analyze Media and click the play button\n5. There is no Task Timer configured, create one if you want to scan daily (by default it will scan after \"MediaLibrary scan\" and when new items are added. You can disable this behaviour in the settings)\n\n## Related projects\n\n- Jellyfin Plugin: [.EDL Creator](https://github.com/endrl/jellyfin-plugin-edl)\n- Tool: [Jellyfin Segment Editor](https://github.com/endrl/segment-editor)\n- Player: [Jellyfin Vue Fork](https://github.com/endrl/jellyfin-vue)\n\n## Current changes compared to ConfusedPolarBear\n\n- [x] Enable Credits detection for episodes and movies (black frame analyzer)\n- [x] No cache option (default: enabled) -> no disk space required\n- [x] Auto analyze after media scanning task ended\n- [x] Filter for tv show names and optional season/s\n- [x] No server side playback influence or frontend script injection (clean!)\n- [x] Move .edl file creation into another [plugin](<https://github.com/endrl/jellyfin-plugin-edl>)\n- [x] Move the extended plugin page for segment edits to a dedicated tool [Media Segment Editor](https://github.com/endrl/segment-editor)\n  - [ ] move additional meta support per plugin like \"get chromaprints of plugin x\"\n\n## Introduction requirements\n\nShow introductions will only be detected if they are:\n\n- Located within the first 30% of an episode, or the first 15 minutes, whichever is smaller\n- Between 15 seconds and 2 minutes long\n\nEnding credits will only be detected if they are shorter than 4 minutes.\n\nAll of these requirements can be customized as needed.\n\n### Debug Logging\n\nChange your logging.json file to output debug logs for `Jellyfin.Plugin.MediaAnalyzer`. Make sure to add a comma to the end of `\"System\": \"Warning\"`\n\n```jsonc\n{\n    \"Serilog\": {\n        \"MinimumLevel\": {\n            \"Default\": \"Information\",\n            \"Override\": {\n                \"Microsoft\": \"Warning\",\n                \"System\": \"Warning\",\n                \"Jellyfin.Plugin.MediaAnalyzer\": \"Debug\"\n            }\n        }\n       // other stuff\n    }\n}\n```\n"
  },
  {
    "path": "build.yaml",
    "content": "---\nname: \"Media Analyzer\"\nguid: \"80885677-DACB-461B-AC97-EE7E971288AA\"\nversion: \"0.1.0.0\"\ntargetAbi: \"10.10.0.0\"\nframework: \"net8.0\"\noverview: \"Analyzes the audio of television episodes to detect intros.\"\ndescription: >\n  Analyzes the audio of television episodes to detect intros.\ncategory: \"General\"\nowner: \"jellyfin\"\nartifacts:\n  - \"Jellyfin.Plugin.MediaAnalyzer.dll\"\nchangelog: >\n  changelog\n"
  },
  {
    "path": "docs/release.md",
    "content": "# Release procedure\n\n## Run tests\n\n1. Run unit tests with `dotnet test`\n2. Run end to end tests with `JELLYFIN_TOKEN=api_key_here python3 main.py`\n\n## Release plugin\n\n1. Run package plugin action and download bundle\n2. Combine generated `manifest.json` with main plugin manifest\n3. Test plugin manifest\n   1. Replace manifest URL with local IP address\n   2. Serve release ZIP and manifest with `python3 -m http.server`\n   3. Test updating plugin\n4. Create release on GitHub with the following files:\n   1. Archived plugin DLL\n"
  },
  {
    "path": "jellyfin.ruleset",
    "content": "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<RuleSet Name=\"Rules for Jellyfin.Server\" Description=\"Code analysis rules for Jellyfin.Server.csproj\" ToolsVersion=\"14.0\">\n    <Rules AnalyzerId=\"StyleCop.Analyzers\" RuleNamespace=\"StyleCop.Analyzers\">\n        <!-- disable warning SA1009: Closing parenthesis should be followed by a space. -->\n        <Rule Id=\"SA1009\" Action=\"None\" />\n        <!-- disable warning SA1011: Closing square bracket should be followed by a space. -->\n        <Rule Id=\"SA1011\" Action=\"None\" />\n        <!-- disable warning SA1101: Prefix local calls with 'this.' -->\n        <Rule Id=\"SA1101\" Action=\"None\" />\n        <!-- disable warning SA1108: Block statements should not contain embedded comments -->\n        <Rule Id=\"SA1108\" Action=\"None\" />\n        <!-- disable warning SA1118: Parameter must not span multiple lines. -->\n        <Rule Id=\"SA1118\" Action=\"None\" />\n        <!-- disable warning SA1128:: Put constructor initializers on their own line -->\n        <Rule Id=\"SA1128\" Action=\"None\" />\n        <!-- disable warning SA1130: Use lambda syntax -->\n        <Rule Id=\"SA1130\" Action=\"None\" />\n        <!-- disable warning SA1200: 'using' directive must appear within a namespace declaration -->\n        <Rule Id=\"SA1200\" Action=\"None\" />\n        <!-- disable warning SA1202: 'public' members must come before 'private' members -->\n        <Rule Id=\"SA1202\" Action=\"None\" />\n        <!-- disable warning SA1204: Static members must appear before non-static members -->\n        <Rule Id=\"SA1204\" Action=\"None\" />\n        <!-- disable warning SA1309: Fields must not begin with an underscore -->\n        <Rule Id=\"SA1309\" Action=\"None\" />\n        <!-- disable warning SA1413: Use trailing comma in multi-line initializers -->\n        <Rule Id=\"SA1413\" Action=\"None\" />\n        <!-- disable warning SA1512: Single-line comments must not be followed by blank line -->\n        <Rule Id=\"SA1512\" Action=\"None\" />\n        <!-- disable warning SA1515: Single-line comment should be preceded by blank line -->\n        <Rule Id=\"SA1515\" Action=\"None\" />\n        <!-- disable warning SA1600: Elements should be documented -->\n        <Rule Id=\"SA1600\" Action=\"None\" />\n        <!-- disable warning SA1602: Enumeration items should be documented -->\n        <Rule Id=\"SA1602\" Action=\"None\" />\n        <!-- disable warning SA1633: The file header is missing or not located at the top of the file -->\n        <Rule Id=\"SA1633\" Action=\"None\" />\n    </Rules>\n\n    <Rules AnalyzerId=\"Microsoft.CodeAnalysis.NetAnalyzers\" RuleNamespace=\"Microsoft.Design\">\n        <!-- error on CA1305: Specify IFormatProvider -->\n        <Rule Id=\"CA1305\" Action=\"Error\" />\n        <!-- error on CA1725: Parameter names should match base declaration -->\n        <Rule Id=\"CA1725\" Action=\"Error\" />\n        <!-- error on CA1725: Call async methods when in an async method -->\n        <Rule Id=\"CA1727\" Action=\"Error\" />\n        <!-- error on CA1843: Do not use 'WaitAll' with a single task -->\n        <Rule Id=\"CA1843\" Action=\"Error\" />\n        <!-- error on CA2016: Forward the CancellationToken parameter to methods that take one\n            or pass in 'CancellationToken.None' explicitly to indicate intentionally not propagating the token -->\n        <Rule Id=\"CA2016\" Action=\"Error\" />\n        <!-- error on CA2254: Template should be a static expression -->\n        <Rule Id=\"CA2254\" Action=\"Error\" />\n\n        <!-- disable warning CA1014: Mark assemblies with CLSCompliantAttribute -->\n        <Rule Id=\"CA1014\" Action=\"Info\" />\n        <!-- disable warning CA1024: Use properties where appropriate -->\n        <Rule Id=\"CA1024\" Action=\"Info\" />\n        <!-- disable warning CA1031: Do not catch general exception types -->\n        <Rule Id=\"CA1031\" Action=\"Info\" />\n        <!-- disable warning CA1032: Implement standard exception constructors -->\n        <Rule Id=\"CA1032\" Action=\"Info\" />\n        <!-- disable warning CA1040: Avoid empty interfaces -->\n        <Rule Id=\"CA1040\" Action=\"Info\" />\n        <!-- disable warning CA1062: Validate arguments of public methods -->\n        <Rule Id=\"CA1062\" Action=\"Info\" />\n        <!-- TODO: enable when false positives are fixed -->\n        <!-- disable warning CA1508: Avoid dead conditional code -->\n        <Rule Id=\"CA1508\" Action=\"Info\" />\n        <!-- disable warning CA1716: Identifiers should not match keywords -->\n        <Rule Id=\"CA1716\" Action=\"Info\" />\n        <!-- disable warning CA1720: Identifiers should not contain type names -->\n        <Rule Id=\"CA1720\" Action=\"Info\" />\n        <!-- disable warning CA1724: Type names should not match namespaces -->\n        <Rule Id=\"CA1724\" Action=\"Info\" />\n        <!-- disable warning CA1805: Do not initialize unnecessarily -->\n        <Rule Id=\"CA1805\" Action=\"Info\" />\n        <!-- disable warning CA1812: internal class that is apparently never instantiated.\n            If so, remove the code from the assembly.\n            If this class is intended to contain only static members, make it static -->\n        <Rule Id=\"CA1812\" Action=\"Info\" />\n        <!-- disable warning CA1822: Member does not access instance data and can be marked as static -->\n        <Rule Id=\"CA1822\" Action=\"Info\" />\n        <!-- disable warning CA2000: Dispose objects before losing scope -->\n        <Rule Id=\"CA2000\" Action=\"Info\" />\n        <!-- disable warning CA2253: Named placeholders should not be numeric values -->\n        <Rule Id=\"CA2253\" Action=\"Info\" />\n        <!-- disable warning CA5394: Do not use insecure randomness -->\n        <Rule Id=\"CA5394\" Action=\"Info\" />\n\n        <!-- disable warning CA1054: Change the type of parameter url from string to System.Uri -->\n        <Rule Id=\"CA1054\" Action=\"None\" />\n        <!-- disable warning CA1055: URI return values should not be strings -->\n        <Rule Id=\"CA1055\" Action=\"None\" />\n        <!-- disable warning CA1056: URI properties should not be strings -->\n        <Rule Id=\"CA1056\" Action=\"None\" />\n        <!-- disable warning CA1303: Do not pass literals as localized parameters -->\n        <Rule Id=\"CA1303\" Action=\"None\" />\n        <!-- disable warning CA1308: Normalize strings to uppercase -->\n        <Rule Id=\"CA1308\" Action=\"None\" />\n        <!-- disable warning CA1848: Use the LoggerMessage delegates -->\n        <Rule Id=\"CA1848\" Action=\"None\" />\n        <!-- disable warning CA2101: Specify marshaling for P/Invoke string arguments -->\n        <Rule Id=\"CA2101\" Action=\"None\" />\n        <!-- disable warning CA2234: Pass System.Uri objects instead of strings -->\n        <Rule Id=\"CA2234\" Action=\"None\" />\n    </Rules>\n</RuleSet>\n"
  }
]