-
-
Notifications
You must be signed in to change notification settings - Fork 13
Add attachment extraction #52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f1d46bd
Use server-native extraction
Shadowghost c369051
Add attachment extraction
Shadowghost 229c10e
Apply suggestions from code review
Shadowghost 9350aa6
Merge branch 'unstable' into extract-attachments
crobibero 11e75d7
Update AttachmentExtractionProvider.cs
crobibero File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
Jellyfin.Plugin.SubtitleExtract/Providers/AttachmentExtractionProvider.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| using System; | ||
| using System.Linq; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using MediaBrowser.Controller.Entities; | ||
| using MediaBrowser.Controller.Entities.Movies; | ||
| using MediaBrowser.Controller.Entities.TV; | ||
| using MediaBrowser.Controller.Library; | ||
| using MediaBrowser.Controller.MediaEncoding; | ||
| using MediaBrowser.Controller.Providers; | ||
| using MediaBrowser.Model.Entities; | ||
| using Microsoft.Extensions.Logging; | ||
|
|
||
| namespace Jellyfin.Plugin.SubtitleExtract.Providers; | ||
|
|
||
| /// <summary> | ||
| /// Extracts embedded attachments while library scanning for immediate access in web player. | ||
| /// </summary> | ||
| public class AttachmentExtractionProvider : ICustomMetadataProvider<Episode>, | ||
| ICustomMetadataProvider<Movie>, | ||
| ICustomMetadataProvider<Video>, | ||
| IHasItemChangeMonitor, | ||
| IHasOrder, | ||
| IForcedProvider | ||
| { | ||
| private readonly ILogger<SubtitleExtractionProvider> _logger; | ||
|
|
||
| private readonly IAttachmentExtractor _extractor; | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="AttachmentExtractionProvider"/> class. | ||
| /// </summary> | ||
| /// <param name="attachmentExtractor"><see cref="IAttachmentExtractor"/> instance.</param> | ||
| /// <param name="logger">Instance of the <see cref="ILogger"/> interface.</param> | ||
| public AttachmentExtractionProvider( | ||
| IAttachmentExtractor attachmentExtractor, | ||
| ILogger<SubtitleExtractionProvider> logger) | ||
| { | ||
| _logger = logger; | ||
| _extractor = attachmentExtractor; | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public string Name => "AttachmentExtractionProvider"; | ||
|
|
||
| /// <summary> | ||
| /// Gets the order in which the provider should be called. (Core provider is = 100). | ||
| /// </summary> | ||
| public int Order => 1000; | ||
|
|
||
| /// <inheritdoc/> | ||
| public bool HasChanged(BaseItem item, IDirectoryService directoryService) | ||
| { | ||
| if (item.IsFileProtocol) | ||
| { | ||
| var file = directoryService.GetFile(item.Path); | ||
| if (file != null && (item.DateModified != file.LastWriteTimeUtc || item.Size != file.Length)) | ||
| { | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| public Task<ItemUpdateType> FetchAsync(Episode item, MetadataRefreshOptions options, CancellationToken cancellationToken) | ||
| { | ||
| return FetchSubtitles(item, cancellationToken); | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| public Task<ItemUpdateType> FetchAsync(Movie item, MetadataRefreshOptions options, CancellationToken cancellationToken) | ||
| { | ||
| return FetchSubtitles(item, cancellationToken); | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| public Task<ItemUpdateType> FetchAsync(Video item, MetadataRefreshOptions options, CancellationToken cancellationToken) | ||
| { | ||
| return FetchSubtitles(item, cancellationToken); | ||
| } | ||
|
|
||
| private async Task<ItemUpdateType> FetchSubtitles(BaseItem item, CancellationToken cancellationToken) | ||
| { | ||
| var config = SubtitleExtractPlugin.Current!.Configuration; | ||
|
|
||
| if (config.ExtractionDuringLibraryScan) | ||
| { | ||
| _logger.LogDebug("Extracting subtitles for: {Video}", item.Path); | ||
Shadowghost marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| foreach (var mediaSource in item.GetMediaSources(false)) | ||
| { | ||
| var streams = mediaSource.MediaStreams.Where(i => i.Type == MediaStreamType.Subtitle).ToList(); | ||
| var mksStreams = streams.Where(i => !string.IsNullOrEmpty(i.Path) && i.Path.EndsWith(".mks", StringComparison.OrdinalIgnoreCase)).ToList(); | ||
| var mksPaths = mksStreams.Select(i => i.Path).ToList(); | ||
| if (mksPaths.Count > 0) | ||
| { | ||
| foreach (var path in mksPaths) | ||
| { | ||
| await _extractor.ExtractAllAttachments(path, mediaSource, cancellationToken).ConfigureAwait(false); | ||
| } | ||
| } | ||
|
|
||
| if (streams.Count != mksStreams.Count) | ||
| { | ||
| await _extractor.ExtractAllAttachments(mediaSource.Path, mediaSource, cancellationToken).ConfigureAwait(false); | ||
| } | ||
| } | ||
|
|
||
| _logger.LogDebug("Finished subtitle extraction for: {Video}", item.Path); | ||
Shadowghost marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| return ItemUpdateType.None; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 0 additions & 18 deletions
18
Jellyfin.Plugin.SubtitleExtract/SubtitleExtractPluginServiceRegistrator.cs
This file was deleted.
Oops, something went wrong.
124 changes: 124 additions & 0 deletions
124
Jellyfin.Plugin.SubtitleExtract/Tasks/ExtractAttachmentsTask.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Jellyfin.Data.Enums; | ||
| using MediaBrowser.Controller.Dto; | ||
| using MediaBrowser.Controller.Entities; | ||
| using MediaBrowser.Controller.Library; | ||
| using MediaBrowser.Controller.MediaEncoding; | ||
| using MediaBrowser.Model.Entities; | ||
| using MediaBrowser.Model.Globalization; | ||
| using MediaBrowser.Model.Tasks; | ||
|
|
||
| namespace Jellyfin.Plugin.SubtitleExtract.Tasks; | ||
|
|
||
| /// <summary> | ||
| /// Scheduled task to extract embedded attachments for immediate access in web player. | ||
| /// </summary> | ||
| public class ExtractAttachmentsTask : IScheduledTask | ||
| { | ||
| private const int QueryPageLimit = 250; | ||
|
|
||
| private readonly ILibraryManager _libraryManager; | ||
| private readonly ILocalizationManager _localization; | ||
| private readonly IAttachmentExtractor _extractor; | ||
|
|
||
| private static readonly BaseItemKind[] _itemTypes = [BaseItemKind.Episode, BaseItemKind.Movie]; | ||
| private static readonly MediaType[] _mediaTypes = [MediaType.Video]; | ||
| private static readonly SourceType[] _sourceTypes = [SourceType.Library]; | ||
| private static readonly DtoOptions _dtoOptions = new(false); | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="ExtractAttachmentsTask" /> class. | ||
| /// </summary> | ||
| /// <param name="libraryManager">Instance of <see cref="ILibraryManager"/> interface.</param> | ||
| /// <param name="attachmentExtractor"><see cref="IAttachmentExtractor"/> instance.</param> | ||
| /// <param name="localization">Instance of <see cref="ILocalizationManager"/> interface.</param> | ||
| public ExtractAttachmentsTask( | ||
| ILibraryManager libraryManager, | ||
| IAttachmentExtractor attachmentExtractor, | ||
| ILocalizationManager localization) | ||
| { | ||
| _libraryManager = libraryManager; | ||
| _localization = localization; | ||
| _extractor = attachmentExtractor; | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public string Key => "ExtractAttachments"; | ||
|
|
||
| /// <inheritdoc /> | ||
| public string Name => "Extract Attachments"; | ||
|
|
||
| /// <inheritdoc /> | ||
| public string Description => "Extracts embedded attachments."; | ||
|
|
||
| /// <inheritdoc /> | ||
| public string Category => _localization.GetLocalizedString("TasksLibraryCategory"); | ||
|
|
||
| /// <inheritdoc /> | ||
| public IEnumerable<TaskTriggerInfo> GetDefaultTriggers() | ||
| { | ||
| return []; | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public async Task ExecuteAsync(IProgress<double> progress, CancellationToken cancellationToken) | ||
| { | ||
| var query = new InternalItemsQuery | ||
| { | ||
| Recursive = true, | ||
| HasSubtitles = true, | ||
| IsVirtualItem = false, | ||
| IncludeItemTypes = _itemTypes, | ||
| DtoOptions = _dtoOptions, | ||
| MediaTypes = _mediaTypes, | ||
| SourceTypes = _sourceTypes, | ||
| Limit = QueryPageLimit, | ||
| }; | ||
|
|
||
| var numberOfVideos = _libraryManager.GetCount(query); | ||
|
|
||
| var startIndex = 0; | ||
| var completedVideos = 0; | ||
|
|
||
| while (startIndex < numberOfVideos) | ||
| { | ||
| query.StartIndex = startIndex; | ||
| var videos = _libraryManager.GetItemList(query); | ||
|
|
||
| foreach (var video in videos) | ||
| { | ||
| cancellationToken.ThrowIfCancellationRequested(); | ||
|
|
||
| foreach (var mediaSource in video.GetMediaSources(false)) | ||
| { | ||
| var streams = mediaSource.MediaStreams.Where(i => i.Type == MediaStreamType.Subtitle).ToList(); | ||
| var mksStreams = streams.Where(i => !string.IsNullOrEmpty(i.Path) && i.Path.EndsWith(".mks", StringComparison.OrdinalIgnoreCase)).ToList(); | ||
| var mksPaths = mksStreams.Select(i => i.Path).ToList(); | ||
| if (mksPaths.Count > 0) | ||
| { | ||
| foreach (var path in mksPaths) | ||
| { | ||
| await _extractor.ExtractAllAttachments(path, mediaSource, cancellationToken).ConfigureAwait(false); | ||
| } | ||
| } | ||
|
|
||
| if (streams.Count != mksStreams.Count) | ||
| { | ||
| await _extractor.ExtractAllAttachments(mediaSource.Path, mediaSource, cancellationToken).ConfigureAwait(false); | ||
| } | ||
| } | ||
|
|
||
| completedVideos++; | ||
| progress.Report(100d * completedVideos / numberOfVideos); | ||
| } | ||
|
|
||
| startIndex += QueryPageLimit; | ||
| } | ||
|
|
||
| progress.Report(100); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.