diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index b84e563..0000000 --- a/.editorconfig +++ /dev/null @@ -1,194 +0,0 @@ -# With more recent updates Visual Studio 2017 supports EditorConfig files out of the box -# Visual Studio Code needs an extension: https://github.com/editorconfig/editorconfig-vscode -# For emacs, vim, np++ and other editors, see here: https://github.com/editorconfig -############################### -# Core EditorConfig Options # -############################### -root = true -# All files -[*] -indent_style = space -indent_size = 4 -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true -end_of_line = lf -max_line_length = off - -# YAML indentation -[*.{yml,yaml}] -indent_size = 2 - -# XML indentation -[*.{csproj,xml}] -indent_size = 2 - -############################### -# .NET Coding Conventions # -############################### -[*.{cs,vb}] -# Organize usings -dotnet_sort_system_directives_first = true -# this. preferences -dotnet_style_qualification_for_field = false:silent -dotnet_style_qualification_for_property = false:silent -dotnet_style_qualification_for_method = false:silent -dotnet_style_qualification_for_event = false:silent -# Language keywords vs BCL types preferences -dotnet_style_predefined_type_for_locals_parameters_members = true:silent -dotnet_style_predefined_type_for_member_access = true:silent -# Parentheses preferences -dotnet_style_parentheses_in_arithmetic_binary_operators = always_for_clarity:silent -dotnet_style_parentheses_in_relational_binary_operators = always_for_clarity:silent -dotnet_style_parentheses_in_other_binary_operators = always_for_clarity:silent -dotnet_style_parentheses_in_other_operators = never_if_unnecessary:silent -# Modifier preferences -dotnet_style_require_accessibility_modifiers = for_non_interface_members:silent -dotnet_style_readonly_field = true:suggestion -# Expression-level preferences -dotnet_style_object_initializer = true:suggestion -dotnet_style_collection_initializer = true:suggestion -dotnet_style_explicit_tuple_names = true:suggestion -dotnet_style_null_propagation = true:suggestion -dotnet_style_coalesce_expression = true:suggestion -dotnet_style_prefer_is_null_check_over_reference_equality_method = true:silent -dotnet_style_prefer_inferred_tuple_names = true:suggestion -dotnet_style_prefer_inferred_anonymous_type_member_names = true:suggestion -dotnet_style_prefer_auto_properties = true:silent -dotnet_style_prefer_conditional_expression_over_assignment = true:silent -dotnet_style_prefer_conditional_expression_over_return = true:silent - -############################### -# Naming Conventions # -############################### -# Style Definitions (From Roslyn) - -# Non-private static fields are PascalCase -dotnet_naming_rule.non_private_static_fields_should_be_pascal_case.severity = suggestion -dotnet_naming_rule.non_private_static_fields_should_be_pascal_case.symbols = non_private_static_fields -dotnet_naming_rule.non_private_static_fields_should_be_pascal_case.style = non_private_static_field_style - -dotnet_naming_symbols.non_private_static_fields.applicable_kinds = field -dotnet_naming_symbols.non_private_static_fields.applicable_accessibilities = public, protected, internal, protected_internal, private_protected -dotnet_naming_symbols.non_private_static_fields.required_modifiers = static - -dotnet_naming_style.non_private_static_field_style.capitalization = pascal_case - -# Constants are PascalCase -dotnet_naming_rule.constants_should_be_pascal_case.severity = suggestion -dotnet_naming_rule.constants_should_be_pascal_case.symbols = constants -dotnet_naming_rule.constants_should_be_pascal_case.style = constant_style - -dotnet_naming_symbols.constants.applicable_kinds = field, local -dotnet_naming_symbols.constants.required_modifiers = const - -dotnet_naming_style.constant_style.capitalization = pascal_case - -# Static fields are camelCase and start with s_ -dotnet_naming_rule.static_fields_should_be_camel_case.severity = suggestion -dotnet_naming_rule.static_fields_should_be_camel_case.symbols = static_fields -dotnet_naming_rule.static_fields_should_be_camel_case.style = static_field_style - -dotnet_naming_symbols.static_fields.applicable_kinds = field -dotnet_naming_symbols.static_fields.required_modifiers = static - -dotnet_naming_style.static_field_style.capitalization = camel_case -dotnet_naming_style.static_field_style.required_prefix = _ - -# Instance fields are camelCase and start with _ -dotnet_naming_rule.instance_fields_should_be_camel_case.severity = suggestion -dotnet_naming_rule.instance_fields_should_be_camel_case.symbols = instance_fields -dotnet_naming_rule.instance_fields_should_be_camel_case.style = instance_field_style - -dotnet_naming_symbols.instance_fields.applicable_kinds = field - -dotnet_naming_style.instance_field_style.capitalization = camel_case -dotnet_naming_style.instance_field_style.required_prefix = _ - -# Locals and parameters are camelCase -dotnet_naming_rule.locals_should_be_camel_case.severity = suggestion -dotnet_naming_rule.locals_should_be_camel_case.symbols = locals_and_parameters -dotnet_naming_rule.locals_should_be_camel_case.style = camel_case_style - -dotnet_naming_symbols.locals_and_parameters.applicable_kinds = parameter, local - -dotnet_naming_style.camel_case_style.capitalization = camel_case - -# Local functions are PascalCase -dotnet_naming_rule.local_functions_should_be_pascal_case.severity = suggestion -dotnet_naming_rule.local_functions_should_be_pascal_case.symbols = local_functions -dotnet_naming_rule.local_functions_should_be_pascal_case.style = local_function_style - -dotnet_naming_symbols.local_functions.applicable_kinds = local_function - -dotnet_naming_style.local_function_style.capitalization = pascal_case - -# By default, name items with PascalCase -dotnet_naming_rule.members_should_be_pascal_case.severity = suggestion -dotnet_naming_rule.members_should_be_pascal_case.symbols = all_members -dotnet_naming_rule.members_should_be_pascal_case.style = pascal_case_style - -dotnet_naming_symbols.all_members.applicable_kinds = * - -dotnet_naming_style.pascal_case_style.capitalization = pascal_case - -############################### -# C# Coding Conventions # -############################### -[*.cs] -# var preferences -csharp_style_var_for_built_in_types = true:silent -csharp_style_var_when_type_is_apparent = true:silent -csharp_style_var_elsewhere = true:silent -# Expression-bodied members -csharp_style_expression_bodied_methods = false:silent -csharp_style_expression_bodied_constructors = false:silent -csharp_style_expression_bodied_operators = false:silent -csharp_style_expression_bodied_properties = true:silent -csharp_style_expression_bodied_indexers = true:silent -csharp_style_expression_bodied_accessors = true:silent -# Pattern matching preferences -csharp_style_pattern_matching_over_is_with_cast_check = true:suggestion -csharp_style_pattern_matching_over_as_with_null_check = true:suggestion -# Null-checking preferences -csharp_style_throw_expression = true:suggestion -csharp_style_conditional_delegate_call = true:suggestion -# Modifier preferences -csharp_preferred_modifier_order = public,private,protected,internal,static,extern,new,virtual,abstract,sealed,override,readonly,unsafe,volatile,async:suggestion -# Expression-level preferences -csharp_prefer_braces = true:silent -csharp_style_deconstructed_variable_declaration = true:suggestion -csharp_prefer_simple_default_expression = true:suggestion -csharp_style_pattern_local_over_anonymous_function = true:suggestion -csharp_style_inlined_variable_declaration = true:suggestion - -############################### -# C# Formatting Rules # -############################### -# New line preferences -csharp_new_line_before_open_brace = all -csharp_new_line_before_else = true -csharp_new_line_before_catch = true -csharp_new_line_before_finally = true -csharp_new_line_before_members_in_object_initializers = true -csharp_new_line_before_members_in_anonymous_types = true -csharp_new_line_between_query_expression_clauses = true -# Indentation preferences -csharp_indent_case_contents = true -csharp_indent_switch_labels = true -csharp_indent_labels = flush_left -# Space preferences -csharp_space_after_cast = false -csharp_space_after_keywords_in_control_flow_statements = true -csharp_space_between_method_call_parameter_list_parentheses = false -csharp_space_between_method_declaration_parameter_list_parentheses = false -csharp_space_between_parentheses = false -csharp_space_before_colon_in_inheritance_clause = true -csharp_space_after_colon_in_inheritance_clause = true -csharp_space_around_binary_operators = before_and_after -csharp_space_between_method_declaration_empty_parameter_list_parentheses = false -csharp_space_between_method_call_name_and_opening_parenthesis = false -csharp_space_between_method_call_empty_parameter_list_parentheses = false -# Wrapping preferences -csharp_preserve_single_line_statements = true -csharp_preserve_single_line_blocks = true diff --git a/.github/ISSUE_TEMPLATE/bug_report_form.yml b/.github/ISSUE_TEMPLATE/bug_report_form.yml deleted file mode 100644 index acf6679..0000000 --- a/.github/ISSUE_TEMPLATE/bug_report_form.yml +++ /dev/null @@ -1,76 +0,0 @@ -name: "Bug report" -description: "Create a report to help us improve" -title: "[Bug]: " -labels: [bug] -body: - - type: checkboxes - id: requirements - attributes: - label: Self service debugging - description: | - Jellyfin 10.9 is actively updated. Please make sure you are using the [latest](https://github.com/jellyfin/jellyfin/releases/latest) release. - - Many servers have permission issues that can be resolved with a few extra steps. - If your skip button is not shown, please see [Troubleshooting](https://github.com/intro-skipper/intro-skipper/wiki/Troubleshooting#skip-button-is-not-visible) before reporting. - options: - - label: I use Jellyfin 10.9.11 (or newer) and my permissions are correct - required: true - - type: textarea - attributes: - label: Describe the bug - description: Also tell us, what did you expect to happen? - placeholder: | - 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. - - This is my issue. - - Steps to Reproduce - 1. In this environment... - 2. With this config... - 3. Run '...' - 4. See error... - validations: - required: true - - - type: input - attributes: - label: Jellyfin install method - description: How you installed Jellyfin or the tool used to install it - placeholder: Docker, Windows installer, etc. - validations: - required: true - - - type: input - attributes: - label: Container image/tag or Jellyfin version - description: The container for Docker or Jellyfin version for a native install - placeholder: jellyfin/jellyfin:10.9.9, jellyfin-intro-skipper:latest, etc. - validations: - required: true - - - type: input - attributes: - label: Operating System - description: The operating system of the Jellyfin / Docker host computer - placeholder: Debian 11, Windows 11, etc. - validations: - required: true - - - type: input - attributes: - label: IMDb ID of that TV Series - placeholder: tt0903747 - - - type: textarea - attributes: - label: Support Bundle - placeholder: go to Dashboard -> Plugins -> Intro Skipper -> Support Bundle (at the bottom of the page) and paste the contents of the textbox here - render: shell - validations: - required: true - - - type: textarea - attributes: - label: Jellyfin logs - placeholder: Paste any relevant logs here - render: shell diff --git a/.github/dependabot.yml b/.github/dependabot.yml deleted file mode 100644 index 60d5597..0000000 --- a/.github/dependabot.yml +++ /dev/null @@ -1,29 +0,0 @@ -version: 2 -updates: - # Fetch and update latest `nuget` pkgs - - package-ecosystem: nuget - directory: / - schedule: - interval: weekly - open-pull-requests-limit: 10 - labels: - - chore - - dependency - - nuget - commit-message: - prefix: chore - include: scope - - # Fetch and update latest `github-actions` pkgs - - package-ecosystem: github-actions - directory: / - schedule: - interval: weekly - open-pull-requests-limit: 10 - labels: - - ci - - dependency - - github_actions - commit-message: - prefix: ci - include: scope diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml deleted file mode 100644 index 7991cd1..0000000 --- a/.github/workflows/build.yml +++ /dev/null @@ -1,104 +0,0 @@ -name: "Build Plugin" - -on: - push: - branches: ["master"] - paths-ignore: - - "**/README.md" - - ".github/ISSUE_TEMPLATE/**" - - "docs/**" - - "images/**" - - "manifest.json" - pull_request: - branches: ["master"] - paths-ignore: - - "**/README.md" - - ".github/ISSUE_TEMPLATE/**" - - "docs/**" - - "images/**" - - "manifest.json" - -permissions: - contents: write - packages: write - -jobs: - build: - if: ${{ ! startsWith(github.event.head_commit.message, 'v0.') }} - - runs-on: ubuntu-latest - - steps: - - name: Sanitize head_ref - run: | - # Get the branch name and sanitize it - SANITIZED_BRANCH_NAME=$(echo "${{ github.head_ref }}" | sed 's/[^a-zA-Z0-9.-]/_/g') - - # Export it as an environment variable - echo "SANITIZED_BRANCH_NAME=$SANITIZED_BRANCH_NAME" >> $GITHUB_ENV - - - uses: actions/checkout@v4 - - - name: Setup .NET - uses: actions/setup-dotnet@v4 - with: - dotnet-version: 8.0.x - - - name: Setup Node.js - uses: actions/setup-node@v4 - with: - node-version: "lts/*" - - - name: Minify HTML - run: | - npx html-minifier-terser --collapse-boolean-attributes --collapse-whitespace --remove-comments --remove-optional-tags --remove-redundant-attributes --remove-script-type-attributes --remove-tag-whitespace --use-short-doctype --minify-css true --minify-js true -o ConfusedPolarBear.Plugin.IntroSkipper/Configuration/configPage.html ConfusedPolarBear.Plugin.IntroSkipper/Configuration/configPage.html - npx terser ConfusedPolarBear.Plugin.IntroSkipper/Configuration/inject.js -o ConfusedPolarBear.Plugin.IntroSkipper/Configuration/inject.js -c -m - npx terser ConfusedPolarBear.Plugin.IntroSkipper/Configuration/visualizer.js -o ConfusedPolarBear.Plugin.IntroSkipper/Configuration/visualizer.js -c -m - - - name: Restore dependencies - run: | - dotnet nuget add source --username ${{ github.repository_owner }} --password ${{ secrets.GITHUB_TOKEN }} --store-password-in-clear-text --name jellyfin-pre "https://nuget.pkg.github.com/jellyfin/index.json" - dotnet tool install --global dotnet-outdated-tool - dotnet outdated -pre Always -u -inc Jellyfin - - - name: Embed version info - run: | - GITHUB_SHA=${{ github.sha }} - sed -i "s/string\.Empty/\"$GITHUB_SHA\"/g" ConfusedPolarBear.Plugin.IntroSkipper/Helper/Commit.cs - - - name: Retrieve commit identification - run: | - GIT_HASH=$(git rev-parse --short HEAD) - echo "GIT_HASH=${GIT_HASH}" >> $GITHUB_ENV - - - name: Build - run: dotnet build --no-restore - - - name: Upload artifact - uses: actions/upload-artifact@v4.3.6 - if: github.event_name != 'pull_request' - with: - name: ConfusedPolarBear.Plugin.IntroSkipper-${{ env.GIT_HASH }}.dll - path: ConfusedPolarBear.Plugin.IntroSkipper/bin/Debug/net8.0/ConfusedPolarBear.Plugin.IntroSkipper.dll - if-no-files-found: error - - - name: Upload artifact - uses: actions/upload-artifact@v4.3.6 - if: github.event_name == 'pull_request' - with: - name: ConfusedPolarBear.Plugin.IntroSkipper-${{ env.SANITIZED_BRANCH_NAME }}.dll - path: ConfusedPolarBear.Plugin.IntroSkipper/bin/Debug/net8.0/ConfusedPolarBear.Plugin.IntroSkipper.dll - retention-days: 7 - if-no-files-found: error - - - name: Create archive - if: github.event_name != 'pull_request' - run: zip -j "intro-skipper-${{ env.GIT_HASH }}.zip" ConfusedPolarBear.Plugin.IntroSkipper/bin/Debug/net8.0/ConfusedPolarBear.Plugin.IntroSkipper.dll - - - name: Create/replace the preview release and upload artifacts - if: github.event_name != 'pull_request' - run: | - gh release delete '10.10/preview' --cleanup-tag --yes || true - gh release create '10.10/preview' "intro-skipper-${{ env.GIT_HASH }}.zip" --prerelease --title "intro-skipper-${{ env.GIT_HASH }}" --notes "This is a prerelease version." - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml deleted file mode 100644 index 2f931b3..0000000 --- a/.github/workflows/codeql.yml +++ /dev/null @@ -1,60 +0,0 @@ -name: "CodeQL" - -on: - push: - branches: [master] - paths-ignore: - - "**/README.md" - - ".github/ISSUE_TEMPLATE/**" - - "docs/**" - - "images/**" - - "manifest.json" - pull_request: - branches: [master] - paths-ignore: - - "**/README.md" - - ".github/ISSUE_TEMPLATE/**" - - "docs/**" - - "images/**" - - "manifest.json" - -permissions: write-all - -jobs: - analyze: - name: Analyze - runs-on: ubuntu-latest - - # This job will only run if the repository is public - if: ${{ github.event.repository.private == false }} - - strategy: - fail-fast: false - matrix: - language: ["csharp"] - - steps: - - uses: actions/checkout@v4 - - - name: Setup .NET - uses: actions/setup-dotnet@v4 - with: - dotnet-version: 8.0.x - - - name: Install dependencies - run: | - dotnet nuget add source --username ${{ github.repository_owner }} --password ${{ secrets.GITHUB_TOKEN }} --store-password-in-clear-text --name jellyfin-pre "https://nuget.pkg.github.com/jellyfin/index.json" - dotnet tool install --global dotnet-outdated-tool - dotnet outdated -pre Always -u -inc Jellyfin - - - name: Initialize CodeQL - uses: github/codeql-action/init@f779452ac5af1c261dce0346a8f964149f49322b # v3.26.13 - with: - languages: ${{ matrix.language }} - queries: +security-extended - - - name: Autobuild - uses: github/codeql-action/autobuild@f779452ac5af1c261dce0346a8f964149f49322b # v3.26.13 - - - name: Perform CodeQL Analysis - uses: github/codeql-action/analyze@f779452ac5af1c261dce0346a8f964149f49322b # v3.26.13 diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml deleted file mode 100644 index 3a0631b..0000000 --- a/.github/workflows/release.yml +++ /dev/null @@ -1,90 +0,0 @@ -name: "Release Plugin" - -on: - workflow_dispatch: - -permissions: - contents: write - packages: write - -jobs: - build: - runs-on: ubuntu-latest - - steps: - - uses: actions/checkout@v4 - - - name: Setup .NET - uses: actions/setup-dotnet@v4 - with: - dotnet-version: 8.0.x - - - name: Setup Node.js - uses: actions/setup-node@v4 - with: - node-version: "lts/*" - - - name: Minify HTML - run: | - npx html-minifier-terser --collapse-boolean-attributes --collapse-whitespace --remove-comments --remove-optional-tags --remove-redundant-attributes --remove-script-type-attributes --remove-tag-whitespace --use-short-doctype --minify-css true --minify-js true -o ConfusedPolarBear.Plugin.IntroSkipper/Configuration/configPage.html ConfusedPolarBear.Plugin.IntroSkipper/Configuration/configPage.html - npx terser ConfusedPolarBear.Plugin.IntroSkipper/Configuration/inject.js -o ConfusedPolarBear.Plugin.IntroSkipper/Configuration/inject.js -c -m - npx terser ConfusedPolarBear.Plugin.IntroSkipper/Configuration/visualizer.js -o ConfusedPolarBear.Plugin.IntroSkipper/Configuration/visualizer.js -c -m - - - name: Restore dependencies - run: | - dotnet nuget add source --username ${{ github.repository_owner }} --password ${{ secrets.GITHUB_TOKEN }} --store-password-in-clear-text --name jellyfin-pre "https://nuget.pkg.github.com/jellyfin/index.json" - dotnet tool install --global dotnet-outdated-tool - dotnet outdated -pre Always -u -inc Jellyfin - - - name: Run update version - uses: intro-skipper/intro-skipper-action-ts@main - with: - task-type: "updateVersion" - - - name: Embed version info - run: | - GITHUB_SHA=${{ github.sha }} - sed -i "s/string\.Empty/\"$GITHUB_SHA\"/g" ConfusedPolarBear.Plugin.IntroSkipper/Helper/Commit.cs - - - name: Build - run: dotnet build --configuration Release --no-restore - - - name: Create archive - run: zip -j "intro-skipper-v${{ env.NEW_FILE_VERSION }}.zip" ConfusedPolarBear.Plugin.IntroSkipper/bin/Release/net8.0/ConfusedPolarBear.Plugin.IntroSkipper.dll - - - name: Remove old release if exits - if: ${{ github.repository == 'intro-skipper/intro-skipper-test' }} - run: gh release delete "10.10/v${{ env.NEW_FILE_VERSION }}" --cleanup-tag --yes || true - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - - - name: Create new release with tag - if: github.event_name != 'pull_request' - run: gh release create "10.10/v${{ env.NEW_FILE_VERSION }}" "intro-skipper-v${{ env.NEW_FILE_VERSION }}.zip" --title "v${{ env.NEW_FILE_VERSION }}" --latest --generate-notes - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - - - name: Run validation and update script - uses: intro-skipper/intro-skipper-action-ts@main - with: - task-type: "updateManifest" - env: - GITHUB_REPO_VISIBILITY: ${{ github.event.repository.visibility }} - CURRENT_VERSION: "10.10.0" - MAIN_VERSION: "10.10" - - - name: Deploy to Cloudflare KV - uses: cloudflare/wrangler-action@v3 - with: - apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} - accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} - command: kv key put --namespace-id=${{ github.repository == 'intro-skipper/intro-skipper-test' && '49c07e5b68074443b940de893d58a997' || '61215c51799a4de59f0a33a8b7aecb0e' }} "10.10" --path=manifest.json - - - name: Commit changes - if: success() - run: | - git config --global user.name "github-actions[bot]" - git config --global user.email "github-actions[bot]@users.noreply.github.com" - git add README.md manifest.json ConfusedPolarBear.Plugin.IntroSkipper/ConfusedPolarBear.Plugin.IntroSkipper.csproj .github/ISSUE_TEMPLATE/bug_report_form.yml - git commit -m "release v${{ env.NEW_FILE_VERSION }}" - git push diff --git a/.github/workflows/webui.yml b/.github/workflows/webui.yml deleted file mode 100644 index 9434079..0000000 --- a/.github/workflows/webui.yml +++ /dev/null @@ -1,41 +0,0 @@ -name: Create Jellyfin-web artifact -on: - release: - types: [published] - workflow_dispatch: -jobs: - build: - runs-on: ubuntu-latest - permissions: - contents: read - packages: write - strategy: - matrix: - jellyfin-web-version: [10.9.11] - steps: - - uses: actions/checkout@v4 - - name: Setup Node.js environment - uses: actions/setup-node@v4 - with: - node-version: ">=20" - - name: Checkout official jellyfin-web - uses: actions/checkout@v4 - with: - repository: jellyfin/jellyfin-web - ref: v${{ matrix.jellyfin-web-version }} - path: web - - name: Apply intro skipper patch - run: | - cd web - git apply ../webui.patch - - name: Build web interface - run: | - cd web - npm ci --no-audit - npm run build:production - - name: Upload web interface - uses: actions/upload-artifact@v4 - with: - name: jellyfin-web-${{ matrix.jellyfin-web-version }}+${{ github.sha }} - path: web/dist - if-no-files-found: error diff --git a/.gitignore b/.gitignore deleted file mode 100644 index bc18978..0000000 --- a/.gitignore +++ /dev/null @@ -1,10 +0,0 @@ -bin/ -obj/ -BenchmarkDotNet.Artifacts/ -/package/ - -# Ignore pre compiled web interface -docker/dist - -# Visual Studio -.vs/ diff --git a/ACKNOWLEDGEMENTS.md b/ACKNOWLEDGEMENTS.md deleted file mode 100644 index 879b460..0000000 --- a/ACKNOWLEDGEMENTS.md +++ /dev/null @@ -1,7 +0,0 @@ -Intro Skipper is made possible by the following open source projects: - -* [acoustid-match](https://github.com/dnknth/acoustid-match) (MIT) -* [chromaprint](https://github.com/acoustid/chromaprint) (LGPL 2.1) -* [JellyScrub](https://github.com/nicknsy/jellyscrub) (MIT) -* [Jellyfin](https://github.com/jellyfin/jellyfin) (GPL) -* [Jellyfin Media Analyzer](https://github.com/endrl/jellyfin-plugin-media-analyzer) (GPL) diff --git a/ConfusedPolarBear.Plugin.IntroSkipper.Tests/ConfusedPolarBear.Plugin.IntroSkipper.Tests.csproj b/ConfusedPolarBear.Plugin.IntroSkipper.Tests/ConfusedPolarBear.Plugin.IntroSkipper.Tests.csproj deleted file mode 100644 index b6fa741..0000000 --- a/ConfusedPolarBear.Plugin.IntroSkipper.Tests/ConfusedPolarBear.Plugin.IntroSkipper.Tests.csproj +++ /dev/null @@ -1,27 +0,0 @@ - - - - net8.0 - enable - - false - - - - - - - runtime; build; native; contentfiles; analyzers; buildtransitive - all - - - runtime; build; native; contentfiles; analyzers; buildtransitive - all - - - - - - - - diff --git a/ConfusedPolarBear.Plugin.IntroSkipper.Tests/TestAudioFingerprinting.cs b/ConfusedPolarBear.Plugin.IntroSkipper.Tests/TestAudioFingerprinting.cs deleted file mode 100644 index b3c354a..0000000 --- a/ConfusedPolarBear.Plugin.IntroSkipper.Tests/TestAudioFingerprinting.cs +++ /dev/null @@ -1,165 +0,0 @@ -/* These tests require that the host system has a version of FFmpeg installed - * which supports both chromaprint and the "-fp_format raw" flag. - */ - -using System; -using System.Collections.Generic; -using ConfusedPolarBear.Plugin.IntroSkipper.Analyzers; -using ConfusedPolarBear.Plugin.IntroSkipper.Data; -using Microsoft.Extensions.Logging; -using Xunit; - -namespace ConfusedPolarBear.Plugin.IntroSkipper.Tests; - -public class TestAudioFingerprinting -{ - [FactSkipFFmpegTests] - public void TestInstallationCheck() - { - Assert.True(FFmpegWrapper.CheckFFmpegVersion()); - } - - [Theory] - [InlineData(0, 0)] - [InlineData(1, 1)] - [InlineData(5, 213)] - [InlineData(10, 56_021)] - [InlineData(16, 16_112_341)] - [InlineData(19, 2_465_585_877)] - public void TestBitCounting(int expectedBits, uint number) - { - var chromaprint = CreateChromaprintAnalyzer(); - Assert.Equal(expectedBits, chromaprint.CountBits(number)); - } - - [FactSkipFFmpegTests] - public void TestFingerprinting() - { - // Generated with `fpcalc -raw audio/big_buck_bunny_intro.mp3` - var expected = new uint[]{ - 3269995649, 3261610160, 3257403872, 1109989680, 1109993760, 1110010656, 1110142768, 1110175504, - 1110109952, 1126874880, 2788611, 2787586, 6981634, 15304754, 28891170, 43579426, 43542561, - 47737888, 41608640, 40559296, 36352644, 53117572, 2851460, 1076465548, 1080662428, 1080662492, - 1089182044, 1148041501, 1148037422, 3291343918, 3290980398, 3429367854, 3437756714, 3433698090, - 3433706282, 3366600490, 3366464314, 2296916250, 3362269210, 3362265115, 3362266441, 3370784472, - 3366605480, 1218990776, 1223217816, 1231602328, 1260950200, 1245491640, 169845176, 1510908120, - 1510911000, 2114365528, 2114370008, 1996929688, 1996921480, 1897171592, 1884588680, 1347470984, - 1343427226, 1345467054, 1349657318, 1348673570, 1356869666, 1356865570, 295837698, 60957698, - 44194818, 48416770, 40011778, 36944210, 303147954, 369146786, 1463847842, 1434488738, 1417709474, - 1417713570, 3699441634, 3712167202, 3741460534, 2585144342, 2597725238, 2596200487, 2595926077, - 2595984141, 2594734600, 2594736648, 2598931176, 2586348264, 2586348264, 2586561257, 2586451659, - 2603225802, 2603225930, 2573860970, 2561151018, 3634901034, 3634896954, 3651674122, 3416793162, - 3416816715, 3404331257, 3395844345, 3395836155, 3408464089, 3374975369, 1282036360, 1290457736, - 1290400440, 1290314408, 1281925800, 1277727404, 1277792932, 1278785460, 1561962388, 1426698196, - 3607924711, 4131892839, 4140215815, 4292259591, 3218515717, 3209938229, 3171964197, 3171956013, - 4229117295, 4229312879, 4242407935, 4240114111, 4239987133, 4239990013, 3703060732, 1547188252, - 1278748677, 1278748935, 1144662786, 1148854786, 1090388802, 1090388962, 1086260130, 1085940098, - 1102709122, 45811586, 44634002, 44596656, 44592544, 1122527648, 1109944736, 1109977504, 1111030243, - 1111017762, 1109969186, 1126721826, 1101556002, 1084844322, 1084979506, 1084914450, 1084914449, - 1084873520, 3228093296, 3224996817, 3225062275, 3241840002, 3346701698, 3349843394, 3349782306, - 3349719842, 3353914146, 3328748322, 3328747810, 3328809266, 3471476754, 3472530451, 3472473123, - 3472417825, 3395841056, 3458735136, 3341420624, 1076496560, 1076501168, 1076501136, 1076497024 - }; - - var actual = FFmpegWrapper.Fingerprint( - QueueEpisode("audio/big_buck_bunny_intro.mp3"), - AnalysisMode.Introduction); - - Assert.Equal(expected, actual); - } - - [Fact] - public void TestIndexGeneration() - { - // 0 1 2 3 4 5 6 7 - var fpr = new uint[] { 1, 2, 3, 1, 5, 77, 42, 2 }; - var expected = new Dictionary() - { - {1, 3}, - {2, 7}, - {3, 2}, - {5, 4}, - {42, 6}, - {77, 5}, - }; - - var actual = FFmpegWrapper.CreateInvertedIndex(Guid.NewGuid(), fpr, AnalysisMode.Introduction); - - Assert.Equal(expected, actual); - } - - [FactSkipFFmpegTests] - public void TestIntroDetection() - { - var chromaprint = CreateChromaprintAnalyzer(); - - var lhsEpisode = QueueEpisode("audio/big_buck_bunny_intro.mp3"); - var rhsEpisode = QueueEpisode("audio/big_buck_bunny_clip.mp3"); - var lhsFingerprint = FFmpegWrapper.Fingerprint(lhsEpisode, AnalysisMode.Introduction); - var rhsFingerprint = FFmpegWrapper.Fingerprint(rhsEpisode, AnalysisMode.Introduction); - - var (lhs, rhs) = chromaprint.CompareEpisodes( - lhsEpisode.EpisodeId, - lhsFingerprint, - rhsEpisode.EpisodeId, - rhsFingerprint); - - Assert.True(lhs.Valid); - Assert.Equal(0, lhs.Start); - Assert.Equal(17.208, lhs.End, 3); - - Assert.True(rhs.Valid); - // because we changed for 0.128 to 0.1238 its 4,952 now but that's too early (<= 5) - Assert.Equal(0, rhs.Start); - Assert.Equal(22.1602, rhs.End); - } - - /// - /// Test that the silencedetect wrapper is working. - /// - [FactSkipFFmpegTests] - public void TestSilenceDetection() - { - var clip = QueueEpisode("audio/big_buck_bunny_clip.mp3"); - - var expected = new TimeRange[] - { - new(44.6310, 44.8072), - new(53.5905, 53.8070), - new(53.8458, 54.2024), - new(54.2611, 54.5935), - new(54.7098, 54.9293), - new(54.9294, 55.2590), - }; - - var range = new TimeRange(0, 60); - var actual = FFmpegWrapper.DetectSilence(clip, range); - - Assert.Equal(expected, actual); - } - - private static QueuedEpisode QueueEpisode(string path) - { - return new QueuedEpisode() - { - EpisodeId = Guid.NewGuid(), - Path = "../../../" + path, - IntroFingerprintEnd = 60 - }; - } - - private static ChromaprintAnalyzer CreateChromaprintAnalyzer() - { - var logger = new LoggerFactory().CreateLogger(); - return new(logger); - } -} - -public class FactSkipFFmpegTests : FactAttribute -{ -#if SKIP_FFMPEG_TESTS - public FactSkipFFmpegTests() { - Skip = "SKIP_FFMPEG_TESTS defined, skipping unit tests that require FFmpeg to be installed"; - } -#endif -} diff --git a/ConfusedPolarBear.Plugin.IntroSkipper.Tests/TestBlackFrames.cs b/ConfusedPolarBear.Plugin.IntroSkipper.Tests/TestBlackFrames.cs deleted file mode 100644 index aac27a0..0000000 --- a/ConfusedPolarBear.Plugin.IntroSkipper.Tests/TestBlackFrames.cs +++ /dev/null @@ -1,75 +0,0 @@ -namespace ConfusedPolarBear.Plugin.IntroSkipper.Tests; - -using System; -using System.Collections.Generic; -using ConfusedPolarBear.Plugin.IntroSkipper.Analyzers; -using ConfusedPolarBear.Plugin.IntroSkipper.Data; -using Microsoft.Extensions.Logging; -using Xunit; - -public class TestBlackFrames -{ - [FactSkipFFmpegTests] - public void TestBlackFrameDetection() - { - var range = 1e-5; - - var expected = new List(); - expected.AddRange(CreateFrameSequence(2.04, 3)); - expected.AddRange(CreateFrameSequence(5, 6)); - expected.AddRange(CreateFrameSequence(8, 9.96)); - - var actual = FFmpegWrapper.DetectBlackFrames(QueueFile("rainbow.mp4"), new(0, 10), 85); - - for (var i = 0; i < expected.Count; i++) - { - var (e, a) = (expected[i], actual[i]); - Assert.Equal(e.Percentage, a.Percentage); - Assert.InRange(a.Time, e.Time - range, e.Time + range); - } - } - - [FactSkipFFmpegTests] - public void TestEndCreditDetection() - { - // new strategy new range - var range = 3; - - var analyzer = CreateBlackFrameAnalyzer(); - - var episode = QueueFile("credits.mp4"); - episode.Duration = (int)new TimeSpan(0, 5, 30).TotalSeconds; - - var result = analyzer.AnalyzeMediaFile(episode, 240, 30, 85); - Assert.NotNull(result); - Assert.InRange(result.Start, 300 - range, 300 + range); - } - - private static QueuedEpisode QueueFile(string path) - { - return new() - { - EpisodeId = Guid.NewGuid(), - Name = path, - Path = "../../../video/" + path - }; - } - - private static BlackFrame[] CreateFrameSequence(double start, double end) - { - var frames = new List(); - - for (var i = start; i < end; i += 0.04) - { - frames.Add(new(100, i)); - } - - return [.. frames]; - } - - private static BlackFrameAnalyzer CreateBlackFrameAnalyzer() - { - var logger = new LoggerFactory().CreateLogger(); - return new(logger); - } -} diff --git a/ConfusedPolarBear.Plugin.IntroSkipper.Tests/TestChapterAnalyzer.cs b/ConfusedPolarBear.Plugin.IntroSkipper.Tests/TestChapterAnalyzer.cs deleted file mode 100644 index afdb811..0000000 --- a/ConfusedPolarBear.Plugin.IntroSkipper.Tests/TestChapterAnalyzer.cs +++ /dev/null @@ -1,85 +0,0 @@ -namespace ConfusedPolarBear.Plugin.IntroSkipper.Tests; - -using System; -using System.Collections.Generic; -using System.Collections.ObjectModel; -using ConfusedPolarBear.Plugin.IntroSkipper.Analyzers; -using ConfusedPolarBear.Plugin.IntroSkipper.Data; -using MediaBrowser.Model.Entities; -using Microsoft.Extensions.Logging; -using Xunit; - -public class TestChapterAnalyzer -{ - [Theory] - [InlineData("Opening")] - [InlineData("OP")] - [InlineData("Intro")] - [InlineData("Intro Start")] - [InlineData("Introduction")] - public void TestIntroductionExpression(string chapterName) - { - var chapters = CreateChapters(chapterName, AnalysisMode.Introduction); - var introChapter = FindChapter(chapters, AnalysisMode.Introduction); - - Assert.NotNull(introChapter); - Assert.Equal(60, introChapter.Start); - Assert.Equal(90, introChapter.End); - } - - [Theory] - [InlineData("End Credits")] - [InlineData("Ending")] - [InlineData("Credit start")] - [InlineData("Closing Credits")] - [InlineData("Credits")] - public void TestEndCreditsExpression(string chapterName) - { - var chapters = CreateChapters(chapterName, AnalysisMode.Credits); - var creditsChapter = FindChapter(chapters, AnalysisMode.Credits); - - Assert.NotNull(creditsChapter); - Assert.Equal(1890, creditsChapter.Start); - Assert.Equal(2000, creditsChapter.End); - } - - private Segment? FindChapter(Collection chapters, AnalysisMode mode) - { - var logger = new LoggerFactory().CreateLogger(); - var analyzer = new ChapterAnalyzer(logger); - - var config = new Configuration.PluginConfiguration(); - var expression = mode == AnalysisMode.Introduction ? - config.ChapterAnalyzerIntroductionPattern : - config.ChapterAnalyzerEndCreditsPattern; - - return analyzer.FindMatchingChapter(new() { Duration = 2000 }, chapters, expression, mode); - } - - private Collection CreateChapters(string name, AnalysisMode mode) - { - var chapters = new[]{ - CreateChapter("Cold Open", 0), - CreateChapter(mode == AnalysisMode.Introduction ? name : "Introduction", 60), - CreateChapter("Main Episode", 90), - CreateChapter(mode == AnalysisMode.Credits ? name : "Credits", 1890) - }; - - return new(new List(chapters)); - } - - /// - /// Create a ChapterInfo object. - /// - /// Chapter name. - /// Chapter position (in seconds). - /// ChapterInfo. - private static ChapterInfo CreateChapter(string name, int position) - { - return new() - { - Name = name, - StartPositionTicks = TimeSpan.FromSeconds(position).Ticks - }; - } -} diff --git a/ConfusedPolarBear.Plugin.IntroSkipper.Tests/TestContiguous.cs b/ConfusedPolarBear.Plugin.IntroSkipper.Tests/TestContiguous.cs deleted file mode 100644 index fb59010..0000000 --- a/ConfusedPolarBear.Plugin.IntroSkipper.Tests/TestContiguous.cs +++ /dev/null @@ -1,93 +0,0 @@ -using ConfusedPolarBear.Plugin.IntroSkipper.Data; -using Xunit; - -namespace ConfusedPolarBear.Plugin.IntroSkipper.Tests; - -public class TestTimeRanges -{ - [Fact] - public void TestSmallRange() - { - var times = new double[]{ - 1, 1.5, 2, 2.5, 3, 3.5, 4, - 100, 100.5, 101, 101.5 - }; - - var expected = new TimeRange(1, 4); - var actual = TimeRangeHelpers.FindContiguous(times, 2); - - Assert.Equal(expected, actual); - } - - [Fact] - public void TestLargeRange() - { - var times = new double[]{ - 1, 1.5, 2, - 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, - 4.5, 5.3122, 5.3123, 5.3124, 5.3125, 5.3126, 5.3127, 5.3128, - 55, 55.5, 55.6, 55.7 - }; - - var expected = new TimeRange(1, 5.3128); - var actual = TimeRangeHelpers.FindContiguous(times, 2); - - Assert.Equal(expected, actual); - } - - [Fact] - public void TestFuturama() - { - // These timestamps were manually extracted from Futurama S01E04 and S01E05. - var times = new double[]{ - 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, - 17.408, 17.536, 17.664, 17.792, 17.92, 18.048, 18.176, 18.304, 18.432, 18.56, 18.688, 18.816, - 18.944, 19.072, 19.2, 19.328, 19.456, 19.584, 19.712, 19.84, 19.968, 20.096, 20.224, 20.352, - 20.48, 20.608, 20.736, 20.864, 20.992, 21.12, 21.248, 21.376, 21.504, 21.632, 21.76, 21.888, - 22.016, 22.144, 22.272, 22.4, 22.528, 22.656, 22.784, 22.912, 23.04, 23.168, 23.296, 23.424, - 23.552, 23.68, 23.808, 23.936, 24.064, 24.192, 24.32, 24.448, 24.576, 24.704, 24.832, 24.96, - 25.088, 25.216, 25.344, 25.472, 25.6, 25.728, 25.856, 25.984, 26.112, 26.24, 26.368, 26.496, - 26.624, 26.752, 26.88, 27.008, 27.136, 27.264, 27.392, 27.52, 27.648, 27.776, 27.904, 28.032, - 28.16, 28.288, 28.416, 28.544, 28.672, 28.8, 28.928, 29.056, 29.184, 29.312, 29.44, 29.568, - 29.696, 29.824, 29.952, 30.08, 30.208, 30.336, 30.464, 30.592, 30.72, 30.848, 30.976, 31.104, - 31.232, 31.36, 31.488, 31.616, 31.744, 31.872, 32, 32.128, 32.256, 32.384, 32.512, 32.64, - 32.768, 32.896, 33.024, 33.152, 33.28, 33.408, 33.536, 33.664, 33.792, 33.92, 34.048, 34.176, - 34.304, 34.432, 34.56, 34.688, 34.816, 34.944, 35.072, 35.2, 35.328, 35.456, 35.584, 35.712, - 35.84, 35.968, 36.096, 36.224, 36.352, 36.48, 36.608, 36.736, 36.864, 36.992, 37.12, 37.248, - 37.376, 37.504, 37.632, 37.76, 37.888, 38.016, 38.144, 38.272, 38.4, 38.528, 38.656, 38.784, - 38.912, 39.04, 39.168, 39.296, 39.424, 39.552, 39.68, 39.808, 39.936, 40.064, 40.192, 40.32, - 40.448, 40.576, 40.704, 40.832, 40.96, 41.088, 41.216, 41.344, 41.472, 41.6, 41.728, 41.856, - 41.984, 42.112, 42.24, 42.368, 42.496, 42.624, 42.752, 42.88, 43.008, 43.136, 43.264, 43.392, - 43.52, 43.648, 43.776, 43.904, 44.032, 44.16, 44.288, 44.416, 44.544, 44.672, 44.8, 44.928, - 45.056, 45.184, 57.344, 62.976, 68.864, 74.368, 81.92, 82.048, 86.528, 100.864, 102.656, - 102.784, 102.912, 103.808, 110.976, 116.864, 125.696, 128.384, 133.248, 133.376, 136.064, - 136.704, 142.976, 150.272, 152.064, 164.864, 164.992, 166.144, 166.272, 175.488, 190.08, - 191.872, 192, 193.28, 193.536, 213.376, 213.504, 225.664, 225.792, 243.2, 243.84, 256, - 264.448, 264.576, 264.704, 269.568, 274.816, 274.944, 276.096, 283.264, 294.784, 294.912, - 295.04, 295.168, 313.984, 325.504, 333.568, 335.872, 336.384 - }; - - var expected = new TimeRange(16, 45.184); - var actual = TimeRangeHelpers.FindContiguous(times, 2); - - Assert.Equal(expected, actual); - } - - /// - /// Tests that TimeRange intersections are detected correctly. - /// Tests each time range against a range of 5 to 10 seconds. - /// - [Theory] - [InlineData(1, 4, false)] // too early - [InlineData(4, 6, true)] // intersects on the left - [InlineData(7, 8, true)] // in the middle - [InlineData(9, 12, true)] // intersects on the right - [InlineData(13, 15, false)] // too late - public void TestTimeRangeIntersection(int start, int end, bool expected) - { - var large = new TimeRange(5, 10); - var testRange = new TimeRange(start, end); - - Assert.Equal(expected, large.Intersects(testRange)); - } -} diff --git a/ConfusedPolarBear.Plugin.IntroSkipper.Tests/TestEdl.cs b/ConfusedPolarBear.Plugin.IntroSkipper.Tests/TestEdl.cs deleted file mode 100644 index 6106b2c..0000000 --- a/ConfusedPolarBear.Plugin.IntroSkipper.Tests/TestEdl.cs +++ /dev/null @@ -1,47 +0,0 @@ -using System; -using ConfusedPolarBear.Plugin.IntroSkipper.Data; -using ConfusedPolarBear.Plugin.IntroSkipper.Manager; -using Xunit; - -namespace ConfusedPolarBear.Plugin.IntroSkipper.Tests; - -public class TestEdl -{ - // Test data is from https://kodi.wiki/view/Edit_decision_list#MPlayer_EDL - [Theory] - [InlineData(5.3, 7.1, EdlAction.Cut, "5.3 7.1 0")] - [InlineData(15, 16.7, EdlAction.Mute, "15 16.7 1")] - [InlineData(420, 822, EdlAction.CommercialBreak, "420 822 3")] - [InlineData(1, 255.3, EdlAction.SceneMarker, "1 255.3 2")] - [InlineData(1.123456789, 5.654647987, EdlAction.CommercialBreak, "1.12 5.65 3")] - public void TestEdlSerialization(double start, double end, EdlAction action, string expected) - { - var intro = MakeIntro(start, end); - var actual = intro.ToEdl(action); - - Assert.Equal(expected, actual); - } - - [Fact] - public void TestEdlInvalidSerialization() - { - Assert.Throws(() => - { - var intro = MakeIntro(0, 5); - intro.ToEdl(EdlAction.None); - }); - } - - [Theory] - [InlineData("Death Note - S01E12 - Love.mkv", "Death Note - S01E12 - Love.edl")] - [InlineData("/full/path/to/file.rm", "/full/path/to/file.edl")] - public void TestEdlPath(string mediaPath, string edlPath) - { - Assert.Equal(edlPath, EdlManager.GetEdlPath(mediaPath)); - } - - private static Segment MakeIntro(double start, double end) - { - return new Segment(Guid.Empty, new TimeRange(start, end)); - } -} diff --git a/ConfusedPolarBear.Plugin.IntroSkipper.Tests/TestWarnings.cs b/ConfusedPolarBear.Plugin.IntroSkipper.Tests/TestWarnings.cs deleted file mode 100644 index 9dde767..0000000 --- a/ConfusedPolarBear.Plugin.IntroSkipper.Tests/TestWarnings.cs +++ /dev/null @@ -1,49 +0,0 @@ -namespace ConfusedPolarBear.Plugin.IntroSkipper.Tests; - -using ConfusedPolarBear.Plugin.IntroSkipper.Data; -using Xunit; - -public class TestFlags -{ - [Fact] - public void TestEmptyFlagSerialization() - { - WarningManager.Clear(); - Assert.Equal("None", WarningManager.GetWarnings()); - } - - [Fact] - public void TestSingleFlagSerialization() - { - WarningManager.Clear(); - WarningManager.SetFlag(PluginWarning.UnableToAddSkipButton); - Assert.Equal("UnableToAddSkipButton", WarningManager.GetWarnings()); - Assert.True(WarningManager.HasFlag(PluginWarning.UnableToAddSkipButton)); - } - - [Fact] - public void TestDoubleFlagSerialization() - { - WarningManager.Clear(); - WarningManager.SetFlag(PluginWarning.UnableToAddSkipButton); - WarningManager.SetFlag(PluginWarning.InvalidChromaprintFingerprint); - WarningManager.SetFlag(PluginWarning.InvalidChromaprintFingerprint); - Assert.True(WarningManager.HasFlag(PluginWarning.UnableToAddSkipButton) && WarningManager.HasFlag(PluginWarning.InvalidChromaprintFingerprint)); - Assert.Equal( - "UnableToAddSkipButton, InvalidChromaprintFingerprint", - WarningManager.GetWarnings()); - } - - [Fact] - public void TestHasFlag() - { - WarningManager.Clear(); - Assert.True(WarningManager.HasFlag(PluginWarning.None)); - Assert.False(WarningManager.HasFlag(PluginWarning.UnableToAddSkipButton) && WarningManager.HasFlag(PluginWarning.InvalidChromaprintFingerprint)); - WarningManager.SetFlag(PluginWarning.UnableToAddSkipButton); - WarningManager.SetFlag(PluginWarning.InvalidChromaprintFingerprint); - Assert.True(WarningManager.HasFlag(PluginWarning.UnableToAddSkipButton) && WarningManager.HasFlag(PluginWarning.InvalidChromaprintFingerprint)); - Assert.False(WarningManager.HasFlag(PluginWarning.IncompatibleFFmpegBuild)); - Assert.True(WarningManager.HasFlag(PluginWarning.None)); - } -} diff --git a/ConfusedPolarBear.Plugin.IntroSkipper.Tests/audio/README.txt b/ConfusedPolarBear.Plugin.IntroSkipper.Tests/audio/README.txt deleted file mode 100644 index 095d013..0000000 --- a/ConfusedPolarBear.Plugin.IntroSkipper.Tests/audio/README.txt +++ /dev/null @@ -1,7 +0,0 @@ -The audio used in the fingerprinting unit tests is from Big Buck Bunny, attributed below. - -Both 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. - -Both files have been downmixed to two audio channels. -big_buck_bunny_intro.mp3 is from 5 to 30 seconds. -big_buck_bunny_clip.mp3 is from 0 to 60 seconds. diff --git a/ConfusedPolarBear.Plugin.IntroSkipper.Tests/audio/big_buck_bunny_clip.mp3 b/ConfusedPolarBear.Plugin.IntroSkipper.Tests/audio/big_buck_bunny_clip.mp3 deleted file mode 100644 index d7132d0..0000000 Binary files a/ConfusedPolarBear.Plugin.IntroSkipper.Tests/audio/big_buck_bunny_clip.mp3 and /dev/null differ diff --git a/ConfusedPolarBear.Plugin.IntroSkipper.Tests/audio/big_buck_bunny_intro.mp3 b/ConfusedPolarBear.Plugin.IntroSkipper.Tests/audio/big_buck_bunny_intro.mp3 deleted file mode 100644 index 95ecf7b..0000000 Binary files a/ConfusedPolarBear.Plugin.IntroSkipper.Tests/audio/big_buck_bunny_intro.mp3 and /dev/null differ diff --git a/ConfusedPolarBear.Plugin.IntroSkipper.Tests/e2e_tests/.gitignore b/ConfusedPolarBear.Plugin.IntroSkipper.Tests/e2e_tests/.gitignore deleted file mode 100644 index 15c88d2..0000000 --- a/ConfusedPolarBear.Plugin.IntroSkipper.Tests/e2e_tests/.gitignore +++ /dev/null @@ -1,14 +0,0 @@ -# Binaries -/verifier/verifier -/run_tests -/plugin_binaries/ - -# Wrapper configuration and base configuration files -config.json -/config/ - -# Timestamp reports -/reports/ - -# Selenium screenshots -selenium/screenshots/ diff --git a/ConfusedPolarBear.Plugin.IntroSkipper.Tests/e2e_tests/README.md b/ConfusedPolarBear.Plugin.IntroSkipper.Tests/e2e_tests/README.md deleted file mode 100644 index d0361be..0000000 --- a/ConfusedPolarBear.Plugin.IntroSkipper.Tests/e2e_tests/README.md +++ /dev/null @@ -1,52 +0,0 @@ -# End to end testing framework - -## wrapper - -The wrapper script (compiled as `run_tests`) runs multiple tests on Jellyfin servers to verify that the plugin works as intended. It tests: - -- Introduction timestamp accuracy (using `verifier`) -- Web interface functionality (using `selenium/main.py`) - -## verifier - -### Description - -This program is responsible for: -* Saving all discovered introduction timestamps into a report -* Comparing two reports against each other to find episodes that: - * Are missing introductions in both reports - * Have introductions in both reports, but with different timestamps - * Newly discovered introductions - * Introductions that were discovered previously, but not anymore -* Validating the schema of returned `Intro` objects from the `/IntroTimestamps` API endpoint - -### Usage examples -* Generate intro timestamp report from a local server: - * `./verifier -address http://127.0.0.1:8096 -key api_key` -* Generate intro timestamp report from a remote server, polling for task completion every 20 seconds: - * `./verifier -address https://example.com -key api_key -poll 20s -o example.json` -* Compare two previously generated reports: - * `./verifier -r1 v0.1.5.json -r2 v0.1.6.json` -* Validate the API schema for three episodes: - * `./verifier -address http://127.0.0.1:8096 -key api_key -validate id1,id2,id3` - -## Selenium web interface tests - -Selenium is used to verify that the plugin's web interface works as expected. It simulates a user: - -* Clicking the skip intro button - * Checks that clicking the button skips the intro and keeps playing the video -* Changing settings (will be added in the future) - * Maximum degree of parallelism - * Selecting libraries for analysis - * EDL settings - * Introduction requirements - * Auto skip - * Show/hide skip prompt -* Timestamp editor (will be added in the future) - * Displays timestamps - * Modifies timestamps - * Erases season timestamps -* Fingerprint visualizer (will be added in the future) - * Suggests shifts - * Visualizer canvas is drawn on diff --git a/ConfusedPolarBear.Plugin.IntroSkipper.Tests/e2e_tests/build.sh b/ConfusedPolarBear.Plugin.IntroSkipper.Tests/e2e_tests/build.sh deleted file mode 100755 index 8a29ab8..0000000 --- a/ConfusedPolarBear.Plugin.IntroSkipper.Tests/e2e_tests/build.sh +++ /dev/null @@ -1,10 +0,0 @@ -#!/bin/bash - -echo "[+] Building timestamp verifier" -(cd verifier && go build -o verifier) || exit 1 - -echo "[+] Building test wrapper" -(cd wrapper && go test ./... && go build -o ../run_tests) || exit 1 - -echo -echo "[+] All programs built successfully" diff --git a/ConfusedPolarBear.Plugin.IntroSkipper.Tests/e2e_tests/config_sample.jsonc b/ConfusedPolarBear.Plugin.IntroSkipper.Tests/e2e_tests/config_sample.jsonc deleted file mode 100644 index a800145..0000000 --- a/ConfusedPolarBear.Plugin.IntroSkipper.Tests/e2e_tests/config_sample.jsonc +++ /dev/null @@ -1,22 +0,0 @@ -{ - "common": { - "library": "/full/path/to/test/library/on/host/TV", - "episode": "Episode title to search for" - }, - "servers": [ - { - "comment": "Optional comment to identify this server", - "image": "ghcr.io/confusedpolarbear/jellyfin-intro-skipper:latest", - "username": "admin", - "password": "hunter2", - "browsers": [ - "chrome", - "firefox" - ], // supported values are "chrome" and "firefox". - "tests": [ - "skip_button", // test skip intro button - "settings" // test plugin administration page - ] - } - ] -} diff --git a/ConfusedPolarBear.Plugin.IntroSkipper.Tests/e2e_tests/docker-compose.yml b/ConfusedPolarBear.Plugin.IntroSkipper.Tests/e2e_tests/docker-compose.yml deleted file mode 100644 index 24da6d2..0000000 --- a/ConfusedPolarBear.Plugin.IntroSkipper.Tests/e2e_tests/docker-compose.yml +++ /dev/null @@ -1,33 +0,0 @@ -version: "3" -services: - chrome: - image: selenium/standalone-chrome:106.0 - shm_size: 2gb - ports: - - 4444:4444 - environment: - - SE_NODE_SESSION_TIMEOUT=10 - - firefox: - image: selenium/standalone-firefox:105.0 - shm_size: 2gb - ports: - - 4445:4444 - environment: - - SE_NODE_SESSION_TIMEOUT=10 - - chrome_video: - image: selenium/video - environment: - - DISPLAY_CONTAINER_NAME=chrome - - FILE_NAME=chrome_video.mp4 - volumes: - - /tmp/selenium/videos:/videos - - firefox_video: - image: selenium/video - environment: - - DISPLAY_CONTAINER_NAME=firefox - - FILE_NAME=firefox_video.mp4 - volumes: - - /tmp/selenium/videos:/videos diff --git a/ConfusedPolarBear.Plugin.IntroSkipper.Tests/e2e_tests/reports/.keep b/ConfusedPolarBear.Plugin.IntroSkipper.Tests/e2e_tests/reports/.keep deleted file mode 100644 index e69de29..0000000 diff --git a/ConfusedPolarBear.Plugin.IntroSkipper.Tests/e2e_tests/selenium/main.py b/ConfusedPolarBear.Plugin.IntroSkipper.Tests/e2e_tests/selenium/main.py deleted file mode 100644 index ceaa25b..0000000 --- a/ConfusedPolarBear.Plugin.IntroSkipper.Tests/e2e_tests/selenium/main.py +++ /dev/null @@ -1,203 +0,0 @@ -import argparse, os, time - -from selenium import webdriver -from selenium.webdriver.common.by import By -from selenium.webdriver.common.keys import Keys - - -# Driver function -def main(): - # Parse CLI arguments and store in a dictionary - parser = argparse.ArgumentParser() - parser.add_argument("-host", help="Jellyfin server address with protocol and port.") - parser.add_argument("-username", help="Username.") - parser.add_argument("-password", help="Password.") - parser.add_argument("-name", help="Name of episode to search for.") - parser.add_argument( - "--tests", help="Space separated list of Selenium tests to run.", type=str, nargs="+" - ) - parser.add_argument( - "--browsers", - help="Space separated list of browsers to run tests with.", - type=str, - nargs="+", - choices=["chrome", "firefox"], - ) - args = parser.parse_args() - - server = { - "host": args.host, - "username": args.username, - "password": args.password, - "episode": args.name, - "browsers": args.browsers, - "tests": args.tests, - } - - # Print the server info for debugging and run the test - print() - print(f"Browsers: {server['browsers']}") - print(f"Address: {server['host']}") - print(f"Username: {server['username']}") - print(f"Episode: \"{server['episode']}\"") - print(f"Tests: {server['tests']}") - print() - - # Setup the list of drivers to run tests with - if server["browsers"] is None: - print("[!] --browsers is required") - exit(1) - - drivers = [] - if "chrome" in server["browsers"]: - drivers = [("http://127.0.0.1:4444", "Chrome")] - if "firefox" in server["browsers"]: - drivers.append(("http://127.0.0.1:4445", "Firefox")) - - # Test with all selected drivers - for driver in drivers: - print(f"[!] Starting new test run using {driver[1]}") - test_server(server, driver[0], driver[1]) - print() - - -# Main server test function -def test_server(server, executor, driver_type): - # Configure Selenium to use a remote driver - print(f"[+] Configuring Selenium to use executor {executor} of type {driver_type}") - - opts = None - if driver_type == "Chrome": - opts = webdriver.ChromeOptions() - elif driver_type == "Firefox": - opts = webdriver.FirefoxOptions() - else: - raise ValueError(f"Unknown driver type {driver_type}") - - driver = webdriver.Remote(command_executor=executor, options=opts) - - try: - # Wait up to two seconds when finding an element before reporting failure - driver.implicitly_wait(2) - - # Login to Jellyfin - driver.get(make_url(server, "/")) - - print(f"[+] Authenticating as {server['username']}") - login(driver, server) - - if "skip_button" in server["tests"]: - # Play the user specified episode and verify skip intro button functionality. This episode is expected to: - # * already have been analyzed for an introduction - # * have an introduction at the beginning of the episode - print("[+] Testing skip intro button") - test_skip_button(driver, server) - - print("[+] All tests completed successfully") - finally: - # Unconditionally end the Selenium session - driver.quit() - - -def login(driver, server): - # Append the Enter key to the password to submit the form - us = server["username"] - pw = server["password"] + Keys.ENTER - - # Fill out and submit the login form - driver.find_element(By.ID, "txtManualName").send_keys(us) - driver.find_element(By.ID, "txtManualPassword").send_keys(pw) - - -def test_skip_button(driver, server): - print(f" [+] Searching for episode \"{server['episode']}\"") - - search = driver.find_element(By.CSS_SELECTOR, ".headerSearchButton span.search") - - if driver.capabilities["browserName"] == "firefox": - # Work around a FF bug where the search element isn't considered clickable right away - time.sleep(1) - - # Click the search button - search.click() - - # Type the episode name - driver.find_element(By.CSS_SELECTOR, ".searchfields-txtSearch").send_keys( - server["episode"] - ) - - # Click the first episode in the search results - driver.find_element( - By.CSS_SELECTOR, ".searchResults button[data-type='Episode']" - ).click() - - # Wait for the episode page to finish loading by searching for the episode description (overview) - driver.find_element(By.CSS_SELECTOR, ".overview") - - print(f" [+] Waiting for playback to start") - - # Click the play button in the toolbar - driver.find_element( - By.CSS_SELECTOR, "div.mainDetailButtons span.play_arrow" - ).click() - - # Wait for playback to start by searching for the lower OSD control bar - driver.find_element(By.CSS_SELECTOR, ".osdControls") - - # Let the video play a little bit so the position before clicking the button can be logged - print(" [+] Playing video") - time.sleep(2) - screenshot(driver, "skip_button_pre_skip") - assert_video_playing(driver) - - # Find the skip intro button and click it, logging the new video position after the seek is preformed - print(" [+] Clicking skip intro button") - driver.find_element(By.CSS_SELECTOR, "div#skipIntro").click() - time.sleep(1) - screenshot(driver, "skip_button_post_skip") - assert_video_playing(driver) - - # Keep playing the video for a few seconds to ensure that: - # * the intro was successfully skipped - # * video playback continued automatically post button click - print(" [+] Verifying post skip position") - time.sleep(4) - - screenshot(driver, "skip_button_post_play") - assert_video_playing(driver) - - -# Utility functions -def make_url(server, url): - final = server["host"] + url - print(f"[+] Navigating to {final}") - return final - - -def screenshot(driver, filename): - dest = f"screenshots/{filename}.png" - driver.save_screenshot(dest) - - -# Returns the current video playback position and if the video is paused. -# Will raise an exception if playback is paused as the video shouldn't ever pause when using this plugin. -def assert_video_playing(driver): - ret = driver.execute_script( - """ - const video = document.querySelector("video"); - return { - "position": video.currentTime, - "paused": video.paused - }; - """ - ) - - if ret["paused"]: - raise Exception("Video should not be paused") - - print(f" [+] Video playback position: {ret['position']}") - - return ret - - -main() diff --git a/ConfusedPolarBear.Plugin.IntroSkipper.Tests/e2e_tests/selenium/requirements.txt b/ConfusedPolarBear.Plugin.IntroSkipper.Tests/e2e_tests/selenium/requirements.txt deleted file mode 100644 index 7978165..0000000 --- a/ConfusedPolarBear.Plugin.IntroSkipper.Tests/e2e_tests/selenium/requirements.txt +++ /dev/null @@ -1 +0,0 @@ -selenium >= 4.3.0 diff --git a/ConfusedPolarBear.Plugin.IntroSkipper.Tests/e2e_tests/selenium/screenshots/.keep b/ConfusedPolarBear.Plugin.IntroSkipper.Tests/e2e_tests/selenium/screenshots/.keep deleted file mode 100644 index e69de29..0000000 diff --git a/ConfusedPolarBear.Plugin.IntroSkipper.Tests/e2e_tests/verifier/go.mod b/ConfusedPolarBear.Plugin.IntroSkipper.Tests/e2e_tests/verifier/go.mod deleted file mode 100644 index 0bc6481..0000000 --- a/ConfusedPolarBear.Plugin.IntroSkipper.Tests/e2e_tests/verifier/go.mod +++ /dev/null @@ -1,3 +0,0 @@ -module github.com/confusedpolarbear/intro_skipper_verifier - -go 1.17 diff --git a/ConfusedPolarBear.Plugin.IntroSkipper.Tests/e2e_tests/verifier/http.go b/ConfusedPolarBear.Plugin.IntroSkipper.Tests/e2e_tests/verifier/http.go deleted file mode 100644 index e3178a8..0000000 --- a/ConfusedPolarBear.Plugin.IntroSkipper.Tests/e2e_tests/verifier/http.go +++ /dev/null @@ -1,78 +0,0 @@ -package main - -import ( - "encoding/json" - "fmt" - "io" - "net/http" - "strings" - "time" - - "github.com/confusedpolarbear/intro_skipper_verifier/structs" -) - -// Gets the contents of the provided URL or panics. -func SendRequest(method, url, apiKey string) []byte { - http.DefaultClient.Timeout = 10 * time.Second - - // Construct the request - req, err := http.NewRequest(method, url, nil) - if err != nil { - panic(err) - } - - // Include the authorization token - req.Header.Set("Authorization", fmt.Sprintf(`MediaBrowser Token="%s"`, apiKey)) - - // Send the request - res, err := http.DefaultClient.Do(req) - - if !strings.Contains(url, "hideUrl") { - fmt.Printf("[+] %s %s: %d\n", method, url, res.StatusCode) - } - - // Panic if any error occurred - if err != nil { - panic(err) - } - - // Check for API key validity - if res.StatusCode == http.StatusUnauthorized { - panic("Server returned 401 (Unauthorized). Check API key validity and try again.") - } - - // Read and return the entire body - defer res.Body.Close() - body, err := io.ReadAll(res.Body) - if err != nil { - panic(err) - } - - return body -} - -func GetServerInfo(hostAddress, apiKey string) structs.PublicInfo { - var info structs.PublicInfo - - fmt.Println("[+] Getting server information") - rawInfo := SendRequest("GET", hostAddress+"/System/Info/Public", apiKey) - - if err := json.Unmarshal(rawInfo, &info); err != nil { - panic(err) - } - - return info -} - -func GetPluginConfiguration(hostAddress, apiKey string) structs.PluginConfiguration { - var config structs.PluginConfiguration - - fmt.Println("[+] Getting plugin configuration") - rawConfig := SendRequest("GET", hostAddress+"/Plugins/c83d86bb-a1e0-4c35-a113-e2101cf4ee6b/Configuration", apiKey) - - if err := json.Unmarshal(rawConfig, &config); err != nil { - panic(err) - } - - return config -} diff --git a/ConfusedPolarBear.Plugin.IntroSkipper.Tests/e2e_tests/verifier/main.go b/ConfusedPolarBear.Plugin.IntroSkipper.Tests/e2e_tests/verifier/main.go deleted file mode 100644 index 5e746ee..0000000 --- a/ConfusedPolarBear.Plugin.IntroSkipper.Tests/e2e_tests/verifier/main.go +++ /dev/null @@ -1,63 +0,0 @@ -package main - -import ( - "flag" - "time" -) - -func flags() { - // Report generation - hostAddress := flag.String("address", "", "Address of Jellyfin server to extract intro information from.") - apiKey := flag.String("key", "", "Administrator API key to authenticate with.") - keepTimestamps := flag.Bool("keep", false, "Keep the current timestamps instead of erasing and reanalyzing.") - pollInterval := flag.Duration("poll", 10*time.Second, "Interval to poll task completion at.") - reportDestination := flag.String("o", "", "Report destination filename. Defaults to intros-ADDRESS-TIMESTAMP.json.") - - // Report comparison - report1 := flag.String("r1", "", "First report.") - report2 := flag.String("r2", "", "Second report.") - - // API schema validator - ids := flag.String("validate", "", "Comma separated item ids to validate the API schema for.") - - // Print usage examples - flag.CommandLine.Usage = func() { - flag.CommandLine.Output().Write([]byte("Flags:\n")) - flag.PrintDefaults() - - usage := "\nUsage:\n" + - "Generate intro timestamp report from a local server:\n" + - "./verifier -address http://127.0.0.1:8096 -key api_key\n\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\n" + - - "Compare two previously generated reports:\n" + - "./verifier -r1 v0.1.5.json -r2 v0.1.6.json\n\n" + - - "Validate the API schema for some item ids:\n" + - "./verifier -address http://127.0.0.1:8096 -key api_key -validate id1,id2,id3\n" - - flag.CommandLine.Output().Write([]byte(usage)) - } - - flag.Parse() - - if *hostAddress != "" && *apiKey != "" { - if *ids == "" { - generateReport(*hostAddress, *apiKey, *reportDestination, *keepTimestamps, *pollInterval) - } else { - validateApiSchema(*hostAddress, *apiKey, *ids) - } - - } else if *report1 != "" && *report2 != "" { - compareReports(*report1, *report2, *reportDestination) - - } else { - panic("Either (-address and -key) or (-r1 and -r2) are required.") - } -} - -func main() { - flags() -} diff --git a/ConfusedPolarBear.Plugin.IntroSkipper.Tests/e2e_tests/verifier/report.html b/ConfusedPolarBear.Plugin.IntroSkipper.Tests/e2e_tests/verifier/report.html deleted file mode 100644 index f030b0d..0000000 --- a/ConfusedPolarBear.Plugin.IntroSkipper.Tests/e2e_tests/verifier/report.html +++ /dev/null @@ -1,389 +0,0 @@ - - - - - - - - - - -
-

