| | | 1 | | #pragma warning disable CA1068, CS1591 |
| | | 2 | | |
| | | 3 | | using System; |
| | | 4 | | using System.Collections.Generic; |
| | | 5 | | using System.Globalization; |
| | | 6 | | using System.Linq; |
| | | 7 | | using System.Threading; |
| | | 8 | | using System.Threading.Tasks; |
| | | 9 | | using Jellyfin.Data.Enums; |
| | | 10 | | using Jellyfin.Extensions; |
| | | 11 | | using MediaBrowser.Common.Configuration; |
| | | 12 | | using MediaBrowser.Controller.Chapters; |
| | | 13 | | using MediaBrowser.Controller.Configuration; |
| | | 14 | | using MediaBrowser.Controller.Entities; |
| | | 15 | | using MediaBrowser.Controller.Entities.Movies; |
| | | 16 | | using MediaBrowser.Controller.Entities.TV; |
| | | 17 | | using MediaBrowser.Controller.Library; |
| | | 18 | | using MediaBrowser.Controller.MediaEncoding; |
| | | 19 | | using MediaBrowser.Controller.Persistence; |
| | | 20 | | using MediaBrowser.Controller.Providers; |
| | | 21 | | using MediaBrowser.Controller.Subtitles; |
| | | 22 | | using MediaBrowser.Model.Configuration; |
| | | 23 | | using MediaBrowser.Model.Dlna; |
| | | 24 | | using MediaBrowser.Model.Dto; |
| | | 25 | | using MediaBrowser.Model.Entities; |
| | | 26 | | using MediaBrowser.Model.Globalization; |
| | | 27 | | using MediaBrowser.Model.MediaInfo; |
| | | 28 | | using MediaBrowser.Model.Providers; |
| | | 29 | | using Microsoft.Extensions.Logging; |
| | | 30 | | |
| | | 31 | | namespace MediaBrowser.Providers.MediaInfo |
| | | 32 | | { |
| | | 33 | | public class FFProbeVideoInfo |
| | | 34 | | { |
| | | 35 | | private readonly ILogger<FFProbeVideoInfo> _logger; |
| | | 36 | | private readonly IMediaSourceManager _mediaSourceManager; |
| | | 37 | | private readonly IMediaEncoder _mediaEncoder; |
| | | 38 | | private readonly IBlurayExaminer _blurayExaminer; |
| | | 39 | | private readonly ILocalizationManager _localization; |
| | | 40 | | private readonly IChapterManager _chapterManager; |
| | | 41 | | private readonly IServerConfigurationManager _config; |
| | | 42 | | private readonly ISubtitleManager _subtitleManager; |
| | | 43 | | private readonly ILibraryManager _libraryManager; |
| | | 44 | | private readonly AudioResolver _audioResolver; |
| | | 45 | | private readonly SubtitleResolver _subtitleResolver; |
| | | 46 | | private readonly IMediaAttachmentRepository _mediaAttachmentRepository; |
| | | 47 | | private readonly IMediaStreamRepository _mediaStreamRepository; |
| | | 48 | | |
| | | 49 | | public FFProbeVideoInfo( |
| | | 50 | | ILogger<FFProbeVideoInfo> logger, |
| | | 51 | | IMediaSourceManager mediaSourceManager, |
| | | 52 | | IMediaEncoder mediaEncoder, |
| | | 53 | | IBlurayExaminer blurayExaminer, |
| | | 54 | | ILocalizationManager localization, |
| | | 55 | | IChapterManager chapterManager, |
| | | 56 | | IServerConfigurationManager config, |
| | | 57 | | ISubtitleManager subtitleManager, |
| | | 58 | | ILibraryManager libraryManager, |
| | | 59 | | AudioResolver audioResolver, |
| | | 60 | | SubtitleResolver subtitleResolver, |
| | | 61 | | IMediaAttachmentRepository mediaAttachmentRepository, |
| | | 62 | | IMediaStreamRepository mediaStreamRepository) |
| | | 63 | | { |
| | 30 | 64 | | _logger = logger; |
| | 30 | 65 | | _mediaSourceManager = mediaSourceManager; |
| | 30 | 66 | | _mediaEncoder = mediaEncoder; |
| | 30 | 67 | | _blurayExaminer = blurayExaminer; |
| | 30 | 68 | | _localization = localization; |
| | 30 | 69 | | _chapterManager = chapterManager; |
| | 30 | 70 | | _config = config; |
| | 30 | 71 | | _subtitleManager = subtitleManager; |
| | 30 | 72 | | _libraryManager = libraryManager; |
| | 30 | 73 | | _audioResolver = audioResolver; |
| | 30 | 74 | | _subtitleResolver = subtitleResolver; |
| | 30 | 75 | | _mediaAttachmentRepository = mediaAttachmentRepository; |
| | 30 | 76 | | _mediaStreamRepository = mediaStreamRepository; |
| | 30 | 77 | | _mediaStreamRepository = mediaStreamRepository; |
| | 30 | 78 | | } |
| | | 79 | | |
| | | 80 | | public async Task<ItemUpdateType> ProbeVideo<T>( |
| | | 81 | | T item, |
| | | 82 | | MetadataRefreshOptions options, |
| | | 83 | | CancellationToken cancellationToken) |
| | | 84 | | where T : Video |
| | | 85 | | { |
| | | 86 | | BlurayDiscInfo? blurayDiscInfo = null; |
| | | 87 | | |
| | | 88 | | Model.MediaInfo.MediaInfo? mediaInfoResult = null; |
| | | 89 | | |
| | | 90 | | if (!item.IsShortcut || options.EnableRemoteContentProbe) |
| | | 91 | | { |
| | | 92 | | if (item.VideoType == VideoType.Dvd) |
| | | 93 | | { |
| | | 94 | | // Get list of playable .vob files |
| | | 95 | | var vobs = _mediaEncoder.GetPrimaryPlaylistVobFiles(item.Path, null); |
| | | 96 | | |
| | | 97 | | // Return if no playable .vob files are found |
| | | 98 | | if (vobs.Count == 0) |
| | | 99 | | { |
| | | 100 | | _logger.LogError("No playable .vob files found in DVD structure, skipping FFprobe."); |
| | | 101 | | return ItemUpdateType.MetadataImport; |
| | | 102 | | } |
| | | 103 | | |
| | | 104 | | // Fetch metadata of first .vob file |
| | | 105 | | mediaInfoResult = await GetMediaInfo( |
| | | 106 | | new Video |
| | | 107 | | { |
| | | 108 | | Path = vobs[0] |
| | | 109 | | }, |
| | | 110 | | cancellationToken).ConfigureAwait(false); |
| | | 111 | | |
| | | 112 | | // Sum up the runtime of all .vob files skipping the first .vob |
| | | 113 | | for (var i = 1; i < vobs.Count; i++) |
| | | 114 | | { |
| | | 115 | | var tmpMediaInfo = await GetMediaInfo( |
| | | 116 | | new Video |
| | | 117 | | { |
| | | 118 | | Path = vobs[i] |
| | | 119 | | }, |
| | | 120 | | cancellationToken).ConfigureAwait(false); |
| | | 121 | | |
| | | 122 | | mediaInfoResult.RunTimeTicks += tmpMediaInfo.RunTimeTicks; |
| | | 123 | | } |
| | | 124 | | } |
| | | 125 | | else if (item.VideoType == VideoType.BluRay) |
| | | 126 | | { |
| | | 127 | | // Get BD disc information |
| | | 128 | | blurayDiscInfo = GetBDInfo(item.Path); |
| | | 129 | | |
| | | 130 | | // Return if no playable .m2ts files are found |
| | | 131 | | if (blurayDiscInfo is null || blurayDiscInfo.Files.Length == 0) |
| | | 132 | | { |
| | | 133 | | _logger.LogError("No playable .m2ts files found in Blu-ray structure, skipping FFprobe."); |
| | | 134 | | return ItemUpdateType.MetadataImport; |
| | | 135 | | } |
| | | 136 | | |
| | | 137 | | // Fetch metadata of first .m2ts file |
| | | 138 | | mediaInfoResult = await GetMediaInfo( |
| | | 139 | | new Video |
| | | 140 | | { |
| | | 141 | | Path = blurayDiscInfo.Files[0] |
| | | 142 | | }, |
| | | 143 | | cancellationToken).ConfigureAwait(false); |
| | | 144 | | } |
| | | 145 | | else |
| | | 146 | | { |
| | | 147 | | mediaInfoResult = await GetMediaInfo(item, cancellationToken).ConfigureAwait(false); |
| | | 148 | | } |
| | | 149 | | |
| | | 150 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 151 | | } |
| | | 152 | | |
| | | 153 | | await Fetch(item, cancellationToken, mediaInfoResult, blurayDiscInfo, options).ConfigureAwait(false); |
| | | 154 | | |
| | | 155 | | return ItemUpdateType.MetadataImport; |
| | | 156 | | } |
| | | 157 | | |
| | | 158 | | private Task<Model.MediaInfo.MediaInfo> GetMediaInfo( |
| | | 159 | | Video item, |
| | | 160 | | CancellationToken cancellationToken) |
| | | 161 | | { |
| | 0 | 162 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 163 | | |
| | 0 | 164 | | var path = item.Path; |
| | 0 | 165 | | var protocol = item.PathProtocol ?? MediaProtocol.File; |
| | | 166 | | |
| | 0 | 167 | | if (item.IsShortcut) |
| | | 168 | | { |
| | 0 | 169 | | path = item.ShortcutPath; |
| | 0 | 170 | | protocol = _mediaSourceManager.GetPathProtocol(path); |
| | | 171 | | } |
| | | 172 | | |
| | 0 | 173 | | return _mediaEncoder.GetMediaInfo( |
| | 0 | 174 | | new MediaInfoRequest |
| | 0 | 175 | | { |
| | 0 | 176 | | ExtractChapters = true, |
| | 0 | 177 | | MediaType = DlnaProfileType.Video, |
| | 0 | 178 | | MediaSource = new MediaSourceInfo |
| | 0 | 179 | | { |
| | 0 | 180 | | Path = path, |
| | 0 | 181 | | Protocol = protocol, |
| | 0 | 182 | | VideoType = item.VideoType, |
| | 0 | 183 | | IsoType = item.IsoType |
| | 0 | 184 | | } |
| | 0 | 185 | | }, |
| | 0 | 186 | | cancellationToken); |
| | | 187 | | } |
| | | 188 | | |
| | | 189 | | protected async Task Fetch( |
| | | 190 | | Video video, |
| | | 191 | | CancellationToken cancellationToken, |
| | | 192 | | Model.MediaInfo.MediaInfo? mediaInfo, |
| | | 193 | | BlurayDiscInfo? blurayInfo, |
| | | 194 | | MetadataRefreshOptions options) |
| | | 195 | | { |
| | | 196 | | List<MediaStream> mediaStreams = new List<MediaStream>(); |
| | | 197 | | IReadOnlyList<MediaAttachment> mediaAttachments; |
| | | 198 | | ChapterInfo[] chapters; |
| | | 199 | | |
| | | 200 | | // Add external streams before adding the streams from the file to preserve stream IDs on remote videos |
| | | 201 | | await AddExternalSubtitlesAsync(video, mediaStreams, options, cancellationToken).ConfigureAwait(false); |
| | | 202 | | |
| | | 203 | | await AddExternalAudioAsync(video, mediaStreams, options, cancellationToken).ConfigureAwait(false); |
| | | 204 | | |
| | | 205 | | var startIndex = mediaStreams.Count == 0 ? 0 : (mediaStreams.Max(i => i.Index) + 1); |
| | | 206 | | |
| | | 207 | | if (mediaInfo is not null) |
| | | 208 | | { |
| | | 209 | | foreach (var mediaStream in mediaInfo.MediaStreams) |
| | | 210 | | { |
| | | 211 | | mediaStream.Index = startIndex++; |
| | | 212 | | mediaStreams.Add(mediaStream); |
| | | 213 | | } |
| | | 214 | | |
| | | 215 | | mediaAttachments = mediaInfo.MediaAttachments; |
| | | 216 | | video.TotalBitrate = mediaInfo.Bitrate; |
| | | 217 | | video.RunTimeTicks = mediaInfo.RunTimeTicks; |
| | | 218 | | video.Container = mediaInfo.Container; |
| | | 219 | | var videoType = video.VideoType; |
| | | 220 | | if (videoType == VideoType.BluRay || videoType == VideoType.Dvd) |
| | | 221 | | { |
| | | 222 | | video.Size = mediaInfo.Size; |
| | | 223 | | } |
| | | 224 | | |
| | | 225 | | chapters = mediaInfo.Chapters ?? []; |
| | | 226 | | if (blurayInfo is not null) |
| | | 227 | | { |
| | | 228 | | FetchBdInfo(video, ref chapters, mediaStreams, blurayInfo); |
| | | 229 | | } |
| | | 230 | | } |
| | | 231 | | else |
| | | 232 | | { |
| | | 233 | | foreach (var mediaStream in video.GetMediaStreams()) |
| | | 234 | | { |
| | | 235 | | if (!mediaStream.IsExternal) |
| | | 236 | | { |
| | | 237 | | mediaStream.Index = startIndex++; |
| | | 238 | | mediaStreams.Add(mediaStream); |
| | | 239 | | } |
| | | 240 | | } |
| | | 241 | | |
| | | 242 | | mediaAttachments = []; |
| | | 243 | | chapters = []; |
| | | 244 | | } |
| | | 245 | | |
| | | 246 | | var libraryOptions = _libraryManager.GetLibraryOptions(video); |
| | | 247 | | |
| | | 248 | | if (mediaInfo is not null) |
| | | 249 | | { |
| | | 250 | | FetchEmbeddedInfo(video, mediaInfo, options, libraryOptions); |
| | | 251 | | FetchPeople(video, mediaInfo, options); |
| | | 252 | | video.Timestamp = mediaInfo.Timestamp; |
| | | 253 | | video.Video3DFormat ??= mediaInfo.Video3DFormat; |
| | | 254 | | } |
| | | 255 | | |
| | | 256 | | if (libraryOptions.AllowEmbeddedSubtitles == EmbeddedSubtitleOptions.AllowText || libraryOptions.AllowEmbedd |
| | | 257 | | { |
| | | 258 | | _logger.LogDebug("Disabling embedded image subtitles for {Path} due to DisableEmbeddedImageSubtitles set |
| | | 259 | | mediaStreams.RemoveAll(i => i.Type == MediaStreamType.Subtitle && !i.IsExternal && !i.IsTextSubtitleStre |
| | | 260 | | } |
| | | 261 | | |
| | | 262 | | if (libraryOptions.AllowEmbeddedSubtitles == EmbeddedSubtitleOptions.AllowImage || libraryOptions.AllowEmbed |
| | | 263 | | { |
| | | 264 | | _logger.LogDebug("Disabling embedded text subtitles for {Path} due to DisableEmbeddedTextSubtitles setti |
| | | 265 | | mediaStreams.RemoveAll(i => i.Type == MediaStreamType.Subtitle && !i.IsExternal && i.IsTextSubtitleStrea |
| | | 266 | | } |
| | | 267 | | |
| | | 268 | | var videoStream = mediaStreams.FirstOrDefault(i => i.Type == MediaStreamType.Video); |
| | | 269 | | |
| | | 270 | | video.Height = videoStream?.Height ?? 0; |
| | | 271 | | video.Width = videoStream?.Width ?? 0; |
| | | 272 | | |
| | | 273 | | video.DefaultVideoStreamIndex = videoStream?.Index; |
| | | 274 | | |
| | | 275 | | video.HasSubtitles = mediaStreams.Any(i => i.Type == MediaStreamType.Subtitle); |
| | | 276 | | |
| | | 277 | | _mediaStreamRepository.SaveMediaStreams(video.Id, mediaStreams, cancellationToken); |
| | | 278 | | |
| | | 279 | | _mediaAttachmentRepository.SaveMediaAttachments(video.Id, mediaAttachments, cancellationToken); |
| | | 280 | | |
| | | 281 | | if (options.MetadataRefreshMode == MetadataRefreshMode.FullRefresh |
| | | 282 | | || options.MetadataRefreshMode == MetadataRefreshMode.Default) |
| | | 283 | | { |
| | | 284 | | if (_config.Configuration.DummyChapterDuration > 0 && chapters.Length == 0 && mediaStreams.Any(i => i.Ty |
| | | 285 | | { |
| | | 286 | | chapters = CreateDummyChapters(video); |
| | | 287 | | } |
| | | 288 | | |
| | | 289 | | NormalizeChapterNames(chapters); |
| | | 290 | | |
| | | 291 | | var extractDuringScan = false; |
| | | 292 | | if (libraryOptions is not null) |
| | | 293 | | { |
| | | 294 | | extractDuringScan = libraryOptions.ExtractChapterImagesDuringLibraryScan; |
| | | 295 | | } |
| | | 296 | | |
| | | 297 | | await _chapterManager.RefreshChapterImages(video, options.DirectoryService, chapters, extractDuringScan, |
| | | 298 | | |
| | | 299 | | _chapterManager.SaveChapters(video, chapters); |
| | | 300 | | } |
| | | 301 | | } |
| | | 302 | | |
| | | 303 | | private void NormalizeChapterNames(ChapterInfo[] chapters) |
| | | 304 | | { |
| | 0 | 305 | | for (int i = 0; i < chapters.Length; i++) |
| | | 306 | | { |
| | 0 | 307 | | string? name = chapters[i].Name; |
| | | 308 | | // Check if the name is empty and/or if the name is a time |
| | | 309 | | // Some ripping programs do that. |
| | 0 | 310 | | if (string.IsNullOrWhiteSpace(name) |
| | 0 | 311 | | || TimeSpan.TryParse(name, out _)) |
| | | 312 | | { |
| | 0 | 313 | | chapters[i].Name = string.Format( |
| | 0 | 314 | | CultureInfo.InvariantCulture, |
| | 0 | 315 | | _localization.GetLocalizedString("ChapterNameValue"), |
| | 0 | 316 | | (i + 1).ToString(CultureInfo.InvariantCulture)); |
| | | 317 | | } |
| | | 318 | | } |
| | 0 | 319 | | } |
| | | 320 | | |
| | | 321 | | private void FetchBdInfo(Video video, ref ChapterInfo[] chapters, List<MediaStream> mediaStreams, BlurayDiscInfo |
| | | 322 | | { |
| | 0 | 323 | | var ffmpegVideoStream = mediaStreams.FirstOrDefault(s => s.Type == MediaStreamType.Video); |
| | 0 | 324 | | var externalStreams = mediaStreams.Where(s => s.IsExternal).ToList(); |
| | | 325 | | |
| | | 326 | | // Fill video properties from the BDInfo result |
| | 0 | 327 | | mediaStreams.Clear(); |
| | | 328 | | |
| | | 329 | | // Rebuild the list with external streams first |
| | 0 | 330 | | int index = 0; |
| | 0 | 331 | | foreach (var stream in externalStreams.Concat(blurayInfo.MediaStreams)) |
| | | 332 | | { |
| | 0 | 333 | | stream.Index = index++; |
| | 0 | 334 | | mediaStreams.Add(stream); |
| | | 335 | | } |
| | | 336 | | |
| | 0 | 337 | | if (blurayInfo.RunTimeTicks.HasValue && blurayInfo.RunTimeTicks.Value > 0) |
| | | 338 | | { |
| | 0 | 339 | | video.RunTimeTicks = blurayInfo.RunTimeTicks; |
| | | 340 | | } |
| | | 341 | | |
| | 0 | 342 | | if (blurayInfo.Chapters is not null) |
| | | 343 | | { |
| | 0 | 344 | | double[] brChapter = blurayInfo.Chapters; |
| | 0 | 345 | | chapters = new ChapterInfo[brChapter.Length]; |
| | 0 | 346 | | for (int i = 0; i < brChapter.Length; i++) |
| | | 347 | | { |
| | 0 | 348 | | chapters[i] = new ChapterInfo |
| | 0 | 349 | | { |
| | 0 | 350 | | StartPositionTicks = TimeSpan.FromSeconds(brChapter[i]).Ticks |
| | 0 | 351 | | }; |
| | | 352 | | } |
| | | 353 | | } |
| | | 354 | | |
| | 0 | 355 | | var blurayVideoStream = mediaStreams.FirstOrDefault(s => s.Type == MediaStreamType.Video); |
| | | 356 | | |
| | | 357 | | // Use the ffprobe values if these are empty |
| | 0 | 358 | | if (blurayVideoStream is not null && ffmpegVideoStream is not null) |
| | | 359 | | { |
| | | 360 | | // Always use ffmpeg's detected codec since that is what the rest of the codebase expects. |
| | 0 | 361 | | blurayVideoStream.Codec = ffmpegVideoStream.Codec; |
| | 0 | 362 | | blurayVideoStream.BitRate = blurayVideoStream.BitRate.GetValueOrDefault() == 0 ? ffmpegVideoStream.BitRa |
| | 0 | 363 | | blurayVideoStream.Width = blurayVideoStream.Width.GetValueOrDefault() == 0 ? ffmpegVideoStream.Width : b |
| | 0 | 364 | | blurayVideoStream.Height = blurayVideoStream.Height.GetValueOrDefault() == 0 ? ffmpegVideoStream.Height |
| | 0 | 365 | | blurayVideoStream.ColorRange = ffmpegVideoStream.ColorRange; |
| | 0 | 366 | | blurayVideoStream.ColorSpace = ffmpegVideoStream.ColorSpace; |
| | 0 | 367 | | blurayVideoStream.ColorTransfer = ffmpegVideoStream.ColorTransfer; |
| | 0 | 368 | | blurayVideoStream.ColorPrimaries = ffmpegVideoStream.ColorPrimaries; |
| | | 369 | | } |
| | 0 | 370 | | } |
| | | 371 | | |
| | | 372 | | /// <summary> |
| | | 373 | | /// Gets information about the longest playlist on a bdrom. |
| | | 374 | | /// </summary> |
| | | 375 | | /// <param name="path">The path.</param> |
| | | 376 | | /// <returns>VideoStream.</returns> |
| | | 377 | | private BlurayDiscInfo? GetBDInfo(string path) |
| | | 378 | | { |
| | 0 | 379 | | ArgumentException.ThrowIfNullOrEmpty(path); |
| | | 380 | | |
| | | 381 | | try |
| | | 382 | | { |
| | 0 | 383 | | return _blurayExaminer.GetDiscInfo(path); |
| | | 384 | | } |
| | 0 | 385 | | catch (Exception ex) |
| | | 386 | | { |
| | 0 | 387 | | _logger.LogError(ex, "Error getting BDInfo"); |
| | 0 | 388 | | return null; |
| | | 389 | | } |
| | 0 | 390 | | } |
| | | 391 | | |
| | | 392 | | private void FetchEmbeddedInfo(Video video, Model.MediaInfo.MediaInfo data, MetadataRefreshOptions refreshOption |
| | | 393 | | { |
| | 0 | 394 | | var replaceData = refreshOptions.ReplaceAllMetadata; |
| | | 395 | | |
| | 0 | 396 | | if (!video.IsLocked && !video.LockedFields.Contains(MetadataField.OfficialRating)) |
| | | 397 | | { |
| | 0 | 398 | | if (string.IsNullOrWhiteSpace(video.OfficialRating) || replaceData) |
| | | 399 | | { |
| | 0 | 400 | | video.OfficialRating = data.OfficialRating; |
| | | 401 | | } |
| | | 402 | | } |
| | | 403 | | |
| | 0 | 404 | | if (!video.IsLocked && !video.LockedFields.Contains(MetadataField.Genres)) |
| | | 405 | | { |
| | 0 | 406 | | if (video.Genres.Length == 0 || replaceData) |
| | | 407 | | { |
| | 0 | 408 | | video.Genres = []; |
| | | 409 | | |
| | 0 | 410 | | foreach (var genre in data.Genres.Trimmed()) |
| | | 411 | | { |
| | 0 | 412 | | video.AddGenre(genre); |
| | | 413 | | } |
| | | 414 | | } |
| | | 415 | | } |
| | | 416 | | |
| | 0 | 417 | | if (!video.IsLocked && !video.LockedFields.Contains(MetadataField.Studios)) |
| | | 418 | | { |
| | 0 | 419 | | if (video.Studios.Length == 0 || replaceData) |
| | | 420 | | { |
| | 0 | 421 | | video.SetStudios(data.Studios); |
| | | 422 | | } |
| | | 423 | | } |
| | | 424 | | |
| | 0 | 425 | | if (!video.IsLocked && video is MusicVideo musicVideo) |
| | | 426 | | { |
| | 0 | 427 | | if (string.IsNullOrEmpty(musicVideo.Album) || replaceData) |
| | | 428 | | { |
| | 0 | 429 | | musicVideo.Album = data.Album; |
| | | 430 | | } |
| | | 431 | | |
| | 0 | 432 | | if (musicVideo.Artists.Count == 0 || replaceData) |
| | | 433 | | { |
| | 0 | 434 | | musicVideo.Artists = data.Artists; |
| | | 435 | | } |
| | | 436 | | } |
| | | 437 | | |
| | 0 | 438 | | if (data.ProductionYear.HasValue) |
| | | 439 | | { |
| | 0 | 440 | | if (!video.ProductionYear.HasValue || replaceData) |
| | | 441 | | { |
| | 0 | 442 | | video.ProductionYear = data.ProductionYear; |
| | | 443 | | } |
| | | 444 | | } |
| | | 445 | | |
| | 0 | 446 | | if (data.PremiereDate.HasValue) |
| | | 447 | | { |
| | 0 | 448 | | if (!video.PremiereDate.HasValue || replaceData) |
| | | 449 | | { |
| | 0 | 450 | | video.PremiereDate = data.PremiereDate; |
| | | 451 | | } |
| | | 452 | | } |
| | | 453 | | |
| | 0 | 454 | | if (data.IndexNumber.HasValue) |
| | | 455 | | { |
| | 0 | 456 | | if (!video.IndexNumber.HasValue || replaceData) |
| | | 457 | | { |
| | 0 | 458 | | video.IndexNumber = data.IndexNumber; |
| | | 459 | | } |
| | | 460 | | } |
| | | 461 | | |
| | 0 | 462 | | if (data.ParentIndexNumber.HasValue) |
| | | 463 | | { |
| | 0 | 464 | | if (!video.ParentIndexNumber.HasValue || replaceData) |
| | | 465 | | { |
| | 0 | 466 | | video.ParentIndexNumber = data.ParentIndexNumber; |
| | | 467 | | } |
| | | 468 | | } |
| | | 469 | | |
| | 0 | 470 | | if (!video.IsLocked && !video.LockedFields.Contains(MetadataField.Name)) |
| | | 471 | | { |
| | 0 | 472 | | if (!string.IsNullOrWhiteSpace(data.Name) && libraryOptions.EnableEmbeddedTitles) |
| | | 473 | | { |
| | | 474 | | // Separate option to use the embedded name for extras because it will often be the same name as the |
| | 0 | 475 | | if (!video.ExtraType.HasValue || libraryOptions.EnableEmbeddedExtrasTitles) |
| | | 476 | | { |
| | 0 | 477 | | video.Name = data.Name; |
| | | 478 | | } |
| | | 479 | | } |
| | | 480 | | |
| | 0 | 481 | | if (!string.IsNullOrWhiteSpace(data.ForcedSortName)) |
| | | 482 | | { |
| | 0 | 483 | | video.ForcedSortName = data.ForcedSortName; |
| | | 484 | | } |
| | | 485 | | } |
| | | 486 | | |
| | | 487 | | // If we don't have a ProductionYear try and get it from PremiereDate |
| | 0 | 488 | | if (video.PremiereDate.HasValue && !video.ProductionYear.HasValue) |
| | | 489 | | { |
| | 0 | 490 | | video.ProductionYear = video.PremiereDate.Value.ToLocalTime().Year; |
| | | 491 | | } |
| | | 492 | | |
| | 0 | 493 | | if (!video.IsLocked && !video.LockedFields.Contains(MetadataField.Overview)) |
| | | 494 | | { |
| | 0 | 495 | | if (string.IsNullOrWhiteSpace(video.Overview) || replaceData) |
| | | 496 | | { |
| | 0 | 497 | | video.Overview = data.Overview; |
| | | 498 | | } |
| | | 499 | | } |
| | 0 | 500 | | } |
| | | 501 | | |
| | | 502 | | private void FetchPeople(Video video, Model.MediaInfo.MediaInfo data, MetadataRefreshOptions options) |
| | | 503 | | { |
| | 0 | 504 | | if (video.IsLocked |
| | 0 | 505 | | || video.LockedFields.Contains(MetadataField.Cast) |
| | 0 | 506 | | || data.People.Length == 0) |
| | | 507 | | { |
| | 0 | 508 | | return; |
| | | 509 | | } |
| | | 510 | | |
| | 0 | 511 | | if (options.ReplaceAllMetadata || _libraryManager.GetPeople(video).Count == 0) |
| | | 512 | | { |
| | 0 | 513 | | var people = new List<PersonInfo>(); |
| | | 514 | | |
| | 0 | 515 | | foreach (var person in data.People) |
| | | 516 | | { |
| | 0 | 517 | | if (!string.IsNullOrWhiteSpace(person.Name)) |
| | | 518 | | { |
| | 0 | 519 | | PeopleHelper.AddPerson(people, new PersonInfo |
| | 0 | 520 | | { |
| | 0 | 521 | | Name = person.Name, |
| | 0 | 522 | | Type = person.Type, |
| | 0 | 523 | | Role = person.Role.Trim() |
| | 0 | 524 | | }); |
| | | 525 | | } |
| | | 526 | | } |
| | | 527 | | |
| | 0 | 528 | | _libraryManager.UpdatePeople(video, people); |
| | | 529 | | } |
| | 0 | 530 | | } |
| | | 531 | | |
| | | 532 | | /// <summary> |
| | | 533 | | /// Adds the external subtitles. |
| | | 534 | | /// </summary> |
| | | 535 | | /// <param name="video">The video.</param> |
| | | 536 | | /// <param name="currentStreams">The current streams.</param> |
| | | 537 | | /// <param name="options">The refreshOptions.</param> |
| | | 538 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | | 539 | | /// <returns>Task.</returns> |
| | | 540 | | private async Task AddExternalSubtitlesAsync( |
| | | 541 | | Video video, |
| | | 542 | | List<MediaStream> currentStreams, |
| | | 543 | | MetadataRefreshOptions options, |
| | | 544 | | CancellationToken cancellationToken) |
| | | 545 | | { |
| | | 546 | | var startIndex = currentStreams.Count == 0 ? 0 : (currentStreams.Select(i => i.Index).Max() + 1); |
| | | 547 | | var externalSubtitleStreams = await _subtitleResolver.GetExternalStreamsAsync(video, startIndex, options.Dir |
| | | 548 | | |
| | | 549 | | var enableSubtitleDownloading = options.MetadataRefreshMode == MetadataRefreshMode.Default || |
| | | 550 | | options.MetadataRefreshMode == MetadataRefreshMode.FullRefresh; |
| | | 551 | | |
| | | 552 | | var subtitleOptions = _config.GetConfiguration<SubtitleOptions>("subtitles"); |
| | | 553 | | |
| | | 554 | | var libraryOptions = _libraryManager.GetLibraryOptions(video); |
| | | 555 | | |
| | | 556 | | string[] subtitleDownloadLanguages; |
| | | 557 | | bool skipIfEmbeddedSubtitlesPresent; |
| | | 558 | | bool skipIfAudioTrackMatches; |
| | | 559 | | bool requirePerfectMatch; |
| | | 560 | | bool enabled; |
| | | 561 | | |
| | | 562 | | if (libraryOptions.SubtitleDownloadLanguages is null) |
| | | 563 | | { |
| | | 564 | | subtitleDownloadLanguages = subtitleOptions.DownloadLanguages; |
| | | 565 | | skipIfEmbeddedSubtitlesPresent = subtitleOptions.SkipIfEmbeddedSubtitlesPresent; |
| | | 566 | | skipIfAudioTrackMatches = subtitleOptions.SkipIfAudioTrackMatches; |
| | | 567 | | requirePerfectMatch = subtitleOptions.RequirePerfectMatch; |
| | | 568 | | enabled = (subtitleOptions.DownloadEpisodeSubtitles && |
| | | 569 | | video is Episode) || |
| | | 570 | | (subtitleOptions.DownloadMovieSubtitles && |
| | | 571 | | video is Movie); |
| | | 572 | | } |
| | | 573 | | else |
| | | 574 | | { |
| | | 575 | | subtitleDownloadLanguages = libraryOptions.SubtitleDownloadLanguages; |
| | | 576 | | skipIfEmbeddedSubtitlesPresent = libraryOptions.SkipSubtitlesIfEmbeddedSubtitlesPresent; |
| | | 577 | | skipIfAudioTrackMatches = libraryOptions.SkipSubtitlesIfAudioTrackMatches; |
| | | 578 | | requirePerfectMatch = libraryOptions.RequirePerfectSubtitleMatch; |
| | | 579 | | enabled = true; |
| | | 580 | | } |
| | | 581 | | |
| | | 582 | | if (enableSubtitleDownloading && enabled) |
| | | 583 | | { |
| | | 584 | | var downloadedLanguages = await new SubtitleDownloader( |
| | | 585 | | _logger, |
| | | 586 | | _subtitleManager).DownloadSubtitles( |
| | | 587 | | video, |
| | | 588 | | currentStreams.Concat(externalSubtitleStreams).ToList(), |
| | | 589 | | skipIfEmbeddedSubtitlesPresent, |
| | | 590 | | skipIfAudioTrackMatches, |
| | | 591 | | requirePerfectMatch, |
| | | 592 | | subtitleDownloadLanguages, |
| | | 593 | | libraryOptions.DisabledSubtitleFetchers, |
| | | 594 | | libraryOptions.SubtitleFetcherOrder, |
| | | 595 | | true, |
| | | 596 | | cancellationToken).ConfigureAwait(false); |
| | | 597 | | |
| | | 598 | | // Rescan |
| | | 599 | | if (downloadedLanguages.Count > 0) |
| | | 600 | | { |
| | | 601 | | externalSubtitleStreams = await _subtitleResolver.GetExternalStreamsAsync(video, startIndex, options |
| | | 602 | | } |
| | | 603 | | } |
| | | 604 | | |
| | | 605 | | video.SubtitleFiles = externalSubtitleStreams.Select(i => i.Path).Distinct().ToArray(); |
| | | 606 | | |
| | | 607 | | currentStreams.AddRange(externalSubtitleStreams); |
| | | 608 | | } |
| | | 609 | | |
| | | 610 | | /// <summary> |
| | | 611 | | /// Adds the external audio. |
| | | 612 | | /// </summary> |
| | | 613 | | /// <param name="video">The video.</param> |
| | | 614 | | /// <param name="currentStreams">The current streams.</param> |
| | | 615 | | /// <param name="options">The refreshOptions.</param> |
| | | 616 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | | 617 | | private async Task AddExternalAudioAsync( |
| | | 618 | | Video video, |
| | | 619 | | List<MediaStream> currentStreams, |
| | | 620 | | MetadataRefreshOptions options, |
| | | 621 | | CancellationToken cancellationToken) |
| | | 622 | | { |
| | | 623 | | var startIndex = currentStreams.Count == 0 ? 0 : currentStreams.Max(i => i.Index) + 1; |
| | | 624 | | var externalAudioStreams = await _audioResolver.GetExternalStreamsAsync(video, startIndex, options.Directory |
| | | 625 | | |
| | | 626 | | video.AudioFiles = externalAudioStreams.Select(i => i.Path).Distinct().ToArray(); |
| | | 627 | | |
| | | 628 | | currentStreams.AddRange(externalAudioStreams); |
| | | 629 | | } |
| | | 630 | | |
| | | 631 | | /// <summary> |
| | | 632 | | /// Creates dummy chapters. |
| | | 633 | | /// </summary> |
| | | 634 | | /// <param name="video">The video.</param> |
| | | 635 | | /// <returns>An array of dummy chapters.</returns> |
| | | 636 | | internal ChapterInfo[] CreateDummyChapters(Video video) |
| | | 637 | | { |
| | 9 | 638 | | var runtime = video.RunTimeTicks.GetValueOrDefault(); |
| | | 639 | | |
| | | 640 | | // Only process files with a runtime greater than 0 and less than 12h. The latter are likely corrupted. |
| | 9 | 641 | | if (runtime < 0 || runtime > TimeSpan.FromHours(12).Ticks) |
| | | 642 | | { |
| | 3 | 643 | | throw new ArgumentException( |
| | 3 | 644 | | string.Format( |
| | 3 | 645 | | CultureInfo.InvariantCulture, |
| | 3 | 646 | | "{0} has an invalid runtime of {1} minutes", |
| | 3 | 647 | | video.Name, |
| | 3 | 648 | | TimeSpan.FromTicks(runtime).TotalMinutes)); |
| | | 649 | | } |
| | | 650 | | |
| | 6 | 651 | | long dummyChapterDuration = TimeSpan.FromSeconds(_config.Configuration.DummyChapterDuration).Ticks; |
| | 6 | 652 | | if (runtime <= dummyChapterDuration) |
| | | 653 | | { |
| | 4 | 654 | | return []; |
| | | 655 | | } |
| | | 656 | | |
| | 2 | 657 | | int chapterCount = (int)(runtime / dummyChapterDuration); |
| | 2 | 658 | | var chapters = new ChapterInfo[chapterCount]; |
| | | 659 | | |
| | 2 | 660 | | long currentChapterTicks = 0; |
| | 26 | 661 | | for (int i = 0; i < chapterCount; i++) |
| | | 662 | | { |
| | 11 | 663 | | chapters[i] = new ChapterInfo |
| | 11 | 664 | | { |
| | 11 | 665 | | StartPositionTicks = currentChapterTicks |
| | 11 | 666 | | }; |
| | | 667 | | |
| | 11 | 668 | | currentChapterTicks += dummyChapterDuration; |
| | | 669 | | } |
| | | 670 | | |
| | 2 | 671 | | return chapters; |
| | | 672 | | } |
| | | 673 | | } |
| | | 674 | | } |