jellyfin-server/MediaBrowser.Providers/MediaInfo/SubtitleDownloader.cs

164 lines
5.6 KiB
C#
Raw Normal View History

2014-05-07 02:28:19 +00:00
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Movies;
using MediaBrowser.Controller.Entities.TV;
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.Entities;
using MediaBrowser.Model.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace MediaBrowser.Providers.MediaInfo
{
public class SubtitleDownloader
{
private readonly ILogger _logger;
private readonly ISubtitleManager _subtitleManager;
public SubtitleDownloader(ILogger logger, ISubtitleManager subtitleManager)
{
_logger = logger;
_subtitleManager = subtitleManager;
}
public async Task<List<string>> DownloadSubtitles(Video video,
2014-05-07 20:24:34 +00:00
List<MediaStream> internalMediaStreams,
2014-05-07 02:28:19 +00:00
List<MediaStream> externalSubtitleStreams,
2014-05-08 02:16:11 +00:00
bool skipIfGraphicalSubtitlesPresent,
bool skipIfAudioTrackMatches,
2014-05-07 02:28:19 +00:00
IEnumerable<string> languages,
CancellationToken cancellationToken)
{
if (video.LocationType != LocationType.FileSystem ||
video.VideoType != VideoType.VideoFile)
{
return new List<string>();
}
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
return new List<string>();
}
var downloadedLanguages = new List<string>();
foreach (var lang in languages)
{
try
{
2014-05-08 02:16:11 +00:00
var downloaded = await DownloadSubtitles(video, internalMediaStreams, externalSubtitleStreams, skipIfGraphicalSubtitlesPresent, skipIfAudioTrackMatches, lang, mediaType, cancellationToken)
2014-05-07 02:28:19 +00:00
.ConfigureAwait(false);
if (downloaded)
{
downloadedLanguages.Add(lang);
}
}
catch (Exception ex)
{
_logger.ErrorException("Error downloading subtitles", ex);
}
}
return downloadedLanguages;
}
private async Task<bool> DownloadSubtitles(Video video,
2014-05-07 20:24:34 +00:00
List<MediaStream> internalMediaStreams,
2014-05-07 02:28:19 +00:00
IEnumerable<MediaStream> externalSubtitleStreams,
2014-05-08 02:16:11 +00:00
bool skipIfGraphicalSubtitlesPresent,
bool skipIfAudioTrackMatches,
2014-05-07 02:28:19 +00:00
string language,
2014-05-11 22:38:10 +00:00
VideoContentType mediaType,
2014-05-07 02:28:19 +00:00
CancellationToken cancellationToken)
{
// There's already subtitles for this language
if (externalSubtitleStreams.Any(i => string.Equals(i.Language, language, StringComparison.OrdinalIgnoreCase)))
{
return false;
}
2014-05-08 20:09:53 +00:00
var audioStreams = internalMediaStreams.Where(i => i.Type == MediaStreamType.Audio).ToList();
var defaultAudioStreams = audioStreams.Where(i => i.IsDefault).ToList();
// If none are marked as default, just take a guess
if (defaultAudioStreams.Count == 0)
{
defaultAudioStreams = audioStreams.Take(1).ToList();
}
2014-05-08 05:04:39 +00:00
// There's already a default audio stream for this language
2014-05-08 02:16:11 +00:00
if (skipIfAudioTrackMatches &&
2014-05-08 20:09:53 +00:00
defaultAudioStreams.Any(i => string.Equals(i.Language, language, StringComparison.OrdinalIgnoreCase)))
2014-05-07 20:28:28 +00:00
{
return false;
}
2014-05-07 20:24:34 +00:00
2014-05-07 02:28:19 +00:00
// There's an internal subtitle stream for this language
2014-05-08 02:16:11 +00:00
if (skipIfGraphicalSubtitlesPresent &&
2014-05-09 04:38:21 +00:00
internalMediaStreams.Any(i => i.Type == MediaStreamType.Subtitle && i.IsGraphicalSubtitleStream && string.Equals(i.Language, language, StringComparison.OrdinalIgnoreCase)))
2014-05-07 02:28:19 +00:00
{
return false;
}
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-17 04:24:10 +00:00
ProviderIds = video.ProviderIds,
// Stop as soon as we find something
SearchAllProviders = false
2014-05-07 02:28:19 +00:00
};
var episode = video as Episode;
if (episode != null)
{
request.IndexNumberEnd = episode.IndexNumberEnd;
request.SeriesName = episode.SeriesName;
}
try
{
var searchResults = await _subtitleManager.SearchSubtitles(request, cancellationToken).ConfigureAwait(false);
var result = searchResults.FirstOrDefault();
if (result != null)
{
2014-05-17 04:24:10 +00:00
await _subtitleManager.DownloadSubtitles(video, result.Id, cancellationToken)
2014-05-07 02:28:19 +00:00
.ConfigureAwait(false);
return true;
}
}
catch (Exception ex)
{
_logger.ErrorException("Error downloading subtitles", ex);
}
return false;
}
}
}