Intro Timestamp Differential

- -
-

First report

- - {{ block "ReportInfo" .OldReport }} - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Path{{ .Path }}
Jellyfin{{ .ServerInfo.Version }} on {{ .ServerInfo.OperatingSystem }}
Analysis Settings{{ printAnalysisSettings .PluginConfig }}
Introduction Requirements{{ printIntroductionReqs .PluginConfig }}
Start time{{ printTime .StartedAt }}
End time{{ printTime .FinishedAt }}
Duration{{ printDuration .Runtime }}
- {{ end }} -
- -
-

Second report

- - {{ template "ReportInfo" .NewReport }} -
-
- -
-

Statistics

- - - - - - - - - - - - - - - - - - - - - - - - -
Total episodes
Never found
Changed
Gains
Losses
-
- -
-

Settings

- -
- -
- - -
- - -
-
-
- - {{/* store a reference to the data before the range query */}} - {{ $p := . }} - - {{/* sort the show names and iterate over them */}} - {{ range $name := sortShows .OldReport.Shows }} -
-
- {{/* get the unsorted seasons for this show */}} - {{ $seasons := index $p.OldReport.Shows $name }} - - {{/* log the show name and number of seasons */}} - - - {{ $name }} - - - - -
- {{/* sort the seasons to ensure they display in numerical order */}} - {{ range $seasonNumber := (sortSeasons $seasons) }} -
-
- - - Season {{ $seasonNumber }} - - - - - {{/* compare each episode in the old report to the same episode in the new report */}} - {{ range $episode := index $seasons $seasonNumber }} - - {{/* lookup and compare both episodes */}} - {{ $comparison := compareEpisodes $episode.EpisodeId $p }} - {{ $old := $comparison.Old }} - {{ $new := $comparison.New }} - - {{/* set attributes indicating if an intro was found in the old and new reports */}} -
-

