2021-10-26 13:49:01 +00:00
|
|
|
#nullable disable
|
|
|
|
|
2021-07-23 03:16:38 +00:00
|
|
|
#pragma warning disable CA1068, CS1591
|
2020-02-23 09:53:51 +00:00
|
|
|
|
2019-01-13 20:03:10 +00:00
|
|
|
using System;
|
2019-01-13 19:26:31 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Globalization;
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using DvdLib.Ifo;
|
2021-11-27 11:10:57 +00:00
|
|
|
using Emby.Naming.Common;
|
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;
|
2014-02-04 20:19:29 +00:00
|
|
|
using MediaBrowser.Controller.Entities;
|
2014-05-07 02:28:19 +00:00
|
|
|
using MediaBrowser.Controller.Entities.Movies;
|
|
|
|
using MediaBrowser.Controller.Entities.TV;
|
2014-02-04 20:19:29 +00:00
|
|
|
using MediaBrowser.Controller.Library;
|
2014-02-20 16:37:41 +00:00
|
|
|
using MediaBrowser.Controller.MediaEncoding;
|
2014-02-04 20:19:29 +00:00
|
|
|
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;
|
2019-01-13 19:26:31 +00:00
|
|
|
using MediaBrowser.Model.Dlna;
|
|
|
|
using MediaBrowser.Model.Dto;
|
2014-02-04 20:19:29 +00:00
|
|
|
using MediaBrowser.Model.Entities;
|
2019-01-13 19:26:31 +00:00
|
|
|
using MediaBrowser.Model.Globalization;
|
2014-02-04 20:19:29 +00:00
|
|
|
using MediaBrowser.Model.MediaInfo;
|
2014-08-10 22:13:17 +00:00
|
|
|
using MediaBrowser.Model.Providers;
|
2019-01-13 19:26:31 +00:00
|
|
|
using Microsoft.Extensions.Logging;
|
2014-02-04 20:19:29 +00:00
|
|
|
|
|
|
|
namespace MediaBrowser.Providers.MediaInfo
|
|
|
|
{
|
|
|
|
public class FFProbeVideoInfo
|
|
|
|
{
|
|
|
|
private readonly ILogger _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;
|
2021-11-27 11:10:57 +00:00
|
|
|
private readonly NamingOptions _namingOptions;
|
2018-09-12 17:26:21 +00:00
|
|
|
private readonly IMediaSourceManager _mediaSourceManager;
|
2014-02-04 20:19:29 +00:00
|
|
|
|
2020-02-23 09:53:51 +00:00
|
|
|
private readonly long _dummyChapterDuration = TimeSpan.FromMinutes(5).Ticks;
|
|
|
|
|
|
|
|
public FFProbeVideoInfo(
|
|
|
|
ILogger logger,
|
|
|
|
IMediaSourceManager mediaSourceManager,
|
|
|
|
IMediaEncoder mediaEncoder,
|
|
|
|
IItemRepository itemRepo,
|
|
|
|
IBlurayExaminer blurayExaminer,
|
|
|
|
ILocalizationManager localization,
|
|
|
|
IEncodingManager encodingManager,
|
|
|
|
IServerConfigurationManager config,
|
|
|
|
ISubtitleManager subtitleManager,
|
|
|
|
IChapterManager chapterManager,
|
2021-11-27 11:10:57 +00:00
|
|
|
ILibraryManager libraryManager,
|
|
|
|
NamingOptions namingOptions)
|
2014-02-04 20:19:29 +00:00
|
|
|
{
|
|
|
|
_logger = logger;
|
|
|
|
_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;
|
2021-11-27 11:10:57 +00:00
|
|
|
_namingOptions = namingOptions;
|
2018-09-12 17:26:21 +00:00
|
|
|
_mediaSourceManager = mediaSourceManager;
|
2014-02-04 20:19:29 +00:00
|
|
|
}
|
|
|
|
|
2020-04-11 10:29:04 +00:00
|
|
|
public async Task<ItemUpdateType> ProbeVideo<T>(
|
|
|
|
T item,
|
2014-06-09 19:16:14 +00:00
|
|
|
MetadataRefreshOptions options,
|
|
|
|
CancellationToken cancellationToken)
|
2014-02-04 20:19:29 +00:00
|
|
|
where T : Video
|
|
|
|
{
|
2014-03-05 02:59:59 +00:00
|
|
|
BlurayDiscInfo blurayDiscInfo = null;
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
Model.MediaInfo.MediaInfo mediaInfoResult = null;
|
|
|
|
|
|
|
|
if (!item.IsShortcut || options.EnableRemoteContentProbe)
|
2014-02-04 20:19:29 +00:00
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
string[] streamFileNames = null;
|
2014-03-05 02:59:59 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
if (item.VideoType == VideoType.Dvd)
|
2017-08-05 19:02:33 +00:00
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
streamFileNames = FetchFromDvdLib(item);
|
2017-08-05 19:02:33 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
if (streamFileNames.Length == 0)
|
2014-02-04 20:19:29 +00:00
|
|
|
{
|
2018-12-13 13:18:25 +00:00
|
|
|
_logger.LogError("No playable vobs found in dvd structure, skipping ffprobe.");
|
2018-09-12 17:26:21 +00:00
|
|
|
return ItemUpdateType.MetadataImport;
|
2014-02-04 20:19:29 +00:00
|
|
|
}
|
2018-09-12 17:26:21 +00:00
|
|
|
}
|
|
|
|
else if (item.VideoType == VideoType.BluRay)
|
|
|
|
{
|
|
|
|
var inputPath = item.Path;
|
2017-08-05 19:02:33 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
blurayDiscInfo = GetBDInfo(inputPath);
|
2017-08-05 19:02:33 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
streamFileNames = blurayDiscInfo.Files;
|
2017-08-05 19:02:33 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
if (streamFileNames.Length == 0)
|
2017-12-03 22:12:46 +00:00
|
|
|
{
|
2018-12-13 13:18:25 +00:00
|
|
|
_logger.LogError("No playable vobs found in bluray structure, skipping ffprobe.");
|
2018-09-12 17:26:21 +00:00
|
|
|
return ItemUpdateType.MetadataImport;
|
2017-12-03 22:12:46 +00:00
|
|
|
}
|
|
|
|
}
|
2014-02-04 20:19:29 +00:00
|
|
|
|
2021-05-05 11:51:14 +00:00
|
|
|
streamFileNames ??= Array.Empty<string>();
|
2018-09-12 17:26:21 +00:00
|
|
|
|
2020-10-01 20:20:28 +00:00
|
|
|
mediaInfoResult = await GetMediaInfo(item, cancellationToken).ConfigureAwait(false);
|
2018-09-12 17:26:21 +00:00
|
|
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
2014-02-04 20:19:29 +00:00
|
|
|
}
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
await Fetch(item, cancellationToken, mediaInfoResult, blurayDiscInfo, options).ConfigureAwait(false);
|
|
|
|
|
2014-02-04 20:19:29 +00:00
|
|
|
return ItemUpdateType.MetadataImport;
|
|
|
|
}
|
|
|
|
|
2020-04-11 10:29:04 +00:00
|
|
|
private Task<Model.MediaInfo.MediaInfo> GetMediaInfo(
|
|
|
|
Video item,
|
2014-06-09 19:16:14 +00:00
|
|
|
CancellationToken cancellationToken)
|
2014-02-04 20:19:29 +00:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
2014-02-04 20:19:29 +00:00
|
|
|
|
2020-04-11 10:29:04 +00:00
|
|
|
return _mediaEncoder.GetMediaInfo(
|
|
|
|
new MediaInfoRequest
|
2018-09-12 17:26:21 +00:00
|
|
|
{
|
2020-04-11 10:29:04 +00:00
|
|
|
ExtractChapters = true,
|
|
|
|
MediaType = DlnaProfileType.Video,
|
|
|
|
MediaSource = new MediaSourceInfo
|
|
|
|
{
|
|
|
|
Path = path,
|
|
|
|
Protocol = protocol,
|
2021-06-10 18:07:28 +00:00
|
|
|
VideoType = item.VideoType,
|
|
|
|
IsoType = item.IsoType
|
2020-04-11 10:29:04 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
cancellationToken);
|
2014-02-04 20:19:29 +00:00
|
|
|
}
|
|
|
|
|
2020-04-11 10:29:04 +00:00
|
|
|
protected async Task Fetch(
|
|
|
|
Video video,
|
2014-06-09 19:16:14 +00:00
|
|
|
CancellationToken cancellationToken,
|
2015-04-05 15:01:57 +00:00
|
|
|
Model.MediaInfo.MediaInfo mediaInfo,
|
2014-06-09 19:16:14 +00:00
|
|
|
BlurayDiscInfo blurayInfo,
|
|
|
|
MetadataRefreshOptions options)
|
2014-02-04 20:19:29 +00:00
|
|
|
{
|
2017-12-03 22:12:46 +00:00
|
|
|
List<MediaStream> mediaStreams;
|
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
|
|
|
|
2017-12-03 22:12:46 +00:00
|
|
|
if (mediaInfo != null)
|
|
|
|
{
|
|
|
|
mediaStreams = mediaInfo.MediaStreams;
|
2019-10-04 17:23:08 +00:00
|
|
|
mediaAttachments = mediaInfo.MediaAttachments;
|
2014-04-22 17:25:54 +00:00
|
|
|
|
2017-12-03 22:12:46 +00:00
|
|
|
video.TotalBitrate = mediaInfo.Bitrate;
|
2020-06-14 09:11:11 +00:00
|
|
|
// video.FormatName = (mediaInfo.Container ?? string.Empty)
|
2017-12-03 22:12:46 +00:00
|
|
|
// .Replace("matroska", "mkv", StringComparison.OrdinalIgnoreCase);
|
2014-04-18 05:03:01 +00:00
|
|
|
|
2017-12-03 22:12:46 +00:00
|
|
|
// For dvd's this may not always be accurate, so don't set the runtime if the item already has one
|
|
|
|
var needToSetRuntime = video.VideoType != VideoType.Dvd || video.RunTimeTicks == null || video.RunTimeTicks.Value == 0;
|
2014-04-18 05:03:01 +00:00
|
|
|
|
2017-12-03 22:12:46 +00:00
|
|
|
if (needToSetRuntime)
|
|
|
|
{
|
|
|
|
video.RunTimeTicks = mediaInfo.RunTimeTicks;
|
|
|
|
}
|
2020-04-01 17:05:41 +00:00
|
|
|
|
2019-01-26 08:07:13 +00:00
|
|
|
video.Size = mediaInfo.Size;
|
2017-12-03 22:12:46 +00:00
|
|
|
|
|
|
|
if (video.VideoType == VideoType.VideoFile)
|
|
|
|
{
|
|
|
|
var extension = (Path.GetExtension(video.Path) ?? string.Empty).TrimStart('.');
|
|
|
|
|
|
|
|
video.Container = extension;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
video.Container = null;
|
|
|
|
}
|
2020-04-01 17:05:41 +00:00
|
|
|
|
2017-12-03 22:12:46 +00:00
|
|
|
video.Container = mediaInfo.Container;
|
2014-04-18 05:03:01 +00:00
|
|
|
|
2020-02-23 09:53:51 +00:00
|
|
|
chapters = mediaInfo.Chapters == null ? Array.Empty<ChapterInfo>() : mediaInfo.Chapters;
|
2017-12-03 22:12:46 +00:00
|
|
|
if (blurayInfo != null)
|
|
|
|
{
|
2020-02-23 09:53:51 +00:00
|
|
|
FetchBdInfo(video, ref chapters, mediaStreams, blurayInfo);
|
2017-12-03 22:12:46 +00:00
|
|
|
}
|
2015-04-04 19:35:29 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-12-03 22:12:46 +00:00
|
|
|
mediaStreams = new List<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>();
|
2014-02-04 20:19:29 +00:00
|
|
|
}
|
|
|
|
|
2014-06-09 19:16:14 +00:00
|
|
|
await AddExternalSubtitles(video, mediaStreams, options, cancellationToken).ConfigureAwait(false);
|
2017-12-03 22:12:46 +00:00
|
|
|
|
2021-11-27 15:50:39 +00:00
|
|
|
await AddExternalAudio(video, mediaStreams, options, cancellationToken).ConfigureAwait(false);
|
2021-11-22 20:47:52 +00:00
|
|
|
|
2017-02-21 22:12:32 +00:00
|
|
|
var libraryOptions = _libraryManager.GetLibraryOptions(video);
|
2014-02-04 20:19:29 +00:00
|
|
|
|
2017-12-03 22:12:46 +00:00
|
|
|
if (mediaInfo != 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;
|
2017-12-03 22:12:46 +00:00
|
|
|
}
|
2014-02-04 20:19:29 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2014-02-04 20:19:29 +00:00
|
|
|
video.DefaultVideoStreamIndex = videoStream == null ? (int?)null : videoStream.Index;
|
|
|
|
|
|
|
|
video.HasSubtitles = mediaStreams.Any(i => i.Type == MediaStreamType.Subtitle);
|
2016-08-09 05:08:36 +00:00
|
|
|
|
2017-08-27 00:32:33 +00:00
|
|
|
_itemRepo.SaveMediaStreams(video.Id, mediaStreams, cancellationToken);
|
2019-10-04 17:23:08 +00:00
|
|
|
_itemRepo.SaveMediaAttachments(video.Id, mediaAttachments, cancellationToken);
|
2014-06-09 19:16:14 +00:00
|
|
|
|
|
|
|
if (options.MetadataRefreshMode == MetadataRefreshMode.FullRefresh ||
|
2014-07-04 02:22:57 +00:00
|
|
|
options.MetadataRefreshMode == MetadataRefreshMode.Default)
|
2014-02-20 16:37:41 +00:00
|
|
|
{
|
2020-02-23 09:53:51 +00:00
|
|
|
if (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-02-04 20:19:29 +00:00
|
|
|
|
2014-07-12 02:31:08 +00:00
|
|
|
NormalizeChapterNames(chapters);
|
|
|
|
|
2016-10-02 04:31:47 +00:00
|
|
|
var extractDuringScan = false;
|
|
|
|
if (libraryOptions != null)
|
2016-08-29 21:06:24 +00:00
|
|
|
{
|
|
|
|
extractDuringScan = libraryOptions.ExtractChapterImagesDuringLibraryScan;
|
|
|
|
}
|
|
|
|
|
2017-11-05 21:51:23 +00:00
|
|
|
await _encodingManager.RefreshChapterImages(video, options.DirectoryService, chapters, extractDuringScan, false, cancellationToken).ConfigureAwait(false);
|
2014-02-04 20:19:29 +00:00
|
|
|
|
2020-02-23 09:53:51 +00:00
|
|
|
_chapterManager.SaveChapters(video.Id, chapters);
|
2014-06-09 19:16:14 +00:00
|
|
|
}
|
2014-02-04 20:19:29 +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
|
|
|
{
|
2020-02-23 09:53:51 +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.
|
2020-02-23 09:53:51 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-23 09:53:51 +00:00
|
|
|
private void FetchBdInfo(BaseItem item, ref ChapterInfo[] chapters, List<MediaStream> mediaStreams, BlurayDiscInfo blurayInfo)
|
2014-02-04 20:19:29 +00:00
|
|
|
{
|
|
|
|
var video = (Video)item;
|
|
|
|
|
2020-06-14 09:11:11 +00:00
|
|
|
// video.PlayableStreamFileNames = blurayInfo.Files.ToList();
|
2014-02-04 20:19:29 +00:00
|
|
|
|
2015-12-22 17:27:04 +00:00
|
|
|
// Use BD Info if it has multiple m2ts. Otherwise, treat it like a video file and rely more on ffprobe output
|
2017-08-19 19:43:35 +00:00
|
|
|
if (blurayInfo.Files.Length > 1)
|
2014-02-04 20:19:29 +00:00
|
|
|
{
|
2015-12-22 17:27:04 +00:00
|
|
|
int? currentHeight = null;
|
|
|
|
int? currentWidth = null;
|
|
|
|
int? currentBitRate = null;
|
2014-02-04 20:19:29 +00:00
|
|
|
|
2015-12-22 17:27:04 +00:00
|
|
|
var videoStream = mediaStreams.FirstOrDefault(s => s.Type == MediaStreamType.Video);
|
2014-02-04 20:19:29 +00:00
|
|
|
|
2015-12-22 17:27:04 +00:00
|
|
|
// Grab the values that ffprobe recorded
|
|
|
|
if (videoStream != null)
|
|
|
|
{
|
|
|
|
currentBitRate = videoStream.BitRate;
|
|
|
|
currentWidth = videoStream.Width;
|
|
|
|
currentHeight = videoStream.Height;
|
|
|
|
}
|
2014-02-04 20:19:29 +00:00
|
|
|
|
2015-12-22 17:27:04 +00:00
|
|
|
// Fill video properties from the BDInfo result
|
|
|
|
mediaStreams.Clear();
|
|
|
|
mediaStreams.AddRange(blurayInfo.MediaStreams);
|
2014-02-04 20:19:29 +00:00
|
|
|
|
2015-12-22 17:27:04 +00:00
|
|
|
if (blurayInfo.RunTimeTicks.HasValue && blurayInfo.RunTimeTicks.Value > 0)
|
|
|
|
{
|
|
|
|
video.RunTimeTicks = blurayInfo.RunTimeTicks;
|
|
|
|
}
|
2014-02-04 20:19:29 +00:00
|
|
|
|
2015-12-22 17:27:04 +00:00
|
|
|
if (blurayInfo.Chapters != null)
|
2014-02-04 20:19:29 +00:00
|
|
|
{
|
2020-02-23 09:53:51 +00:00
|
|
|
double[] brChapter = blurayInfo.Chapters;
|
|
|
|
chapters = new ChapterInfo[brChapter.Length];
|
|
|
|
for (int i = 0; i < brChapter.Length; i++)
|
2015-12-22 17:27:04 +00:00
|
|
|
{
|
2020-02-23 09:53:51 +00:00
|
|
|
chapters[i] = new ChapterInfo
|
|
|
|
{
|
|
|
|
StartPositionTicks = TimeSpan.FromSeconds(brChapter[i]).Ticks
|
|
|
|
};
|
|
|
|
}
|
2015-12-22 17:27:04 +00:00
|
|
|
}
|
2014-03-05 02:59:59 +00:00
|
|
|
|
2015-12-22 17:27:04 +00:00
|
|
|
videoStream = mediaStreams.FirstOrDefault(s => s.Type == MediaStreamType.Video);
|
|
|
|
|
|
|
|
// Use the ffprobe values if these are empty
|
|
|
|
if (videoStream != null)
|
|
|
|
{
|
|
|
|
videoStream.BitRate = IsEmpty(videoStream.BitRate) ? currentBitRate : videoStream.BitRate;
|
|
|
|
videoStream.Width = IsEmpty(videoStream.Width) ? currentWidth : videoStream.Width;
|
|
|
|
videoStream.Height = IsEmpty(videoStream.Height) ? currentHeight : videoStream.Height;
|
|
|
|
}
|
2014-03-05 02:59:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private bool IsEmpty(int? num)
|
|
|
|
{
|
|
|
|
return !num.HasValue || num.Value == 0;
|
2014-02-04 20:19:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2020-06-15 22:37:52 +00:00
|
|
|
/// Gets information about the longest playlist on a bdrom.
|
2014-02-04 20:19:29 +00:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="path">The path.</param>
|
|
|
|
/// <returns>VideoStream.</returns>
|
|
|
|
private BlurayDiscInfo GetBDInfo(string path)
|
|
|
|
{
|
2016-12-24 07:41:25 +00:00
|
|
|
if (string.IsNullOrWhiteSpace(path))
|
|
|
|
{
|
2019-01-06 20:50:43 +00:00
|
|
|
throw new ArgumentNullException(nameof(path));
|
2016-12-24 07:41:25 +00:00
|
|
|
}
|
|
|
|
|
2016-01-29 01:15:59 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
return _blurayExaminer.GetDiscInfo(path);
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
2018-12-20 12:11:26 +00:00
|
|
|
_logger.LogError(ex, "Error getting BDInfo");
|
2016-01-29 01:15:59 +00:00
|
|
|
return null;
|
|
|
|
}
|
2014-02-04 20:19:29 +00:00
|
|
|
}
|
|
|
|
|
2017-02-21 22:12:32 +00:00
|
|
|
private void FetchEmbeddedInfo(Video video, Model.MediaInfo.MediaInfo data, MetadataRefreshOptions refreshOptions, LibraryOptions libraryOptions)
|
2014-02-04 20:19:29 +00:00
|
|
|
{
|
2017-02-21 22:12:32 +00:00
|
|
|
var isFullRefresh = refreshOptions.MetadataRefreshMode == MetadataRefreshMode.FullRefresh;
|
2015-04-20 18:04:02 +00:00
|
|
|
|
2020-06-09 22:12:53 +00:00
|
|
|
if (!video.IsLocked && !video.LockedFields.Contains(MetadataField.OfficialRating))
|
2014-02-04 20:19:29 +00:00
|
|
|
{
|
2015-04-20 18:04:02 +00:00
|
|
|
if (!string.IsNullOrWhiteSpace(data.OfficialRating) || isFullRefresh)
|
2014-02-04 20:19:29 +00:00
|
|
|
{
|
2015-04-04 19:35:29 +00:00
|
|
|
video.OfficialRating = data.OfficialRating;
|
2014-02-04 20:19:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-09 22:12:53 +00:00
|
|
|
if (!video.IsLocked && !video.LockedFields.Contains(MetadataField.Genres))
|
2015-03-05 06:34:36 +00:00
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
if (video.Genres.Length == 0 || isFullRefresh)
|
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);
|
|
|
|
}
|
2014-02-04 20:19:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-09 22:12:53 +00:00
|
|
|
if (!video.IsLocked && !video.LockedFields.Contains(MetadataField.Studios))
|
2014-02-04 20:19:29 +00:00
|
|
|
{
|
2017-08-09 19:56:38 +00:00
|
|
|
if (video.Studios.Length == 0 || isFullRefresh)
|
2014-02-04 20:19:29 +00:00
|
|
|
{
|
2017-08-09 19:56:38 +00:00
|
|
|
video.SetStudios(data.Studios);
|
2015-03-05 06:34:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-04 21:34:29 +00:00
|
|
|
if (video is MusicVideo musicVideo)
|
|
|
|
{
|
|
|
|
musicVideo.Album = data.Album;
|
|
|
|
musicVideo.Artists = data.Artists;
|
|
|
|
}
|
|
|
|
|
2015-04-04 19:35:29 +00:00
|
|
|
if (data.ProductionYear.HasValue)
|
2015-03-05 06:34:36 +00:00
|
|
|
{
|
2015-04-20 18:04:02 +00:00
|
|
|
if (!video.ProductionYear.HasValue || isFullRefresh)
|
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
2015-04-20 18:04:02 +00:00
|
|
|
if (!video.PremiereDate.HasValue || isFullRefresh)
|
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
2015-04-20 18:04:02 +00:00
|
|
|
if (!video.IndexNumber.HasValue || isFullRefresh)
|
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
2015-04-20 18:04:02 +00:00
|
|
|
if (!video.ParentIndexNumber.HasValue || isFullRefresh)
|
|
|
|
{
|
|
|
|
video.ParentIndexNumber = data.ParentIndexNumber;
|
|
|
|
}
|
2015-04-04 19:35:29 +00:00
|
|
|
}
|
2017-02-21 18:04:18 +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
|
|
|
{
|
2017-02-21 22:12:32 +00:00
|
|
|
if (!string.IsNullOrWhiteSpace(data.Name) && libraryOptions.EnableEmbeddedTitles)
|
2016-02-18 02:55:15 +00:00
|
|
|
{
|
2017-02-21 22:12:32 +00:00
|
|
|
// Don't use the embedded name for extras because it will often be the same name as the movie
|
2018-09-12 17:26:21 +00:00
|
|
|
if (!video.ExtraType.HasValue)
|
2016-05-01 20:56:26 +00:00
|
|
|
{
|
2017-02-21 22:12:32 +00:00
|
|
|
video.Name = data.Name;
|
2016-05-01 20:56:26 +00:00
|
|
|
}
|
2016-02-18 02:55:15 +00:00
|
|
|
}
|
2021-04-04 21:34:29 +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;
|
2014-02-04 20:19:29 +00:00
|
|
|
}
|
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
|
|
|
{
|
2015-04-20 18:04:02 +00:00
|
|
|
if (string.IsNullOrWhiteSpace(video.Overview) || isFullRefresh)
|
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
|
|
|
}
|
|
|
|
}
|
2014-02-04 20:19:29 +00:00
|
|
|
}
|
|
|
|
|
2017-08-27 00:32:33 +00:00
|
|
|
private void FetchPeople(Video video, Model.MediaInfo.MediaInfo data, MetadataRefreshOptions options)
|
2015-06-28 17:00:36 +00:00
|
|
|
{
|
|
|
|
var isFullRefresh = options.MetadataRefreshMode == MetadataRefreshMode.FullRefresh;
|
|
|
|
|
2020-06-09 22:12:53 +00:00
|
|
|
if (!video.IsLocked && !video.LockedFields.Contains(MetadataField.Cast))
|
2015-06-28 17:00:36 +00:00
|
|
|
{
|
|
|
|
if (isFullRefresh || _libraryManager.GetPeople(video).Count == 0)
|
|
|
|
{
|
|
|
|
var people = new List<PersonInfo>();
|
|
|
|
|
|
|
|
foreach (var person in data.People)
|
|
|
|
{
|
|
|
|
PeopleHelper.AddPerson(people, new PersonInfo
|
|
|
|
{
|
|
|
|
Name = person.Name,
|
|
|
|
Type = person.Type,
|
|
|
|
Role = person.Role
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-08-27 00:32:33 +00:00
|
|
|
_libraryManager.UpdatePeople(video, people);
|
2015-06-28 17:00:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-10 22:13:17 +00:00
|
|
|
private SubtitleOptions GetOptions()
|
|
|
|
{
|
|
|
|
return _config.GetConfiguration<SubtitleOptions>("subtitles");
|
|
|
|
}
|
|
|
|
|
2014-02-04 20:19:29 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Adds the external subtitles.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="video">The video.</param>
|
|
|
|
/// <param name="currentStreams">The current streams.</param>
|
2017-02-21 22:12:32 +00:00
|
|
|
/// <param name="options">The refreshOptions.</param>
|
2014-05-17 04:24:10 +00:00
|
|
|
/// <param name="cancellationToken">The cancellation token.</param>
|
|
|
|
/// <returns>Task.</returns>
|
2020-04-11 10:29:04 +00:00
|
|
|
private async Task AddExternalSubtitles(
|
|
|
|
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
|
|
|
{
|
2020-04-11 10:29:04 +00:00
|
|
|
var subtitleResolver = new SubtitleResolver(_localization);
|
2014-05-16 19:16:29 +00:00
|
|
|
|
2015-09-23 04:00:30 +00:00
|
|
|
var startIndex = currentStreams.Count == 0 ? 0 : (currentStreams.Select(i => i.Index).Max() + 1);
|
2017-11-05 21:51:23 +00:00
|
|
|
var externalSubtitleStreams = subtitleResolver.GetExternalSubtitleStreams(video, startIndex, options.DirectoryService, false);
|
2014-06-09 19:16:14 +00:00
|
|
|
|
2014-07-04 02:22:57 +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
|
|
|
|
2014-08-10 22:13:17 +00:00
|
|
|
var subtitleOptions = GetOptions();
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
if (libraryOptions.SubtitleDownloadLanguages == null)
|
|
|
|
{
|
|
|
|
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,
|
2021-10-19 19:06:05 +00:00
|
|
|
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)
|
|
|
|
{
|
2017-11-05 21:51:23 +00:00
|
|
|
externalSubtitleStreams = subtitleResolver.GetExternalSubtitleStreams(video, startIndex, options.DirectoryService, true);
|
2014-05-07 02:28:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
video.SubtitleFiles = externalSubtitleStreams.Select(i => i.Path).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>
|
2021-11-27 11:10:57 +00:00
|
|
|
private async Task AddExternalAudio(
|
2021-11-22 20:47:52 +00:00
|
|
|
Video video,
|
|
|
|
List<MediaStream> currentStreams,
|
|
|
|
MetadataRefreshOptions options,
|
|
|
|
CancellationToken cancellationToken)
|
|
|
|
{
|
2021-11-27 11:10:57 +00:00
|
|
|
var audioResolver = new AudioResolver();
|
2021-11-22 20:47:52 +00:00
|
|
|
|
2021-11-24 20:53:36 +00:00
|
|
|
var startIndex = currentStreams.Count == 0 ? 0 : currentStreams.Max(i => i.Index) + 1;
|
2021-11-27 11:10:57 +00:00
|
|
|
var externalAudioStreams = await audioResolver.GetExternalAudioStreams(video, startIndex, options.DirectoryService, _namingOptions, false, _localization, _mediaEncoder, cancellationToken);
|
2021-11-22 20:47:52 +00:00
|
|
|
|
2021-11-27 11:10:57 +00:00
|
|
|
video.AudioFiles = externalAudioStreams.Select(i => i.Path).Distinct().ToArray();
|
2021-11-22 20:47:52 +00:00
|
|
|
|
|
|
|
currentStreams.AddRange(externalAudioStreams);
|
|
|
|
}
|
|
|
|
|
2014-02-04 20:19:29 +00:00
|
|
|
/// <summary>
|
2020-04-01 17:05:41 +00:00
|
|
|
/// Creates dummy chapters.
|
2014-02-04 20:19:29 +00:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="video">The video.</param>
|
2020-06-19 18:24:13 +00:00
|
|
|
/// <returns>An array of dummy chapters.</returns>
|
2020-04-01 17:05:41 +00:00
|
|
|
private ChapterInfo[] CreateDummyChapters(Video video)
|
2014-02-04 20:19:29 +00:00
|
|
|
{
|
|
|
|
var runtime = video.RunTimeTicks ?? 0;
|
|
|
|
|
|
|
|
if (runtime < 0)
|
|
|
|
{
|
2020-02-23 09:53:51 +00:00
|
|
|
throw new ArgumentException(
|
|
|
|
string.Format(
|
|
|
|
CultureInfo.InvariantCulture,
|
|
|
|
"{0} has invalid runtime of {1}",
|
|
|
|
video.Name,
|
|
|
|
runtime));
|
2014-02-04 20:19:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (runtime < _dummyChapterDuration)
|
|
|
|
{
|
2020-04-01 17:05:41 +00:00
|
|
|
return Array.Empty<ChapterInfo>();
|
2014-02-04 20:19:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Limit to 100 chapters just in case there's some incorrect metadata here
|
2020-02-23 09:53:51 +00:00
|
|
|
int chapterCount = (int)Math.Min(runtime / _dummyChapterDuration, 100);
|
2020-04-01 17:05:41 +00:00
|
|
|
var chapters = new ChapterInfo[chapterCount];
|
2020-02-23 09:53:51 +00:00
|
|
|
|
|
|
|
long currentChapterTicks = 0;
|
|
|
|
for (int i = 0; i < chapterCount; i++)
|
2014-02-04 20:19:29 +00:00
|
|
|
{
|
2020-02-23 09:53:51 +00:00
|
|
|
chapters[i] = new ChapterInfo
|
2014-02-04 20:19:29 +00:00
|
|
|
{
|
|
|
|
StartPositionTicks = currentChapterTicks
|
2020-02-23 09:53:51 +00:00
|
|
|
};
|
2014-02-04 20:19:29 +00:00
|
|
|
|
|
|
|
currentChapterTicks += _dummyChapterDuration;
|
|
|
|
}
|
2020-04-01 17:05:41 +00:00
|
|
|
|
|
|
|
return chapters;
|
2014-02-04 20:19:29 +00:00
|
|
|
}
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
private string[] FetchFromDvdLib(Video item)
|
2014-02-04 20:19:29 +00:00
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
var path = item.Path;
|
2020-04-11 10:29:04 +00:00
|
|
|
var dvd = new Dvd(path);
|
2014-05-07 02:28:19 +00:00
|
|
|
|
2014-02-04 20:19:29 +00:00
|
|
|
var primaryTitle = dvd.Titles.OrderByDescending(GetRuntime).FirstOrDefault();
|
|
|
|
|
2014-02-22 22:40:44 +00:00
|
|
|
byte? titleNumber = null;
|
2014-02-04 20:19:29 +00:00
|
|
|
|
|
|
|
if (primaryTitle != null)
|
|
|
|
{
|
2014-02-22 22:40:44 +00:00
|
|
|
titleNumber = primaryTitle.VideoTitleSetNumber;
|
2014-02-22 20:20:22 +00:00
|
|
|
item.RunTimeTicks = GetRuntime(primaryTitle);
|
2014-02-04 20:19:29 +00:00
|
|
|
}
|
2017-08-27 00:32:33 +00:00
|
|
|
|
2020-12-03 14:34:15 +00:00
|
|
|
return _mediaEncoder.GetPrimaryPlaylistVobFiles(item.Path, titleNumber)
|
2014-02-04 20:19:29 +00:00
|
|
|
.Select(Path.GetFileName)
|
2017-08-19 19:43:35 +00:00
|
|
|
.ToArray();
|
2014-02-04 20:19:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private long GetRuntime(Title title)
|
|
|
|
{
|
|
|
|
return title.ProgramChains
|
|
|
|
.Select(i => (TimeSpan)i.PlaybackTime)
|
|
|
|
.Select(i => i.Ticks)
|
|
|
|
.Sum();
|
|
|
|
}
|
|
|
|
}
|
2018-12-13 13:18:25 +00:00
|
|
|
}
|