jellyfin/MediaBrowser.Providers/Subtitles/SubtitleManager.cs

415 lines
14 KiB
C#
Raw Normal View History

#pragma warning disable CS1591
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
2021-12-20 12:31:07 +00:00
using Jellyfin.Extensions;
using MediaBrowser.Common.Extensions;
2014-05-07 02:28:19 +00:00
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Movies;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Library;
2014-05-17 04:24:10 +00:00
using MediaBrowser.Controller.Persistence;
2014-05-11 22:38:10 +00:00
using MediaBrowser.Controller.Providers;
2014-05-07 02:28:19 +00:00
using MediaBrowser.Controller.Subtitles;
using MediaBrowser.Model.Configuration;
2014-05-07 02:28:19 +00:00
using MediaBrowser.Model.Entities;
2018-09-12 17:26:21 +00:00
using MediaBrowser.Model.Globalization;
using MediaBrowser.Model.IO;
using MediaBrowser.Model.Providers;
using Microsoft.Extensions.Logging;
2014-05-07 02:28:19 +00:00
namespace MediaBrowser.Providers.Subtitles
{
public class SubtitleManager : ISubtitleManager
{
2020-06-06 00:15:56 +00:00
private readonly ILogger<SubtitleManager> _logger;
2014-05-07 02:28:19 +00:00
private readonly IFileSystem _fileSystem;
private readonly ILibraryMonitor _monitor;
private readonly IMediaSourceManager _mediaSourceManager;
2020-04-04 19:12:02 +00:00
private readonly ILocalizationManager _localization;
2014-05-07 02:28:19 +00:00
private readonly ISubtitleProvider[] _subtitleProviders;
2018-09-12 17:26:21 +00:00
public SubtitleManager(
2020-04-04 19:12:02 +00:00
ILogger<SubtitleManager> logger,
IFileSystem fileSystem,
ILibraryMonitor monitor,
IMediaSourceManager mediaSourceManager,
ILocalizationManager localizationManager,
IEnumerable<ISubtitleProvider> subtitleProviders)
2014-05-07 02:28:19 +00:00
{
2020-04-04 19:12:02 +00:00
_logger = logger;
2014-05-07 02:28:19 +00:00
_fileSystem = fileSystem;
_monitor = monitor;
_mediaSourceManager = mediaSourceManager;
2018-09-12 17:26:21 +00:00
_localization = localizationManager;
_subtitleProviders = subtitleProviders
2019-10-11 16:16:42 +00:00
.OrderBy(i => i is IHasOrder hasOrder ? hasOrder.Order : 0)
2018-09-12 17:26:21 +00:00
.ToArray();
2014-05-07 02:28:19 +00:00
}
/// <inheritdoc />
2023-02-23 18:09:16 +00:00
public event EventHandler<SubtitleDownloadFailureEventArgs>? SubtitleDownloadFailure;
2019-09-10 20:37:53 +00:00
/// <inheritdoc />
2017-08-19 19:43:35 +00:00
public async Task<RemoteSubtitleInfo[]> SearchSubtitles(SubtitleSearchRequest request, CancellationToken cancellationToken)
2014-05-07 02:28:19 +00:00
{
2022-12-05 14:01:13 +00:00
if (request.Language is not null)
2018-09-12 17:26:21 +00:00
{
var culture = _localization.FindLanguageInfo(request.Language);
2022-12-05 14:01:13 +00:00
if (culture is not null)
2018-09-12 17:26:21 +00:00
{
request.TwoLetterISOLanguageName = culture.TwoLetterISOLanguageName;
}
}
2014-05-17 04:24:10 +00:00
var contentType = request.ContentType;
2014-05-07 02:28:19 +00:00
var providers = _subtitleProviders
2021-12-20 12:31:07 +00:00
.Where(i => i.SupportedMediaTypes.Contains(contentType) && !request.DisabledSubtitleFetchers.Contains(i.Name, StringComparison.OrdinalIgnoreCase))
2018-09-12 17:26:21 +00:00
.OrderBy(i =>
{
var index = request.SubtitleFetcherOrder.IndexOf(i.Name);
2018-09-12 17:26:21 +00:00
return index == -1 ? int.MaxValue : index;
})
2017-08-19 19:43:35 +00:00
.ToArray();
2014-05-07 02:28:19 +00:00
2014-05-17 04:24:10 +00:00
// If not searching all, search one at a time until something is found
if (!request.SearchAllProviders)
{
foreach (var provider in providers)
{
try
{
var searchResults = await provider.Search(request, cancellationToken).ConfigureAwait(false);
2017-08-19 19:43:35 +00:00
var list = searchResults.ToArray();
2014-05-17 04:24:10 +00:00
2017-08-19 19:43:35 +00:00
if (list.Length > 0)
2014-05-17 04:24:10 +00:00
{
Normalize(list);
return list;
}
}
catch (Exception ex)
{
2018-12-20 12:11:26 +00:00
_logger.LogError(ex, "Error downloading subtitles from {Provider}", provider.Name);
2014-05-17 04:24:10 +00:00
}
}
2020-06-15 21:43:52 +00:00
2019-09-10 20:37:53 +00:00
return Array.Empty<RemoteSubtitleInfo>();
2014-05-17 04:24:10 +00:00
}
2014-05-07 02:28:19 +00:00
var tasks = providers.Select(async i =>
{
try
{
2014-05-17 04:24:10 +00:00
var searchResults = await i.Search(request, cancellationToken).ConfigureAwait(false);
2017-08-19 19:43:35 +00:00
var list = searchResults.ToArray();
2014-05-17 04:24:10 +00:00
Normalize(list);
return list;
2014-05-07 02:28:19 +00:00
}
catch (Exception ex)
{
_logger.LogError(ex, "Error downloading subtitles from {Name}", i.Name);
2019-09-10 20:37:53 +00:00
return Array.Empty<RemoteSubtitleInfo>();
2014-05-07 02:28:19 +00:00
}
});
var results = await Task.WhenAll(tasks).ConfigureAwait(false);
2017-08-19 19:43:35 +00:00
return results.SelectMany(i => i).ToArray();
2014-05-07 02:28:19 +00:00
}
2019-09-10 20:37:53 +00:00
/// <inheritdoc />
2018-09-12 17:26:21 +00:00
public Task DownloadSubtitles(Video video, string subtitleId, CancellationToken cancellationToken)
{
var libraryOptions = BaseItem.LibraryManager.GetLibraryOptions(video);
return DownloadSubtitles(video, libraryOptions, subtitleId, cancellationToken);
}
2019-09-10 20:37:53 +00:00
/// <inheritdoc />
public async Task DownloadSubtitles(
Video video,
2018-09-12 17:26:21 +00:00
LibraryOptions libraryOptions,
2014-05-07 02:28:19 +00:00
string subtitleId,
CancellationToken cancellationToken)
{
var parts = subtitleId.Split('_', 2);
2020-08-07 15:38:01 +00:00
var provider = GetProvider(parts[0]);
2014-05-07 02:28:19 +00:00
2014-08-10 22:13:17 +00:00
try
2014-05-07 02:28:19 +00:00
{
2014-08-10 22:13:17 +00:00
var response = await GetRemoteSubtitles(subtitleId, cancellationToken).ConfigureAwait(false);
2014-05-17 04:24:10 +00:00
2020-11-08 10:20:28 +00:00
await TrySaveSubtitle(video, libraryOptions, response).ConfigureAwait(false);
2014-08-10 22:13:17 +00:00
}
catch (RateLimitExceededException)
{
throw;
}
2014-08-10 22:13:17 +00:00
catch (Exception ex)
{
SubtitleDownloadFailure?.Invoke(this, new SubtitleDownloadFailureEventArgs
2014-05-07 02:28:19 +00:00
{
2014-08-10 22:13:17 +00:00
Item = video,
Exception = ex,
Provider = provider.Name
});
2017-05-27 07:19:09 +00:00
2014-08-10 22:13:17 +00:00
throw;
2014-05-07 02:28:19 +00:00
}
}
2020-05-08 19:53:38 +00:00
/// <inheritdoc />
2020-11-08 10:20:28 +00:00
public Task UploadSubtitle(Video video, SubtitleResponse response)
2020-05-08 19:53:38 +00:00
{
var libraryOptions = BaseItem.LibraryManager.GetLibraryOptions(video);
2020-11-08 10:20:28 +00:00
return TrySaveSubtitle(video, libraryOptions, response);
2020-05-08 19:53:38 +00:00
}
private async Task TrySaveSubtitle(
Video video,
LibraryOptions libraryOptions,
SubtitleResponse response)
{
var saveInMediaFolder = libraryOptions.SaveSubtitlesWithMedia;
var memoryStream = new MemoryStream();
await using (memoryStream.ConfigureAwait(false))
{
var stream = response.Stream;
await using (stream.ConfigureAwait(false))
{
await stream.CopyToAsync(memoryStream).ConfigureAwait(false);
memoryStream.Position = 0;
}
2020-05-08 19:53:38 +00:00
2023-02-17 19:47:07 +00:00
var savePaths = new List<string>();
var saveFileName = Path.GetFileNameWithoutExtension(video.Path) + "." + response.Language.ToLowerInvariant();
2020-05-08 19:53:38 +00:00
2023-02-17 19:47:07 +00:00
if (response.IsForced)
{
saveFileName += ".forced";
}
2020-05-08 19:53:38 +00:00
if (response.IsHearingImpaired)
{
saveFileName += ".sdh";
}
2023-02-17 19:47:07 +00:00
if (saveInMediaFolder)
2020-05-08 19:53:38 +00:00
{
2023-02-17 19:47:07 +00:00
var mediaFolderPath = Path.GetFullPath(Path.Combine(video.ContainingFolderPath, saveFileName));
savePaths.Add(mediaFolderPath);
2020-05-08 19:53:38 +00:00
}
2023-02-17 19:47:07 +00:00
var internalPath = Path.GetFullPath(Path.Combine(video.GetInternalMetadataPath(), saveFileName));
savePaths.Add(internalPath);
2020-05-08 19:53:38 +00:00
await TrySaveToFiles(memoryStream, savePaths, video, response.Format.ToLowerInvariant()).ConfigureAwait(false);
2020-05-08 19:53:38 +00:00
}
}
private async Task TrySaveToFiles(Stream stream, List<string> savePaths, Video video, string extension)
{
2023-02-23 18:09:16 +00:00
List<Exception>? exs = null;
foreach (var savePath in savePaths)
{
var path = savePath + "." + extension;
try
{
if (path.StartsWith(video.ContainingFolderPath, StringComparison.Ordinal)
|| path.StartsWith(video.GetInternalMetadataPath(), StringComparison.Ordinal))
{
var fileExists = File.Exists(path);
var counter = 0;
while (fileExists)
{
path = string.Format(CultureInfo.InvariantCulture, "{0}.{1}.{2}", savePath, counter, extension);
fileExists = File.Exists(path);
counter++;
}
_logger.LogInformation("Saving subtitles to {SavePath}", path);
_monitor.ReportFileSystemChangeBeginning(path);
Directory.CreateDirectory(Path.GetDirectoryName(path) ?? throw new InvalidOperationException("Path can't be a root directory."));
2018-09-12 17:26:21 +00:00
var fileOptions = AsyncFile.WriteOptions;
fileOptions.Mode = FileMode.CreateNew;
fileOptions.PreallocationSize = stream.Length;
var fs = new FileStream(path, fileOptions);
await using (fs.ConfigureAwait(false))
{
await stream.CopyToAsync(fs).ConfigureAwait(false);
}
return;
}
else
2022-01-22 22:36:42 +00:00
{
// TODO: Add some error handling to the API user: return BadRequest("Could not save subtitle, bad path.");
_logger.LogError("An uploaded subtitle could not be saved because the resulting path was invalid.");
2022-01-22 22:36:42 +00:00
}
}
catch (Exception ex)
{
(exs ??= []).Add(ex);
}
finally
{
_monitor.ReportFileSystemChangeComplete(path, false);
}
stream.Position = 0;
}
2022-12-05 14:01:13 +00:00
if (exs is not null)
{
2021-05-05 12:39:50 +00:00
throw new AggregateException(exs);
}
}
2019-09-10 20:37:53 +00:00
/// <inheritdoc />
public Task<RemoteSubtitleInfo[]> SearchSubtitles(Video video, string language, bool? isPerfectMatch, bool isAutomated, CancellationToken cancellationToken)
2014-05-07 02:28:19 +00:00
{
2018-09-12 17:26:21 +00:00
if (video.VideoType != VideoType.VideoFile)
2014-05-07 02:28:19 +00:00
{
2019-09-10 20:37:53 +00:00
return Task.FromResult(Array.Empty<RemoteSubtitleInfo>());
2014-05-07 02:28:19 +00:00
}
2014-05-11 22:38:10 +00:00
VideoContentType mediaType;
2014-05-07 02:28:19 +00:00
if (video is Episode)
{
2014-05-11 22:38:10 +00:00
mediaType = VideoContentType.Episode;
2014-05-07 02:28:19 +00:00
}
else if (video is Movie)
{
2014-05-11 22:38:10 +00:00
mediaType = VideoContentType.Movie;
2014-05-07 02:28:19 +00:00
}
else
{
// These are the only supported types
2019-09-10 20:37:53 +00:00
return Task.FromResult(Array.Empty<RemoteSubtitleInfo>());
2014-05-07 02:28:19 +00:00
}
var request = new SubtitleSearchRequest
{
ContentType = mediaType,
IndexNumber = video.IndexNumber,
Language = language,
MediaPath = video.Path,
Name = video.Name,
ParentIndexNumber = video.ParentIndexNumber,
ProductionYear = video.ProductionYear,
2014-05-11 22:38:10 +00:00
ProviderIds = video.ProviderIds,
RuntimeTicks = video.RunTimeTicks,
IsPerfectMatch = isPerfectMatch ?? false,
IsAutomated = isAutomated
2014-05-07 02:28:19 +00:00
};
2019-09-10 20:37:53 +00:00
if (video is Episode episode)
2014-05-07 02:28:19 +00:00
{
request.IndexNumberEnd = episode.IndexNumberEnd;
request.SeriesName = episode.SeriesName;
}
return SearchSubtitles(request, cancellationToken);
}
2014-05-17 04:24:10 +00:00
private void Normalize(IEnumerable<RemoteSubtitleInfo> subtitles)
{
foreach (var sub in subtitles)
{
sub.Id = GetProviderId(sub.ProviderName) + "_" + sub.Id;
}
}
private string GetProviderId(string name)
{
return name.ToLowerInvariant().GetMD5().ToString("N", CultureInfo.InvariantCulture);
2014-05-17 04:24:10 +00:00
}
private ISubtitleProvider GetProvider(string id)
{
2020-09-08 14:12:47 +00:00
return _subtitleProviders.First(i => string.Equals(id, GetProviderId(i.Name), StringComparison.Ordinal));
2014-05-17 04:24:10 +00:00
}
2019-09-10 20:37:53 +00:00
/// <inheritdoc />
2018-09-12 17:26:21 +00:00
public Task DeleteSubtitles(BaseItem item, int index)
2014-05-17 04:24:10 +00:00
{
var stream = _mediaSourceManager.GetMediaStreams(new MediaStreamQuery
2014-05-17 04:24:10 +00:00
{
Index = index,
2018-09-12 17:26:21 +00:00
ItemId = item.Id,
2014-05-17 04:24:10 +00:00
Type = MediaStreamType.Subtitle
})[0];
2014-05-17 04:24:10 +00:00
var path = stream.Path;
_monitor.ReportFileSystemChangeBeginning(path);
try
{
_fileSystem.DeleteFile(path);
2014-05-17 04:24:10 +00:00
}
finally
{
_monitor.ReportFileSystemChangeComplete(path, false);
}
2018-09-12 17:26:21 +00:00
return item.RefreshMetadata(CancellationToken.None);
2014-05-17 04:24:10 +00:00
}
2019-09-10 20:37:53 +00:00
/// <inheritdoc />
2014-05-17 04:24:10 +00:00
public Task<SubtitleResponse> GetRemoteSubtitles(string id, CancellationToken cancellationToken)
{
var parts = id.Split('_', 2);
2014-05-17 04:24:10 +00:00
2019-09-10 20:37:53 +00:00
var provider = GetProvider(parts[0]);
id = parts[^1];
2014-05-17 04:24:10 +00:00
return provider.GetSubtitles(id, cancellationToken);
}
2019-09-10 20:37:53 +00:00
/// <inheritdoc />
public SubtitleProviderInfo[] GetSupportedProviders(BaseItem item)
2014-05-17 04:24:10 +00:00
{
VideoContentType mediaType;
if (item is Episode)
2014-05-17 04:24:10 +00:00
{
mediaType = VideoContentType.Episode;
}
else if (item is Movie)
2014-05-17 04:24:10 +00:00
{
mediaType = VideoContentType.Movie;
}
else
{
// These are the only supported types
2019-09-10 20:37:53 +00:00
return Array.Empty<SubtitleProviderInfo>();
2014-05-17 04:24:10 +00:00
}
2017-08-19 19:43:35 +00:00
return _subtitleProviders
.Where(i => i.SupportedMediaTypes.Contains(mediaType))
.Select(i => new SubtitleProviderInfo
{
Name = i.Name,
Id = GetProviderId(i.Name)
}).ToArray();
2014-05-17 04:24:10 +00:00
}
2014-05-07 02:28:19 +00:00
}
}