{{ $episode.Title }}

- -

- Old: {{ $old.FormattedStart }} - {{ $old.FormattedEnd }} - ({{ $old.Duration }}) - (valid: {{ $old.Valid }})
- - New: {{ $new.FormattedStart }} - {{ $new.FormattedEnd }} - ({{ $new.Duration }}) - (valid: {{ $new.Valid }})
- - {{ if ne $comparison.WarningShort "okay" }} - Warning: {{ $comparison.Warning }} - {{ end }} -

- -
-
- {{ end }} -
-
- {{ end }} -
-
-
- {{ end }} - - - - - diff --git a/ConfusedPolarBear.Plugin.IntroSkipper.Tests/e2e_tests/verifier/report_comparison.go b/ConfusedPolarBear.Plugin.IntroSkipper.Tests/e2e_tests/verifier/report_comparison.go deleted file mode 100644 index 92c62e1..0000000 --- a/ConfusedPolarBear.Plugin.IntroSkipper.Tests/e2e_tests/verifier/report_comparison.go +++ /dev/null @@ -1,139 +0,0 @@ -package main - -import ( - _ "embed" - "encoding/json" - "fmt" - "html/template" - "math" - "os" - "time" - - "github.com/confusedpolarbear/intro_skipper_verifier/structs" -) - -//go:embed report.html -var reportTemplate []byte - -func compareReports(oldReportPath, newReportPath, destination string) { - start := time.Now() - - // Populate the destination filename if none was provided - if destination == "" { - destination = fmt.Sprintf("report-%d.html", start.Unix()) - } - - // Open the report for writing - f, err := os.OpenFile(destination, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600) - if err != nil { - panic(err) - } else { - defer f.Close() - } - - fmt.Printf("Started at: %s\n", start.Format(time.RFC1123)) - fmt.Printf("First report: %s\n", oldReportPath) - fmt.Printf("Second report: %s\n", newReportPath) - fmt.Printf("Destination: %s\n\n", destination) - - // Unmarshal both reports - oldReport, newReport := unmarshalReport(oldReportPath), unmarshalReport(newReportPath) - - fmt.Println("[+] Comparing reports") - - // Setup a function map with helper functions to use in the template - tmp := template.New("report") - - funcs := make(template.FuncMap) - - funcs["printTime"] = func(t time.Time) string { - return t.Format(time.RFC1123) - } - - funcs["printDuration"] = func(d time.Duration) string { - return d.Round(time.Second).String() - } - - funcs["printAnalysisSettings"] = func(pc structs.PluginConfiguration) string { - return pc.AnalysisSettings() - } - - funcs["printIntroductionReqs"] = func(pc structs.PluginConfiguration) string { - return pc.IntroductionRequirements() - } - - funcs["sortShows"] = templateSortShows - funcs["sortSeasons"] = templateSortSeason - funcs["compareEpisodes"] = templateCompareEpisodes - tmp.Funcs(funcs) - - // Load the template or panic - report := template.Must(tmp.Parse(string(reportTemplate))) - - err = report.Execute(f, - structs.TemplateReportData{ - OldReport: oldReport, - NewReport: newReport, - }) - - if err != nil { - panic(err) - } - - // Log success - fmt.Printf("[+] Reports successfully compared in %s\n", time.Since(start).Round(time.Millisecond)) -} - -func unmarshalReport(path string) structs.Report { - // Read the provided report - contents, err := os.ReadFile(path) - if err != nil { - panic(err) - } - - // Unmarshal - var report structs.Report - if err := json.Unmarshal(contents, &report); err != nil { - panic(err) - } - - // Setup maps and template data for later use - report.Path = path - report.Shows = make(map[string]structs.Seasons) - report.IntroMap = make(map[string]structs.Intro) - - // Sort episodes by show and season - for _, intro := range report.Intros { - // Round the duration to the nearest second to avoid showing 8 decimal places in the report - intro.Duration = float32(math.Round(float64(intro.Duration))) - - // Pretty print the intro start and end times - intro.FormattedStart = (time.Duration(intro.IntroStart) * time.Second).String() - intro.FormattedEnd = (time.Duration(intro.IntroEnd) * time.Second).String() - - show, season := intro.Series, intro.Season - - // If this show hasn't been seen before, allocate space for it - if _, ok := report.Shows[show]; !ok { - report.Shows[show] = make(structs.Seasons) - } - - // Store this intro in the season of this show - episodes := report.Shows[show][season] - episodes = append(episodes, intro) - report.Shows[show][season] = episodes - - // Store a reference to this intro in a lookup table - report.IntroMap[intro.EpisodeId] = intro - } - - // Print report info - fmt.Printf("Report %s:\n", path) - fmt.Printf("Generated with Jellyfin %s running on %s\n", report.ServerInfo.Version, report.ServerInfo.OperatingSystem) - fmt.Printf("Analysis settings: %s\n", report.PluginConfig.AnalysisSettings()) - fmt.Printf("Introduction reqs: %s\n", report.PluginConfig.IntroductionRequirements()) - fmt.Printf("Episodes analyzed: %d\n", len(report.Intros)) - fmt.Println() - - return report -} diff --git a/ConfusedPolarBear.Plugin.IntroSkipper.Tests/e2e_tests/verifier/report_comparison_util.go b/ConfusedPolarBear.Plugin.IntroSkipper.Tests/e2e_tests/verifier/report_comparison_util.go deleted file mode 100644 index ec51c6a..0000000 --- a/ConfusedPolarBear.Plugin.IntroSkipper.Tests/e2e_tests/verifier/report_comparison_util.go +++ /dev/null @@ -1,81 +0,0 @@ -package main - -import ( - "fmt" - "math" - "sort" - - "github.com/confusedpolarbear/intro_skipper_verifier/structs" -) - -// report template helper functions - -// Sort show names alphabetically -func templateSortShows(shows map[string]structs.Seasons) []string { - var showNames []string - - for show := range shows { - showNames = append(showNames, show) - } - - sort.Strings(showNames) - - return showNames -} - -// Sort season numbers -func templateSortSeason(show structs.Seasons) []int { - var keys []int - - for season := range show { - keys = append(keys, season) - } - - sort.Ints(keys) - - return keys -} - -// Compare the episode with the provided ID in the old report to the episode in the new report. -func templateCompareEpisodes(id string, reports structs.TemplateReportData) structs.IntroPair { - var pair structs.IntroPair - var tolerance int = 5 - - // Locate both episodes - pair.Old = reports.OldReport.IntroMap[id] - pair.New = reports.NewReport.IntroMap[id] - - // Mark the timestamps as similar if they are within a few seconds of each other - similar := func(oldTime, newTime float32) bool { - diff := math.Abs(float64(newTime) - float64(oldTime)) - return diff <= float64(tolerance) - } - - if pair.Old.Valid && !pair.New.Valid { - // If an intro was found previously, but not now, flag it - pair.WarningShort = "only_previous" - pair.Warning = "Introduction found in previous report, but not the current one" - - } else if !pair.Old.Valid && pair.New.Valid { - // If an intro was not found previously, but found now, flag it - pair.WarningShort = "improvement" - pair.Warning = "New introduction discovered" - - } else if !pair.Old.Valid && !pair.New.Valid { - // If an intro has never been found for this episode - pair.WarningShort = "missing" - pair.Warning = "No introduction has ever been found for this episode" - - } else if !similar(pair.Old.IntroStart, pair.New.IntroStart) || !similar(pair.Old.IntroEnd, pair.New.IntroEnd) { - // If the intro timestamps are too different, flag it - pair.WarningShort = "different" - pair.Warning = fmt.Sprintf("Timestamps differ by more than %d seconds", tolerance) - - } else { - // No warning was generated - pair.WarningShort = "okay" - pair.Warning = "Okay" - } - - return pair -} diff --git a/ConfusedPolarBear.Plugin.IntroSkipper.Tests/e2e_tests/verifier/report_generator.go b/ConfusedPolarBear.Plugin.IntroSkipper.Tests/e2e_tests/verifier/report_generator.go deleted file mode 100644 index 2c4fb3b..0000000 --- a/ConfusedPolarBear.Plugin.IntroSkipper.Tests/e2e_tests/verifier/report_generator.go +++ /dev/null @@ -1,180 +0,0 @@ -package main - -import ( - "encoding/json" - "fmt" - "os" - "os/exec" - "strings" - "time" - - "github.com/confusedpolarbear/intro_skipper_verifier/structs" -) - -var spinners []string -var spinnerIndex int - -func generateReport(hostAddress, apiKey, reportDestination string, keepTimestamps bool, pollInterval time.Duration) { - start := time.Now() - - // Setup the spinner - spinners = strings.Split("⣷⣯⣟⡿⢿⣻⣽⣾", "") - spinnerIndex = -1 // start the spinner on the first graphic - - // Setup the filename to save intros to - if reportDestination == "" { - reportDestination = fmt.Sprintf("intros-%s-%d.json", hostAddress, time.Now().Unix()) - reportDestination = strings.ReplaceAll(reportDestination, "http://", "") - reportDestination = strings.ReplaceAll(reportDestination, "https://", "") - } - - // Ensure the file is writable - if err := os.WriteFile(reportDestination, nil, 0600); err != nil { - panic(err) - } - - fmt.Printf("Started at: %s\n", start.Format(time.RFC1123)) - fmt.Printf("Address: %s\n", hostAddress) - fmt.Printf("Destination: %s\n", reportDestination) - fmt.Println() - - // Get Jellyfin server information and plugin configuration - info := GetServerInfo(hostAddress, apiKey) - config := GetPluginConfiguration(hostAddress, apiKey) - fmt.Println() - - fmt.Printf("Jellyfin OS: %s\n", info.OperatingSystem) - fmt.Printf("Jellyfin version: %s\n", info.Version) - fmt.Printf("Analysis settings: %s\n", config.AnalysisSettings()) - fmt.Printf("Introduction reqs: %s\n", config.IntroductionRequirements()) - fmt.Printf("Erase timestamps: %t\n", !keepTimestamps) - fmt.Println() - - // If not keeping timestamps, run the fingerprint task. - // Otherwise, log that the task isn't being run - if !keepTimestamps { - runAnalysisAndWait(hostAddress, apiKey, pollInterval) - } else { - fmt.Println("[+] Using previously discovered intros") - } - fmt.Println() - - // Save all intros from the server - fmt.Println("[+] Saving intros") - - var report structs.Report - rawIntros := SendRequest("GET", hostAddress+"/Intros/All", apiKey) - if err := json.Unmarshal(rawIntros, &report.Intros); err != nil { - panic(err) - } - - // Calculate the durations of all intros - for i := range report.Intros { - intro := report.Intros[i] - intro.Duration = intro.IntroEnd - intro.IntroStart - report.Intros[i] = intro - } - - fmt.Println() - fmt.Println("[+] Saving report") - - // Store timing data, server information, and plugin configuration - report.StartedAt = start - report.FinishedAt = time.Now() - report.Runtime = report.FinishedAt.Sub(report.StartedAt) - report.ServerInfo = info - report.PluginConfig = config - - // Marshal the report - marshalled, err := json.Marshal(report) - if err != nil { - panic(err) - } - - if err := os.WriteFile(reportDestination, marshalled, 0600); err != nil { - panic(err) - } - - // Change report permissions - exec.Command("chown", "1000:1000", reportDestination).Run() - - fmt.Println("[+] Done") -} - -func runAnalysisAndWait(hostAddress, apiKey string, pollInterval time.Duration) { - var taskId string = "" - - type taskInfo struct { - State string - CurrentProgressPercentage int - } - - fmt.Println("[+] Erasing previously discovered intros") - SendRequest("POST", hostAddress+"/Intros/EraseTimestamps", apiKey) - fmt.Println() - - var taskIds = []string{ - "f64d8ad58e3d7b98548e1a07697eb100", // v0.1.8 - "8863329048cc357f7dfebf080f2fe204", - "6adda26c5261c40e8fa4a7e7df568be2"} - - fmt.Println("[+] Starting analysis task") - for _, id := range taskIds { - body := SendRequest("POST", hostAddress+"/ScheduledTasks/Running/"+id, apiKey) - fmt.Println() - - // If the scheduled task was found, store the task ID for later - if !strings.Contains(string(body), "Not Found") { - taskId = id - break - } - } - - if taskId == "" { - panic("unable to find scheduled task") - } - - fmt.Println("[+] Waiting for analysis task to complete") - fmt.Print("[+] Episodes analyzed: 0%") - - var info taskInfo // Last known scheduled task state - var lastQuery time.Time // Time the task info was last updated - - for { - time.Sleep(500 * time.Millisecond) - - // Update the spinner - if spinnerIndex++; spinnerIndex >= len(spinners) { - spinnerIndex = 0 - } - - fmt.Printf("\r[%s] Episodes analyzed: %d%%", spinners[spinnerIndex], info.CurrentProgressPercentage) - - if info.CurrentProgressPercentage == 100 { - fmt.Printf("\r[+]") // reset the spinner - fmt.Println() - break - } - - // Get the latest task state & unmarshal (only if enough time has passed since the last update) - if time.Since(lastQuery) <= pollInterval { - continue - } - - lastQuery = time.Now() - - raw := SendRequest("GET", hostAddress+"/ScheduledTasks/"+taskId+"?hideUrl=1", apiKey) - - if err := json.Unmarshal(raw, &info); err != nil { - fmt.Printf("[!] Unable to unmarshal response into taskInfo struct: %s\n", err) - fmt.Printf("%s\n", raw) - continue - } - - // Print the latest task state - switch info.State { - case "Idle": - info.CurrentProgressPercentage = 100 - } - } -} diff --git a/ConfusedPolarBear.Plugin.IntroSkipper.Tests/e2e_tests/verifier/schema_validation.go b/ConfusedPolarBear.Plugin.IntroSkipper.Tests/e2e_tests/verifier/schema_validation.go deleted file mode 100644 index 0ae77dd..0000000 --- a/ConfusedPolarBear.Plugin.IntroSkipper.Tests/e2e_tests/verifier/schema_validation.go +++ /dev/null @@ -1,116 +0,0 @@ -package main - -import ( - "encoding/json" - "fmt" - "strings" - "time" - - "github.com/confusedpolarbear/intro_skipper_verifier/structs" -) - -// Given a comma separated list of item IDs, validate the returned API schema. -func validateApiSchema(hostAddress, apiKey, rawIds string) { - // Iterate over the raw item IDs and validate the schema of API responses - ids := strings.Split(rawIds, ",") - - start := time.Now() - - fmt.Printf("Started at: %s\n", start.Format(time.RFC1123)) - fmt.Printf("Address: %s\n", hostAddress) - fmt.Println() - - // Get Jellyfin server information - info := GetServerInfo(hostAddress, apiKey) - fmt.Println() - - fmt.Printf("Jellyfin OS: %s\n", info.OperatingSystem) - fmt.Printf("Jellyfin version: %s\n", info.Version) - fmt.Println() - - for _, id := range ids { - fmt.Printf("[+] Validating item %s\n", id) - - fmt.Println(" [+] Validating API v1 (implicitly versioned)") - intro, schema := getTimestampsV1(hostAddress, apiKey, id, "") - validateV1Intro(id, intro, schema) - - fmt.Println(" [+] Validating API v1 (explicitly versioned)") - intro, schema = getTimestampsV1(hostAddress, apiKey, id, "v1") - validateV1Intro(id, intro, schema) - - fmt.Println() - } - - fmt.Printf("Validated %d items in %s\n", len(ids), time.Since(start).Round(time.Millisecond)) -} - -// Validates the returned intro object, panicking on any error. -func validateV1Intro(id string, intro structs.Intro, schema map[string]interface{}) { - // Validate the item ID - if intro.EpisodeId != id { - panic(fmt.Sprintf("Intro struct has incorrect item ID. Expected '%s', found '%s'", id, intro.EpisodeId)) - } - - // Validate the intro start and end times - if intro.IntroStart < 0 || intro.IntroEnd < 0 { - panic("Intro struct has a negative intro start or end time") - } - - if intro.ShowSkipPromptAt > intro.IntroStart { - panic("Intro struct show prompt time is after intro start") - } - - if intro.HideSkipPromptAt > intro.IntroEnd { - panic("Intro struct hide prompt time is after intro end") - } - - // Validate the intro duration - if duration := intro.IntroEnd - intro.IntroStart; duration < 15 { - panic(fmt.Sprintf("Intro struct has duration %0.2f but the minimum allowed is 15", duration)) - } - - // Ensure the intro is marked as valid. - if !intro.Valid { - panic("Intro struct is not marked as valid") - } - - // Check for any extraneous properties - allowedProperties := []string{"EpisodeId", "Valid", "IntroStart", "IntroEnd", "ShowSkipPromptAt", "HideSkipPromptAt"} - - for schemaKey := range schema { - okay := false - - for _, allowed := range allowedProperties { - if allowed == schemaKey { - okay = true - break - } - } - - if !okay { - panic(fmt.Sprintf("Intro object contains unknown key '%s'", schemaKey)) - } - } -} - -// Gets the timestamps for the provided item or panics. -func getTimestampsV1(hostAddress, apiKey, id, version string) (structs.Intro, map[string]interface{}) { - var rawResponse map[string]interface{} - var intro structs.Intro - - // Make an authenticated GET request to {Host}/Episode/{ItemId}/IntroTimestamps/{Version} - raw := SendRequest("GET", fmt.Sprintf("%s/Episode/%s/IntroTimestamps/%s?hideUrl=1", hostAddress, id, version), apiKey) - - // Unmarshal the response as a version 1 API response, ignoring any unknown fields. - if err := json.Unmarshal(raw, &intro); err != nil { - panic(err) - } - - // Second, unmarshal the response into a map so that any unknown fields can be detected and alerted on. - if err := json.Unmarshal(raw, &rawResponse); err != nil { - panic(err) - } - - return intro, rawResponse -} diff --git a/ConfusedPolarBear.Plugin.IntroSkipper.Tests/e2e_tests/verifier/structs/intro.go b/ConfusedPolarBear.Plugin.IntroSkipper.Tests/e2e_tests/verifier/structs/intro.go deleted file mode 100644 index 1a6ba2c..0000000 --- a/ConfusedPolarBear.Plugin.IntroSkipper.Tests/e2e_tests/verifier/structs/intro.go +++ /dev/null @@ -1,20 +0,0 @@ -package structs - -type Intro struct { - EpisodeId string - - Series string - Season int - Title string - - IntroStart float32 - IntroEnd float32 - Duration float32 - Valid bool - - FormattedStart string - FormattedEnd string - - ShowSkipPromptAt float32 - HideSkipPromptAt float32 -} diff --git a/ConfusedPolarBear.Plugin.IntroSkipper.Tests/e2e_tests/verifier/structs/plugin_configuration.go b/ConfusedPolarBear.Plugin.IntroSkipper.Tests/e2e_tests/verifier/structs/plugin_configuration.go deleted file mode 100644 index 594e910..0000000 --- a/ConfusedPolarBear.Plugin.IntroSkipper.Tests/e2e_tests/verifier/structs/plugin_configuration.go +++ /dev/null @@ -1,44 +0,0 @@ -package structs - -import ( - "fmt" - "strings" -) - -type PluginConfiguration struct { - CacheFingerprints bool - MaxParallelism int - SelectedLibraries string - - AnalysisPercent int - AnalysisLengthLimit int - MinimumIntroDuration int -} - -func (c PluginConfiguration) AnalysisSettings() string { - // If no libraries have been selected, display a star. - // Otherwise, quote each library before displaying the slice. - var libs []string - if c.SelectedLibraries == "" { - libs = []string{"*"} - } else { - for _, tmp := range strings.Split(c.SelectedLibraries, ",") { - tmp = `"` + strings.TrimSpace(tmp) + `"` - libs = append(libs, tmp) - } - } - - return fmt.Sprintf( - "cfp=%t thr=%d lbs=%v", - c.CacheFingerprints, - c.MaxParallelism, - libs) -} - -func (c PluginConfiguration) IntroductionRequirements() string { - return fmt.Sprintf( - "per=%d%% max=%dm min=%ds", - c.AnalysisPercent, - c.AnalysisLengthLimit, - c.MinimumIntroDuration) -} diff --git a/ConfusedPolarBear.Plugin.IntroSkipper.Tests/e2e_tests/verifier/structs/public_info.go b/ConfusedPolarBear.Plugin.IntroSkipper.Tests/e2e_tests/verifier/structs/public_info.go deleted file mode 100644 index 901307d..0000000 --- a/ConfusedPolarBear.Plugin.IntroSkipper.Tests/e2e_tests/verifier/structs/public_info.go +++ /dev/null @@ -1,6 +0,0 @@ -package structs - -type PublicInfo struct { - Version string - OperatingSystem string -} diff --git a/ConfusedPolarBear.Plugin.IntroSkipper.Tests/e2e_tests/verifier/structs/report.go b/ConfusedPolarBear.Plugin.IntroSkipper.Tests/e2e_tests/verifier/structs/report.go deleted file mode 100644 index 5e8a3b3..0000000 --- a/ConfusedPolarBear.Plugin.IntroSkipper.Tests/e2e_tests/verifier/structs/report.go +++ /dev/null @@ -1,48 +0,0 @@ -package structs - -import "time" - -type Seasons map[int][]Intro - -type Report struct { - Path string `json:"-"` - - StartedAt time.Time - FinishedAt time.Time - Runtime time.Duration - - ServerInfo PublicInfo - PluginConfig PluginConfiguration - - Intros []Intro - - // Intro lookup table. Only populated when loading a report. - IntroMap map[string]Intro `json:"-"` - - // Intros which have been sorted by show and season number. Only populated when loading a report. - Shows map[string]Seasons `json:"-"` -} - -// Data passed to the report template. -type TemplateReportData struct { - // First report. - OldReport Report - - // Second report. - NewReport Report -} - -// A pair of introductions from an old and new reports. -type IntroPair struct { - Old Intro - New Intro - - // Recognized warning types: - // * okay: no warning - // * different: timestamps are too dissimilar - // * only_previous: introduction found in old report but not new one - WarningShort string - - // If this pair of intros is not okay, a short description about the cause - Warning string -} diff --git a/ConfusedPolarBear.Plugin.IntroSkipper.Tests/e2e_tests/wrapper/exec.go b/ConfusedPolarBear.Plugin.IntroSkipper.Tests/e2e_tests/wrapper/exec.go deleted file mode 100644 index 488cd52..0000000 --- a/ConfusedPolarBear.Plugin.IntroSkipper.Tests/e2e_tests/wrapper/exec.go +++ /dev/null @@ -1,64 +0,0 @@ -package main - -import ( - "bufio" - "context" - "fmt" - "io" - "os/exec" - "regexp" - "strings" - "time" -) - -// Run an external program -func RunProgram(program string, args []string, timeout time.Duration) { - // Flag if we are starting or stopping a container - managingContainer := program == "docker" - - // Create context and command - ctx, cancel := context.WithTimeout(context.Background(), timeout) - defer cancel() - cmd := exec.CommandContext(ctx, program, args...) - - // Stringify and censor the program's arguments - strArgs := redactString(strings.Join(args, " ")) - fmt.Printf(" [+] Running %s %s\n", program, strArgs) - - // Setup pipes - stdout, err := cmd.StdoutPipe() - if err != nil { - panic(err) - } - - stderr, err := cmd.StderrPipe() - if err != nil { - panic(err) - } - - // Start the command - if err := cmd.Start(); err != nil { - panic(err) - } - - // Stream any messages to the terminal - for _, r := range []io.Reader{stdout, stderr} { - // Don't log stdout from the container - if managingContainer && r == stdout { - continue - } - - scanner := bufio.NewScanner(r) - scanner.Split(bufio.ScanRunes) - - for scanner.Scan() { - fmt.Print(scanner.Text()) - } - } -} - -// Redacts sensitive command line arguments. -func redactString(raw string) string { - redactionRegex := regexp.MustCompilePOSIX(`-(user|pass|key) [^ ]+`) - return redactionRegex.ReplaceAllString(raw, "-$1 REDACTED") -} diff --git a/ConfusedPolarBear.Plugin.IntroSkipper.Tests/e2e_tests/wrapper/exec_test.go b/ConfusedPolarBear.Plugin.IntroSkipper.Tests/e2e_tests/wrapper/exec_test.go deleted file mode 100644 index ba25cae..0000000 --- a/ConfusedPolarBear.Plugin.IntroSkipper.Tests/e2e_tests/wrapper/exec_test.go +++ /dev/null @@ -1,13 +0,0 @@ -package main - -import "testing" - -func TestStringRedaction(t *testing.T) { - raw := "-key deadbeef -first second -user admin -third fourth -pass hunter2" - expected := "-key REDACTED -first second -user REDACTED -third fourth -pass REDACTED" - actual := redactString(raw) - - if expected != actual { - t.Errorf(`String was redacted incorrectly: "%s"`, actual) - } -} diff --git a/ConfusedPolarBear.Plugin.IntroSkipper.Tests/e2e_tests/wrapper/go.mod b/ConfusedPolarBear.Plugin.IntroSkipper.Tests/e2e_tests/wrapper/go.mod deleted file mode 100644 index d3ab43e..0000000 --- a/ConfusedPolarBear.Plugin.IntroSkipper.Tests/e2e_tests/wrapper/go.mod +++ /dev/null @@ -1,3 +0,0 @@ -module github.com/confusedpolarbear/intro_skipper_wrapper - -go 1.17 diff --git a/ConfusedPolarBear.Plugin.IntroSkipper.Tests/e2e_tests/wrapper/library.json b/ConfusedPolarBear.Plugin.IntroSkipper.Tests/e2e_tests/wrapper/library.json deleted file mode 100644 index 5c35999..0000000 --- a/ConfusedPolarBear.Plugin.IntroSkipper.Tests/e2e_tests/wrapper/library.json +++ /dev/null @@ -1,93 +0,0 @@ -{ - "LibraryOptions": { - "EnableArchiveMediaFiles": false, - "EnablePhotos": false, - "EnableRealtimeMonitor": false, - "ExtractChapterImagesDuringLibraryScan": false, - "EnableChapterImageExtraction": false, - "EnableInternetProviders": false, - "SaveLocalMetadata": false, - "EnableAutomaticSeriesGrouping": false, - "PreferredMetadataLanguage": "", - "MetadataCountryCode": "", - "SeasonZeroDisplayName": "Specials", - "AutomaticRefreshIntervalDays": 0, - "EnableEmbeddedTitles": false, - "EnableEmbeddedEpisodeInfos": false, - "AllowEmbeddedSubtitles": "AllowAll", - "SkipSubtitlesIfEmbeddedSubtitlesPresent": false, - "SkipSubtitlesIfAudioTrackMatches": false, - "SaveSubtitlesWithMedia": true, - "RequirePerfectSubtitleMatch": true, - "AutomaticallyAddToCollection": false, - "MetadataSavers": [], - "TypeOptions": [ - { - "Type": "Series", - "MetadataFetchers": [ - "TheMovieDb", - "The Open Movie Database" - ], - "MetadataFetcherOrder": [ - "TheMovieDb", - "The Open Movie Database" - ], - "ImageFetchers": [ - "TheMovieDb" - ], - "ImageFetcherOrder": [ - "TheMovieDb" - ] - }, - { - "Type": "Season", - "MetadataFetchers": [ - "TheMovieDb" - ], - "MetadataFetcherOrder": [ - "TheMovieDb" - ], - "ImageFetchers": [ - "TheMovieDb" - ], - "ImageFetcherOrder": [ - "TheMovieDb" - ] - }, - { - "Type": "Episode", - "MetadataFetchers": [ - "TheMovieDb", - "The Open Movie Database" - ], - "MetadataFetcherOrder": [ - "TheMovieDb", - "The Open Movie Database" - ], - "ImageFetchers": [ - "TheMovieDb", - "The Open Movie Database", - "Embedded Image Extractor", - "Screen Grabber" - ], - "ImageFetcherOrder": [ - "TheMovieDb", - "The Open Movie Database", - "Embedded Image Extractor", - "Screen Grabber" - ] - } - ], - "LocalMetadataReaderOrder": [ - "Nfo" - ], - "SubtitleDownloadLanguages": [], - "DisabledSubtitleFetchers": [], - "SubtitleFetcherOrder": [], - "PathInfos": [ - { - "Path": "/media/TV" - } - ] - } -} diff --git a/ConfusedPolarBear.Plugin.IntroSkipper.Tests/e2e_tests/wrapper/main.go b/ConfusedPolarBear.Plugin.IntroSkipper.Tests/e2e_tests/wrapper/main.go deleted file mode 100644 index 3d1a477..0000000 --- a/ConfusedPolarBear.Plugin.IntroSkipper.Tests/e2e_tests/wrapper/main.go +++ /dev/null @@ -1,380 +0,0 @@ -package main - -import ( - "bufio" - "bytes" - "crypto/rand" - "encoding/hex" - "encoding/json" - "flag" - "fmt" - "io" - "net/http" - "os" - "path" - "strings" - "time" -) - -// IP address to use when connecting to local containers. -var containerAddress string - -// Path to compiled plugin DLL to install in local containers. -var pluginPath string - -// Randomly generated password used to setup container with. -var containerPassword string - -func flags() { - flag.StringVar(&pluginPath, "dll", "", "Path to plugin DLL to install in container images.") - flag.StringVar(&containerAddress, "caddr", "", "IP address to use when connecting to local containers.") - flag.Parse() - - // Randomize the container's password - rawPassword := make([]byte, 32) - if _, err := rand.Read(rawPassword); err != nil { - panic(err) - } - - containerPassword = hex.EncodeToString(rawPassword) -} - -func main() { - flags() - - start := time.Now() - - fmt.Printf("[+] Start time: %s\n", start) - - // Load list of servers - fmt.Println("[+] Loading configuration") - config := loadConfiguration() - fmt.Println() - - // Start Selenium by bringing up the compose file in detatched mode - fmt.Println("[+] Starting Selenium") - RunProgram("docker-compose", []string{"up", "-d"}, 10*time.Second) - - // If any error occurs, bring Selenium down before exiting - defer func() { - fmt.Println("[+] Stopping Selenium") - RunProgram("docker-compose", []string{"down"}, 15*time.Second) - }() - - // Test all provided Jellyfin servers - for _, server := range config.Servers { - if server.Skip { - continue - } - - var configurationDirectory string - var apiKey string - var seleniumArgs []string - - // LSIO containers use some slighly different paths & permissions - lsioImage := strings.Contains(server.Image, "linuxserver") - - fmt.Println() - fmt.Printf("[+] Testing %s\n", server.Comment) - - if server.Docker { - var err error - - // Setup a temporary folder for the container's configuration - configurationDirectory, err = os.MkdirTemp("/dev/shm", "jf-e2e-*") - if err != nil { - panic(err) - } - - // Create a folder to install the plugin into - pluginDirectory := path.Join(configurationDirectory, "plugins", "intro-skipper") - if lsioImage { - pluginDirectory = path.Join(configurationDirectory, "data", "plugins", "intro-skipper") - } - - fmt.Println(" [+] Creating plugin directory") - if err := os.MkdirAll(pluginDirectory, 0700); err != nil { - fmt.Printf(" [!] Failed to create plugin directory: %s\n", err) - goto cleanup - } - - // If this is an LSIO container, adjust the permissions on the plugin directory - if lsioImage { - RunProgram( - "chown", - []string{ - "911:911", - "-R", - path.Join(configurationDirectory, "data", "plugins")}, - 2*time.Second) - } - - // Install the plugin - fmt.Printf(" [+] Copying plugin %s to %s\n", pluginPath, pluginDirectory) - RunProgram("cp", []string{pluginPath, pluginDirectory}, 2*time.Second) - fmt.Println() - - /* Start the container with the following settings: - * Name: jf-e2e - * Port: 8097 - * Media: Mounted to /media, read only - */ - containerArgs := []string{"run", "--name", "jf-e2e", "--rm", "-p", "8097:8096", - "-v", fmt.Sprintf("%s:%s:rw", configurationDirectory, "/config"), - "-v", fmt.Sprintf("%s:%s:ro", config.Common.Library, "/media"), - server.Image} - - fmt.Printf(" [+] Starting container %s\n", server.Image) - go RunProgram("docker", containerArgs, 60*time.Second) - - // Wait for the container to fully start - waitForServerStartup(server.Address) - fmt.Println() - - fmt.Println(" [+] Setting up container") - - // Set up the container - SetupServer(server.Address, containerPassword) - - // Restart the container and wait for it to come back up - RunProgram("docker", []string{"restart", "jf-e2e"}, 10*time.Second) - time.Sleep(time.Second) - waitForServerStartup(server.Address) - fmt.Println() - } else { - fmt.Println("[+] Remote instance, assuming plugin is already installed") - } - - // Get an API key - apiKey = login(server) - - // Rescan the library if this is a server that we just setup - if server.Docker { - fmt.Println(" [+] Rescanning library") - - sendRequest( - server.Address+"/ScheduledTasks/Running/7738148ffcd07979c7ceb148e06b3aed?api_key="+apiKey, - "POST", - "") - - // TODO: poll for task completion - time.Sleep(10 * time.Second) - - fmt.Println() - } - - // Analyze episodes and save report - fmt.Println(" [+] Analyzing episodes") - fmt.Print("\033[37;1m") // change the color of the verifier's text - RunProgram( - "./verifier/verifier", - []string{ - "-address", server.Address, - "-key", apiKey, "-o", - fmt.Sprintf("reports/%s-%d.json", server.Comment, start.Unix())}, - 5*time.Minute) - fmt.Print("\033[39;0m") // reset terminal text color - - // Pause for any manual tests - if server.ManualTests { - fmt.Println(" [!] Pausing for manual tests") - reader := bufio.NewReader(os.Stdin) - reader.ReadString('\n') - } - - // Setup base Selenium arguments - seleniumArgs = []string{ - "-u", // force stdout to be unbuffered - "main.py", - "-host", server.Address, - "-user", server.Username, - "-pass", server.Password, - "-name", config.Common.Episode} - - // Append all requested Selenium tests - seleniumArgs = append(seleniumArgs, "--tests") - seleniumArgs = append(seleniumArgs, server.Tests...) - - // Append all requested browsers - seleniumArgs = append(seleniumArgs, "--browsers") - seleniumArgs = append(seleniumArgs, server.Browsers...) - - // Run Selenium - os.Chdir("selenium") - RunProgram("python3", seleniumArgs, time.Minute) - os.Chdir("..") - - cleanup: - if server.Docker { - // Stop the container - fmt.Println(" [+] Stopping and removing container") - RunProgram("docker", []string{"stop", "jf-e2e"}, 10*time.Second) - - // Cleanup the container's configuration - fmt.Printf(" [+] Deleting %s\n", configurationDirectory) - - if err := os.RemoveAll(configurationDirectory); err != nil { - panic(err) - } - } - } -} - -// Login to the specified Jellyfin server and return an API key -func login(server Server) string { - type AuthenticateUserByName struct { - AccessToken string - } - - fmt.Println(" [+] Sending authentication request") - - // Create request body - rawBody := fmt.Sprintf(`{"Username":"%s","Pw":"%s"}`, server.Username, server.Password) - body := bytes.NewBufferString(rawBody) - - // Create the request - req, err := http.NewRequest( - "POST", - fmt.Sprintf("%s/Users/AuthenticateByName", server.Address), - body) - - if err != nil { - panic(err) - } - - // Set headers - req.Header.Set("Content-Type", "application/json") - req.Header.Set( - "X-Emby-Authorization", - `MediaBrowser Client="JF E2E Tests", Version="0.0.1", DeviceId="E2E", Device="E2E"`) - - // Authenticate - res, err := http.DefaultClient.Do(req) - if err != nil { - panic(err) - } else if res.StatusCode != http.StatusOK { - panic(fmt.Sprintf("authentication returned code %d", res.StatusCode)) - } - - defer res.Body.Close() - - // Read body - fullBody, err := io.ReadAll(res.Body) - if err != nil { - panic(err) - } - - // Unmarshal body and return token - var token AuthenticateUserByName - if err := json.Unmarshal(fullBody, &token); err != nil { - panic(err) - } - - return token.AccessToken -} - -// Wait up to ten seconds for the provided Jellyfin server to fully startup -func waitForServerStartup(address string) { - attempts := 10 - fmt.Println(" [+] Waiting for server to finish starting") - - for { - // Sleep in between requests - time.Sleep(time.Second) - - // Ping the /System/Info/Public endpoint - res, err := http.Get(fmt.Sprintf("%s/System/Info/Public", address)) - - // If the server didn't return 200 OK, loop - if err != nil || res.StatusCode != http.StatusOK { - if attempts--; attempts <= 0 { - panic("server is taking too long to startup") - } - - continue - } - - // Assume startup has finished, break - break - } -} - -// Read configuration from config.json -func loadConfiguration() Configuration { - var config Configuration - - // Load the contents of the configuration file - raw, err := os.ReadFile("config.json") - if err != nil { - panic(err) - } - - // Unmarshal - if err := json.Unmarshal(raw, &config); err != nil { - panic(err) - } - - // Print debugging info - fmt.Printf("Library: %s\n", config.Common.Library) - fmt.Printf("Episode: \"%s\"\n", config.Common.Episode) - fmt.Printf("Password: %s\n", containerPassword) - fmt.Println() - - // Check the validity of all entries - for i, server := range config.Servers { - // If this is an entry for a local container, ensure the server address is correct - if server.Image != "" { - // Ensure that values were provided for the host's IP address, base configuration directory, - // and a path to the compiled plugin DLL to install. - if containerAddress == "" { - panic("The -caddr argument is required.") - } - - if pluginPath == "" { - panic("The -dll argument is required.") - } - - server.Username = "admin" - server.Password = containerPassword - server.Address = fmt.Sprintf("http://%s:8097", containerAddress) - server.Docker = true - } - - // If no browsers were specified, default to Chrome (for speed) - if len(server.Browsers) == 0 { - server.Browsers = []string{"chrome"} - } - - // If no tests were specified, only test that the plugin settings page works - if len(server.Tests) == 0 { - server.Tests = []string{"settings"} - } - - // Verify that an address was provided - if len(server.Address) == 0 { - panic("Server address is required") - } - - fmt.Printf("===== Server: %s =====\n", server.Comment) - - if server.Skip { - fmt.Println("Skip: true") - } - - fmt.Printf("Docker: %t\n", server.Docker) - if server.Docker { - fmt.Printf("Image: %s\n", server.Image) - } - - fmt.Printf("Address: %s\n", server.Address) - fmt.Printf("Browsers: %v\n", server.Browsers) - fmt.Printf("Tests: %v\n", server.Tests) - fmt.Println() - - config.Servers[i] = server - } - - fmt.Println("=================") - - return config -} diff --git a/ConfusedPolarBear.Plugin.IntroSkipper.Tests/e2e_tests/wrapper/setup.go b/ConfusedPolarBear.Plugin.IntroSkipper.Tests/e2e_tests/wrapper/setup.go deleted file mode 100644 index b428c6d..0000000 --- a/ConfusedPolarBear.Plugin.IntroSkipper.Tests/e2e_tests/wrapper/setup.go +++ /dev/null @@ -1,79 +0,0 @@ -package main - -import ( - "bytes" - _ "embed" - "fmt" - "net/http" -) - -//go:embed library.json -var librarySetupPayload string - -func SetupServer(server, password string) { - makeUrl := func(u string) string { - return fmt.Sprintf("%s/%s", server, u) - } - - // Set the server language to English - sendRequest( - makeUrl("Startup/Configuration"), - "POST", - `{"UICulture":"en-US","MetadataCountryCode":"US","PreferredMetadataLanguage":"en"}`) - - // Get the first user - sendRequest(makeUrl("Startup/User"), "GET", "") - - // Create the first user - sendRequest( - makeUrl("Startup/User"), - "POST", - fmt.Sprintf(`{"Name":"admin","Password":"%s"}`, password)) - - // Create a TV library from the media at /media/TV. - sendRequest( - makeUrl("Library/VirtualFolders?collectionType=tvshows&refreshLibrary=false&name=Shows"), - "POST", - librarySetupPayload) - - // Setup remote access - sendRequest( - makeUrl("Startup/RemoteAccess"), - "POST", - `{"EnableRemoteAccess":true,"EnableAutomaticPortMapping":false}`) - - // Mark the wizard as complete - sendRequest( - makeUrl("Startup/Complete"), - "POST", - ``) -} - -func sendRequest(url string, method string, body string) { - // Create the request - req, err := http.NewRequest(method, url, bytes.NewBuffer([]byte(body))) - if err != nil { - panic(err) - } - - // Set required headers - req.Header.Set("Content-Type", "application/json") - req.Header.Set( - "X-Emby-Authorization", - `MediaBrowser Client="JF E2E Tests", Version="0.0.1", DeviceId="E2E", Device="E2E"`) - - // Send it - fmt.Printf(" [+] %s %s", method, url) - res, err := http.DefaultClient.Do(req) - - if err != nil { - fmt.Println() - panic(err) - } - - fmt.Printf(" %d\n", res.StatusCode) - - if res.StatusCode != http.StatusNoContent && res.StatusCode != http.StatusOK { - panic("invalid status code received during setup") - } -} diff --git a/ConfusedPolarBear.Plugin.IntroSkipper.Tests/e2e_tests/wrapper/structs.go b/ConfusedPolarBear.Plugin.IntroSkipper.Tests/e2e_tests/wrapper/structs.go deleted file mode 100644 index bec93c9..0000000 --- a/ConfusedPolarBear.Plugin.IntroSkipper.Tests/e2e_tests/wrapper/structs.go +++ /dev/null @@ -1,26 +0,0 @@ -package main - -type Configuration struct { - Common Common `json:"common"` - Servers []Server `json:"servers"` -} - -type Common struct { - Library string `json:"library"` - Episode string `json:"episode"` -} - -type Server struct { - Skip bool `json:"skip"` - Comment string `json:"comment"` - Address string `json:"address"` - Image string `json:"image"` - Username string `json:"username"` - Password string `json:"password"` - Browsers []string `json:"browsers"` - Tests []string `json:"tests"` - ManualTests bool `json:"manual_tests"` - - // These properties are set at runtime - Docker bool `json:"-"` -} diff --git a/ConfusedPolarBear.Plugin.IntroSkipper.Tests/video/credits.mp4 b/ConfusedPolarBear.Plugin.IntroSkipper.Tests/video/credits.mp4 deleted file mode 100644 index c8fa8d2..0000000 Binary files a/ConfusedPolarBear.Plugin.IntroSkipper.Tests/video/credits.mp4 and /dev/null differ diff --git a/ConfusedPolarBear.Plugin.IntroSkipper.Tests/video/rainbow.mp4 b/ConfusedPolarBear.Plugin.IntroSkipper.Tests/video/rainbow.mp4 deleted file mode 100644 index 8e01cce..0000000 Binary files a/ConfusedPolarBear.Plugin.IntroSkipper.Tests/video/rainbow.mp4 and /dev/null differ diff --git a/ConfusedPolarBear.Plugin.IntroSkipper.sln b/ConfusedPolarBear.Plugin.IntroSkipper.sln deleted file mode 100644 index 394a21f..0000000 --- a/ConfusedPolarBear.Plugin.IntroSkipper.sln +++ /dev/null @@ -1,22 +0,0 @@ -Microsoft Visual Studio Solution File, Format Version 12.00 -# -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConfusedPolarBear.Plugin.IntroSkipper", "ConfusedPolarBear.Plugin.IntroSkipper\ConfusedPolarBear.Plugin.IntroSkipper.csproj", "{D921B930-CF91-406F-ACBC-08914DCD0D34}" -EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConfusedPolarBear.Plugin.IntroSkipper.Tests", "ConfusedPolarBear.Plugin.IntroSkipper.Tests\ConfusedPolarBear.Plugin.IntroSkipper.Tests.csproj", "{9E30DA42-983E-46E0-A3BF-A2BA56FE9718}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|Any CPU = Debug|Any CPU - Release|Any CPU = Release|Any CPU - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {D921B930-CF91-406F-ACBC-08914DCD0D34}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {D921B930-CF91-406F-ACBC-08914DCD0D34}.Debug|Any CPU.Build.0 = Debug|Any CPU - {D921B930-CF91-406F-ACBC-08914DCD0D34}.Release|Any CPU.ActiveCfg = Release|Any CPU - {D921B930-CF91-406F-ACBC-08914DCD0D34}.Release|Any CPU.Build.0 = Release|Any CPU - {9E30DA42-983E-46E0-A3BF-A2BA56FE9718}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {9E30DA42-983E-46E0-A3BF-A2BA56FE9718}.Debug|Any CPU.Build.0 = Debug|Any CPU - {9E30DA42-983E-46E0-A3BF-A2BA56FE9718}.Release|Any CPU.ActiveCfg = Release|Any CPU - {9E30DA42-983E-46E0-A3BF-A2BA56FE9718}.Release|Any CPU.Build.0 = Release|Any CPU - EndGlobalSection -EndGlobal diff --git a/ConfusedPolarBear.Plugin.IntroSkipper/Analyzers/AnalyzerHelper.cs b/ConfusedPolarBear.Plugin.IntroSkipper/Analyzers/AnalyzerHelper.cs deleted file mode 100644 index 3170689..0000000 --- a/ConfusedPolarBear.Plugin.IntroSkipper/Analyzers/AnalyzerHelper.cs +++ /dev/null @@ -1,117 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using ConfusedPolarBear.Plugin.IntroSkipper.Configuration; -using ConfusedPolarBear.Plugin.IntroSkipper.Data; -using Microsoft.Extensions.Logging; - -namespace ConfusedPolarBear.Plugin.IntroSkipper.Analyzers -{ - /// - /// Analyzer Helper. - /// - public class AnalyzerHelper - { - private readonly ILogger _logger; - private readonly double _silenceDetectionMinimumDuration; - - /// - /// Initializes a new instance of the class. - /// - /// Logger. - public AnalyzerHelper(ILogger logger) - { - var config = Plugin.Instance?.Configuration ?? new PluginConfiguration(); - _silenceDetectionMinimumDuration = config.SilenceDetectionMinimumDuration; - _logger = logger; - } - - /// - /// Adjusts the end timestamps of all intros so that they end at silence. - /// - /// QueuedEpisodes to adjust. - /// Original introductions. - /// Analysis mode. - /// Modified Intro Timestamps. - public Dictionary AdjustIntroTimes( - IReadOnlyList episodes, - IReadOnlyDictionary originalIntros, - AnalysisMode mode) - { - return episodes - .Where(episode => originalIntros.TryGetValue(episode.EpisodeId, out var _)) - .ToDictionary( - episode => episode.EpisodeId, - episode => AdjustIntroForEpisode(episode, originalIntros[episode.EpisodeId], mode)); - } - - private Segment AdjustIntroForEpisode(QueuedEpisode episode, Segment originalIntro, AnalysisMode mode) - { - _logger.LogTrace("{Name} original intro: {Start} - {End}", episode.Name, originalIntro.Start, originalIntro.End); - - var adjustedIntro = new Segment(originalIntro); - var originalIntroStart = new TimeRange(Math.Max(0, (int)originalIntro.Start - 5), (int)originalIntro.Start + 10); - var originalIntroEnd = new TimeRange((int)originalIntro.End - 10, Math.Min(episode.Duration, (int)originalIntro.End + 5)); - - if (!AdjustIntroBasedOnChapters(episode, adjustedIntro, originalIntroStart, originalIntroEnd) && mode == AnalysisMode.Introduction) - { - AdjustIntroBasedOnSilence(episode, adjustedIntro, originalIntroEnd); - } - - return adjustedIntro; - } - - private bool AdjustIntroBasedOnChapters(QueuedEpisode episode, Segment adjustedIntro, TimeRange originalIntroStart, TimeRange originalIntroEnd) - { - var chapters = Plugin.Instance?.GetChapters(episode.EpisodeId) ?? []; - double previousTime = 0; - - for (int i = 0; i <= chapters.Count; i++) - { - double currentTime = i < chapters.Count - ? TimeSpan.FromTicks(chapters[i].StartPositionTicks).TotalSeconds - : episode.Duration; - - if (originalIntroStart.Start < previousTime && previousTime < originalIntroStart.End) - { - adjustedIntro.Start = previousTime; - _logger.LogTrace("{Name} chapter found close to intro start: {Start}", episode.Name, previousTime); - } - - if (originalIntroEnd.Start < currentTime && currentTime < originalIntroEnd.End) - { - adjustedIntro.End = currentTime; - _logger.LogTrace("{Name} chapter found close to intro end: {End}", episode.Name, currentTime); - return true; - } - - previousTime = currentTime; - } - - return false; - } - - private void AdjustIntroBasedOnSilence(QueuedEpisode episode, Segment adjustedIntro, TimeRange originalIntroEnd) - { - var silence = FFmpegWrapper.DetectSilence(episode, originalIntroEnd); - - foreach (var currentRange in silence) - { - _logger.LogTrace("{Name} silence: {Start} - {End}", episode.Name, currentRange.Start, currentRange.End); - - if (IsValidSilenceForIntroAdjustment(currentRange, originalIntroEnd, adjustedIntro)) - { - adjustedIntro.End = currentRange.Start; - break; - } - } - } - - private bool IsValidSilenceForIntroAdjustment(TimeRange silenceRange, TimeRange originalIntroEnd, Segment adjustedIntro) - { - return originalIntroEnd.Intersects(silenceRange) && - silenceRange.Duration >= _silenceDetectionMinimumDuration && - silenceRange.Start >= adjustedIntro.Start; - } - } -} diff --git a/ConfusedPolarBear.Plugin.IntroSkipper/Analyzers/BlackFrameAnalyzer.cs b/ConfusedPolarBear.Plugin.IntroSkipper/Analyzers/BlackFrameAnalyzer.cs deleted file mode 100644 index 8027450..0000000 --- a/ConfusedPolarBear.Plugin.IntroSkipper/Analyzers/BlackFrameAnalyzer.cs +++ /dev/null @@ -1,207 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading; -using ConfusedPolarBear.Plugin.IntroSkipper.Configuration; -using ConfusedPolarBear.Plugin.IntroSkipper.Data; -using Microsoft.Extensions.Logging; - -namespace ConfusedPolarBear.Plugin.IntroSkipper.Analyzers; - -/// -/// Media file analyzer used to detect end credits that consist of text overlaid on a black background. -/// Bisects the end of the video file to perform an efficient search. -/// -public class BlackFrameAnalyzer : IMediaFileAnalyzer -{ - private readonly TimeSpan _maximumError = new(0, 0, 4); - - private readonly ILogger _logger; - - private readonly int _minimumCreditsDuration; - - private readonly int _maximumCreditsDuration; - - private readonly int _blackFrameMinimumPercentage; - - /// - /// Initializes a new instance of the class. - /// - /// Logger. - public BlackFrameAnalyzer(ILogger logger) - { - var config = Plugin.Instance?.Configuration ?? new PluginConfiguration(); - _minimumCreditsDuration = config.MinimumCreditsDuration; - _maximumCreditsDuration = 2 * config.MaximumCreditsDuration; - _blackFrameMinimumPercentage = config.BlackFrameMinimumPercentage; - - _logger = logger; - } - - /// - public IReadOnlyList AnalyzeMediaFiles( - IReadOnlyList analysisQueue, - AnalysisMode mode, - CancellationToken cancellationToken) - { - if (mode != AnalysisMode.Credits) - { - throw new NotImplementedException("mode must equal Credits"); - } - - var creditTimes = new Dictionary(); - - var episodeAnalysisQueue = new List(analysisQueue); - - bool isFirstEpisode = true; - - double searchStart = _minimumCreditsDuration; - - var searchDistance = 2 * _minimumCreditsDuration; - - foreach (var episode in episodeAnalysisQueue.Where(e => !e.State.IsAnalyzed(mode))) - { - if (cancellationToken.IsCancellationRequested) - { - break; - } - - // Pre-check to find reasonable starting point. - if (isFirstEpisode) - { - var scanTime = episode.Duration - searchStart; - var tr = new TimeRange(scanTime - 0.5, scanTime); // Short search range since accuracy isn't important here. - - var frames = FFmpegWrapper.DetectBlackFrames(episode, tr, _blackFrameMinimumPercentage); - - while (frames.Length > 0) // While black frames are found increase searchStart - { - searchStart += searchDistance; - - scanTime = episode.Duration - searchStart; - tr = new TimeRange(scanTime - 0.5, scanTime); - - frames = FFmpegWrapper.DetectBlackFrames(episode, tr, _blackFrameMinimumPercentage); - - if (searchStart > _maximumCreditsDuration) - { - searchStart = _maximumCreditsDuration; - break; - } - } - - if (searchStart == _minimumCreditsDuration) // Skip if no black frames were found - { - continue; - } - - isFirstEpisode = false; - } - - var credit = AnalyzeMediaFile( - episode, - searchStart, - searchDistance, - _blackFrameMinimumPercentage); - - if (credit is null) - { - // If no credits were found, reset the first-episode search logic for the next episode in the sequence. - searchStart = _minimumCreditsDuration; - isFirstEpisode = true; - continue; - } - - searchStart = episode.Duration - credit.Start + (0.5 * searchDistance); - - creditTimes.Add(episode.EpisodeId, credit); - episode.State.SetAnalyzed(mode, true); - } - - var analyzerHelper = new AnalyzerHelper(_logger); - creditTimes = analyzerHelper.AdjustIntroTimes(analysisQueue, creditTimes, mode); - - Plugin.Instance!.UpdateTimestamps(creditTimes, mode); - - return episodeAnalysisQueue; - } - - /// - /// Analyzes an individual media file. Only public because of unit tests. - /// - /// Media file to analyze. - /// Search Start Piont. - /// Search Distance. - /// Percentage of the frame that must be black. - /// Credits timestamp. - public Segment? AnalyzeMediaFile(QueuedEpisode episode, double searchStart, int searchDistance, int minimum) - { - // Start by analyzing the last N minutes of the file. - var upperLimit = searchStart; - var lowerLimit = Math.Max(searchStart - searchDistance, _minimumCreditsDuration); - var start = TimeSpan.FromSeconds(upperLimit); - var end = TimeSpan.FromSeconds(lowerLimit); - var firstFrameTime = 0.0; - - // Continue bisecting the end of the file until the range that contains the first black - // frame is smaller than the maximum permitted error. - while (start - end > _maximumError) - { - // Analyze the middle two seconds from the current bisected range - var midpoint = (start + end) / 2; - var scanTime = episode.Duration - midpoint.TotalSeconds; - var tr = new TimeRange(scanTime, scanTime + 2); - - _logger.LogTrace( - "{Episode}, dur {Duration}, bisect [{BStart}, {BEnd}], time [{Start}, {End}]", - episode.Name, - episode.Duration, - start, - end, - tr.Start, - tr.End); - - var frames = FFmpegWrapper.DetectBlackFrames(episode, tr, minimum); - _logger.LogTrace( - "{Episode} at {Start} has {Count} black frames", - episode.Name, - tr.Start, - frames.Length); - - if (frames.Length == 0) - { - // Since no black frames were found, slide the range closer to the end - start = midpoint - TimeSpan.FromSeconds(2); - - if (midpoint - TimeSpan.FromSeconds(lowerLimit) < _maximumError) - { - lowerLimit = Math.Max(lowerLimit - (0.5 * searchDistance), _minimumCreditsDuration); - - // Reset end for a new search with the increased duration - end = TimeSpan.FromSeconds(lowerLimit); - } - } - else - { - // Some black frames were found, slide the range closer to the start - end = midpoint; - firstFrameTime = frames[0].Time + scanTime; - - if (TimeSpan.FromSeconds(upperLimit) - midpoint < _maximumError) - { - upperLimit = Math.Min(upperLimit + (0.5 * searchDistance), _maximumCreditsDuration); - - // Reset start for a new search with the increased duration - start = TimeSpan.FromSeconds(upperLimit); - } - } - } - - if (firstFrameTime > 0) - { - return new(episode.EpisodeId, new TimeRange(firstFrameTime, episode.Duration)); - } - - return null; - } -} diff --git a/ConfusedPolarBear.Plugin.IntroSkipper/Analyzers/ChapterAnalyzer.cs b/ConfusedPolarBear.Plugin.IntroSkipper/Analyzers/ChapterAnalyzer.cs deleted file mode 100644 index 88a121c..0000000 --- a/ConfusedPolarBear.Plugin.IntroSkipper/Analyzers/ChapterAnalyzer.cs +++ /dev/null @@ -1,168 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Collections.ObjectModel; -using System.Globalization; -using System.Linq; -using System.Text.RegularExpressions; -using System.Threading; -using ConfusedPolarBear.Plugin.IntroSkipper.Configuration; -using ConfusedPolarBear.Plugin.IntroSkipper.Data; -using MediaBrowser.Model.Entities; -using Microsoft.Extensions.Logging; - -namespace ConfusedPolarBear.Plugin.IntroSkipper.Analyzers; - -/// -/// Chapter name analyzer. -/// -/// -/// Initializes a new instance of the class. -/// -/// Logger. -public class ChapterAnalyzer(ILogger logger) : IMediaFileAnalyzer -{ - private ILogger _logger = logger; - - /// - public IReadOnlyList AnalyzeMediaFiles( - IReadOnlyList analysisQueue, - AnalysisMode mode, - CancellationToken cancellationToken) - { - var skippableRanges = new Dictionary(); - - // Episode analysis queue. - var episodeAnalysisQueue = new List(analysisQueue); - - var expression = mode == AnalysisMode.Introduction ? - Plugin.Instance!.Configuration.ChapterAnalyzerIntroductionPattern : - Plugin.Instance!.Configuration.ChapterAnalyzerEndCreditsPattern; - - if (string.IsNullOrWhiteSpace(expression)) - { - return analysisQueue; - } - - foreach (var episode in episodeAnalysisQueue.Where(e => !e.State.IsAnalyzed(mode))) - { - if (cancellationToken.IsCancellationRequested) - { - break; - } - - var skipRange = FindMatchingChapter( - episode, - Plugin.Instance.GetChapters(episode.EpisodeId), - expression, - mode); - - if (skipRange is null) - { - continue; - } - - skippableRanges.Add(episode.EpisodeId, skipRange); - episode.State.SetAnalyzed(mode, true); - } - - Plugin.Instance.UpdateTimestamps(skippableRanges, mode); - - return episodeAnalysisQueue; - } - - /// - /// Searches a list of chapter names for one that matches the provided regular expression. - /// Only public to allow for unit testing. - /// - /// Episode. - /// Media item chapters. - /// Regular expression pattern. - /// Analysis mode. - /// Intro object containing skippable time range, or null if no chapter matched. - public Segment? FindMatchingChapter( - QueuedEpisode episode, - IReadOnlyList chapters, - string expression, - AnalysisMode mode) - { - var count = chapters.Count; - if (count == 0) - { - return null; - } - - var config = Plugin.Instance?.Configuration ?? new PluginConfiguration(); - var reversed = mode != AnalysisMode.Introduction; - var (minDuration, maxDuration) = reversed - ? (config.MinimumCreditsDuration, config.MaximumCreditsDuration) - : (config.MinimumIntroDuration, config.MaximumIntroDuration); - - // Check all chapters - for (int i = reversed ? count - 1 : 0; reversed ? i >= 0 : i < count; i += reversed ? -1 : 1) - { - var chapter = chapters[i]; - var next = chapters.ElementAtOrDefault(i + 1) ?? - new ChapterInfo { StartPositionTicks = TimeSpan.FromSeconds(episode.Duration).Ticks }; // Since the ending credits chapter may be the last chapter in the file, append a virtual chapter. - - if (string.IsNullOrWhiteSpace(chapter.Name)) - { - continue; - } - - var currentRange = new TimeRange( - TimeSpan.FromTicks(chapter.StartPositionTicks).TotalSeconds, - TimeSpan.FromTicks(next.StartPositionTicks).TotalSeconds); - - var baseMessage = string.Format( - CultureInfo.InvariantCulture, - "{0}: Chapter \"{1}\" ({2} - {3})", - episode.Path, - chapter.Name, - currentRange.Start, - currentRange.End); - - if (currentRange.Duration < minDuration || currentRange.Duration > maxDuration) - { - _logger.LogTrace("{Base}: ignoring (invalid duration)", baseMessage); - continue; - } - - // Regex.IsMatch() is used here in order to allow the runtime to cache the compiled regex - // between function invocations. - var match = Regex.IsMatch( - chapter.Name, - expression, - RegexOptions.None, - TimeSpan.FromSeconds(1)); - - if (!match) - { - _logger.LogTrace("{Base}: ignoring (does not match regular expression)", baseMessage); - continue; - } - - // Check if the next (or previous for Credits) chapter also matches - var adjacentChapter = reversed ? chapters.ElementAtOrDefault(i - 1) : next; - if (adjacentChapter != null && !string.IsNullOrWhiteSpace(adjacentChapter.Name)) - { - // Check for possibility of overlapping keywords - var overlap = Regex.IsMatch( - adjacentChapter.Name, - expression, - RegexOptions.None, - TimeSpan.FromSeconds(1)); - - if (overlap) - { - _logger.LogTrace("{Base}: ignoring (adjacent chapter also matches)", baseMessage); - continue; - } - } - - _logger.LogTrace("{Base}: okay", baseMessage); - return new Segment(episode.EpisodeId, currentRange); - } - - return null; - } -} diff --git a/ConfusedPolarBear.Plugin.IntroSkipper/Analyzers/ChromaprintAnalyzer.cs b/ConfusedPolarBear.Plugin.IntroSkipper/Analyzers/ChromaprintAnalyzer.cs deleted file mode 100644 index cc4e1a9..0000000 --- a/ConfusedPolarBear.Plugin.IntroSkipper/Analyzers/ChromaprintAnalyzer.cs +++ /dev/null @@ -1,411 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Numerics; -using System.Threading; -using ConfusedPolarBear.Plugin.IntroSkipper.Configuration; -using ConfusedPolarBear.Plugin.IntroSkipper.Data; -using Microsoft.Extensions.Logging; - -namespace ConfusedPolarBear.Plugin.IntroSkipper.Analyzers; - -/// -/// Chromaprint audio analyzer. -/// -public class ChromaprintAnalyzer : IMediaFileAnalyzer -{ - /// - /// Seconds of audio in one fingerprint point. - /// This value is defined by the Chromaprint library and should not be changed. - /// - private const double SamplesToSeconds = 0.1238; - - private readonly int _minimumIntroDuration; - - private readonly int _maximumDifferences; - - private readonly int _invertedIndexShift; - - private readonly double _maximumTimeSkip; - - private readonly ILogger _logger; - - private AnalysisMode _analysisMode; - - /// - /// Initializes a new instance of the class. - /// - /// Logger. - public ChromaprintAnalyzer(ILogger logger) - { - var config = Plugin.Instance?.Configuration ?? new PluginConfiguration(); - _maximumDifferences = config.MaximumFingerprintPointDifferences; - _invertedIndexShift = config.InvertedIndexShift; - _maximumTimeSkip = config.MaximumTimeSkip; - _minimumIntroDuration = config.MinimumIntroDuration; - - _logger = logger; - } - - /// - public IReadOnlyList AnalyzeMediaFiles( - IReadOnlyList analysisQueue, - AnalysisMode mode, - CancellationToken cancellationToken) - { - // All intros for this season. - var seasonIntros = new Dictionary(); - - // Cache of all fingerprints for this season. - var fingerprintCache = new Dictionary(); - - // Episode analysis queue based on not analyzed episodes - var episodeAnalysisQueue = new List(analysisQueue); - - // Episodes that were analyzed and do not have an introduction. - var episodesWithoutIntros = episodeAnalysisQueue.Where(e => !e.State.IsAnalyzed(mode)).ToList(); - - _analysisMode = mode; - - if (episodesWithoutIntros.Count == 0 || episodeAnalysisQueue.Count <= 1) - { - return analysisQueue; - } - - var episodesWithFingerprint = new List(episodesWithoutIntros); - - // Load fingerprints from cache if available. - episodesWithFingerprint.AddRange(episodeAnalysisQueue.Where(e => e.State.IsAnalyzed(mode) && File.Exists(FFmpegWrapper.GetFingerprintCachePath(e, mode)))); - - // Ensure at least two fingerprints are present. - if (episodesWithFingerprint.Count == 1) - { - var indexInAnalysisQueue = episodeAnalysisQueue.FindIndex(episode => episode == episodesWithoutIntros[0]); - episodesWithFingerprint.AddRange(episodeAnalysisQueue - .Where((episode, index) => Math.Abs(index - indexInAnalysisQueue) <= 1 && index != indexInAnalysisQueue)); - } - - seasonIntros = episodesWithFingerprint.Where(e => e.State.IsAnalyzed(mode)).ToDictionary(e => e.EpisodeId, e => Plugin.GetIntroByMode(e.EpisodeId, mode)); - - // Compute fingerprints for all episodes in the season - foreach (var episode in episodesWithFingerprint) - { - try - { - fingerprintCache[episode.EpisodeId] = FFmpegWrapper.Fingerprint(episode, mode); - - // Use reversed fingerprints for credits - if (_analysisMode == AnalysisMode.Credits) - { - Array.Reverse(fingerprintCache[episode.EpisodeId]); - } - - if (cancellationToken.IsCancellationRequested) - { - return analysisQueue; - } - } - catch (FingerprintException ex) - { - _logger.LogDebug("Caught fingerprint error: {Ex}", ex); - WarningManager.SetFlag(PluginWarning.InvalidChromaprintFingerprint); - - // Fallback to an empty fingerprint on any error - fingerprintCache[episode.EpisodeId] = []; - } - } - - // While there are still episodes in the queue - while (episodesWithoutIntros.Count > 0) - { - // Pop the first episode from the queue - var currentEpisode = episodesWithoutIntros[0]; - episodesWithoutIntros.RemoveAt(0); - episodesWithFingerprint.Remove(currentEpisode); - - // Search through all remaining episodes. - foreach (var remainingEpisode in episodesWithFingerprint) - { - // Compare the current episode to all remaining episodes in the queue. - var (currentIntro, remainingIntro) = CompareEpisodes( - currentEpisode.EpisodeId, - fingerprintCache[currentEpisode.EpisodeId], - remainingEpisode.EpisodeId, - fingerprintCache[remainingEpisode.EpisodeId]); - - // Ignore this comparison result if: - // - one of the intros isn't valid, or - // - the introduction exceeds the configured limit - if ( - !remainingIntro.Valid || - (_analysisMode == AnalysisMode.Introduction && remainingIntro.Duration > Plugin.Instance!.Configuration.MaximumIntroDuration)) - { - continue; - } - - /* Since the Fingerprint() function returns an array of Chromaprint points without time - * information, the times reported from the index search function start from 0. - * - * While this is desired behavior for detecting introductions, it breaks credit - * detection, as the audio we're analyzing was extracted from some point into the file. - * - * To fix this, the starting and ending times need to be switched, as they were previously reversed - * and subtracted from the episode duration to get the reported time range. - */ - if (_analysisMode == AnalysisMode.Credits) - { - // Calculate new values for the current intro - double currentOriginalIntroStart = currentIntro.Start; - currentIntro.Start = currentEpisode.Duration - currentIntro.End; - currentIntro.End = currentEpisode.Duration - currentOriginalIntroStart; - - // Calculate new values for the remaining intro - double remainingIntroOriginalStart = remainingIntro.Start; - remainingIntro.Start = remainingEpisode.Duration - remainingIntro.End; - remainingIntro.End = remainingEpisode.Duration - remainingIntroOriginalStart; - } - - // Only save the discovered intro if it is: - // - the first intro discovered for this episode - // - longer than the previously discovered intro - if ( - !seasonIntros.TryGetValue(currentIntro.EpisodeId, out var savedCurrentIntro) || - currentIntro.Duration > savedCurrentIntro.Duration) - { - seasonIntros[currentIntro.EpisodeId] = currentIntro; - } - - if ( - !seasonIntros.TryGetValue(remainingIntro.EpisodeId, out var savedRemainingIntro) || - remainingIntro.Duration > savedRemainingIntro.Duration) - { - seasonIntros[remainingIntro.EpisodeId] = remainingIntro; - } - - break; - } - - // If no intro is found at this point, the popped episode is not reinserted into the queue. - if (seasonIntros.ContainsKey(currentEpisode.EpisodeId)) - { - episodesWithFingerprint.Add(currentEpisode); - episodeAnalysisQueue.FirstOrDefault(x => x.EpisodeId == currentEpisode.EpisodeId)?.State.SetAnalyzed(mode, true); - } - } - - // If cancellation was requested, report that no episodes were analyzed. - if (cancellationToken.IsCancellationRequested) - { - return analysisQueue; - } - - // Adjust all introduction times. - var analyzerHelper = new AnalyzerHelper(_logger); - seasonIntros = analyzerHelper.AdjustIntroTimes(analysisQueue, seasonIntros, _analysisMode); - - Plugin.Instance!.UpdateTimestamps(seasonIntros, _analysisMode); - - return episodeAnalysisQueue; - } - - /// - /// Analyze two episodes to find an introduction sequence shared between them. - /// - /// First episode id. - /// First episode fingerprint points. - /// Second episode id. - /// Second episode fingerprint points. - /// Intros for the first and second episodes. - public (Segment Lhs, Segment Rhs) CompareEpisodes( - Guid lhsId, - uint[] lhsPoints, - Guid rhsId, - uint[] rhsPoints) - { - // Creates an inverted fingerprint point index for both episodes. - // For every point which is a 100% match, search for an introduction at that point. - var (lhsRanges, rhsRanges) = SearchInvertedIndex(lhsId, lhsPoints, rhsId, rhsPoints); - - if (lhsRanges.Count > 0) - { - _logger.LogTrace("Index search successful"); - - return GetLongestTimeRange(lhsId, lhsRanges, rhsId, rhsRanges); - } - - _logger.LogTrace( - "Unable to find a shared introduction sequence between {LHS} and {RHS}", - lhsId, - rhsId); - - return (new Segment(lhsId), new Segment(rhsId)); - } - - /// - /// Locates the longest range of similar audio and returns an Intro class for each range. - /// - /// First episode id. - /// First episode shared timecodes. - /// Second episode id. - /// Second episode shared timecodes. - /// Intros for the first and second episodes. - private static (Segment Lhs, Segment Rhs) GetLongestTimeRange( - Guid lhsId, - List lhsRanges, - Guid rhsId, - List rhsRanges) - { - // Store the longest time range as the introduction. - lhsRanges.Sort(); - rhsRanges.Sort(); - - var lhsIntro = lhsRanges[0]; - var rhsIntro = rhsRanges[0]; - - // If the intro starts early in the episode, move it to the beginning. - if (lhsIntro.Start <= 5) - { - lhsIntro.Start = 0; - } - - if (rhsIntro.Start <= 5) - { - rhsIntro.Start = 0; - } - - // Create Intro classes for each time range. - return (new Segment(lhsId, lhsIntro), new Segment(rhsId, rhsIntro)); - } - - /// - /// Search for a shared introduction sequence using inverted indexes. - /// - /// LHS ID. - /// Left episode fingerprint points. - /// RHS ID. - /// Right episode fingerprint points. - /// List of shared TimeRanges between the left and right episodes. - private (List Lhs, List Rhs) SearchInvertedIndex( - Guid lhsId, - uint[] lhsPoints, - Guid rhsId, - uint[] rhsPoints) - { - var lhsRanges = new List(); - var rhsRanges = new List(); - - // Generate inverted indexes for the left and right episodes. - var lhsIndex = FFmpegWrapper.CreateInvertedIndex(lhsId, lhsPoints, _analysisMode); - var rhsIndex = FFmpegWrapper.CreateInvertedIndex(rhsId, rhsPoints, _analysisMode); - var indexShifts = new HashSet(); - - // For all audio points in the left episode, check if the right episode has a point which matches exactly. - // If an exact match is found, calculate the shift that must be used to align the points. - foreach (var kvp in lhsIndex) - { - var originalPoint = kvp.Key; - - for (var i = -1 * _invertedIndexShift; i <= _invertedIndexShift; i++) - { - var modifiedPoint = (uint)(originalPoint + i); - - if (rhsIndex.TryGetValue(modifiedPoint, out var rhsModifiedPoint)) - { - var lhsFirst = lhsIndex[originalPoint]; - var rhsFirst = rhsModifiedPoint; - indexShifts.Add(rhsFirst - lhsFirst); - } - } - } - - // Use all discovered shifts to compare the episodes. - foreach (var shift in indexShifts) - { - var (lhsIndexContiguous, rhsIndexContiguous) = FindContiguous(lhsPoints, rhsPoints, shift); - if (lhsIndexContiguous.End > 0 && rhsIndexContiguous.End > 0) - { - lhsRanges.Add(lhsIndexContiguous); - rhsRanges.Add(rhsIndexContiguous); - } - } - - return (lhsRanges, rhsRanges); - } - - /// - /// Finds the longest contiguous region of similar audio between two fingerprints using the provided shift amount. - /// - /// First fingerprint to compare. - /// Second fingerprint to compare. - /// Amount to shift one fingerprint by. - private (TimeRange Lhs, TimeRange Rhs) FindContiguous( - uint[] lhs, - uint[] rhs, - int shiftAmount) - { - var leftOffset = 0; - var rightOffset = 0; - - // Calculate the offsets for the left and right hand sides. - if (shiftAmount < 0) - { - leftOffset -= shiftAmount; - } - else - { - rightOffset += shiftAmount; - } - - // Store similar times for both LHS and RHS. - var lhsTimes = new List(); - var rhsTimes = new List(); - var upperLimit = Math.Min(lhs.Length, rhs.Length) - Math.Abs(shiftAmount); - - // XOR all elements in LHS and RHS, using the shift amount from above. - for (var i = 0; i < upperLimit; i++) - { - // XOR both samples at the current position. - var lhsPosition = i + leftOffset; - var rhsPosition = i + rightOffset; - var diff = lhs[lhsPosition] ^ rhs[rhsPosition]; - - // If the difference between the samples is small, flag both times as similar. - if (CountBits(diff) > _maximumDifferences) - { - continue; - } - - var lhsTime = lhsPosition * SamplesToSeconds; - var rhsTime = rhsPosition * SamplesToSeconds; - - lhsTimes.Add(lhsTime); - rhsTimes.Add(rhsTime); - } - - // Ensure the last timestamp is checked - lhsTimes.Add(double.MaxValue); - rhsTimes.Add(double.MaxValue); - - // Now that both fingerprints have been compared at this shift, see if there's a contiguous time range. - var lContiguous = TimeRangeHelpers.FindContiguous(lhsTimes.ToArray(), _maximumTimeSkip); - if (lContiguous is null || lContiguous.Duration < _minimumIntroDuration) - { - return (new TimeRange(), new TimeRange()); - } - - // Since LHS had a contiguous time range, RHS must have one also. - var rContiguous = TimeRangeHelpers.FindContiguous(rhsTimes.ToArray(), _maximumTimeSkip)!; - return (lContiguous, rContiguous); - } - - /// - /// Count the number of bits that are set in the provided number. - /// - /// Number to count bits in. - /// Number of bits that are equal to 1. - public int CountBits(uint number) - { - return BitOperations.PopCount(number); - } -} diff --git a/ConfusedPolarBear.Plugin.IntroSkipper/Analyzers/IMediaFileAnalyzer.cs b/ConfusedPolarBear.Plugin.IntroSkipper/Analyzers/IMediaFileAnalyzer.cs deleted file mode 100644 index 9c97c4f..0000000 --- a/ConfusedPolarBear.Plugin.IntroSkipper/Analyzers/IMediaFileAnalyzer.cs +++ /dev/null @@ -1,23 +0,0 @@ -using System.Collections.Generic; -using System.Threading; -using ConfusedPolarBear.Plugin.IntroSkipper.Data; - -namespace ConfusedPolarBear.Plugin.IntroSkipper.Analyzers; - -/// -/// Media file analyzer interface. -/// -public interface IMediaFileAnalyzer -{ - /// - /// Analyze media files for shared introductions or credits, returning all media files that were **not successfully analyzed**. - /// - /// Collection of unanalyzed media files. - /// Analysis mode. - /// Cancellation token from scheduled task. - /// Collection of media files that were **unsuccessfully analyzed**. - public IReadOnlyList AnalyzeMediaFiles( - IReadOnlyList analysisQueue, - AnalysisMode mode, - CancellationToken cancellationToken); -} diff --git a/ConfusedPolarBear.Plugin.IntroSkipper/Analyzers/SegmentAnalyzer.cs b/ConfusedPolarBear.Plugin.IntroSkipper/Analyzers/SegmentAnalyzer.cs deleted file mode 100644 index 0b20f25..0000000 --- a/ConfusedPolarBear.Plugin.IntroSkipper/Analyzers/SegmentAnalyzer.cs +++ /dev/null @@ -1,32 +0,0 @@ -using System.Collections.Generic; -using System.Threading; -using ConfusedPolarBear.Plugin.IntroSkipper.Data; -using Microsoft.Extensions.Logging; - -namespace ConfusedPolarBear.Plugin.IntroSkipper.Analyzers; - -/// -/// Chapter name analyzer. -/// -public class SegmentAnalyzer : IMediaFileAnalyzer -{ - private readonly ILogger _logger; - - /// - /// Initializes a new instance of the class. - /// - /// Logger. - public SegmentAnalyzer(ILogger logger) - { - _logger = logger; - } - - /// - public IReadOnlyList AnalyzeMediaFiles( - IReadOnlyList analysisQueue, - AnalysisMode mode, - CancellationToken cancellationToken) - { - return analysisQueue; - } -} diff --git a/ConfusedPolarBear.Plugin.IntroSkipper/Configuration/PluginConfiguration.cs b/ConfusedPolarBear.Plugin.IntroSkipper/Configuration/PluginConfiguration.cs deleted file mode 100644 index 61a0bd0..0000000 --- a/ConfusedPolarBear.Plugin.IntroSkipper/Configuration/PluginConfiguration.cs +++ /dev/null @@ -1,246 +0,0 @@ -using System.Diagnostics; -using ConfusedPolarBear.Plugin.IntroSkipper.Data; -using MediaBrowser.Model.Plugins; - -namespace ConfusedPolarBear.Plugin.IntroSkipper.Configuration; - -/// -/// Plugin configuration. -/// -public class PluginConfiguration : BasePluginConfiguration -{ - /// - /// Initializes a new instance of the class. - /// - public PluginConfiguration() - { - } - - // ===== Analysis settings ===== - - /// - /// Gets or sets the max degree of parallelism used when analyzing episodes. - /// - public int MaxParallelism { get; set; } = 2; - - /// - /// Gets or sets the comma separated list of library names to analyze. - /// - public string SelectedLibraries { get; set; } = string.Empty; - - /// - /// Gets or sets a value indicating whether all libraries should be analyzed. - /// - public bool SelectAllLibraries { get; set; } = true; - - /// - /// Gets or sets the list of client to auto skip for. - /// - public string ClientList { get; set; } = string.Empty; - - /// - /// Gets or sets a value indicating whether to scan for intros during a scheduled task. - /// - public bool AutoDetectIntros { get; set; } = true; - - /// - /// Gets or sets a value indicating whether to scan for credits during a scheduled task. - /// - public bool AutoDetectCredits { get; set; } = true; - - /// - /// Gets or sets a value indicating whether to analyze season 0. - /// - public bool AnalyzeSeasonZero { get; set; } - - /// - /// Gets or sets a value indicating whether the episode's fingerprint should be cached to the filesystem. - /// - public bool CacheFingerprints { get; set; } = true; - - /// - /// Gets or sets a value indicating whether analysis will use Chromaprint to determine fingerprints. - /// - public bool WithChromaprint { get; set; } = true; - - // ===== EDL handling ===== - - /// - /// Gets or sets a value indicating the action to write to created EDL files. - /// - public EdlAction EdlAction { get; set; } = EdlAction.None; - - /// - /// Gets or sets a value indicating whether to regenerate all EDL files during the next scan. - /// By default, EDL files are only written for a season if the season had at least one newly analyzed episode. - /// If this is set, all EDL files will be regenerated and overwrite any existing EDL file. - /// - public bool RegenerateEdlFiles { get; set; } - - // ===== Custom analysis settings ===== - - /// - /// Gets or sets the percentage of each episode's audio track to analyze. - /// - public int AnalysisPercent { get; set; } = 25; - - /// - /// Gets or sets the upper limit (in minutes) on the length of each episode's audio track that will be analyzed. - /// - public int AnalysisLengthLimit { get; set; } = 10; - - /// - /// Gets or sets the minimum length of similar audio that will be considered an introduction. - /// - public int MinimumIntroDuration { get; set; } = 15; - - /// - /// Gets or sets the maximum length of similar audio that will be considered an introduction. - /// - public int MaximumIntroDuration { get; set; } = 120; - - /// - /// Gets or sets the minimum length of similar audio that will be considered ending credits. - /// - public int MinimumCreditsDuration { get; set; } = 15; - - /// - /// 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. - /// - public int MaximumCreditsDuration { get; set; } = 300; - - /// - /// Gets or sets the minimum percentage of a frame that must consist of black pixels before it is considered a black frame. - /// - public int BlackFrameMinimumPercentage { get; set; } = 85; - - /// - /// Gets or sets the regular expression used to detect introduction chapters. - /// - public string ChapterAnalyzerIntroductionPattern { get; set; } = - @"(^|\s)(Intro|Introduction|OP|Opening)(\s|$)"; - - /// - /// Gets or sets the regular expression used to detect ending credit chapters. - /// - public string ChapterAnalyzerEndCreditsPattern { get; set; } = - @"(^|\s)(Credits?|ED|Ending|End|Outro)(\s|$)"; - - // ===== Playback settings ===== - - /// - /// Gets or sets a value indicating whether to show the skip intro button. - /// - public bool SkipButtonVisible { get; set; } = false; - - /// - /// Gets a value indicating whether to show the skip intro warning. - /// - public bool SkipButtonWarning { get => WarningManager.HasFlag(PluginWarning.UnableToAddSkipButton); } - - /// - /// Gets or sets a value indicating whether introductions should be automatically skipped. - /// - public bool AutoSkip { get; set; } - - /// - /// Gets or sets a value indicating whether credits should be automatically skipped. - /// - public bool AutoSkipCredits { get; set; } - - /// - /// Gets or sets the seconds before the intro starts to show the skip prompt at. - /// - public int ShowPromptAdjustment { get; set; } = 5; - - /// - /// Gets or sets the seconds after the intro starts to hide the skip prompt at. - /// - public int HidePromptAdjustment { get; set; } = 10; - - /// - /// Gets or sets a value indicating whether the introduction in the first episode of a season should be ignored. - /// - public bool SkipFirstEpisode { get; set; } = true; - - /// - /// Gets or sets a value indicating whether the skip button should be displayed for the duration of the intro. - /// - public bool PersistSkipButton { get; set; } = true; - - /// - /// Gets or sets the amount of intro to play (in seconds). - /// - public int RemainingSecondsOfIntro { get; set; } = 2; - - /// - /// Gets or sets the amount of intro at start to play (in seconds). - /// - public int SecondsOfIntroStartToPlay { get; set; } - - /// - /// Gets or sets the amount of credit at start to play (in seconds). - /// - public int SecondsOfCreditsStartToPlay { get; set; } - - // ===== Internal algorithm settings ===== - - /// - /// 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. - /// Defaults to 6 (81% similar). - /// - public int MaximumFingerprintPointDifferences { get; set; } = 6; - - /// - /// Gets or sets the maximum number of seconds that can pass between two similar fingerprint points before a new time range is started. - /// - public double MaximumTimeSkip { get; set; } = 3.5; - - /// - /// Gets or sets the amount to shift inverted indexes by. - /// - public int InvertedIndexShift { get; set; } = 2; - - /// - /// Gets or sets the maximum amount of noise (in dB) that is considered silent. - /// Lowering this number will increase the filter's sensitivity to noise. - /// - public int SilenceDetectionMaximumNoise { get; set; } = -50; - - /// - /// Gets or sets the minimum duration of audio (in seconds) that is considered silent. - /// - public double SilenceDetectionMinimumDuration { get; set; } = 0.33; - - // ===== Localization support ===== - - /// - /// Gets or sets the text to display in the skip button in introduction mode. - /// - public string SkipButtonIntroText { get; set; } = "Skip Intro"; - - /// - /// Gets or sets the text to display in the skip button in end credits mode. - /// - public string SkipButtonEndCreditsText { get; set; } = "Next"; - - /// - /// Gets or sets the notification text sent after automatically skipping an introduction. - /// - public string AutoSkipNotificationText { get; set; } = "Intro skipped"; - - /// - /// Gets or sets the notification text sent after automatically skipping credits. - /// - public string AutoSkipCreditsNotificationText { get; set; } = "Credits skipped"; - - /// - /// Gets or sets the number of threads for an ffmpeg process. - /// - public int ProcessThreads { get; set; } - - /// - /// Gets or sets the relative priority for an ffmpeg process. - /// - public ProcessPriorityClass ProcessPriority { get; set; } = ProcessPriorityClass.BelowNormal; -} diff --git a/ConfusedPolarBear.Plugin.IntroSkipper/Configuration/UserInterfaceConfiguration.cs b/ConfusedPolarBear.Plugin.IntroSkipper/Configuration/UserInterfaceConfiguration.cs deleted file mode 100644 index e854257..0000000 --- a/ConfusedPolarBear.Plugin.IntroSkipper/Configuration/UserInterfaceConfiguration.cs +++ /dev/null @@ -1,46 +0,0 @@ -namespace ConfusedPolarBear.Plugin.IntroSkipper.Configuration; - -/// -/// User interface configuration. -/// -/// -/// Initializes a new instance of the class. -/// -/// Skip button visibility. -/// Skip button intro text. -/// Skip button end credits text. -/// Auto Skip Intro. -/// Auto Skip Credits. -/// Auto Skip Clients. -public class UserInterfaceConfiguration(bool visible, string introText, string creditsText, bool autoSkip, bool autoSkipCredits, string clientList) -{ - /// - /// Gets or sets a value indicating whether to show the skip intro button. - /// - public bool SkipButtonVisible { get; set; } = visible; - - /// - /// Gets or sets the text to display in the skip intro button in introduction mode. - /// - public string SkipButtonIntroText { get; set; } = introText; - - /// - /// Gets or sets the text to display in the skip intro button in end credits mode. - /// - public string SkipButtonEndCreditsText { get; set; } = creditsText; - - /// - /// Gets or sets a value indicating whether auto skip intro. - /// - public bool AutoSkip { get; set; } = autoSkip; - - /// - /// Gets or sets a value indicating whether auto skip credits. - /// - public bool AutoSkipCredits { get; set; } = autoSkipCredits; - - /// - /// Gets or sets a value indicating clients to auto skip for. - /// - public string ClientList { get; set; } = clientList; -} diff --git a/ConfusedPolarBear.Plugin.IntroSkipper/Configuration/configPage.html b/ConfusedPolarBear.Plugin.IntroSkipper/Configuration/configPage.html deleted file mode 100644 index 0ec85b0..0000000 --- a/ConfusedPolarBear.Plugin.IntroSkipper/Configuration/configPage.html +++ /dev/null @@ -1,1405 +0,0 @@ - - - - - - - -
-
- -
-
-
- Analysis - -
- - -
If enabled, introductions will be automatically analyzed for new media
-
- -
- - -
- If enabled, credits will be automatically analyzed for new media -
-
- Note: Not selecting at least one automatic detection type will disable automatic scans. To configure the scheduled task, see scheduled tasks. -
-
- -
- - -
- If checked, season 0 (specials / extras) will be included in analysis. -
-
- Note: Shows containing both a specials and extra folder will identify extras as season 0 and ignore specials, regardless of this setting. -
-
- -
- - -
Maximum number of simultaneous async episode analysis operations.
-
- -
- - -
-
-

