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