jellyfin/MediaBrowser.Providers/MediaInfo/FFProbeVideoInfo.cs

664 lines
26 KiB
C#
Raw Normal View History

#pragma warning disable CA1068, CS1591
2020-02-23 09:53:51 +00:00
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
2014-02-09 21:11:11 +00:00
using MediaBrowser.Common.Configuration;
2014-06-09 19:16:14 +00:00
using MediaBrowser.Controller.Chapters;
2014-05-07 02:28:19 +00:00
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Entities;
2014-05-07 02:28:19 +00:00
using MediaBrowser.Controller.Entities.Movies;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Library;
2014-02-20 16:37:41 +00:00
using MediaBrowser.Controller.MediaEncoding;
using MediaBrowser.Controller.Persistence;
2014-02-10 18:39:41 +00:00
using MediaBrowser.Controller.Providers;
2014-05-07 02:28:19 +00:00
using MediaBrowser.Controller.Subtitles;
2014-08-10 22:13:17 +00:00
using MediaBrowser.Model.Configuration;
using MediaBrowser.Model.Dlna;
using MediaBrowser.Model.Dto;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Globalization;
using MediaBrowser.Model.MediaInfo;
2014-08-10 22:13:17 +00:00
using MediaBrowser.Model.Providers;
using Microsoft.Extensions.Logging;
namespace MediaBrowser.Providers.MediaInfo
{
public class FFProbeVideoInfo
{
2022-11-27 13:35:07 +00:00
private readonly ILogger<FFProbeVideoInfo> _logger;
private readonly IMediaEncoder _mediaEncoder;
private readonly IItemRepository _itemRepo;
private readonly IBlurayExaminer _blurayExaminer;
private readonly ILocalizationManager _localization;
2014-02-20 16:37:41 +00:00
private readonly IEncodingManager _encodingManager;
2014-05-07 02:28:19 +00:00
private readonly IServerConfigurationManager _config;
private readonly ISubtitleManager _subtitleManager;
2014-06-09 19:16:14 +00:00
private readonly IChapterManager _chapterManager;
2015-06-28 17:00:36 +00:00
private readonly ILibraryManager _libraryManager;
private readonly AudioResolver _audioResolver;
private readonly SubtitleResolver _subtitleResolver;
2018-09-12 17:26:21 +00:00
private readonly IMediaSourceManager _mediaSourceManager;
2020-02-23 09:53:51 +00:00
public FFProbeVideoInfo(
2022-11-27 13:35:07 +00:00
ILogger<FFProbeVideoInfo> logger,
2020-02-23 09:53:51 +00:00
IMediaSourceManager mediaSourceManager,
IMediaEncoder mediaEncoder,
IItemRepository itemRepo,
IBlurayExaminer blurayExaminer,
2020-02-23 09:53:51 +00:00
ILocalizationManager localization,
IEncodingManager encodingManager,
IServerConfigurationManager config,
ISubtitleManager subtitleManager,
IChapterManager chapterManager,
ILibraryManager libraryManager,
AudioResolver audioResolver,
SubtitleResolver subtitleResolver)
{
_logger = logger;
_mediaSourceManager = mediaSourceManager;
_mediaEncoder = mediaEncoder;
_itemRepo = itemRepo;
_blurayExaminer = blurayExaminer;
_localization = localization;
2014-02-20 16:37:41 +00:00
_encodingManager = encodingManager;
2014-05-07 02:28:19 +00:00
_config = config;
_subtitleManager = subtitleManager;
2014-06-09 19:16:14 +00:00
_chapterManager = chapterManager;
2015-06-28 17:00:36 +00:00
_libraryManager = libraryManager;
_audioResolver = audioResolver;
_subtitleResolver = subtitleResolver;
}
public async Task<ItemUpdateType> ProbeVideo<T>(
T item,
2014-06-09 19:16:14 +00:00
MetadataRefreshOptions options,
CancellationToken cancellationToken)
where T : Video
{
BlurayDiscInfo? blurayDiscInfo = null;
Model.MediaInfo.MediaInfo? mediaInfoResult = null;
2018-09-12 17:26:21 +00:00
if (!item.IsShortcut || options.EnableRemoteContentProbe)
{
if (item.VideoType == VideoType.Dvd)
{
// Get list of playable .vob files
2023-02-18 13:42:35 +00:00
var vobs = _mediaEncoder.GetPrimaryPlaylistVobFiles(item.Path, null);
// Return if no playable .vob files are found
if (vobs.Count == 0)
{
_logger.LogError("No playable .vob files found in DVD structure, skipping FFprobe.");
return ItemUpdateType.MetadataImport;
}
// Fetch metadata of first .vob file
mediaInfoResult = await GetMediaInfo(
new Video
{
2023-02-18 13:42:35 +00:00
Path = vobs[0]
},
cancellationToken).ConfigureAwait(false);
2023-02-18 13:42:35 +00:00
// Sum up the runtime of all .vob files skipping the first .vob
for (var i = 1; i < vobs.Count; i++)
{
var tmpMediaInfo = await GetMediaInfo(
2023-02-18 13:42:35 +00:00
new Video
{
Path = vobs[i]
},
cancellationToken).ConfigureAwait(false);
mediaInfoResult.RunTimeTicks += tmpMediaInfo.RunTimeTicks;
}
}
else if (item.VideoType == VideoType.BluRay)
{
// Get BD disc information
blurayDiscInfo = GetBDInfo(item.Path);
// Get playable .m2ts files
2023-02-18 13:42:35 +00:00
var m2ts = _mediaEncoder.GetPrimaryPlaylistM2tsFiles(item.Path);
// Return if no playable .m2ts files are found
2023-08-22 19:14:54 +00:00
if (blurayDiscInfo is null || blurayDiscInfo.Files.Length == 0 || m2ts.Count == 0)
{
_logger.LogError("No playable .m2ts files found in Blu-ray structure, skipping FFprobe.");
return ItemUpdateType.MetadataImport;
}
// Fetch metadata of first .m2ts file
mediaInfoResult = await GetMediaInfo(
new Video
{
2023-02-18 13:42:35 +00:00
Path = m2ts[0]
},
cancellationToken).ConfigureAwait(false);
}
else
{
mediaInfoResult = await GetMediaInfo(item, cancellationToken).ConfigureAwait(false);
}
2018-09-12 17:26:21 +00:00
cancellationToken.ThrowIfCancellationRequested();
}
await Fetch(item, cancellationToken, mediaInfoResult, blurayDiscInfo, options).ConfigureAwait(false);
2018-09-12 17:26:21 +00:00
return ItemUpdateType.MetadataImport;
}
private Task<Model.MediaInfo.MediaInfo> GetMediaInfo(
Video item,
2014-06-09 19:16:14 +00:00
CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
2018-09-12 17:26:21 +00:00
var path = item.Path;
var protocol = item.PathProtocol ?? MediaProtocol.File;
if (item.IsShortcut)
{
path = item.ShortcutPath;
protocol = _mediaSourceManager.GetPathProtocol(path);
}
return _mediaEncoder.GetMediaInfo(
new MediaInfoRequest
2018-09-12 17:26:21 +00:00
{
ExtractChapters = true,
MediaType = DlnaProfileType.Video,
MediaSource = new MediaSourceInfo
{
Path = path,
Protocol = protocol,
VideoType = item.VideoType,
IsoType = item.IsoType
}
},
cancellationToken);
}
protected async Task Fetch(
Video video,
2014-06-09 19:16:14 +00:00
CancellationToken cancellationToken,
Model.MediaInfo.MediaInfo? mediaInfo,
BlurayDiscInfo? blurayInfo,
2014-06-09 19:16:14 +00:00
MetadataRefreshOptions options)
{
List<MediaStream> mediaStreams = new List<MediaStream>();
2019-12-26 22:20:31 +00:00
IReadOnlyList<MediaAttachment> mediaAttachments;
2020-02-23 09:53:51 +00:00
ChapterInfo[] chapters;
2014-04-22 17:25:54 +00:00
// Add external streams before adding the streams from the file to preserve stream IDs on remote videos
await AddExternalSubtitlesAsync(video, mediaStreams, options, cancellationToken).ConfigureAwait(false);
await AddExternalAudioAsync(video, mediaStreams, options, cancellationToken).ConfigureAwait(false);
var startIndex = mediaStreams.Count == 0 ? 0 : (mediaStreams.Max(i => i.Index) + 1);
2022-12-05 14:01:13 +00:00
if (mediaInfo is not null)
{
foreach (var mediaStream in mediaInfo.MediaStreams)
{
mediaStream.Index = startIndex++;
mediaStreams.Add(mediaStream);
}
2019-10-04 17:23:08 +00:00
mediaAttachments = mediaInfo.MediaAttachments;
video.TotalBitrate = mediaInfo.Bitrate;
video.RunTimeTicks = mediaInfo.RunTimeTicks;
2019-01-26 08:07:13 +00:00
video.Size = mediaInfo.Size;
video.Container = mediaInfo.Container;
2014-04-18 05:03:01 +00:00
2022-01-22 14:40:05 +00:00
chapters = mediaInfo.Chapters ?? Array.Empty<ChapterInfo>();
if (blurayInfo is not null)
{
FetchBdInfo(video, ref chapters, mediaStreams, blurayInfo);
}
2015-04-04 19:35:29 +00:00
}
else
{
foreach (var mediaStream in video.GetMediaStreams())
{
if (!mediaStream.IsExternal)
{
mediaStream.Index = startIndex++;
mediaStreams.Add(mediaStream);
}
}
2019-12-26 22:20:31 +00:00
mediaAttachments = Array.Empty<MediaAttachment>();
2020-02-23 09:53:51 +00:00
chapters = Array.Empty<ChapterInfo>();
}
var libraryOptions = _libraryManager.GetLibraryOptions(video);
2022-12-05 14:01:13 +00:00
if (mediaInfo is not null)
{
FetchEmbeddedInfo(video, mediaInfo, options, libraryOptions);
FetchPeople(video, mediaInfo, options);
video.Timestamp = mediaInfo.Timestamp;
2019-12-26 22:20:31 +00:00
video.Video3DFormat ??= mediaInfo.Video3DFormat;
}
2022-01-26 16:09:05 +00:00
if (libraryOptions.AllowEmbeddedSubtitles == EmbeddedSubtitleOptions.AllowText || libraryOptions.AllowEmbeddedSubtitles == EmbeddedSubtitleOptions.AllowNone)
{
_logger.LogDebug("Disabling embedded image subtitles for {Path} due to DisableEmbeddedImageSubtitles setting", video.Path);
mediaStreams.RemoveAll(i => i.Type == MediaStreamType.Subtitle && !i.IsExternal && !i.IsTextSubtitleStream);
}
2022-01-26 16:09:05 +00:00
if (libraryOptions.AllowEmbeddedSubtitles == EmbeddedSubtitleOptions.AllowImage || libraryOptions.AllowEmbeddedSubtitles == EmbeddedSubtitleOptions.AllowNone)
{
_logger.LogDebug("Disabling embedded text subtitles for {Path} due to DisableEmbeddedTextSubtitles setting", video.Path);
mediaStreams.RemoveAll(i => i.Type == MediaStreamType.Subtitle && !i.IsExternal && i.IsTextSubtitleStream);
}
var videoStream = mediaStreams.FirstOrDefault(i => i.Type == MediaStreamType.Video);
2019-12-26 22:20:31 +00:00
video.Height = videoStream?.Height ?? 0;
video.Width = videoStream?.Width ?? 0;
2018-09-12 17:26:21 +00:00
2022-01-22 14:40:05 +00:00
video.DefaultVideoStreamIndex = videoStream?.Index;
video.HasSubtitles = mediaStreams.Any(i => i.Type == MediaStreamType.Subtitle);
2016-08-09 05:08:36 +00:00
_itemRepo.SaveMediaStreams(video.Id, mediaStreams, cancellationToken);
if (mediaAttachments.Any())
{
_itemRepo.SaveMediaAttachments(video.Id, mediaAttachments, cancellationToken);
}
2014-06-09 19:16:14 +00:00
if (options.MetadataRefreshMode == MetadataRefreshMode.FullRefresh
|| options.MetadataRefreshMode == MetadataRefreshMode.Default)
2014-02-20 16:37:41 +00:00
{
if (_config.Configuration.DummyChapterDuration > 0 && chapters.Length == 0 && mediaStreams.Any(i => i.Type == MediaStreamType.Video))
2014-06-09 19:16:14 +00:00
{
2020-04-01 17:05:41 +00:00
chapters = CreateDummyChapters(video);
2014-06-09 19:16:14 +00:00
}
2014-07-12 02:31:08 +00:00
NormalizeChapterNames(chapters);
var extractDuringScan = false;
2022-12-05 14:01:13 +00:00
if (libraryOptions is not null)
{
extractDuringScan = libraryOptions.ExtractChapterImagesDuringLibraryScan;
}
await _encodingManager.RefreshChapterImages(video, options.DirectoryService, chapters, extractDuringScan, false, cancellationToken).ConfigureAwait(false);
2020-02-23 09:53:51 +00:00
_chapterManager.SaveChapters(video.Id, chapters);
2014-06-09 19:16:14 +00:00
}
}
2020-02-23 09:53:51 +00:00
private void NormalizeChapterNames(ChapterInfo[] chapters)
2014-07-12 02:31:08 +00:00
{
2020-02-23 09:53:51 +00:00
for (int i = 0; i < chapters.Length; i++)
2014-07-12 02:31:08 +00:00
{
string? name = chapters[i].Name;
2014-07-12 02:31:08 +00:00
// Check if the name is empty and/or if the name is a time
// Some ripping programs do that.
if (string.IsNullOrWhiteSpace(name)
|| TimeSpan.TryParse(name, out _))
2014-07-12 02:31:08 +00:00
{
2020-02-23 09:53:51 +00:00
chapters[i].Name = string.Format(
CultureInfo.InvariantCulture,
_localization.GetLocalizedString("ChapterNameValue"),
(i + 1).ToString(CultureInfo.InvariantCulture));
2014-07-12 02:31:08 +00:00
}
}
}
2023-02-18 13:42:35 +00:00
private void FetchBdInfo(Video video, ref ChapterInfo[] chapters, List<MediaStream> mediaStreams, BlurayDiscInfo blurayInfo)
{
2023-02-05 16:24:13 +00:00
if (blurayInfo.Files.Length <= 1)
{
2023-02-05 16:24:13 +00:00
return;
}
2024-02-28 21:10:44 +00:00
var ffmpegVideoStream = mediaStreams.FirstOrDefault(s => s.Type == MediaStreamType.Video);
2023-02-05 16:24:13 +00:00
// Fill video properties from the BDInfo result
mediaStreams.Clear();
mediaStreams.AddRange(blurayInfo.MediaStreams);
2023-02-05 16:24:13 +00:00
if (blurayInfo.RunTimeTicks.HasValue && blurayInfo.RunTimeTicks.Value > 0)
{
video.RunTimeTicks = blurayInfo.RunTimeTicks;
}
if (blurayInfo.Chapters is not null)
{
double[] brChapter = blurayInfo.Chapters;
chapters = new ChapterInfo[brChapter.Length];
for (int i = 0; i < brChapter.Length; i++)
{
2023-02-05 16:24:13 +00:00
chapters[i] = new ChapterInfo
{
2023-02-05 16:24:13 +00:00
StartPositionTicks = TimeSpan.FromSeconds(brChapter[i]).Ticks
};
}
2023-02-05 16:24:13 +00:00
}
2024-02-28 21:10:44 +00:00
var blurayVideoStream = mediaStreams.FirstOrDefault(s => s.Type == MediaStreamType.Video);
2023-02-05 16:24:13 +00:00
// Use the ffprobe values if these are empty
2024-02-28 21:10:44 +00:00
if (blurayVideoStream is not null && ffmpegVideoStream is not null)
2023-02-05 16:24:13 +00:00
{
2024-02-28 21:10:44 +00:00
// Always use ffmpeg's detected codec since that is what the rest of the codebase expects.
blurayVideoStream.Codec = ffmpegVideoStream.Codec;
blurayVideoStream.BitRate = blurayVideoStream.BitRate.GetValueOrDefault() == 0 ? ffmpegVideoStream.BitRate : blurayVideoStream.BitRate;
blurayVideoStream.Width = blurayVideoStream.Width.GetValueOrDefault() == 0 ? ffmpegVideoStream.Width : blurayVideoStream.Width;
blurayVideoStream.Height = blurayVideoStream.Height.GetValueOrDefault() == 0 ? ffmpegVideoStream.Width : blurayVideoStream.Height;
}
}
/// <summary>
/// Gets information about the longest playlist on a bdrom.
/// </summary>
/// <param name="path">The path.</param>
/// <returns>VideoStream.</returns>
private BlurayDiscInfo? GetBDInfo(string path)
{
2023-02-05 16:24:13 +00:00
ArgumentException.ThrowIfNullOrEmpty(path);
try
{
return _blurayExaminer.GetDiscInfo(path);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error getting BDInfo");
return null;
}
}
private void FetchEmbeddedInfo(Video video, Model.MediaInfo.MediaInfo data, MetadataRefreshOptions refreshOptions, LibraryOptions libraryOptions)
{
var replaceData = refreshOptions.ReplaceAllMetadata;
2015-04-20 18:04:02 +00:00
2020-06-09 22:12:53 +00:00
if (!video.IsLocked && !video.LockedFields.Contains(MetadataField.OfficialRating))
{
if (string.IsNullOrWhiteSpace(video.OfficialRating) || replaceData)
{
2015-04-04 19:35:29 +00:00
video.OfficialRating = data.OfficialRating;
}
}
2020-06-09 22:12:53 +00:00
if (!video.IsLocked && !video.LockedFields.Contains(MetadataField.Genres))
2015-03-05 06:34:36 +00:00
{
if (video.Genres.Length == 0 || replaceData)
2015-03-05 06:34:36 +00:00
{
2018-09-12 17:26:21 +00:00
video.Genres = Array.Empty<string>();
2015-04-20 18:04:02 +00:00
foreach (var genre in data.Genres)
{
video.AddGenre(genre);
}
}
}
2020-06-09 22:12:53 +00:00
if (!video.IsLocked && !video.LockedFields.Contains(MetadataField.Studios))
{
if (video.Studios.Length == 0 || replaceData)
{
video.SetStudios(data.Studios);
2015-03-05 06:34:36 +00:00
}
}
if (!video.IsLocked && video is MusicVideo musicVideo)
{
if (string.IsNullOrEmpty(musicVideo.Album) || replaceData)
{
musicVideo.Album = data.Album;
}
if (musicVideo.Artists.Count == 0 || replaceData)
{
musicVideo.Artists = data.Artists;
}
}
2015-04-04 19:35:29 +00:00
if (data.ProductionYear.HasValue)
2015-03-05 06:34:36 +00:00
{
if (!video.ProductionYear.HasValue || replaceData)
2015-04-20 18:04:02 +00:00
{
video.ProductionYear = data.ProductionYear;
}
2015-04-04 19:35:29 +00:00
}
2020-06-15 21:43:52 +00:00
2015-04-04 19:35:29 +00:00
if (data.PremiereDate.HasValue)
{
if (!video.PremiereDate.HasValue || replaceData)
2015-04-20 18:04:02 +00:00
{
video.PremiereDate = data.PremiereDate;
}
2015-04-04 19:35:29 +00:00
}
2020-06-15 21:43:52 +00:00
2015-04-04 19:35:29 +00:00
if (data.IndexNumber.HasValue)
{
if (!video.IndexNumber.HasValue || replaceData)
2015-04-20 18:04:02 +00:00
{
video.IndexNumber = data.IndexNumber;
}
2015-04-04 19:35:29 +00:00
}
2020-06-15 21:43:52 +00:00
2015-04-04 19:35:29 +00:00
if (data.ParentIndexNumber.HasValue)
{
if (!video.ParentIndexNumber.HasValue || replaceData)
2015-04-20 18:04:02 +00:00
{
video.ParentIndexNumber = data.ParentIndexNumber;
}
2015-04-04 19:35:29 +00:00
}
2020-06-09 22:12:53 +00:00
if (!video.IsLocked && !video.LockedFields.Contains(MetadataField.Name))
2016-02-18 02:55:15 +00:00
{
if (!string.IsNullOrWhiteSpace(data.Name) && libraryOptions.EnableEmbeddedTitles)
2016-02-18 02:55:15 +00:00
{
// Separate option to use the embedded name for extras because it will often be the same name as the movie
if (!video.ExtraType.HasValue || libraryOptions.EnableEmbeddedExtrasTitles)
2016-05-01 20:56:26 +00:00
{
video.Name = data.Name;
2016-05-01 20:56:26 +00:00
}
2016-02-18 02:55:15 +00:00
}
if (!string.IsNullOrWhiteSpace(data.ForcedSortName))
{
video.ForcedSortName = data.ForcedSortName;
}
2016-02-18 02:55:15 +00:00
}
2015-03-05 06:34:36 +00:00
2015-04-04 19:35:29 +00:00
// If we don't have a ProductionYear try and get it from PremiereDate
if (video.PremiereDate.HasValue && !video.ProductionYear.HasValue)
{
video.ProductionYear = video.PremiereDate.Value.ToLocalTime().Year;
}
2015-03-05 06:34:36 +00:00
2020-06-09 22:12:53 +00:00
if (!video.IsLocked && !video.LockedFields.Contains(MetadataField.Overview))
2015-03-05 06:34:36 +00:00
{
if (string.IsNullOrWhiteSpace(video.Overview) || replaceData)
2015-03-05 06:34:36 +00:00
{
2015-04-04 19:35:29 +00:00
video.Overview = data.Overview;
2015-03-05 06:34:36 +00:00
}
}
}
private void FetchPeople(Video video, Model.MediaInfo.MediaInfo data, MetadataRefreshOptions options)
2015-06-28 17:00:36 +00:00
{
if (video.IsLocked
|| video.LockedFields.Contains(MetadataField.Cast)
|| data.People.Length == 0)
{
return;
}
2015-06-28 17:00:36 +00:00
if (options.ReplaceAllMetadata || _libraryManager.GetPeople(video).Count == 0)
2015-06-28 17:00:36 +00:00
{
var people = new List<PersonInfo>();
2015-06-28 17:00:36 +00:00
foreach (var person in data.People)
{
PeopleHelper.AddPerson(people, new PersonInfo
2015-06-28 17:00:36 +00:00
{
Name = person.Name,
Type = person.Type,
Role = person.Role
});
2015-06-28 17:00:36 +00:00
}
_libraryManager.UpdatePeople(video, people);
}
2014-08-10 22:13:17 +00:00
}
/// <summary>
/// Adds the external subtitles.
/// </summary>
/// <param name="video">The video.</param>
/// <param name="currentStreams">The current streams.</param>
/// <param name="options">The refreshOptions.</param>
2014-05-17 04:24:10 +00:00
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>Task.</returns>
private async Task AddExternalSubtitlesAsync(
Video video,
2014-06-09 19:16:14 +00:00
List<MediaStream> currentStreams,
MetadataRefreshOptions options,
CancellationToken cancellationToken)
2014-05-07 02:28:19 +00:00
{
2015-09-23 04:00:30 +00:00
var startIndex = currentStreams.Count == 0 ? 0 : (currentStreams.Select(i => i.Index).Max() + 1);
var externalSubtitleStreams = await _subtitleResolver.GetExternalStreamsAsync(video, startIndex, options.DirectoryService, false, cancellationToken).ConfigureAwait(false);
2014-06-09 19:16:14 +00:00
var enableSubtitleDownloading = options.MetadataRefreshMode == MetadataRefreshMode.Default ||
2014-06-09 19:16:14 +00:00
options.MetadataRefreshMode == MetadataRefreshMode.FullRefresh;
2014-05-07 02:28:19 +00:00
var subtitleOptions = _config.GetConfiguration<SubtitleOptions>("subtitles");
2014-08-10 22:13:17 +00:00
2018-09-12 17:26:21 +00:00
var libraryOptions = _libraryManager.GetLibraryOptions(video);
string[] subtitleDownloadLanguages;
2020-02-23 09:53:51 +00:00
bool skipIfEmbeddedSubtitlesPresent;
bool skipIfAudioTrackMatches;
bool requirePerfectMatch;
2018-09-12 17:26:21 +00:00
bool enabled;
2022-12-05 14:00:20 +00:00
if (libraryOptions.SubtitleDownloadLanguages is null)
2018-09-12 17:26:21 +00:00
{
subtitleDownloadLanguages = subtitleOptions.DownloadLanguages;
2020-02-23 09:53:51 +00:00
skipIfEmbeddedSubtitlesPresent = subtitleOptions.SkipIfEmbeddedSubtitlesPresent;
skipIfAudioTrackMatches = subtitleOptions.SkipIfAudioTrackMatches;
requirePerfectMatch = subtitleOptions.RequirePerfectMatch;
2018-09-12 17:26:21 +00:00
enabled = (subtitleOptions.DownloadEpisodeSubtitles &&
2014-05-07 02:28:19 +00:00
video is Episode) ||
2014-08-10 22:13:17 +00:00
(subtitleOptions.DownloadMovieSubtitles &&
2018-09-12 17:26:21 +00:00
video is Movie);
}
else
{
subtitleDownloadLanguages = libraryOptions.SubtitleDownloadLanguages;
2020-02-23 09:53:51 +00:00
skipIfEmbeddedSubtitlesPresent = libraryOptions.SkipSubtitlesIfEmbeddedSubtitlesPresent;
skipIfAudioTrackMatches = libraryOptions.SkipSubtitlesIfAudioTrackMatches;
requirePerfectMatch = libraryOptions.RequirePerfectSubtitleMatch;
2018-09-12 17:26:21 +00:00
enabled = true;
}
if (enableSubtitleDownloading && enabled)
2014-05-07 02:28:19 +00:00
{
2020-09-07 11:20:39 +00:00
var downloadedLanguages = await new SubtitleDownloader(
_logger,
_subtitleManager).DownloadSubtitles(
video,
currentStreams.Concat(externalSubtitleStreams).ToList(),
skipIfEmbeddedSubtitlesPresent,
skipIfAudioTrackMatches,
requirePerfectMatch,
subtitleDownloadLanguages,
libraryOptions.DisabledSubtitleFetchers,
libraryOptions.SubtitleFetcherOrder,
true,
2020-09-07 11:20:39 +00:00
cancellationToken).ConfigureAwait(false);
2014-05-07 02:28:19 +00:00
// Rescan
if (downloadedLanguages.Count > 0)
{
externalSubtitleStreams = await _subtitleResolver.GetExternalStreamsAsync(video, startIndex, options.DirectoryService, true, cancellationToken).ConfigureAwait(false);
2014-05-07 02:28:19 +00:00
}
}
video.SubtitleFiles = externalSubtitleStreams.Select(i => i.Path).Distinct().ToArray();
2014-05-07 02:28:19 +00:00
currentStreams.AddRange(externalSubtitleStreams);
}
2021-11-22 20:47:52 +00:00
/// <summary>
/// Adds the external audio.
/// </summary>
/// <param name="video">The video.</param>
/// <param name="currentStreams">The current streams.</param>
/// <param name="options">The refreshOptions.</param>
/// <param name="cancellationToken">The cancellation token.</param>
private async Task AddExternalAudioAsync(
2021-11-22 20:47:52 +00:00
Video video,
List<MediaStream> currentStreams,
MetadataRefreshOptions options,
CancellationToken cancellationToken)
{
var startIndex = currentStreams.Count == 0 ? 0 : currentStreams.Max(i => i.Index) + 1;
var externalAudioStreams = await _audioResolver.GetExternalStreamsAsync(video, startIndex, options.DirectoryService, false, cancellationToken).ConfigureAwait(false);
2021-11-22 20:47:52 +00:00
video.AudioFiles = externalAudioStreams.Select(i => i.Path).Distinct().ToArray();
2021-11-22 20:47:52 +00:00
currentStreams.AddRange(externalAudioStreams);
2021-11-22 20:47:52 +00:00
}
/// <summary>
2020-04-01 17:05:41 +00:00
/// Creates dummy chapters.
/// </summary>
/// <param name="video">The video.</param>
/// <returns>An array of dummy chapters.</returns>
internal ChapterInfo[] CreateDummyChapters(Video video)
{
var runtime = video.RunTimeTicks.GetValueOrDefault();
// Only process files with a runtime higher than 0 and lower than 12h. The latter are likely corrupted.
if (runtime < 0 || runtime > TimeSpan.FromHours(12).Ticks)
{
2020-02-23 09:53:51 +00:00
throw new ArgumentException(
string.Format(
CultureInfo.InvariantCulture,
"{0} has an invalid runtime of {1} minutes",
2020-02-23 09:53:51 +00:00
video.Name,
TimeSpan.FromTicks(runtime).TotalMinutes));
}
long dummyChapterDuration = TimeSpan.FromSeconds(_config.Configuration.DummyChapterDuration).Ticks;
if (runtime <= dummyChapterDuration)
{
return Array.Empty<ChapterInfo>();
}
int chapterCount = (int)(runtime / dummyChapterDuration);
var chapters = new ChapterInfo[chapterCount];
long currentChapterTicks = 0;
for (int i = 0; i < chapterCount; i++)
{
chapters[i] = new ChapterInfo
{
StartPositionTicks = currentChapterTicks
};
currentChapterTicks += dummyChapterDuration;
}
2020-04-01 17:05:41 +00:00
return chapters;
}
}
}