Limit analysis to the following libraries

-
-
- - -
-
- -
- Modify Segment Parameters - -
-
- - -
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.
-
- -
- - -
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.
-
- -
- - -
Similar sounding audio which is shorter than this duration will not be considered an introduction.
-
- -
- - -
Similar sounding audio which is longer than this duration will not be considered an introduction.
-
- -
- - -
Similar sounding audio which is shorter than this duration will not be considered credits.
-
- -
- - -
Similar sounding audio which is longer than this duration will not be considered credits.
-
- -

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.

- -

- 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 will have to be deleted. - - Increasing either of these settings will cause episode analysis to take much longer. -

-
- -
- EDL File Generation - -
-
- - - -
- If set to a value other than None, specifies which action to write to - MPlayer compatible EDL files - alongside your episode files.
- - If this value is changed after EDL files are generated, you must check the "Regenerate EDL files" checkbox below. -
-
- -
- - -
If checked, the plugin will overwrite all EDL files associated with your episodes with the currently discovered introduction/credit timestamps and EDL action.
-
-
- -
- Silence Detection Options - -
-
- - -
Noise tolerance in negative decibels.
-
- -
- - -
Minimum silence duration in seconds before adjusting introduction end time.
-
-
- -
- Process Configuration - -
-
- - -
- If checked, episode fingerprints will be saved on the filesystem to improve analysis speed. -
- WARNING: Disabling the cache will cause all libraries to be re-scanned, which can take a very long time! -
-
-
- -
- - - -
Sets the relative priority of the analysis ffmpeg process to other parallel operations (ie. transcoding, chapter detection, etc).
-
- -
- - -
- Number of simultaneous processes to use for ffmpeg operations. -
- This value is most often defined as 1 thread per CPU core, but setting a value of 0 (default) will use the maximum threads available. -
-
-
-
- -
- Playback - -
- - -
- If checked, intros will be automatically skipped for all clients. Note: Clients cannot disable this setting from the player popup (gear icon).
- If you access Jellyfin through a reverse proxy, it must be configured to proxy websockets.
-
-
- -
- - -
If checked, auto skip will play the introduction of the first episode in a season.
-
-
- -
- - -
Seconds of introduction start that should be played. Defaults to 0.
-
-
- -
- - -
- If checked, credits will be automatically skipped for all clients. Note: Clients cannot disable this setting from the player popup (gear icon).
- If you access Jellyfin through a reverse proxy, it must be configured to proxy websockets.
-
-
- -
- - -
Seconds of credits start that should be played. Defaults to 0.
-
-
- -
- Auto Skip Client List -
-
- - -
- -
- - -
- (Restart required!) If checked, a skip button will be displayed according to the settings below, while clients selected in the Auto Skip Client List will still skip automatically. -
-
-
- -
Failed to add skip button to web interface. See troubleshooting guide for the most common issues.
- -
-
- - -
- If checked, skip button will remain visible throught the intro (offset and timeout are ignored). -
- Note: If unchecked, button will only appear in the player controls after the set timeout. -
-
- -
- - -
Seconds to display skip prompt before introduction begins.
-
-
- -
- - -
Seconds after introduction before skip prompt is hidden.
-
-
-
- -
- - -
Seconds of introduction ending that should be played. Defaults to 2.
-
- -
- User Interface Customization - -
-
- - -
Text to display in the skip intro button.
-
- -
- - -
Text to display in the skip end credits button.
-
- -
- - -
Message shown after automatically skipping an introduction. Leave blank to disable notification.
-
- -
- - -
Message shown after automatically skipping credits. Leave blank to disable notification.
-
-
-
- -
- -
-
- -
- Advanced - -
- Support Bundle Info - - -
- -
- Manage Timestamps & Fingerprints - -
- - - - -
- - -
- - - - - -
- - - - - -

Fingerprint Visualizer

-

- Interactively compare the audio fingerprints of two episodes.
- The blue and red bar to the right of the fingerprint diff turns blue when the corresponding fingerprint points are at least 80% similar. -

- - - - - - - - - - - - - - - - - - - - - - - - - -
KeyFunction
Up arrowShift the left episode up by 0.1238 seconds. Holding control will shift the episode by 10 seconds.
Down arrowShift the left episode down by 0.1238 seconds. Holding control will shift the episode by 10 seconds.
Right arrowAdvance to the next pair of episodes.
Left arrowGo back to the previous pair of episodes.
-
- - Shift amount: - -
- Suggested shifts: -
-
- - - -
- -
-
-
- - -
- - -
- - -
- - - -
- -
-
- Storage Usage -
See how much space each library uses.
- -
-
-
-
-
- - - - -
- - diff --git a/ConfusedPolarBear.Plugin.IntroSkipper/Configuration/inject.js b/ConfusedPolarBear.Plugin.IntroSkipper/Configuration/inject.js deleted file mode 100644 index abbb75e..0000000 --- a/ConfusedPolarBear.Plugin.IntroSkipper/Configuration/inject.js +++ /dev/null @@ -1,489 +0,0 @@ -const introSkipper = { - originalFetch: window.fetch.bind(window), - originalXHROpen: XMLHttpRequest.prototype.open, - d: msg => console.debug("[intro skipper] ", msg), - setup() { - const self = this; - this.initializeState(); - this.initializeObserver(); - this.currentOption = localStorage.getItem('introskipperOption') || 'Show Button'; - window.fetch = this.fetchWrapper.bind(this); - XMLHttpRequest.prototype.open = function(...args) { - self.xhrOpenWrapper(this, ...args); - }; - document.addEventListener("viewshow", this.viewShow.bind(this)); - this.videoPositionChanged = this.videoPositionChanged.bind(this); - this.handleEscapeKey = this.handleEscapeKey.bind(this); - this.d("Registered hooks"); - }, - initializeState() { - Object.assign(this, { allowEnter: true, skipSegments: {}, videoPlayer: null, skipButton: null, osdElement: null, skipperData: null, currentEpisodeId: null, injectMetadata: false }); - }, - initializeObserver() { - this.observer = new MutationObserver(mutations => { - const actionSheet = mutations[mutations.length - 1].target.querySelector('.actionSheet'); - if (actionSheet && !actionSheet.querySelector(`[data-id="${'introskipperMenu'}"]`)) this.injectIntroSkipperOptions(actionSheet); - }); - }, - fetchWrapper(resource, options) { - const response = this.originalFetch(resource, options); - const url = new URL(resource); - if (this.injectMetadata && url.pathname.includes("/MetadataEditor")) - { - this.processMetadata(url.pathname); - } - return response; - }, - xhrOpenWrapper(xhr, method, url, ...rest) { - url.includes("/PlaybackInfo") && this.processPlaybackInfo(url); - return this.originalXHROpen.apply(xhr, [method, url, ...rest]); - }, - async processPlaybackInfo(url) { - const id = this.extractId(url); - if (id) { - try { - this.skipSegments = await this.secureFetch(`Episode/${id}/IntroSkipperSegments`); - } catch (error) { - this.d(`Error fetching skip segments: ${error.message}`); - } - } - }, - async processMetadata(url) { - const id = this.extractId(url); - if (id) { - try { - this.skipperData = await this.secureFetch(`Episode/${id}/Timestamps`); - if (this.skipperData) { - this.currentEpisodeId = id; - requestAnimationFrame(() => { - const metadataFormFields = document.querySelector('.metadataFormFields'); - metadataFormFields && this.injectSkipperFields(metadataFormFields); - }); - } - } catch (e) { - console.error("Error processing", e); - } - } - }, - extractId(searchString) { - const startIndex = searchString.indexOf('Items/') + 6; - const endIndex = searchString.indexOf('/', startIndex); - return endIndex !== -1 ? searchString.substring(startIndex, endIndex) : searchString.substring(startIndex); - }, - /** - * Event handler that runs whenever the current view changes. - * Used to detect the start of video playback. - */ - viewShow() { - const location = window.location.hash; - this.d(`Location changed to ${location}`); - this.allowEnter = true; - this.injectMetadata = /#\/(tv|details|home|search)/.test(location); - if (location === "#/video") { - this.injectCss(); - this.injectButton(); - this.videoPlayer = document.querySelector("video"); - if (this.videoPlayer) { - this.d("Hooking video timeupdate"); - this.videoPlayer.addEventListener("timeupdate", this.videoPositionChanged); - this.osdElement = document.querySelector("div.videoOsdBottom") - this.observer.observe(document.body, { childList: true, subtree: false }); - } - } - else { - this.observer.disconnect(); - } - }, - /** - * Injects the CSS used by the skip intro button. - * Calling this function is a no-op if the CSS has already been injected. - */ - injectCss() { - if (document.querySelector("style#introSkipperCss")) { - this.d("CSS already added"); - return; - } - this.d("Adding CSS"); - const styleElement = document.createElement("style"); - styleElement.id = "introSkipperCss"; - styleElement.textContent = ` - :root { - --rounding: 4px; - --accent: 0, 164, 220; - } - #skipIntro.upNextContainer { - width: unset; - margin: unset; - } - #skipIntro { - position: absolute; - bottom: 7.5em; - right: 5em; - background-color: transparent; - } - #skipIntro .emby-button { - color: #ffffff; - font-size: 110%; - background: rgba(0, 0, 0, 0.7); - border-radius: var(--rounding); - box-shadow: 0 0 4px rgba(0, 0, 0, 0.6); - transition: opacity 0.3s cubic-bezier(0.4,0,0.2,1), - transform 0.3s cubic-bezier(0.4,0,0.2,1), - background-color 0.2s ease-out, - box-shadow 0.2s ease-out; - opacity: 0; - transform: translateY(50%); - } - #skipIntro.show .emby-button { - opacity: 1; - transform: translateY(0); - } - #skipIntro .emby-button:hover { - background: rgb(var(--accent)); - box-shadow: 0 0 8px rgba(var(--accent), 0.6); - filter: brightness(1.2); - } - #skipIntro .emby-button:focus { - background: rgb(var(--accent)); - box-shadow: 0 0 8px rgba(var(--accent), 0.6); - } - #btnSkipSegmentText { - letter-spacing: 0.5px; - padding: 0 5px 0 5px; - } - `; - document.querySelector("head").appendChild(styleElement); - }, - /** - * Inject the skip intro button into the video player. - * Calling this function is a no-op if the CSS has already been injected. - */ - async injectButton() { - // Ensure the button we're about to inject into the page doesn't conflict with a pre-existing one - const preExistingButton = document.querySelector("div.skipIntro"); - if (preExistingButton) { - preExistingButton.style.display = "none"; - } - if (document.querySelector(".btnSkipIntro.injected")) { - this.d("Button already added"); - this.skipButton = document.querySelector("#skipIntro"); - return; - } - const config = await this.secureFetch("Intros/UserInterfaceConfiguration"); - if (!config.SkipButtonVisible) { - this.d("Not adding button: not visible"); - return; - } - this.d("Adding button"); - this.skipButton = document.createElement("div"); - this.skipButton.id = "skipIntro"; - this.skipButton.classList.add("hide", "upNextContainer"); - this.skipButton.addEventListener("click", this.doSkip.bind(this)); - this.skipButton.addEventListener("keydown", this.eventHandler.bind(this)); - this.skipButton.innerHTML = ` - - `; - this.skipButton.dataset.Introduction = config.SkipButtonIntroText; - this.skipButton.dataset.Credits = config.SkipButtonEndCreditsText; - const controls = document.querySelector("div#videoOsdPage"); - controls.appendChild(this.skipButton); - }, - /** Tests if the OSD controls are visible. */ - osdVisible() { - return this.osdElement ? !this.osdElement.classList.contains("hide") : false; - }, - /** Get the currently playing skippable segment. */ - getCurrentSegment(position) { - for (const [key, segment] of Object.entries(this.skipSegments)) { - if ((position > segment.ShowSkipPromptAt && position < segment.HideSkipPromptAt - 1) || - (this.osdVisible() && position > segment.IntroStart && position < segment.IntroEnd - 1)) { - segment.SegmentType = key; - return segment; - } - } - return { SegmentType: "None" }; - }, - overrideBlur(button) { - if (!button.originalBlur) { - button.originalBlur = button.blur; - button.blur = function() { - if (!this.contains(document.activeElement)) { - this.originalBlur(); - } - }; - } - }, - /** Playback position changed, check if the skip button needs to be displayed. */ - videoPositionChanged() { - if (!this.skipButton) return; - const embyButton = this.skipButton.querySelector(".emby-button"); - const segmentType = this.getCurrentSegment(this.videoPlayer.currentTime).SegmentType; - if (segmentType === "None" || this.currentOption === "Off" || !this.allowEnter) { - if (this.skipButton.classList.contains('show')) { - this.skipButton.classList.remove('show'); - embyButton.addEventListener("transitionend", () => { - this.skipButton.classList.add("hide"); - if (this.osdVisible()) { - this.osdElement.querySelector('button.btnPause').focus(); - } else { - embyButton.originalBlur(); - } - }, { once: true }); - } - return; - } - if (this.currentOption === "Automatically Skip" || (this.currentOption === "Button w/ auto PiP" && document.pictureInPictureElement)) { - this.doSkip(); - return; - } - this.skipButton.querySelector("#btnSkipSegmentText").textContent = this.skipButton.dataset[segmentType]; - if (!this.skipButton.classList.contains("hide")) { - if (!this.osdVisible() && !embyButton.contains(document.activeElement)) embyButton.focus(); - return; - } - requestAnimationFrame(() => { - this.skipButton.classList.remove("hide"); - requestAnimationFrame(() => { - this.skipButton.classList.add('show'); - this.overrideBlur(embyButton); - embyButton.focus(); - }); - }); - }, - /** Seeks to the end of the intro. */ - doSkip() { - if (!this.allowEnter) return; - const segment = this.getCurrentSegment(this.videoPlayer.currentTime); - if (segment.SegmentType === "None") { - console.warn("[intro skipper] doSkip() called without an active segment"); - return; - } - this.d(`Skipping ${segment.SegmentType}`); - this.allowEnter = false; - const seekedHandler = () => { - this.videoPlayer.removeEventListener('seeked', seekedHandler); - setTimeout(() => { - this.allowEnter = true; - }, 500); - }; - this.videoPlayer.addEventListener('seeked', seekedHandler); - this.videoPlayer.currentTime = segment.SegmentType === "Credits" && this.videoPlayer.duration - segment.IntroEnd < 3 - ? this.videoPlayer.duration + 10 - : segment.IntroEnd; - }, - createButton(ref, id, innerHTML, clickHandler) { - const button = ref.cloneNode(true); - button.setAttribute('data-id', id); - button.innerHTML = innerHTML; - button.addEventListener('click', clickHandler); - return button; - }, - closeSubmenu(fullscreen) { - document.querySelector('.dialogContainer').remove(); - document.querySelector('.dialogBackdrop').remove() - document.dispatchEvent(new KeyboardEvent('keydown', { key: 'Control' })); - if (!fullscreen) return; - document.removeEventListener('keydown', this.handleEscapeKey); - document.querySelector('.btnVideoOsdSettings').focus(); - }, - openSubmenu(ref, menu) { - const options = ['Show Button', 'Button w/ auto PiP', 'Automatically Skip', 'Off']; - const submenu = menu.cloneNode(true); - const scroller = submenu.querySelector('.actionSheetScroller'); - scroller.innerHTML = ''; - options.forEach(option => { - if (option !== 'Button w/ auto PiP' || document.pictureInPictureEnabled) { - const button = this.createButton(ref, `introskipper-${option.toLowerCase().replace(' ', '-')}`, - `
${option}
`, - () => this.selectOption(option)); - scroller.appendChild(button); - } - }); - const backdrop = document.createElement('div'); - backdrop.className = 'dialogBackdrop dialogBackdropOpened'; - document.body.append(backdrop, submenu); - const actionSheet = submenu.querySelector('.actionSheet'); - if (actionSheet.classList.contains('actionsheet-not-fullscreen')) { - this.adjustPosition(actionSheet, document.querySelector('.btnVideoOsdSettings')); - submenu.addEventListener('click', () => this.closeSubmenu(false)); - } else { - submenu.querySelector('.btnCloseActionSheet').addEventListener('click', () => this.closeSubmenu(true)) - scroller.addEventListener('click', () => this.closeSubmenu(true)) - document.addEventListener('keydown', this.handleEscapeKey); - setTimeout(() => scroller.firstElementChild.focus(), 240); - } - }, - selectOption(option) { - this.currentOption = option; - localStorage.setItem('introskipperOption', option); - this.d(`Introskipper option selected and saved: ${option}`); - }, - isAutoSkipLocked(config) { - const isAutoSkip = config.AutoSkip && config.AutoSkipCredits; - const isAutoSkipClient = new Set(config.ClientList.split(',')).has(ApiClient.appName()); - return isAutoSkip || (config.SkipButtonVisible && isAutoSkipClient); - }, - async injectIntroSkipperOptions(actionSheet) { - if (!this.skipButton) return; - const config = await this.secureFetch("Intros/UserInterfaceConfiguration"); - if (this.isAutoSkipLocked(config)) { - this.d("Auto skip enforced by server"); - return; - } - const statsButton = actionSheet.querySelector('[data-id="stats"]'); - if (!statsButton) return; - const menuItem = this.createButton(statsButton, 'introskipperMenu', - `
Intro Skipper
${this.currentOption}
`, - () => this.openSubmenu(statsButton, actionSheet.closest('.dialogContainer'))); - const originalWidth = actionSheet.offsetWidth; - statsButton.before(menuItem); - if (actionSheet.classList.contains('actionsheet-not-fullscreen')) this.adjustPosition(actionSheet, menuItem, originalWidth); - }, - adjustPosition(element, reference, originalWidth) { - if (originalWidth) { - const currentTop = parseInt(element.style.top, 10) || 0; - element.style.top = `${currentTop - reference.offsetHeight}px`; - const newWidth = Math.max(reference.offsetWidth - originalWidth, 0); - const originalLeft = parseInt(element.style.left, 10) || 0; - element.style.left = `${originalLeft - newWidth / 2}px`; - } else { - const rect = reference.getBoundingClientRect(); - element.style.left = `${Math.min(rect.left - (element.offsetWidth - rect.width) / 2, window.innerWidth - element.offsetWidth - 10)}px`; - element.style.top = `${rect.top - element.offsetHeight + rect.height}px`; - } - }, - injectSkipperFields(metadataFormFields) { - const skipperFields = document.createElement('div'); - skipperFields.className = 'detailSection introskipperSection'; - skipperFields.innerHTML = ` -

Intro Skipper

-
-
- - - -
-
- - - -
-
-
-
- - - -
-
- - - -
-
- `; - metadataFormFields.querySelector('#metadataSettingsCollapsible').insertAdjacentElement('afterend', skipperFields); - this.attachSaveListener(metadataFormFields); - this.updateSkipperFields(skipperFields); - this.setTimeInputs(skipperFields); - }, - updateSkipperFields(skipperFields) { - const { Introduction = {}, Credits = {} } = this.skipperData; - skipperFields.querySelector('#introStartEdit').value = Introduction.Start || 0; - skipperFields.querySelector('#introEndEdit').value = Introduction.End || 0; - skipperFields.querySelector('#creditsStartEdit').value = Credits.Start || 0; - skipperFields.querySelector('#creditsEndEdit').value = Credits.End || 0; - }, - attachSaveListener(metadataFormFields) { - const saveButton = metadataFormFields.querySelector('.formDialogFooter .btnSave'); - if (saveButton) { - saveButton.addEventListener('click', this.saveSkipperData.bind(this)); - } else { - console.error('Save button not found'); - } - }, - setTimeInputs(skipperFields) { - const inputContainers = skipperFields.querySelectorAll('.inputContainer'); - inputContainers.forEach(container => { - const displayInput = container.querySelector('[id$="Display"]'); - const editInput = container.querySelector('[id$="Edit"]'); - displayInput.addEventListener('pointerdown', (e) => { - e.preventDefault(); - this.switchToEdit(displayInput, editInput); - }); - editInput.addEventListener('blur', () => this.switchToDisplay(displayInput, editInput)); - displayInput.value = this.formatTime(parseFloat(editInput.value) || 0); - }); - }, - formatTime(totalSeconds) { - const totalRoundedSeconds = Math.round(totalSeconds); - const hours = Math.floor(totalRoundedSeconds / 3600); - const minutes = Math.floor((totalRoundedSeconds % 3600) / 60); - const seconds = totalRoundedSeconds % 60; - let result = []; - if (hours > 0) result.push(`${hours} hour${hours !== 1 ? 's' : ''}`); - if (minutes > 0) result.push(`${minutes} minute${minutes !== 1 ? 's' : ''}`); - if (seconds > 0 || result.length === 0) result.push(`${seconds} second${seconds !== 1 ? 's' : ''}`); - return result.join(' '); - }, - switchToEdit(displayInput, editInput) { - displayInput.style.display = 'none'; - editInput.style.display = ''; - editInput.focus(); - }, - switchToDisplay(displayInput, editInput) { - editInput.style.display = 'none'; - displayInput.style.display = ''; - displayInput.value = this.formatTime(parseFloat(editInput.value) || 0); - }, - async saveSkipperData() { - const newTimestamps = { - Introduction: { - Start: parseFloat(document.getElementById('introStartEdit').value || 0), - End: parseFloat(document.getElementById('introEndEdit').value || 0) - }, - Credits: { - Start: parseFloat(document.getElementById('creditsStartEdit').value || 0), - End: parseFloat(document.getElementById('creditsEndEdit').value || 0) - } - }; - const { Introduction = {}, Credits = {} } = this.skipperData; - if (newTimestamps.Introduction.Start !== (Introduction.Start || 0) || - newTimestamps.Introduction.End !== (Introduction.End || 0) || - newTimestamps.Credits.Start !== (Credits.Start || 0) || - newTimestamps.Credits.End !== (Credits.End || 0)) { - const response = await this.secureFetch(`Episode/${this.currentEpisodeId}/Timestamps`, "POST", JSON.stringify(newTimestamps)); - this.d(response.ok ? 'Timestamps updated successfully' : 'Failed to update timestamps:', response.status); - } else { - this.d('Timestamps have not changed, skipping update'); - } - }, - /** Make an authenticated fetch to the Jellyfin server and parse the response body as JSON. */ - async secureFetch(url, method = "GET", body = null) { - const response = await fetch(`${ApiClient.serverAddress()}/${url}`, { - method, - headers: Object.assign({ "Authorization": `MediaBrowser Token=${ApiClient.accessToken()}` }, - method === "POST" ? {"Content-Type": "application/json"} : {}), - body }); - return response.ok ? (method === "POST" ? response : response.json()) : - response.status === 404 ? null : - console.error(`Error ${response.status} from ${url}`) || null; - }, - /** Handle keydown events. */ - eventHandler(e) { - if (e.key !== "Enter") return; - e.stopPropagation(); - e.preventDefault(); - this.doSkip(); - }, - handleEscapeKey(e) { - if (e.key === 'Escape' || e.keyCode === 461 || e.keyCode === 10009) { - e.stopPropagation(); - this.closeSubmenu(true); - } - } -}; -introSkipper.setup(); diff --git a/ConfusedPolarBear.Plugin.IntroSkipper/Configuration/visualizer.js b/ConfusedPolarBear.Plugin.IntroSkipper/Configuration/visualizer.js deleted file mode 100644 index d1bd8b0..0000000 --- a/ConfusedPolarBear.Plugin.IntroSkipper/Configuration/visualizer.js +++ /dev/null @@ -1,234 +0,0 @@ -// re-render the troubleshooter with the latest offset -function renderTroubleshooter() { - paintFingerprintDiff(canvas, lhs, rhs, Number(offset.value)); - findIntros(); -} - -// refresh the upper & lower bounds for the offset -function refreshBounds() { - const len = Math.min(lhs.length, rhs.length) - 1; - offset.min = -1 * len; - offset.max = len; -} - -function findIntros() { - let times = []; - - // get the times of all similar fingerprint points - for (let i in fprDiffs) { - if (fprDiffs[i] > fprDiffMinimum) { - times.push(i * 0.1238); - } - } - - // always close the last range - times.push(Number.MAX_VALUE); - - let last = times[0]; - let start = last; - let end = last; - let ranges = []; - - for (let t of times) { - const diff = t - last; - - if (diff <= 3.5) { - end = t; - last = t; - continue; - } - - const dur = Math.round(end - start); - if (dur >= 15) { - ranges.push({ - "start": start, - "end": end, - "duration": dur - }); - } - - start = t; - end = t; - last = t; - } - - const introsLog = document.querySelector("span#intros"); - introsLog.style.position = "relative"; - introsLog.style.left = "115px"; - introsLog.innerHTML = ""; - - const offset = Number(txtOffset.value) * 0.1238; - for (let r of ranges) { - let lStart, lEnd, rStart, rEnd; - - if (offset < 0) { - // negative offset, the diff is aligned with the RHS - lStart = r.start - offset; - lEnd = r.end - offset; - rStart = r.start; - rEnd = r.end; - - } else { - // positive offset, the diff is aligned with the LHS - lStart = r.start; - lEnd = r.end; - rStart = r.start + offset; - rEnd = r.end + offset; - } - - const lTitle = selectEpisode1.options[selectEpisode1.selectedIndex].text; - const rTitle = selectEpisode2.options[selectEpisode2.selectedIndex].text; - introsLog.innerHTML += "" + lTitle + ": " + - secondsToString(lStart) + " - " + secondsToString(lEnd) + "
"; - introsLog.innerHTML += "" + rTitle + ": " + - secondsToString(rStart) + " - " + secondsToString(rEnd) + "
"; - } -} - -// find all shifts which align exact matches of audio. -function findExactMatches() { - let shifts = []; - - for (let lhsIndex in lhs) { - let lhsPoint = lhs[lhsIndex]; - let rhsIndex = rhs.findIndex((x) => x === lhsPoint); - - if (rhsIndex === -1) { - continue; - } - - let shift = rhsIndex - lhsIndex; - if (shifts.includes(shift)) { - continue; - } - - shifts.push(shift); - } - - // Only suggest up to 20 shifts - shifts = shifts.slice(0, 20); - - txtSuggested.textContent = "Suggested shifts: "; - if (shifts.length === 0) { - txtSuggested.textContent += "none available"; - } else { - shifts.sort((a, b) => { return a - b }); - txtSuggested.textContent += shifts.join(", "); - } -} - -// The below two functions were modified from https://github.com/dnknth/acoustid-match/blob/ffbf21d8c53c40d3b3b4c92238c35846545d3cd7/fingerprints/static/fingerprints/fputils.js -// Originally licensed as MIT. -function renderFingerprintData(ctx, fp, xor = false) { - const pixels = ctx.createImageData(32, fp.length); - let idx = 0; - - for (let i = 0; i < fp.length; i++) { - for (let j = 0; j < 32; j++) { - if (fp[i] & (1 << j)) { - pixels.data[idx + 0] = 255; - pixels.data[idx + 1] = 255; - pixels.data[idx + 2] = 255; - - } else { - pixels.data[idx + 0] = 0; - pixels.data[idx + 1] = 0; - pixels.data[idx + 2] = 0; - } - - pixels.data[idx + 3] = 255; - idx += 4; - } - } - - if (!xor) { - return pixels; - } - - // if rendering the XOR of the fingerprints, count how many bits are different at each timecode - fprDiffs = []; - - for (let i = 0; i < fp.length; i++) { - let count = 0; - - for (let j = 0; j < 32; j++) { - if (fp[i] & (1 << j)) { - count++; - } - } - - // push the percentage similarity - fprDiffs[i] = 100 - (count * 100) / 32; - } - - return pixels; -} - -function paintFingerprintDiff(canvas, fp1, fp2, offset) { - if (fp1.length == 0) { - return; - } - - canvas.style.display = "unset"; - - let leftOffset = 0, rightOffset = 0; - if (offset < 0) { - leftOffset -= offset; - } else { - rightOffset += offset; - } - - let fpDiff = []; - fpDiff.length = Math.min(fp1.length, fp2.length) - Math.abs(offset); - for (let i = 0; i < fpDiff.length; i++) { - fpDiff[i] = fp1[i + leftOffset] ^ fp2[i + rightOffset]; - } - - const ctx = canvas.getContext('2d'); - const pixels1 = renderFingerprintData(ctx, fp1); - const pixels2 = renderFingerprintData(ctx, fp2); - const pixelsDiff = renderFingerprintData(ctx, fpDiff, true); - const border = 4; - - canvas.width = pixels1.width + border + // left fingerprint - pixels2.width + border + // right fingerprint - pixelsDiff.width + border // fingerprint diff - + 4; // if diff[x] >= fprDiffMinimum - - canvas.height = Math.max(pixels1.height, pixels2.height) + Math.abs(offset); - - ctx.rect(0, 0, canvas.width, canvas.height); - ctx.fillStyle = "#C5C5C5"; - ctx.fill(); - - // draw left fingerprint - let dx = 0; - ctx.putImageData(pixels1, dx, rightOffset); - dx += pixels1.width + border; - - // draw right fingerprint - ctx.putImageData(pixels2, dx, leftOffset); - dx += pixels2.width + border; - - // draw fingerprint diff - ctx.putImageData(pixelsDiff, dx, Math.abs(offset)); - dx += pixelsDiff.width + border; - - // draw the fingerprint diff similarity indicator - // https://davidmathlogic.com/colorblind/#%23EA3535-%232C92EF - for (let i in fprDiffs) { - const j = Number(i); - const y = Math.abs(offset) + j; - const point = fprDiffs[j]; - - if (point >= 100) { - ctx.fillStyle = "#002FFF" - } else if (point >= fprDiffMinimum) { - ctx.fillStyle = "#2C92EF"; - } else { - ctx.fillStyle = "#EA3535"; - } - - ctx.fillRect(dx, y, 4, 1); - } -} diff --git a/ConfusedPolarBear.Plugin.IntroSkipper/ConfusedPolarBear.Plugin.IntroSkipper.csproj b/ConfusedPolarBear.Plugin.IntroSkipper/ConfusedPolarBear.Plugin.IntroSkipper.csproj deleted file mode 100644 index ff595c2..0000000 --- a/ConfusedPolarBear.Plugin.IntroSkipper/ConfusedPolarBear.Plugin.IntroSkipper.csproj +++ /dev/null @@ -1,26 +0,0 @@ - - - net8.0 - ConfusedPolarBear.Plugin.IntroSkipper - 1.0.1.0 - 1.0.1.0 - true - true - enable - AllEnabledByDefault - ../jellyfin.ruleset - - - - - - - - - - - - - - - diff --git a/ConfusedPolarBear.Plugin.IntroSkipper/Controllers/SkipIntroController.cs b/ConfusedPolarBear.Plugin.IntroSkipper/Controllers/SkipIntroController.cs deleted file mode 100644 index 03a5c20..0000000 --- a/ConfusedPolarBear.Plugin.IntroSkipper/Controllers/SkipIntroController.cs +++ /dev/null @@ -1,229 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Net.Mime; -using ConfusedPolarBear.Plugin.IntroSkipper.Configuration; -using ConfusedPolarBear.Plugin.IntroSkipper.Data; -using MediaBrowser.Common.Api; -using MediaBrowser.Controller.Entities.TV; -using Microsoft.AspNetCore.Authorization; -using Microsoft.AspNetCore.Mvc; - -namespace ConfusedPolarBear.Plugin.IntroSkipper.Controllers; - -/// -/// Skip intro controller. -/// -[Authorize] -[ApiController] -[Produces(MediaTypeNames.Application.Json)] -public class SkipIntroController : ControllerBase -{ - /// - /// Initializes a new instance of the class. - /// - public SkipIntroController() - { - } - - /// - /// Returns the timestamps of the introduction in a television episode. Responses are in API version 1 format. - /// - /// ID of the episode. Required. - /// Timestamps to return. Optional. Defaults to Introduction for backwards compatibility. - /// Episode contains an intro. - /// Failed to find an intro in the provided episode. - /// Detected intro. - [HttpGet("Episode/{id}/IntroTimestamps")] - [HttpGet("Episode/{id}/IntroTimestamps/v1")] - public ActionResult GetIntroTimestamps( - [FromRoute] Guid id, - [FromQuery] AnalysisMode mode = AnalysisMode.Introduction) - { - var intro = GetIntro(id, mode); - - if (intro is null || !intro.Valid) - { - return NotFound(); - } - - return intro; - } - - /// - /// Updates the timestamps for the provided episode. - /// - /// Episode ID to update timestamps for. - /// New timestamps Introduction/Credits start and end times. - /// New timestamps saved. - /// Given ID is not an Episode. - /// No content. - [Authorize(Policy = Policies.RequiresElevation)] - [HttpPost("Episode/{Id}/Timestamps")] - public ActionResult UpdateTimestamps([FromRoute] Guid id, [FromBody] TimeStamps timestamps) - { - // only update existing episodes - var rawItem = Plugin.Instance!.GetItem(id); - if (rawItem == null || rawItem is not Episode episode) - { - return NotFound(); - } - - if (timestamps?.Introduction.End > 0.0) - { - var tr = new TimeRange(timestamps.Introduction.Start, timestamps.Introduction.End); - Plugin.Instance!.Intros[id] = new Segment(id, tr); - } - - if (timestamps?.Credits.End > 0.0) - { - var cr = new TimeRange(timestamps.Credits.Start, timestamps.Credits.End); - Plugin.Instance!.Credits[id] = new Segment(id, cr); - } - - Plugin.Instance!.SaveTimestamps(AnalysisMode.Introduction); - Plugin.Instance!.SaveTimestamps(AnalysisMode.Credits); - - return NoContent(); - } - - /// - /// Gets the timestamps for the provided episode. - /// - /// Episode ID. - /// Sucess. - /// Given ID is not an Episode. - /// Episode Timestamps. - [HttpGet("Episode/{Id}/Timestamps")] - [ActionName("UpdateTimestamps")] - public ActionResult GetTimestamps([FromRoute] Guid id) - { - // only get return content for episodes - var rawItem = Plugin.Instance!.GetItem(id); - if (rawItem == null || rawItem is not Episode episode) - { - return NotFound(); - } - - var times = new TimeStamps(); - if (Plugin.Instance!.Intros.TryGetValue(id, out var introValue)) - { - times.Introduction = introValue; - } - - if (Plugin.Instance!.Credits.TryGetValue(id, out var creditValue)) - { - times.Credits = creditValue; - } - - return times; - } - - /// - /// Gets a dictionary of all skippable segments. - /// - /// Media ID. - /// Skippable segments dictionary. - /// Dictionary of skippable segments. - [HttpGet("Episode/{id}/IntroSkipperSegments")] - public ActionResult> GetSkippableSegments([FromRoute] Guid id) - { - var segments = new Dictionary(); - - if (GetIntro(id, AnalysisMode.Introduction) is Intro intro) - { - segments[AnalysisMode.Introduction] = intro; - } - - if (GetIntro(id, AnalysisMode.Credits) is Intro credits) - { - segments[AnalysisMode.Credits] = credits; - } - - return segments; - } - - /// Lookup and return the skippable timestamps for the provided item. - /// Unique identifier of this episode. - /// Mode. - /// Intro object if the provided item has an intro, null otherwise. - private static Intro? GetIntro(Guid id, AnalysisMode mode) - { - try - { - var timestamp = Plugin.GetIntroByMode(id, mode); - - // Operate on a copy to avoid mutating the original Intro object stored in the dictionary. - var segment = new Intro(timestamp); - - var config = Plugin.Instance!.Configuration; - segment.IntroEnd -= config.RemainingSecondsOfIntro; - if (config.PersistSkipButton) - { - segment.ShowSkipPromptAt = segment.IntroStart; - segment.HideSkipPromptAt = segment.IntroEnd; - } - else - { - segment.ShowSkipPromptAt = Math.Max(0, segment.IntroStart - config.ShowPromptAdjustment); - segment.HideSkipPromptAt = Math.Min( - segment.IntroStart + config.HidePromptAdjustment, - segment.IntroEnd); - } - - return segment; - } - catch (KeyNotFoundException) - { - return null; - } - } - - /// - /// Erases all previously discovered introduction timestamps. - /// - /// Mode. - /// Erase cache. - /// Operation successful. - /// No content. - [Authorize(Policy = Policies.RequiresElevation)] - [HttpPost("Intros/EraseTimestamps")] - public ActionResult ResetIntroTimestamps([FromQuery] AnalysisMode mode, [FromQuery] bool eraseCache = false) - { - if (mode == AnalysisMode.Introduction) - { - Plugin.Instance!.Intros.Clear(); - } - else if (mode == AnalysisMode.Credits) - { - Plugin.Instance!.Credits.Clear(); - } - - if (eraseCache) - { - FFmpegWrapper.DeleteCacheFiles(mode); - } - - Plugin.Instance!.EpisodeStates.Clear(); - Plugin.Instance!.SaveTimestamps(mode); - return NoContent(); - } - - /// - /// Gets the user interface configuration. - /// - /// UserInterfaceConfiguration returned. - /// UserInterfaceConfiguration. - [HttpGet] - [Route("Intros/UserInterfaceConfiguration")] - public ActionResult GetUserInterfaceConfiguration() - { - var config = Plugin.Instance!.Configuration; - return new UserInterfaceConfiguration( - config.SkipButtonVisible, - config.SkipButtonIntroText, - config.SkipButtonEndCreditsText, - config.AutoSkip, - config.AutoSkipCredits, - config.ClientList); - } -} diff --git a/ConfusedPolarBear.Plugin.IntroSkipper/Controllers/TroubleshootingController.cs b/ConfusedPolarBear.Plugin.IntroSkipper/Controllers/TroubleshootingController.cs deleted file mode 100644 index 24fc87d..0000000 --- a/ConfusedPolarBear.Plugin.IntroSkipper/Controllers/TroubleshootingController.cs +++ /dev/null @@ -1,153 +0,0 @@ -using System; -using System.Globalization; -using System.IO; -using System.Net.Mime; -using System.Text; -using ConfusedPolarBear.Plugin.IntroSkipper.Data; -using ConfusedPolarBear.Plugin.IntroSkipper.Helper; -using MediaBrowser.Common; -using MediaBrowser.Common.Api; -using MediaBrowser.Controller.Library; -using Microsoft.AspNetCore.Authorization; -using Microsoft.AspNetCore.Mvc; -using Microsoft.Extensions.Logging; - -namespace ConfusedPolarBear.Plugin.IntroSkipper.Controllers; - -/// -/// Troubleshooting controller. -/// -[Authorize(Policy = Policies.RequiresElevation)] -[ApiController] -[Produces(MediaTypeNames.Application.Json)] -[Route("IntroSkipper")] -public class TroubleshootingController : ControllerBase -{ - private readonly ILibraryManager _libraryManager; - private readonly IApplicationHost _applicationHost; - private readonly ILogger _logger; - - /// - /// Initializes a new instance of the class. - /// - /// Application host. - /// Library Manager. - /// Logger. - public TroubleshootingController( - IApplicationHost applicationHost, - ILibraryManager libraryManager, - ILogger logger) - { - _libraryManager = libraryManager; - _applicationHost = applicationHost; - _logger = logger; - } - - /// - /// Gets a Markdown formatted support bundle. - /// - /// Support bundle created. - /// Support bundle. - [HttpGet("SupportBundle")] - [Produces(MediaTypeNames.Text.Plain)] - public ActionResult GetSupportBundle() - { - ArgumentNullException.ThrowIfNull(Plugin.Instance); - - var bundle = new StringBuilder(); - - bundle.Append("* Jellyfin version: "); - bundle.Append(_applicationHost.ApplicationVersionString); - bundle.Append('\n'); - - var version = Plugin.Instance.Version.ToString(3); - - try - { - var commit = Commit.CommitHash; - if (!string.IsNullOrWhiteSpace(commit)) - { - version += string.Concat("+", commit.AsSpan(0, 12)); - } - } - catch (Exception ex) - { - _logger.LogWarning("Unable to append commit to version: {Exception}", ex); - } - - bundle.Append("* Plugin version: "); - bundle.Append(version); - bundle.Append('\n'); - - bundle.Append("* Queue contents: "); - bundle.Append(Plugin.Instance.TotalQueued); - bundle.Append(" episodes, "); - bundle.Append(Plugin.Instance.TotalSeasons); - bundle.Append(" seasons\n"); - - bundle.Append("* Warnings: `"); - bundle.Append(WarningManager.GetWarnings()); - bundle.Append("`\n"); - - bundle.Append(FFmpegWrapper.GetChromaprintLogs()); - - return bundle.ToString(); - } - - /// - /// Gets a Markdown formatted support bundle. - /// - /// Support bundle created. - /// Support bundle. - [HttpGet("Storage")] - [Produces(MediaTypeNames.Text.Plain)] - public ActionResult GetFreeSpace() - { - ArgumentNullException.ThrowIfNull(Plugin.Instance); - var bundle = new StringBuilder(); - - var libraries = _libraryManager.GetVirtualFolders(); - foreach (var library in libraries) - { - try - { - DriveInfo driveInfo = new DriveInfo(library.Locations[0]); - // Get available free space in bytes - long availableFreeSpace = driveInfo.AvailableFreeSpace; - - // Get total size of the drive in bytes - long totalSize = driveInfo.TotalSize; - - // Get total used space in Percentage - double usedSpacePercentage = totalSize > 0 ? (totalSize - availableFreeSpace) / (double)totalSize * 100 : 0; - - bundle.Append(CultureInfo.CurrentCulture, $"Library: {library.Name}\n"); - bundle.Append(CultureInfo.CurrentCulture, $"Drive: {driveInfo.Name}\n"); - bundle.Append(CultureInfo.CurrentCulture, $"Total Size: {GetHumanReadableSize(totalSize)}\n"); - bundle.Append(CultureInfo.CurrentCulture, $"Available Free Space: {GetHumanReadableSize(availableFreeSpace)}\n"); - bundle.Append(CultureInfo.CurrentCulture, $"Total used in Percentage: {Math.Round(usedSpacePercentage, 2)}%\n\n"); - } - catch (Exception ex) - { - _logger.LogWarning("Unable to get DriveInfo: {Exception}", ex); - } - } - - return bundle.ToString().TrimEnd('\n'); - } - - private static string GetHumanReadableSize(long bytes) - { - string[] sizes = ["Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"]; - double len = bytes; - int order = 0; - - while (len >= 1024 && order < sizes.Length - 1) - { - order++; - len /= 1024; - } - - return $"{len:0.##} {sizes[order]}"; - } -} diff --git a/ConfusedPolarBear.Plugin.IntroSkipper/Controllers/VisualizationController.cs b/ConfusedPolarBear.Plugin.IntroSkipper/Controllers/VisualizationController.cs deleted file mode 100644 index 5d3ae9f..0000000 --- a/ConfusedPolarBear.Plugin.IntroSkipper/Controllers/VisualizationController.cs +++ /dev/null @@ -1,309 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Globalization; -using System.Linq; -using System.Net.Mime; -using ConfusedPolarBear.Plugin.IntroSkipper.Data; -using MediaBrowser.Common.Api; -using Microsoft.AspNetCore.Authorization; -using Microsoft.AspNetCore.Mvc; -using Microsoft.Extensions.Logging; - -namespace ConfusedPolarBear.Plugin.IntroSkipper.Controllers; - -/// -/// Audio fingerprint visualization controller. Allows browsing fingerprints on a per episode basis. -/// -/// -/// Initializes a new instance of the class. -/// -/// Logger. -[Authorize(Policy = Policies.RequiresElevation)] -[ApiController] -[Produces(MediaTypeNames.Application.Json)] -[Route("Intros")] -public class VisualizationController(ILogger logger) : ControllerBase -{ - private readonly ILogger _logger = logger; - - /// - /// Returns all show names and seasons. - /// - /// Dictionary of show names to a list of season names. - [HttpGet("Shows")] - public ActionResult> GetShowSeasons() - { - _logger.LogDebug("Returning season IDs by series name"); - - var showSeasons = new Dictionary(); - - foreach (var kvp in Plugin.Instance!.QueuedMediaItems) - { - if (kvp.Value.FirstOrDefault() is QueuedEpisode first) - { - var seriesId = first.SeriesId; - var seasonId = kvp.Key; - - var seasonNumber = first.SeasonNumber; - if (!showSeasons.TryGetValue(seriesId, out var showInfo)) - { - showInfo = new ShowInfos { SeriesName = first.SeriesName, ProductionYear = GetProductionYear(seriesId), LibraryName = GetLibraryName(seriesId), Seasons = [] }; - showSeasons[seriesId] = showInfo; - } - - showInfo.Seasons[seasonId] = seasonNumber; - } - } - - // Sort the dictionary by SeriesName and the seasons by SeasonName - var sortedShowSeasons = showSeasons - .OrderBy(kvp => kvp.Value.SeriesName) - .ToDictionary( - kvp => kvp.Key, - kvp => new ShowInfos - { - SeriesName = kvp.Value.SeriesName, - ProductionYear = kvp.Value.ProductionYear, - LibraryName = kvp.Value.LibraryName, - Seasons = kvp.Value.Seasons - .OrderBy(s => s.Value) - .ToDictionary(s => s.Key, s => s.Value) - }); - - return sortedShowSeasons; - } - - /// - /// Returns the ignore list for the provided season. - /// - /// Season ID. - /// List of episode titles. - [HttpGet("IgnoreListSeason/{SeasonId}")] - public ActionResult GetIgnoreListSeason([FromRoute] Guid seasonId) - { - if (!Plugin.Instance!.QueuedMediaItems.ContainsKey(seasonId)) - { - return NotFound(); - } - - if (!Plugin.Instance!.IgnoreList.TryGetValue(seasonId, out _)) - { - return new IgnoreListItem(seasonId); - } - - return new IgnoreListItem(Plugin.Instance!.IgnoreList[seasonId]); - } - - /// - /// Returns the ignore list for the provided series. - /// - /// Show ID. - /// List of episode titles. - [HttpGet("IgnoreListSeries/{SeriesId}")] - public ActionResult GetIgnoreListSeries([FromRoute] Guid seriesId) - { - var seasonIds = Plugin.Instance!.QueuedMediaItems - .Where(kvp => kvp.Value.Any(e => e.SeriesId == seriesId)) - .Select(kvp => kvp.Key) - .ToList(); - - if (seasonIds.Count == 0) - { - return NotFound(); - } - - return new IgnoreListItem(Guid.Empty) - { - IgnoreIntro = seasonIds.All(seasonId => Plugin.Instance!.IsIgnored(seasonId, AnalysisMode.Introduction)), - IgnoreCredits = seasonIds.All(seasonId => Plugin.Instance!.IsIgnored(seasonId, AnalysisMode.Credits)) - }; - } - - /// - /// Returns the names and unique identifiers of all episodes in the provided season. - /// - /// Show ID. - /// Season ID. - /// List of episode titles. - [HttpGet("Show/{SeriesId}/{SeasonId}")] - public ActionResult> GetSeasonEpisodes([FromRoute] Guid seriesId, [FromRoute] Guid seasonId) - { - if (!Plugin.Instance!.QueuedMediaItems.TryGetValue(seasonId, out var episodes)) - { - return NotFound(); - } - - if (!episodes.Any(e => e.SeriesId == seriesId)) - { - return NotFound(); - } - - var showName = episodes.FirstOrDefault()?.SeriesName!; - - return episodes.Select(e => new EpisodeVisualization(e.EpisodeId, e.Name)).ToList(); - } - - /// - /// Fingerprint the provided episode and returns the uncompressed fingerprint data points. - /// - /// Episode id. - /// Read only collection of fingerprint points. - [HttpGet("Episode/{Id}/Chromaprint")] - public ActionResult GetEpisodeFingerprint([FromRoute] Guid id) - { - // Search through all queued episodes to find the requested id - foreach (var season in Plugin.Instance!.QueuedMediaItems) - { - foreach (var needle in season.Value) - { - if (needle.EpisodeId == id) - { - return FFmpegWrapper.Fingerprint(needle, AnalysisMode.Introduction); - } - } - } - - return NotFound(); - } - - /// - /// Erases all timestamps for the provided season. - /// - /// Show ID. - /// Season ID. - /// Erase cache. - /// Season timestamps erased. - /// Unable to find season in provided series. - /// No content. - [HttpDelete("Show/{SeriesId}/{SeasonId}")] - public ActionResult EraseSeason([FromRoute] Guid seriesId, [FromRoute] Guid seasonId, [FromQuery] bool eraseCache = false) - { - var episodes = Plugin.Instance!.QueuedMediaItems - .Where(kvp => kvp.Key == seasonId) - .SelectMany(kvp => kvp.Value.Where(e => e.SeriesId == seriesId)) - .ToList(); - - if (episodes.Count == 0) - { - return NotFound(); - } - - _logger.LogInformation("Erasing timestamps for series {SeriesId} season {SeasonId} at user request", seriesId, seasonId); - - foreach (var e in episodes) - { - Plugin.Instance!.Intros.TryRemove(e.EpisodeId, out _); - Plugin.Instance!.Credits.TryRemove(e.EpisodeId, out _); - e.State.ResetStates(); - if (eraseCache) - { - FFmpegWrapper.DeleteEpisodeCache(e.EpisodeId); - } - } - - Plugin.Instance!.SaveTimestamps(AnalysisMode.Introduction | AnalysisMode.Credits); - - return NoContent(); - } - - /// - /// Updates the ignore list for the provided season. - /// - /// New ignore list items. - /// Save the ignore list. - /// No content. - [HttpPost("IgnoreList/UpdateSeason")] - public ActionResult UpdateIgnoreListSeason([FromBody] IgnoreListItem ignoreListItem, bool save = true) - { - if (!Plugin.Instance!.QueuedMediaItems.ContainsKey(ignoreListItem.SeasonId)) - { - return NotFound(); - } - - if (ignoreListItem.IgnoreIntro || ignoreListItem.IgnoreCredits) - { - Plugin.Instance!.IgnoreList.AddOrUpdate(ignoreListItem.SeasonId, ignoreListItem, (_, _) => ignoreListItem); - } - else - { - Plugin.Instance!.IgnoreList.TryRemove(ignoreListItem.SeasonId, out _); - } - - if (save) - { - Plugin.Instance!.SaveIgnoreList(); - } - - return NoContent(); - } - - /// - /// Updates the ignore list for the provided series. - /// - /// Series ID. - /// New ignore list items. - /// No content. - [HttpPost("IgnoreList/UpdateSeries/{SeriesId}")] - public ActionResult UpdateIgnoreListSeries([FromRoute] Guid seriesId, [FromBody] IgnoreListItem ignoreListItem) - { - var seasonIds = Plugin.Instance!.QueuedMediaItems - .Where(kvp => kvp.Value.Any(e => e.SeriesId == seriesId)) - .Select(kvp => kvp.Key) - .ToList(); - - if (seasonIds.Count == 0) - { - return NotFound(); - } - - foreach (var seasonId in seasonIds) - { - UpdateIgnoreListSeason(new IgnoreListItem(ignoreListItem) { SeasonId = seasonId }, false); - } - - Plugin.Instance!.SaveIgnoreList(); - - return NoContent(); - } - - /// - /// Updates the introduction timestamps for the provided episode. - /// - /// Episode ID to update timestamps for. - /// New introduction start and end times. - /// New introduction timestamps saved. - /// No content. - [HttpPost("Episode/{Id}/UpdateIntroTimestamps")] - [Obsolete("deprecated use Episode/{Id}/Timestamps")] - public ActionResult UpdateIntroTimestamps([FromRoute] Guid id, [FromBody] Intro timestamps) - { - if (timestamps.IntroEnd > 0.0) - { - var tr = new TimeRange(timestamps.IntroStart, timestamps.IntroEnd); - Plugin.Instance!.Intros[id] = new Segment(id, tr); - Plugin.Instance.SaveTimestamps(AnalysisMode.Introduction); - } - - return NoContent(); - } - - private static string GetProductionYear(Guid seriesId) - { - return seriesId == Guid.Empty - ? "Unknown" - : Plugin.Instance?.GetItem(seriesId)?.ProductionYear?.ToString(CultureInfo.InvariantCulture) ?? "Unknown"; - } - - private static string GetLibraryName(Guid seriesId) - { - if (seriesId == Guid.Empty) - { - return "Unknown"; - } - - var collectionFolders = Plugin.Instance?.GetCollectionFolders(seriesId); - return collectionFolders?.Count > 0 - ? string.Join(", ", collectionFolders.Select(folder => folder.Name)) - : "Unknown"; - } -} diff --git a/ConfusedPolarBear.Plugin.IntroSkipper/Data/AnalysisMode.cs b/ConfusedPolarBear.Plugin.IntroSkipper/Data/AnalysisMode.cs deleted file mode 100644 index 7da010a..0000000 --- a/ConfusedPolarBear.Plugin.IntroSkipper/Data/AnalysisMode.cs +++ /dev/null @@ -1,17 +0,0 @@ -namespace ConfusedPolarBear.Plugin.IntroSkipper.Data; - -/// -/// Type of media file analysis to perform. -/// -public enum AnalysisMode -{ - /// - /// Detect introduction sequences. - /// - Introduction, - - /// - /// Detect credits. - /// - Credits, -} diff --git a/ConfusedPolarBear.Plugin.IntroSkipper/Data/BlackFrame.cs b/ConfusedPolarBear.Plugin.IntroSkipper/Data/BlackFrame.cs deleted file mode 100644 index 5eefe24..0000000 --- a/ConfusedPolarBear.Plugin.IntroSkipper/Data/BlackFrame.cs +++ /dev/null @@ -1,22 +0,0 @@ -namespace ConfusedPolarBear.Plugin.IntroSkipper.Data; - -/// -/// A frame of video that partially (or entirely) consists of black pixels. -/// -/// -/// Initializes a new instance of the class. -/// -/// Percentage of the frame that is black. -/// Time this frame appears at. -public class BlackFrame(int percent, double time) -{ - /// - /// Gets or sets the percentage of the frame that is black. - /// - public int Percentage { get; set; } = percent; - - /// - /// Gets or sets the time (in seconds) this frame appeared at. - /// - public double Time { get; set; } = time; -} diff --git a/ConfusedPolarBear.Plugin.IntroSkipper/Data/EdlAction.cs b/ConfusedPolarBear.Plugin.IntroSkipper/Data/EdlAction.cs deleted file mode 100644 index 165b703..0000000 --- a/ConfusedPolarBear.Plugin.IntroSkipper/Data/EdlAction.cs +++ /dev/null @@ -1,32 +0,0 @@ -namespace ConfusedPolarBear.Plugin.IntroSkipper.Data; - -/// -/// Taken from https://kodi.wiki/view/Edit_decision_list#MPlayer_EDL. -/// -public enum EdlAction -{ - /// - /// Do not create EDL files. - /// - None = -1, - - /// - /// Completely remove the segment from playback as if it was never in the original video. - /// - Cut = 0, - - /// - /// Mute audio, continue playback. - /// - Mute = 1, - - /// - /// Inserts a new scene marker. - /// - SceneMarker = 2, - - /// - /// Automatically skip once during playback. - /// - CommercialBreak = 3 -} diff --git a/ConfusedPolarBear.Plugin.IntroSkipper/Data/EpisodeState.cs b/ConfusedPolarBear.Plugin.IntroSkipper/Data/EpisodeState.cs deleted file mode 100644 index b5fd801..0000000 --- a/ConfusedPolarBear.Plugin.IntroSkipper/Data/EpisodeState.cs +++ /dev/null @@ -1,50 +0,0 @@ -using System; - -namespace ConfusedPolarBear.Plugin.IntroSkipper.Data; - -/// -/// Represents the state of an episode regarding analysis and blacklist status. -/// -public class EpisodeState -{ - private readonly bool[] _analyzedStates = new bool[2]; - - private readonly bool[] _blacklistedStates = new bool[2]; - - /// - /// Checks if the specified analysis mode has been analyzed. - /// - /// The analysis mode to check. - /// True if the mode has been analyzed, false otherwise. - public bool IsAnalyzed(AnalysisMode mode) => _analyzedStates[(int)mode]; - - /// - /// Sets the analyzed state for the specified analysis mode. - /// - /// The analysis mode to set. - /// The analyzed state to set. - public void SetAnalyzed(AnalysisMode mode, bool value) => _analyzedStates[(int)mode] = value; - - /// - /// Checks if the specified analysis mode has been blacklisted. - /// - /// The analysis mode to check. - /// True if the mode has been blacklisted, false otherwise. - public bool IsBlacklisted(AnalysisMode mode) => _blacklistedStates[(int)mode]; - - /// - /// Sets the blacklisted state for the specified analysis mode. - /// - /// The analysis mode to set. - /// The blacklisted state to set. - public void SetBlacklisted(AnalysisMode mode, bool value) => _blacklistedStates[(int)mode] = value; - - /// - /// Resets the analyzed states. - /// - public void ResetStates() - { - Array.Clear(_analyzedStates); - Array.Clear(_blacklistedStates); - } -} diff --git a/ConfusedPolarBear.Plugin.IntroSkipper/Data/EpisodeVisualization.cs b/ConfusedPolarBear.Plugin.IntroSkipper/Data/EpisodeVisualization.cs deleted file mode 100644 index 0e67101..0000000 --- a/ConfusedPolarBear.Plugin.IntroSkipper/Data/EpisodeVisualization.cs +++ /dev/null @@ -1,24 +0,0 @@ -using System; - -namespace ConfusedPolarBear.Plugin.IntroSkipper.Data; - -/// -/// Episode name and internal ID as returned by the visualization controller. -/// -/// -/// Initializes a new instance of the class. -/// -/// Episode id. -/// Episode name. -public class EpisodeVisualization(Guid id, string name) -{ - /// - /// Gets the id. - /// - public Guid Id { get; private set; } = id; - - /// - /// Gets the name. - /// - public string Name { get; private set; } = name; -} diff --git a/ConfusedPolarBear.Plugin.IntroSkipper/Data/FingerprintException.cs b/ConfusedPolarBear.Plugin.IntroSkipper/Data/FingerprintException.cs deleted file mode 100644 index 5878cf2..0000000 --- a/ConfusedPolarBear.Plugin.IntroSkipper/Data/FingerprintException.cs +++ /dev/null @@ -1,33 +0,0 @@ -using System; - -namespace ConfusedPolarBear.Plugin.IntroSkipper.Data; - -/// -/// Exception raised when an error is encountered analyzing audio. -/// -public class FingerprintException : Exception -{ - /// - /// Initializes a new instance of the class. - /// - public FingerprintException() - { - } - - /// - /// Initializes a new instance of the class. - /// - /// Exception message. - public FingerprintException(string message) : base(message) - { - } - - /// - /// Initializes a new instance of the class. - /// - /// Exception message. - /// Inner exception. - public FingerprintException(string message, Exception inner) : base(message, inner) - { - } -} diff --git a/ConfusedPolarBear.Plugin.IntroSkipper/Data/IgnoreListItem.cs b/ConfusedPolarBear.Plugin.IntroSkipper/Data/IgnoreListItem.cs deleted file mode 100644 index 933bd3d..0000000 --- a/ConfusedPolarBear.Plugin.IntroSkipper/Data/IgnoreListItem.cs +++ /dev/null @@ -1,89 +0,0 @@ -using System; -using System.Runtime.Serialization; - -namespace ConfusedPolarBear.Plugin.IntroSkipper.Data; - -/// -/// Represents an item to ignore. -/// -[DataContract(Namespace = "http://schemas.datacontract.org/2004/07/ConfusedPolarBear.Plugin.IntroSkipper")] -public class IgnoreListItem -{ - /// - /// Initializes a new instance of the class. - /// - public IgnoreListItem() - { - } - - /// - /// Initializes a new instance of the class. - /// - /// The season id. - public IgnoreListItem(Guid seasonId) - { - SeasonId = seasonId; - } - - /// - /// Initializes a new instance of the class. - /// - /// The item to copy. - public IgnoreListItem(IgnoreListItem item) - { - SeasonId = item.SeasonId; - IgnoreIntro = item.IgnoreIntro; - IgnoreCredits = item.IgnoreCredits; - } - - /// - /// Gets or sets the season id. - /// - [DataMember] - public Guid SeasonId { get; set; } = Guid.Empty; - - /// - /// Gets or sets a value indicating whether to ignore the intro. - /// - [DataMember] - public bool IgnoreIntro { get; set; } = false; - - /// - /// Gets or sets a value indicating whether to ignore the credits. - /// - [DataMember] - public bool IgnoreCredits { get; set; } = false; - - /// - /// Toggles the provided mode to the provided value. - /// - /// Analysis mode. - /// Value to set. - public void Toggle(AnalysisMode mode, bool value) - { - switch (mode) - { - case AnalysisMode.Introduction: - IgnoreIntro = value; - break; - case AnalysisMode.Credits: - IgnoreCredits = value; - break; - } - } - - /// - /// Checks if the provided mode is ignored. - /// - /// Analysis mode. - /// True if ignored, false otherwise. - public bool IsIgnored(AnalysisMode mode) - { - return mode switch - { - AnalysisMode.Introduction => IgnoreIntro, - AnalysisMode.Credits => IgnoreCredits, - _ => false, - }; - } -} diff --git a/ConfusedPolarBear.Plugin.IntroSkipper/Data/Intro.cs b/ConfusedPolarBear.Plugin.IntroSkipper/Data/Intro.cs deleted file mode 100644 index 912341f..0000000 --- a/ConfusedPolarBear.Plugin.IntroSkipper/Data/Intro.cs +++ /dev/null @@ -1,50 +0,0 @@ -using System; -using System.Runtime.Serialization; - -namespace ConfusedPolarBear.Plugin.IntroSkipper.Data; - -/// -/// Result of fingerprinting and analyzing two episodes in a season. -/// All times are measured in seconds relative to the beginning of the media file. -/// -/// -/// Initializes a new instance of the class. -/// -/// intro. -[DataContract(Namespace = "http://schemas.datacontract.org/2004/07/ConfusedPolarBear.Plugin.IntroSkipper")] -public class Intro(Segment intro) -{ - /// - /// Gets or sets the Episode ID. - /// - [DataMember] - public Guid EpisodeId { get; set; } = intro.EpisodeId; - - /// - /// Gets a value indicating whether this introduction is valid or not. - /// Invalid results must not be returned through the API. - /// - public bool Valid => IntroEnd > 0; - - /// - /// Gets or sets the introduction sequence start time. - /// - [DataMember] - public double IntroStart { get; set; } = intro.Start; - - /// - /// Gets or sets the introduction sequence end time. - /// - [DataMember] - public double IntroEnd { get; set; } = intro.End; - - /// - /// Gets or sets the recommended time to display the skip intro prompt. - /// - public double ShowSkipPromptAt { get; set; } - - /// - /// Gets or sets the recommended time to hide the skip intro prompt. - /// - public double HideSkipPromptAt { get; set; } -} diff --git a/ConfusedPolarBear.Plugin.IntroSkipper/Data/PluginWarning.cs b/ConfusedPolarBear.Plugin.IntroSkipper/Data/PluginWarning.cs deleted file mode 100644 index 02d68d5..0000000 --- a/ConfusedPolarBear.Plugin.IntroSkipper/Data/PluginWarning.cs +++ /dev/null @@ -1,30 +0,0 @@ -using System; - -namespace ConfusedPolarBear.Plugin.IntroSkipper.Data; - -/// -/// Support bundle warning. -/// -[Flags] -public enum PluginWarning -{ - /// - /// No warnings have been added. - /// - None = 0, - - /// - /// Attempted to add skip button to web interface, but was unable to. - /// - UnableToAddSkipButton = 1, - - /// - /// At least one media file on the server was unable to be fingerprinted by Chromaprint. - /// - InvalidChromaprintFingerprint = 2, - - /// - /// The version of ffmpeg installed on the system is not compatible with the plugin. - /// - IncompatibleFFmpegBuild = 4, -} diff --git a/ConfusedPolarBear.Plugin.IntroSkipper/Data/QueuedEpisode.cs b/ConfusedPolarBear.Plugin.IntroSkipper/Data/QueuedEpisode.cs deleted file mode 100644 index bab97be..0000000 --- a/ConfusedPolarBear.Plugin.IntroSkipper/Data/QueuedEpisode.cs +++ /dev/null @@ -1,64 +0,0 @@ -using System; - -namespace ConfusedPolarBear.Plugin.IntroSkipper.Data; - -/// -/// Episode queued for analysis. -/// -public class QueuedEpisode -{ - /// - /// Gets or sets the series name. - /// - public string SeriesName { get; set; } = string.Empty; - - /// - /// Gets or sets the season number. - /// - public int SeasonNumber { get; set; } - - /// - /// Gets or sets the episode id. - /// - public Guid EpisodeId { get; set; } - - /// - /// Gets or sets the series id. - /// - public Guid SeriesId { get; set; } - - /// - /// Gets the state of the episode. - /// - public EpisodeState State => Plugin.Instance!.GetState(EpisodeId); - - /// - /// Gets or sets the full path to episode. - /// - public string Path { get; set; } = string.Empty; - - /// - /// Gets or sets the name of the episode. - /// - public string Name { get; set; } = string.Empty; - - /// - /// Gets or sets a value indicating whether an episode is Anime. - /// - public bool IsAnime { get; set; } - - /// - /// Gets or sets the timestamp (in seconds) to stop searching for an introduction at. - /// - public int IntroFingerprintEnd { get; set; } - - /// - /// Gets or sets the timestamp (in seconds) to start looking for end credits at. - /// - public int CreditsFingerprintStart { get; set; } - - /// - /// Gets or sets the total duration of this media file (in seconds). - /// - public int Duration { get; set; } -} diff --git a/ConfusedPolarBear.Plugin.IntroSkipper/Data/Segment.cs b/ConfusedPolarBear.Plugin.IntroSkipper/Data/Segment.cs deleted file mode 100644 index 6862862..0000000 --- a/ConfusedPolarBear.Plugin.IntroSkipper/Data/Segment.cs +++ /dev/null @@ -1,114 +0,0 @@ -using System; -using System.Globalization; -using System.Runtime.Serialization; -using System.Text.Json.Serialization; - -namespace ConfusedPolarBear.Plugin.IntroSkipper.Data; - -/// -/// Result of fingerprinting and analyzing two episodes in a season. -/// All times are measured in seconds relative to the beginning of the media file. -/// -[DataContract(Namespace = "http://schemas.datacontract.org/2004/07/ConfusedPolarBear.Plugin.IntroSkipper.Segment")] -public class Segment -{ - /// - /// Initializes a new instance of the class. - /// - /// Episode. - /// Introduction time range. - public Segment(Guid episode, TimeRange segment) - { - EpisodeId = episode; - Start = segment.Start; - End = segment.End; - } - - /// - /// Initializes a new instance of the class. - /// - /// Episode. - public Segment(Guid episode) - { - EpisodeId = episode; - Start = 0; - End = 0; - } - - /// - /// Initializes a new instance of the class. - /// - /// intro. - public Segment(Segment intro) - { - EpisodeId = intro.EpisodeId; - Start = intro.Start; - End = intro.End; - } - - /// - /// Initializes a new instance of the class. - /// - /// intro. - public Segment(Intro intro) - { - EpisodeId = intro.EpisodeId; - Start = intro.IntroStart; - End = intro.IntroEnd; - } - - /// - /// Initializes a new instance of the class. - /// - public Segment() - { - } - - /// - /// Gets or sets the Episode ID. - /// - [DataMember] - public Guid EpisodeId { get; set; } - - /// - /// Gets or sets the introduction sequence start time. - /// - [DataMember] - public double Start { get; set; } - - /// - /// Gets or sets the introduction sequence end time. - /// - [DataMember] - public double End { get; set; } - - /// - /// Gets a value indicating whether this introduction is valid or not. - /// Invalid results must not be returned through the API. - /// - public bool Valid => End > 0; - - /// - /// Gets the duration of this intro. - /// - [JsonIgnore] - public double Duration => End - Start; - - /// - /// Convert this Intro object to a Kodi compatible EDL entry. - /// - /// User specified configuration EDL action. - /// String. - public string ToEdl(EdlAction action) - { - if (action == EdlAction.None) - { - throw new ArgumentException("Cannot serialize an EdlAction of None"); - } - - var start = Math.Round(Start, 2); - var end = Math.Round(End, 2); - - return string.Format(CultureInfo.InvariantCulture, "{0} {1} {2}", start, end, (int)action); - } -} diff --git a/ConfusedPolarBear.Plugin.IntroSkipper/Data/ShowInfos.cs b/ConfusedPolarBear.Plugin.IntroSkipper/Data/ShowInfos.cs deleted file mode 100644 index 99b0fa3..0000000 --- a/ConfusedPolarBear.Plugin.IntroSkipper/Data/ShowInfos.cs +++ /dev/null @@ -1,31 +0,0 @@ -using System; -using System.Collections.Generic; - -namespace ConfusedPolarBear.Plugin.IntroSkipper.Controllers -{ - /// - /// Contains information about a show. - /// - public class ShowInfos - { - /// - /// Gets or sets the Name of the show. - /// - public required string SeriesName { get; set; } - - /// - /// Gets or sets the Year of the show. - /// - public required string ProductionYear { get; set; } - - /// - /// Gets or sets the Library of the show. - /// - public required string LibraryName { get; set; } - - /// - /// Gets the Seasons of the show. - /// - public required Dictionary Seasons { get; init; } - } -} diff --git a/ConfusedPolarBear.Plugin.IntroSkipper/Data/TimeRange.cs b/ConfusedPolarBear.Plugin.IntroSkipper/Data/TimeRange.cs deleted file mode 100644 index 2daae19..0000000 --- a/ConfusedPolarBear.Plugin.IntroSkipper/Data/TimeRange.cs +++ /dev/null @@ -1,83 +0,0 @@ -using System; - -namespace ConfusedPolarBear.Plugin.IntroSkipper.Data; - -#pragma warning disable CA1036 // Override methods on comparable types - -/// -/// Range of contiguous time. -/// -public class TimeRange : IComparable -{ - /// - /// Initializes a new instance of the class. - /// - public TimeRange() - { - Start = 0; - End = 0; - } - - /// - /// Initializes a new instance of the class. - /// - /// Time range start. - /// Time range end. - public TimeRange(double start, double end) - { - Start = start; - End = end; - } - - /// - /// Initializes a new instance of the class. - /// - /// Original TimeRange. - public TimeRange(TimeRange original) - { - Start = original.Start; - End = original.End; - } - - /// - /// Gets or sets the time range start (in seconds). - /// - public double Start { get; set; } - - /// - /// Gets or sets the time range end (in seconds). - /// - public double End { get; set; } - - /// - /// Gets the duration of this time range (in seconds). - /// - public double Duration => End - Start; - - /// - /// Compare TimeRange durations. - /// - /// Object to compare with. - /// int. - public int CompareTo(object? obj) - { - if (obj is not TimeRange tr) - { - throw new ArgumentException("obj must be a TimeRange"); - } - - return tr.Duration.CompareTo(Duration); - } - - /// - /// Tests if this TimeRange object intersects the provided TimeRange. - /// - /// Second TimeRange object to test. - /// true if tr intersects the current TimeRange, false otherwise. - public bool Intersects(TimeRange tr) - { - return - (Start < tr.Start && tr.Start < End) || - (Start < tr.End && tr.End < End); - } -} diff --git a/ConfusedPolarBear.Plugin.IntroSkipper/Data/TimeRangeHelpers.cs b/ConfusedPolarBear.Plugin.IntroSkipper/Data/TimeRangeHelpers.cs deleted file mode 100644 index 5765516..0000000 --- a/ConfusedPolarBear.Plugin.IntroSkipper/Data/TimeRangeHelpers.cs +++ /dev/null @@ -1,50 +0,0 @@ -using System; -using System.Collections.Generic; - -namespace ConfusedPolarBear.Plugin.IntroSkipper.Data; - -/// -/// Time range helpers. -/// -public static class TimeRangeHelpers -{ - /// - /// Finds the longest contiguous time range. - /// - /// Sorted timestamps to search. - /// Maximum distance permitted between contiguous timestamps. - /// The longest contiguous time range (if one was found), or null (if none was found). - public static TimeRange? FindContiguous(double[] times, double maximumDistance) - { - if (times.Length == 0) - { - return null; - } - - Array.Sort(times); - - var ranges = new List(); - var currentRange = new TimeRange(times[0], times[0]); - - // For all provided timestamps, check if it is contiguous with its neighbor. - for (var i = 0; i < times.Length - 1; i++) - { - var current = times[i]; - var next = times[i + 1]; - - if (next - current <= maximumDistance) - { - currentRange.End = next; - continue; - } - - ranges.Add(new TimeRange(currentRange)); - currentRange = new TimeRange(next, next); - } - - // Find and return the longest contiguous range. - ranges.Sort(); - - return (ranges.Count > 0) ? ranges[0] : null; - } -} diff --git a/ConfusedPolarBear.Plugin.IntroSkipper/Data/TimeStamps.cs b/ConfusedPolarBear.Plugin.IntroSkipper/Data/TimeStamps.cs deleted file mode 100644 index 6295578..0000000 --- a/ConfusedPolarBear.Plugin.IntroSkipper/Data/TimeStamps.cs +++ /dev/null @@ -1,19 +0,0 @@ -namespace ConfusedPolarBear.Plugin.IntroSkipper.Data -{ - /// - /// Result of fingerprinting and analyzing two episodes in a season. - /// All times are measured in seconds relative to the beginning of the media file. - /// - public class TimeStamps - { - /// - /// Gets or sets Introduction. - /// - public Segment Introduction { get; set; } = new Segment(); - - /// - /// Gets or sets Credits. - /// - public Segment Credits { get; set; } = new Segment(); - } -} diff --git a/ConfusedPolarBear.Plugin.IntroSkipper/Data/WarningManager.cs b/ConfusedPolarBear.Plugin.IntroSkipper/Data/WarningManager.cs deleted file mode 100644 index 34afbeb..0000000 --- a/ConfusedPolarBear.Plugin.IntroSkipper/Data/WarningManager.cs +++ /dev/null @@ -1,45 +0,0 @@ -namespace ConfusedPolarBear.Plugin.IntroSkipper.Data; - -/// -/// Warning manager. -/// -public static class WarningManager -{ - private static PluginWarning _warnings; - - /// - /// Set warning. - /// - /// Warning. - public static void SetFlag(PluginWarning warning) - { - _warnings |= warning; - } - - /// - /// Clear warnings. - /// - public static void Clear() - { - _warnings = PluginWarning.None; - } - - /// - /// Get warnings. - /// - /// Warnings. - public static string GetWarnings() - { - return _warnings.ToString(); - } - - /// - /// Check if a specific warning flag is set. - /// - /// Warning flag to check. - /// True if the flag is set, otherwise false. - public static bool HasFlag(PluginWarning warning) - { - return (_warnings & warning) == warning; - } -} diff --git a/ConfusedPolarBear.Plugin.IntroSkipper/FFmpegWrapper.cs b/ConfusedPolarBear.Plugin.IntroSkipper/FFmpegWrapper.cs deleted file mode 100644 index 789be1d..0000000 --- a/ConfusedPolarBear.Plugin.IntroSkipper/FFmpegWrapper.cs +++ /dev/null @@ -1,701 +0,0 @@ -using System; -using System.Collections.Concurrent; -using System.Collections.Generic; -using System.Diagnostics; -using System.Globalization; -using System.IO; -using System.Text; -using System.Text.RegularExpressions; -using ConfusedPolarBear.Plugin.IntroSkipper.Data; -using Microsoft.Extensions.Logging; - -namespace ConfusedPolarBear.Plugin.IntroSkipper; - -/// -/// Wrapper for libchromaprint and the silencedetect filter. -/// -public static partial class FFmpegWrapper -{ - /// - /// Used with FFmpeg's silencedetect filter to extract the start and end times of silence. - /// - private static readonly Regex _silenceDetectionExpression = SilenceRegex(); - - /// - /// Used with FFmpeg's blackframe filter to extract the time and percentage of black pixels. - /// - private static readonly Regex _blackFrameRegex = BlackFrameRegex(); - - /// - /// Gets or sets the logger. - /// - public static ILogger? Logger { get; set; } - - private static Dictionary ChromaprintLogs { get; set; } = []; - - private static ConcurrentDictionary<(Guid Id, AnalysisMode Mode), Dictionary> InvertedIndexCache { get; set; } = new(); - - /// - /// Check that the installed version of ffmpeg supports chromaprint. - /// - /// true if a compatible version of ffmpeg is installed, false on any error. - public static bool CheckFFmpegVersion() - { - try - { - // Always log ffmpeg's version information. - if (!CheckFFmpegRequirement( - "-version", - "ffmpeg", - "version", - "Unknown error with FFmpeg version")) - { - ChromaprintLogs["error"] = "unknown_error"; - WarningManager.SetFlag(PluginWarning.IncompatibleFFmpegBuild); - return false; - } - - // First, validate that the installed version of ffmpeg supports chromaprint at all. - if (!CheckFFmpegRequirement( - "-muxers", - "chromaprint", - "muxer list", - "The installed version of ffmpeg does not support chromaprint")) - { - ChromaprintLogs["error"] = "chromaprint_not_supported"; - WarningManager.SetFlag(PluginWarning.IncompatibleFFmpegBuild); - return false; - } - - // Second, validate that the Chromaprint muxer understands the "-fp_format raw" option. - if (!CheckFFmpegRequirement( - "-h muxer=chromaprint", - "binary raw fingerprint", - "chromaprint options", - "The installed version of ffmpeg does not support raw binary fingerprints")) - { - ChromaprintLogs["error"] = "fp_format_not_supported"; - WarningManager.SetFlag(PluginWarning.IncompatibleFFmpegBuild); - return false; - } - - // Third, validate that ffmpeg supports of the all required silencedetect options. - if (!CheckFFmpegRequirement( - "-h filter=silencedetect", - "noise tolerance", - "silencedetect options", - "The installed version of ffmpeg does not support the silencedetect filter")) - { - ChromaprintLogs["error"] = "silencedetect_not_supported"; - WarningManager.SetFlag(PluginWarning.IncompatibleFFmpegBuild); - return false; - } - - Logger?.LogDebug("Installed version of ffmpeg meets fingerprinting requirements"); - ChromaprintLogs["error"] = "okay"; - return true; - } - catch - { - ChromaprintLogs["error"] = "unknown_error"; - WarningManager.SetFlag(PluginWarning.IncompatibleFFmpegBuild); - return false; - } - } - - /// - /// Fingerprint a queued episode. - /// - /// Queued episode to fingerprint. - /// Portion of media file to fingerprint. Introduction = first 25% / 10 minutes and Credits = last 4 minutes. - /// Numerical fingerprint points. - public static uint[] Fingerprint(QueuedEpisode episode, AnalysisMode mode) - { - int start, end; - - if (mode == AnalysisMode.Introduction) - { - start = 0; - end = episode.IntroFingerprintEnd; - } - else if (mode == AnalysisMode.Credits) - { - start = episode.CreditsFingerprintStart; - end = episode.Duration; - } - else - { - throw new ArgumentException("Unknown analysis mode " + mode); - } - - return Fingerprint(episode, mode, start, end); - } - - /// - /// Transforms a Chromaprint into an inverted index of fingerprint points to the last index it appeared at. - /// - /// Episode ID. - /// Chromaprint fingerprint. - /// Mode. - /// Inverted index. - public static Dictionary CreateInvertedIndex(Guid id, uint[] fingerprint, AnalysisMode mode) - { - if (InvertedIndexCache.TryGetValue((id, mode), out var cached)) - { - return cached; - } - - var invIndex = new Dictionary(); - - for (int i = 0; i < fingerprint.Length; i++) - { - // Get the current point. - var point = fingerprint[i]; - - // Append the current sample's timecode to the collection for this point. - invIndex[point] = i; - } - - InvertedIndexCache[(id, mode)] = invIndex; - - return invIndex; - } - - /// - /// Detect ranges of silence in the provided episode. - /// - /// Queued episode. - /// Time range to search. - /// Array of TimeRange objects that are silent in the queued episode. - public static TimeRange[] DetectSilence(QueuedEpisode episode, TimeRange range) - { - Logger?.LogTrace( - "Detecting silence in \"{File}\" (range {Start}-{End}, id {Id})", - episode.Path, - range.Start, - range.End, - episode.EpisodeId); - - // -vn, -sn, -dn: ignore video, subtitle, and data tracks - var args = string.Format( - CultureInfo.InvariantCulture, - "-vn -sn -dn " + - "-ss {0} -i \"{1}\" -to {2} -af \"silencedetect=noise={3}dB:duration=0.1\" -f null -", - range.Start, - episode.Path, - range.End - range.Start, - Plugin.Instance?.Configuration.SilenceDetectionMaximumNoise ?? -50); - - // Cache the output of this command to "GUID-intro-silence-v2" - var cacheKey = string.Format( - CultureInfo.InvariantCulture, - "{0}-silence-{1}-{2}-v2", - episode.EpisodeId.ToString("N"), - range.Start, - range.End); - - var currentRange = new TimeRange(); - var silenceRanges = new List(); - - /* Each match will have a type (either "start" or "end") and a timecode (a double). - * - * Sample output: - * [silencedetect @ 0x000000000000] silence_start: 12.34 - * [silencedetect @ 0x000000000000] silence_end: 56.123 | silence_duration: 43.783 - */ - var raw = Encoding.UTF8.GetString(GetOutput(args, cacheKey, true)); - foreach (Match match in _silenceDetectionExpression.Matches(raw)) - { - var isStart = match.Groups["type"].Value == "start"; - var time = Convert.ToDouble(match.Groups["time"].Value, CultureInfo.InvariantCulture); - - if (isStart) - { - currentRange.Start = time + range.Start; - } - else - { - currentRange.End = time + range.Start; - silenceRanges.Add(new TimeRange(currentRange)); - } - } - - return silenceRanges.ToArray(); - } - - /// - /// Finds the location of all black frames in a media file within a time range. - /// - /// Media file to analyze. - /// Time range to search. - /// Percentage of the frame that must be black. - /// Array of frames that are mostly black. - public static BlackFrame[] DetectBlackFrames( - QueuedEpisode episode, - TimeRange range, - int minimum) - { - // Seek to the start of the time range and find frames that are at least 50% black. - var args = string.Format( - CultureInfo.InvariantCulture, - "-ss {0} -i \"{1}\" -to {2} -an -dn -sn -vf \"blackframe=amount=50\" -f null -", - range.Start, - episode.Path, - range.End - range.Start); - - // Cache the results to GUID-blackframes-START-END-v1. - var cacheKey = string.Format( - CultureInfo.InvariantCulture, - "{0}-blackframes-{1}-{2}-v1", - episode.EpisodeId.ToString("N"), - range.Start, - range.End); - - var blackFrames = new List(); - - /* Run the blackframe filter. - * - * Sample output: - * [Parsed_blackframe_0 @ 0x0000000] frame:1 pblack:99 pts:43 t:0.043000 type:B last_keyframe:0 - * [Parsed_blackframe_0 @ 0x0000000] frame:2 pblack:99 pts:85 t:0.085000 type:B last_keyframe:0 - */ - var raw = Encoding.UTF8.GetString(GetOutput(args, cacheKey, true)); - foreach (var line in raw.Split('\n')) - { - // There is no FFmpeg flag to hide metadata such as description - // In our case, the metadata contained something that matched the regex. - if (line.StartsWith("[Parsed_blackframe_", StringComparison.OrdinalIgnoreCase)) - { - var matches = _blackFrameRegex.Matches(line); - if (matches.Count != 2) - { - continue; - } - - var (strPercent, strTime) = ( - matches[0].Value.Split(':')[1], - matches[1].Value.Split(':')[1] - ); - - var bf = new BlackFrame( - Convert.ToInt32(strPercent, CultureInfo.InvariantCulture), - Convert.ToDouble(strTime, CultureInfo.InvariantCulture)); - - if (bf.Percentage > minimum) - { - blackFrames.Add(bf); - } - } - } - - return blackFrames.ToArray(); - } - - /// - /// Gets Chromaprint debugging logs. - /// - /// Markdown formatted logs. - public static string GetChromaprintLogs() - { - // Print the FFmpeg detection status at the top. - // Format: "* FFmpeg: `error`" - // Append two newlines to separate the bulleted list from the logs - var logs = string.Format( - CultureInfo.InvariantCulture, - "* FFmpeg: `{0}`\n\n", - ChromaprintLogs["error"]); - - // Always include ffmpeg version information - logs += FormatFFmpegLog("version"); - - // Don't print feature detection logs if the plugin started up okay - if (ChromaprintLogs["error"] == "okay") - { - return logs; - } - - // Print all remaining logs - foreach (var kvp in ChromaprintLogs) - { - if (kvp.Key == "error" || kvp.Key == "version") - { - continue; - } - - logs += FormatFFmpegLog(kvp.Key); - } - - return logs; - } - - /// - /// Run an FFmpeg command with the provided arguments and validate that the output contains - /// the provided string. - /// - /// Arguments to pass to FFmpeg. - /// String that the output must contain. Case insensitive. - /// Support bundle key to store FFmpeg's output under. - /// Error message to log if this requirement is not met. - /// true on success, false on error. - private static bool CheckFFmpegRequirement( - string arguments, - string mustContain, - string bundleName, - string errorMessage) - { - Logger?.LogDebug("Checking FFmpeg requirement {Arguments}", arguments); - - var output = Encoding.UTF8.GetString(GetOutput(arguments, string.Empty, false, 2000)); - Logger?.LogTrace("Output of ffmpeg {Arguments}: {Output}", arguments, output); - ChromaprintLogs[bundleName] = output; - - if (!output.Contains(mustContain, StringComparison.OrdinalIgnoreCase)) - { - Logger?.LogError("{ErrorMessage}", errorMessage); - return false; - } - - Logger?.LogDebug("FFmpeg requirement {Arguments} met", arguments); - - return true; - } - - /// - /// Runs ffmpeg and returns standard output (or error). - /// If caching is enabled, will use cacheFilename to cache the output of this command. - /// - /// Arguments to pass to ffmpeg. - /// Filename to cache the output of this command to, or string.Empty if this command should not be cached. - /// If standard error should be returned. - /// Timeout (in miliseconds) to wait for ffmpeg to exit. - private static ReadOnlySpan GetOutput( - string args, - string cacheFilename, - bool stderr = false, - int timeout = 60 * 1000) - { - var ffmpegPath = Plugin.Instance?.FFmpegPath ?? "ffmpeg"; - - // The silencedetect and blackframe filters output data at the info log level. - var useInfoLevel = args.Contains("silencedetect", StringComparison.OrdinalIgnoreCase) || - args.Contains("blackframe", StringComparison.OrdinalIgnoreCase); - - var logLevel = useInfoLevel ? "info" : "warning"; - - var cacheOutput = - (Plugin.Instance?.Configuration.CacheFingerprints ?? false) && - !string.IsNullOrEmpty(cacheFilename); - - // If caching is enabled, try to load the output of this command from the cached file. - if (cacheOutput) - { - // Calculate the absolute path to the cached file. - cacheFilename = Path.Join(Plugin.Instance!.FingerprintCachePath, cacheFilename); - - // If the cached file exists, return whatever it holds. - if (File.Exists(cacheFilename)) - { - Logger?.LogTrace("Returning contents of cache {Cache}", cacheFilename); - return File.ReadAllBytes(cacheFilename); - } - - Logger?.LogTrace("Not returning contents of cache {Cache} (not found)", cacheFilename); - } - - // Prepend some flags to prevent FFmpeg from logging it's banner and progress information - // for each file that is fingerprinted. - var prependArgument = string.Format( - CultureInfo.InvariantCulture, - "-hide_banner -loglevel {0} -threads {1} ", - logLevel, - Plugin.Instance?.Configuration.ProcessThreads ?? 0); - - var info = new ProcessStartInfo(ffmpegPath, args.Insert(0, prependArgument)) - { - WindowStyle = ProcessWindowStyle.Hidden, - CreateNoWindow = true, - UseShellExecute = false, - ErrorDialog = false, - - RedirectStandardOutput = !stderr, - RedirectStandardError = stderr - }; - - using var ffmpeg = new Process { StartInfo = info }; - Logger?.LogDebug("Starting ffmpeg with the following arguments: {Arguments}", ffmpeg.StartInfo.Arguments); - - ffmpeg.Start(); - - try - { - ffmpeg.PriorityClass = Plugin.Instance?.Configuration.ProcessPriority ?? ProcessPriorityClass.BelowNormal; - } - catch (Exception e) - { - Logger?.LogDebug("ffmpeg priority could not be modified. {Message}", e.Message); - } - - using var ms = new MemoryStream(); - var buf = new byte[4096]; - int bytesRead; - - using (var streamReader = stderr ? ffmpeg.StandardError : ffmpeg.StandardOutput) - { - while ((bytesRead = streamReader.BaseStream.Read(buf, 0, buf.Length)) > 0) - { - ms.Write(buf, 0, bytesRead); - } - } - - ffmpeg.WaitForExit(timeout); - - var output = ms.ToArray(); - - // If caching is enabled, cache the output of this command. - if (cacheOutput) - { - File.WriteAllBytes(cacheFilename, output); - } - - return output; - } - - /// - /// Fingerprint a queued episode. - /// - /// Queued episode to fingerprint. - /// Portion of media file to fingerprint. - /// Time (in seconds) relative to the start of the file to start fingerprinting from. - /// Time (in seconds) relative to the start of the file to stop fingerprinting at. - /// Numerical fingerprint points. - private static uint[] Fingerprint(QueuedEpisode episode, AnalysisMode mode, int start, int end) - { - // Try to load this episode from cache before running ffmpeg. - if (LoadCachedFingerprint(episode, mode, out uint[] cachedFingerprint)) - { - Logger?.LogTrace("Fingerprint cache hit on {File}", episode.Path); - return cachedFingerprint; - } - - Logger?.LogDebug( - "Fingerprinting [{Start}, {End}] from \"{File}\" (id {Id})", - start, - end, - episode.Path, - episode.EpisodeId); - - var args = string.Format( - CultureInfo.InvariantCulture, - "-ss {0} -i \"{1}\" -to {2} -ac 2 -f chromaprint -fp_format raw -", - start, - episode.Path, - end - start); - - // Returns all fingerprint points as raw 32 bit unsigned integers (little endian). - var rawPoints = GetOutput(args, string.Empty); - if (rawPoints.Length == 0 || rawPoints.Length % 4 != 0) - { - Logger?.LogWarning("Chromaprint returned {Count} points for \"{Path}\"", rawPoints.Length, episode.Path); - throw new FingerprintException("chromaprint output for \"" + episode.Path + "\" was malformed"); - } - - var results = new List(); - for (var i = 0; i < rawPoints.Length; i += 4) - { - var rawPoint = rawPoints.Slice(i, 4); - results.Add(BitConverter.ToUInt32(rawPoint)); - } - - // Try to cache this fingerprint. - CacheFingerprint(episode, mode, results); - - return results.ToArray(); - } - - /// - /// Tries to load an episode's fingerprint from cache. If caching is not enabled, calling this function is a no-op. - /// This function was created before the unified caching mechanism was introduced (in v0.1.7). - /// - /// Episode to try to load from cache. - /// Analysis mode. - /// Array to store the fingerprint in. - /// true if the episode was successfully loaded from cache, false on any other error. - private static bool LoadCachedFingerprint( - QueuedEpisode episode, - AnalysisMode mode, - out uint[] fingerprint) - { - fingerprint = Array.Empty(); - - // If fingerprint caching isn't enabled, don't try to load anything. - if (!(Plugin.Instance?.Configuration.CacheFingerprints ?? false)) - { - return false; - } - - var path = GetFingerprintCachePath(episode, mode); - - // If this episode isn't cached, bail out. - if (!File.Exists(path)) - { - return false; - } - - var raw = File.ReadAllLines(path, Encoding.UTF8); - var result = new List(); - - // Read each stringified uint. - result.EnsureCapacity(raw.Length); - - try - { - foreach (var rawNumber in raw) - { - result.Add(Convert.ToUInt32(rawNumber, CultureInfo.InvariantCulture)); - } - } - catch (FormatException) - { - // Occurs when the cached fingerprint is corrupt. - Logger?.LogDebug( - "Cached fingerprint for {Path} ({Id}) is corrupt, ignoring cache", - episode.Path, - episode.EpisodeId); - - return false; - } - - fingerprint = result.ToArray(); - return true; - } - - /// - /// Cache an episode's fingerprint to disk. If caching is not enabled, calling this function is a no-op. - /// This function was created before the unified caching mechanism was introduced (in v0.1.7). - /// - /// Episode to store in cache. - /// Analysis mode. - /// Fingerprint of the episode to store. - private static void CacheFingerprint( - QueuedEpisode episode, - AnalysisMode mode, - List fingerprint) - { - // Bail out if caching isn't enabled. - if (!(Plugin.Instance?.Configuration.CacheFingerprints ?? false)) - { - return; - } - - // Stringify each data point. - var lines = new List(); - foreach (var number in fingerprint) - { - lines.Add(number.ToString(CultureInfo.InvariantCulture)); - } - - // Cache the episode. - File.WriteAllLinesAsync( - GetFingerprintCachePath(episode, mode), - lines, - Encoding.UTF8).ConfigureAwait(false); - } - - /// - /// Remove a cached episode fingerprint from disk. - /// - /// Episode to remove from cache. - public static void DeleteEpisodeCache(Guid id) - { - var cachePath = Path.Join( - Plugin.Instance!.FingerprintCachePath, - id.ToString("N")); - - // File.Delete(cachePath); - // File.Delete(cachePath + "-intro-silence-v1"); - // File.Delete(cachePath + "-credits"); - - var filePattern = Path.GetFileName(cachePath) + "*"; - foreach (var filePath in Directory.EnumerateFiles(Plugin.Instance!.FingerprintCachePath, filePattern)) - { - Logger?.LogDebug("DeleteEpisodeCache {FilePath}", filePath); - File.Delete(filePath); - } - } - - /// - /// Remove cached fingerprints from disk by mode. - /// - /// Analysis mode. - public static void DeleteCacheFiles(AnalysisMode mode) - { - foreach (var filePath in Directory.EnumerateFiles(Plugin.Instance!.FingerprintCachePath)) - { - var shouldDelete = (mode == AnalysisMode.Introduction) - ? !filePath.Contains("credit", StringComparison.OrdinalIgnoreCase) - && !filePath.Contains("blackframes", StringComparison.OrdinalIgnoreCase) - : filePath.Contains("credit", StringComparison.OrdinalIgnoreCase) - || filePath.Contains("blackframes", StringComparison.OrdinalIgnoreCase); - - if (shouldDelete) - { - File.Delete(filePath); - } - } - } - - /// - /// Determines the path an episode should be cached at. - /// This function was created before the unified caching mechanism was introduced (in v0.1.7). - /// - /// Episode. - /// Analysis mode. - /// Path. - public static string GetFingerprintCachePath(QueuedEpisode episode, AnalysisMode mode) - { - var basePath = Path.Join( - Plugin.Instance!.FingerprintCachePath, - episode.EpisodeId.ToString("N")); - - if (mode == AnalysisMode.Introduction) - { - return basePath; - } - - if (mode == AnalysisMode.Credits) - { - return basePath + "-credits"; - } - - throw new ArgumentException("Unknown analysis mode " + mode); - } - - private static string FormatFFmpegLog(string key) - { - /* Format: - * FFmpeg NAME: - * ``` - * LOGS - * ``` - */ - - var formatted = string.Format(CultureInfo.InvariantCulture, "FFmpeg {0}:\n```\n", key); - formatted += ChromaprintLogs[key]; - - // Ensure the closing triple backtick is on a separate line - if (!formatted.EndsWith('\n')) - { - formatted += "\n"; - } - - formatted += "```\n\n"; - - return formatted; - } - - [GeneratedRegex("silence_(?start|end): (?