| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697 | 
							- using DvdLib.Ifo;
 
- using MediaBrowser.Common.Configuration;
 
- using MediaBrowser.Model.Dlna;
 
- using MediaBrowser.Controller.Chapters;
 
- using MediaBrowser.Controller.Configuration;
 
- using MediaBrowser.Controller.Entities;
 
- using MediaBrowser.Controller.Entities.Movies;
 
- using MediaBrowser.Controller.Entities.TV;
 
- using MediaBrowser.Controller.Library;
 
- using MediaBrowser.Controller.MediaEncoding;
 
- using MediaBrowser.Controller.Persistence;
 
- using MediaBrowser.Controller.Providers;
 
- using MediaBrowser.Controller.Subtitles;
 
- using MediaBrowser.Model.Configuration;
 
- using MediaBrowser.Model.Entities;
 
- using MediaBrowser.Model.IO;
 
- using MediaBrowser.Model.Logging;
 
- using MediaBrowser.Model.MediaInfo;
 
- using MediaBrowser.Model.Providers;
 
- using MediaBrowser.Model.Serialization;
 
- using System;
 
- using System.Collections.Generic;
 
- using System.Globalization;
 
- using System.IO;
 
- using System.Linq;
 
- using System.Threading;
 
- using System.Threading.Tasks;
 
- using MediaBrowser.Controller.IO;
 
- using MediaBrowser.Model.IO;
 
- using MediaBrowser.Model.Globalization;
 
- namespace MediaBrowser.Providers.MediaInfo
 
- {
 
-     public class FFProbeVideoInfo
 
-     {
 
-         private readonly ILogger _logger;
 
-         private readonly IIsoManager _isoManager;
 
-         private readonly IMediaEncoder _mediaEncoder;
 
-         private readonly IItemRepository _itemRepo;
 
-         private readonly IBlurayExaminer _blurayExaminer;
 
-         private readonly ILocalizationManager _localization;
 
-         private readonly IApplicationPaths _appPaths;
 
-         private readonly IJsonSerializer _json;
 
-         private readonly IEncodingManager _encodingManager;
 
-         private readonly IFileSystem _fileSystem;
 
-         private readonly IServerConfigurationManager _config;
 
-         private readonly ISubtitleManager _subtitleManager;
 
-         private readonly IChapterManager _chapterManager;
 
-         private readonly ILibraryManager _libraryManager;
 
-         private readonly CultureInfo _usCulture = new CultureInfo("en-US");
 
-         public FFProbeVideoInfo(ILogger logger, IIsoManager isoManager, IMediaEncoder mediaEncoder, IItemRepository itemRepo, IBlurayExaminer blurayExaminer, ILocalizationManager localization, IApplicationPaths appPaths, IJsonSerializer json, IEncodingManager encodingManager, IFileSystem fileSystem, IServerConfigurationManager config, ISubtitleManager subtitleManager, IChapterManager chapterManager, ILibraryManager libraryManager)
 
-         {
 
-             _logger = logger;
 
-             _isoManager = isoManager;
 
-             _mediaEncoder = mediaEncoder;
 
-             _itemRepo = itemRepo;
 
-             _blurayExaminer = blurayExaminer;
 
-             _localization = localization;
 
-             _appPaths = appPaths;
 
-             _json = json;
 
-             _encodingManager = encodingManager;
 
-             _fileSystem = fileSystem;
 
-             _config = config;
 
-             _subtitleManager = subtitleManager;
 
-             _chapterManager = chapterManager;
 
-             _libraryManager = libraryManager;
 
-         }
 
-         public async Task<ItemUpdateType> ProbeVideo<T>(T item,
 
-             MetadataRefreshOptions options,
 
-             CancellationToken cancellationToken)
 
-             where T : Video
 
-         {
 
-             var isoMount = await MountIsoIfNeeded(item, cancellationToken).ConfigureAwait(false);
 
-             BlurayDiscInfo blurayDiscInfo = null;
 
-             try
 
-             {
 
-                 List<string> streamFileNames = null;
 
-                 if (item.VideoType == VideoType.Iso)
 
-                 {
 
-                     item.IsoType = DetermineIsoType(isoMount);
 
-                 }
 
-                 if (item.VideoType == VideoType.Dvd || (item.IsoType.HasValue && item.IsoType == IsoType.Dvd))
 
-                 {
 
-                     streamFileNames = FetchFromDvdLib(item, isoMount);
 
-                     if (streamFileNames.Count == 0)
 
-                     {
 
-                         _logger.Error("No playable vobs found in dvd structure, skipping ffprobe.");
 
-                         return ItemUpdateType.MetadataImport;
 
-                     }
 
-                 }
 
-                 else if (item.VideoType == VideoType.BluRay || (item.IsoType.HasValue && item.IsoType == IsoType.BluRay))
 
-                 {
 
-                     var inputPath = isoMount != null ? isoMount.MountedPath : item.Path;
 
-                     blurayDiscInfo = GetBDInfo(inputPath);
 
-                     streamFileNames = blurayDiscInfo.Files;
 
-                     if (streamFileNames.Count == 0)
 
-                     {
 
-                         _logger.Error("No playable vobs found in bluray structure, skipping ffprobe.");
 
-                         return ItemUpdateType.MetadataImport;
 
-                     }
 
-                 }
 
-                 if (streamFileNames == null)
 
-                 {
 
-                     streamFileNames = new List<string>();
 
-                 }
 
-                 var result = await GetMediaInfo(item, isoMount, streamFileNames, cancellationToken).ConfigureAwait(false);
 
-                 cancellationToken.ThrowIfCancellationRequested();
 
-                 await Fetch(item, cancellationToken, result, isoMount, blurayDiscInfo, options).ConfigureAwait(false);
 
-             }
 
-             finally
 
-             {
 
-                 if (isoMount != null)
 
-                 {
 
-                     isoMount.Dispose();
 
-                 }
 
-             }
 
-             return ItemUpdateType.MetadataImport;
 
-         }
 
-         private Task<Model.MediaInfo.MediaInfo> GetMediaInfo(Video item,
 
-             IIsoMount isoMount,
 
-             List<string> streamFileNames,
 
-             CancellationToken cancellationToken)
 
-         {
 
-             cancellationToken.ThrowIfCancellationRequested();
 
-             var protocol = item.LocationType == LocationType.Remote
 
-                 ? MediaProtocol.Http
 
-                 : MediaProtocol.File;
 
-             return _mediaEncoder.GetMediaInfo(new MediaInfoRequest
 
-             {
 
-                 PlayableStreamFileNames = streamFileNames,
 
-                 MountedIso = isoMount,
 
-                 ExtractChapters = true,
 
-                 VideoType = item.VideoType,
 
-                 MediaType = DlnaProfileType.Video,
 
-                 InputPath = item.Path,
 
-                 Protocol = protocol
 
-             }, cancellationToken);
 
-         }
 
-         protected async Task Fetch(Video video,
 
-             CancellationToken cancellationToken,
 
-             Model.MediaInfo.MediaInfo mediaInfo,
 
-             IIsoMount isoMount,
 
-             BlurayDiscInfo blurayInfo,
 
-             MetadataRefreshOptions options)
 
-         {
 
-             var mediaStreams = mediaInfo.MediaStreams;
 
-             video.TotalBitrate = mediaInfo.Bitrate;
 
-             //video.FormatName = (mediaInfo.Container ?? string.Empty)
 
-             //    .Replace("matroska", "mkv", StringComparison.OrdinalIgnoreCase);
 
-             // 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;
 
-             if (needToSetRuntime)
 
-             {
 
-                 video.RunTimeTicks = mediaInfo.RunTimeTicks;
 
-             }
 
-             if (video.VideoType == VideoType.VideoFile)
 
-             {
 
-                 var extension = (Path.GetExtension(video.Path) ?? string.Empty).TrimStart('.');
 
-                 video.Container = extension;
 
-             }
 
-             else
 
-             {
 
-                 video.Container = null;
 
-             }
 
-             video.Container = mediaInfo.Container;
 
-             var chapters = mediaInfo.Chapters ?? new List<ChapterInfo>();
 
-             if (blurayInfo != null)
 
-             {
 
-                 FetchBdInfo(video, chapters, mediaStreams, blurayInfo);
 
-             }
 
-             await AddExternalSubtitles(video, mediaStreams, options, cancellationToken).ConfigureAwait(false);
 
-             var libraryOptions = _libraryManager.GetLibraryOptions(video);
 
-             FetchEmbeddedInfo(video, mediaInfo, options, libraryOptions);
 
-             await FetchPeople(video, mediaInfo, options).ConfigureAwait(false);
 
-             video.IsHD = mediaStreams.Any(i => i.Type == MediaStreamType.Video && i.Width.HasValue && i.Width.Value >= 1260);
 
-             var videoStream = mediaStreams.FirstOrDefault(i => i.Type == MediaStreamType.Video);
 
-             video.DefaultVideoStreamIndex = videoStream == null ? (int?)null : videoStream.Index;
 
-             video.HasSubtitles = mediaStreams.Any(i => i.Type == MediaStreamType.Subtitle);
 
-             video.Timestamp = mediaInfo.Timestamp;
 
-             video.Video3DFormat = video.Video3DFormat ?? mediaInfo.Video3DFormat;
 
-             await _itemRepo.SaveMediaStreams(video.Id, mediaStreams, cancellationToken).ConfigureAwait(false);
 
-             if (options.MetadataRefreshMode == MetadataRefreshMode.FullRefresh ||
 
-                 options.MetadataRefreshMode == MetadataRefreshMode.Default)
 
-             {
 
-                 if (chapters.Count == 0 && mediaStreams.Any(i => i.Type == MediaStreamType.Video))
 
-                 {
 
-                     AddDummyChapters(video, chapters);
 
-                 }
 
-                 NormalizeChapterNames(chapters);
 
-                 var extractDuringScan = false;
 
-                 if (libraryOptions != null)
 
-                 {
 
-                     extractDuringScan = libraryOptions.ExtractChapterImagesDuringLibraryScan;
 
-                 }
 
-                 await _encodingManager.RefreshChapterImages(video, chapters, extractDuringScan, false, cancellationToken).ConfigureAwait(false);
 
-                 await _chapterManager.SaveChapters(video.Id.ToString(), chapters).ConfigureAwait(false);
 
-             }
 
-         }
 
-         private void NormalizeChapterNames(List<ChapterInfo> chapters)
 
-         {
 
-             var index = 1;
 
-             foreach (var chapter in chapters)
 
-             {
 
-                 TimeSpan time;
 
-                 // Check if the name is empty and/or if the name is a time
 
-                 // Some ripping programs do that.
 
-                 if (string.IsNullOrWhiteSpace(chapter.Name) ||
 
-                     TimeSpan.TryParse(chapter.Name, out time))
 
-                 {
 
-                     chapter.Name = string.Format(_localization.GetLocalizedString("LabelChapterName"), index.ToString(CultureInfo.InvariantCulture));
 
-                 }
 
-                 index++;
 
-             }
 
-         }
 
-         private void FetchBdInfo(BaseItem item, List<ChapterInfo> chapters, List<MediaStream> mediaStreams, BlurayDiscInfo blurayInfo)
 
-         {
 
-             var video = (Video)item;
 
-             //video.PlayableStreamFileNames = blurayInfo.Files.ToList();
 
-             // Use BD Info if it has multiple m2ts. Otherwise, treat it like a video file and rely more on ffprobe output
 
-             if (blurayInfo.Files.Count > 1)
 
-             {
 
-                 int? currentHeight = null;
 
-                 int? currentWidth = null;
 
-                 int? currentBitRate = null;
 
-                 var videoStream = mediaStreams.FirstOrDefault(s => s.Type == MediaStreamType.Video);
 
-                 // Grab the values that ffprobe recorded
 
-                 if (videoStream != null)
 
-                 {
 
-                     currentBitRate = videoStream.BitRate;
 
-                     currentWidth = videoStream.Width;
 
-                     currentHeight = videoStream.Height;
 
-                 }
 
-                 // Fill video properties from the BDInfo result
 
-                 mediaStreams.Clear();
 
-                 mediaStreams.AddRange(blurayInfo.MediaStreams);
 
-                 if (blurayInfo.RunTimeTicks.HasValue && blurayInfo.RunTimeTicks.Value > 0)
 
-                 {
 
-                     video.RunTimeTicks = blurayInfo.RunTimeTicks;
 
-                 }
 
-                 if (blurayInfo.Chapters != null)
 
-                 {
 
-                     chapters.Clear();
 
-                     chapters.AddRange(blurayInfo.Chapters.Select(c => new ChapterInfo
 
-                     {
 
-                         StartPositionTicks = TimeSpan.FromSeconds(c).Ticks
 
-                     }));
 
-                 }
 
-                 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;
 
-                 }
 
-             }
 
-         }
 
-         private bool IsEmpty(int? num)
 
-         {
 
-             return !num.HasValue || num.Value == 0;
 
-         }
 
-         /// <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)
 
-         {
 
-             if (string.IsNullOrWhiteSpace(path))
 
-             {
 
-                 throw new ArgumentNullException("path");
 
-             }
 
-             try
 
-             {
 
-                 return _blurayExaminer.GetDiscInfo(path);
 
-             }
 
-             catch (Exception ex)
 
-             {
 
-                 _logger.ErrorException("Error getting BDInfo", ex);
 
-                 return null;
 
-             }
 
-         }
 
-         private void FetchEmbeddedInfo(Video video, Model.MediaInfo.MediaInfo data, MetadataRefreshOptions refreshOptions, LibraryOptions libraryOptions)
 
-         {
 
-             var isFullRefresh = refreshOptions.MetadataRefreshMode == MetadataRefreshMode.FullRefresh;
 
-             if (!video.IsLocked && !video.LockedFields.Contains(MetadataFields.OfficialRating))
 
-             {
 
-                 if (!string.IsNullOrWhiteSpace(data.OfficialRating) || isFullRefresh)
 
-                 {
 
-                     video.OfficialRating = data.OfficialRating;
 
-                 }
 
-             }
 
-             if (!video.IsLocked && !video.LockedFields.Contains(MetadataFields.Genres))
 
-             {
 
-                 if (video.Genres.Count == 0 || isFullRefresh)
 
-                 {
 
-                     video.Genres.Clear();
 
-                     foreach (var genre in data.Genres)
 
-                     {
 
-                         video.AddGenre(genre);
 
-                     }
 
-                 }
 
-             }
 
-             if (!video.IsLocked && !video.LockedFields.Contains(MetadataFields.Studios))
 
-             {
 
-                 if (video.Studios.Length == 0 || isFullRefresh)
 
-                 {
 
-                     video.SetStudios(data.Studios);
 
-                 }
 
-             }
 
-             if (data.ProductionYear.HasValue)
 
-             {
 
-                 if (!video.ProductionYear.HasValue || isFullRefresh)
 
-                 {
 
-                     video.ProductionYear = data.ProductionYear;
 
-                 }
 
-             }
 
-             if (data.PremiereDate.HasValue)
 
-             {
 
-                 if (!video.PremiereDate.HasValue || isFullRefresh)
 
-                 {
 
-                     video.PremiereDate = data.PremiereDate;
 
-                 }
 
-             }
 
-             if (data.IndexNumber.HasValue)
 
-             {
 
-                 if (!video.IndexNumber.HasValue || isFullRefresh)
 
-                 {
 
-                     video.IndexNumber = data.IndexNumber;
 
-                 }
 
-             }
 
-             if (data.ParentIndexNumber.HasValue)
 
-             {
 
-                 if (!video.ParentIndexNumber.HasValue || isFullRefresh)
 
-                 {
 
-                     video.ParentIndexNumber = data.ParentIndexNumber;
 
-                 }
 
-             }
 
-             if (!video.IsLocked && !video.LockedFields.Contains(MetadataFields.Name))
 
-             {
 
-                 if (!string.IsNullOrWhiteSpace(data.Name) && libraryOptions.EnableEmbeddedTitles)
 
-                 {
 
-                     // Don't use the embedded name for extras because it will often be the same name as the movie
 
-                     if (!video.ExtraType.HasValue && !video.IsOwnedItem)
 
-                     {
 
-                         video.Name = data.Name;
 
-                     }
 
-                 }
 
-             }
 
-             // 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;
 
-             }
 
-             if (!video.IsLocked && !video.LockedFields.Contains(MetadataFields.Overview))
 
-             {
 
-                 if (string.IsNullOrWhiteSpace(video.Overview) || isFullRefresh)
 
-                 {
 
-                     video.Overview = data.Overview;
 
-                 }
 
-             }
 
-         }
 
-         private async Task FetchPeople(Video video, Model.MediaInfo.MediaInfo data, MetadataRefreshOptions options)
 
-         {
 
-             var isFullRefresh = options.MetadataRefreshMode == MetadataRefreshMode.FullRefresh;
 
-             if (!video.IsLocked && !video.LockedFields.Contains(MetadataFields.Cast))
 
-             {
 
-                 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
 
-                         });
 
-                     }
 
-                     await _libraryManager.UpdatePeople(video, people);
 
-                 }
 
-             }
 
-         }
 
-         private SubtitleOptions GetOptions()
 
-         {
 
-             return _config.GetConfiguration<SubtitleOptions>("subtitles");
 
-         }
 
-         /// <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>
 
-         /// <param name="cancellationToken">The cancellation token.</param>
 
-         /// <returns>Task.</returns>
 
-         private async Task AddExternalSubtitles(Video video,
 
-             List<MediaStream> currentStreams,
 
-             MetadataRefreshOptions options,
 
-             CancellationToken cancellationToken)
 
-         {
 
-             var subtitleResolver = new SubtitleResolver(_localization, _fileSystem);
 
-             var startIndex = currentStreams.Count == 0 ? 0 : (currentStreams.Select(i => i.Index).Max() + 1);
 
-             var externalSubtitleStreams = subtitleResolver.GetExternalSubtitleStreams(video, startIndex, options.DirectoryService, false).ToList();
 
-             var enableSubtitleDownloading = options.MetadataRefreshMode == MetadataRefreshMode.Default ||
 
-                                             options.MetadataRefreshMode == MetadataRefreshMode.FullRefresh;
 
-             var subtitleOptions = GetOptions();
 
-             if (enableSubtitleDownloading && (subtitleOptions.DownloadEpisodeSubtitles &&
 
-                 video is Episode) ||
 
-                 (subtitleOptions.DownloadMovieSubtitles &&
 
-                 video is Movie))
 
-             {
 
-                 var downloadedLanguages = await new SubtitleDownloader(_logger,
 
-                     _subtitleManager)
 
-                     .DownloadSubtitles(video,
 
-                     currentStreams.Concat(externalSubtitleStreams).ToList(),
 
-                     subtitleOptions.SkipIfEmbeddedSubtitlesPresent,
 
-                     subtitleOptions.SkipIfAudioTrackMatches,
 
-                     subtitleOptions.RequirePerfectMatch,
 
-                     subtitleOptions.DownloadLanguages,
 
-                     cancellationToken).ConfigureAwait(false);
 
-                 // Rescan
 
-                 if (downloadedLanguages.Count > 0)
 
-                 {
 
-                     externalSubtitleStreams = subtitleResolver.GetExternalSubtitleStreams(video, startIndex, options.DirectoryService, true).ToList();
 
-                 }
 
-             }
 
-             video.SubtitleFiles = externalSubtitleStreams.Select(i => i.Path).OrderBy(i => i).ToArray();
 
-             currentStreams.AddRange(externalSubtitleStreams);
 
-         }
 
-         /// <summary>
 
-         /// The dummy chapter duration
 
-         /// </summary>
 
-         private readonly long _dummyChapterDuration = TimeSpan.FromMinutes(5).Ticks;
 
-         /// <summary>
 
-         /// Adds the dummy chapters.
 
-         /// </summary>
 
-         /// <param name="video">The video.</param>
 
-         /// <param name="chapters">The chapters.</param>
 
-         private void AddDummyChapters(Video video, List<ChapterInfo> chapters)
 
-         {
 
-             var runtime = video.RunTimeTicks ?? 0;
 
-             if (runtime < 0)
 
-             {
 
-                 throw new ArgumentException(string.Format("{0} has invalid runtime of {1}", video.Name, runtime));
 
-             }
 
-             if (runtime < _dummyChapterDuration)
 
-             {
 
-                 return;
 
-             }
 
-             long currentChapterTicks = 0;
 
-             var index = 1;
 
-             // Limit to 100 chapters just in case there's some incorrect metadata here
 
-             while (currentChapterTicks < runtime && index < 100)
 
-             {
 
-                 chapters.Add(new ChapterInfo
 
-                 {
 
-                     StartPositionTicks = currentChapterTicks
 
-                 });
 
-                 index++;
 
-                 currentChapterTicks += _dummyChapterDuration;
 
-             }
 
-         }
 
-         private List<string> FetchFromDvdLib(Video item, IIsoMount mount)
 
-         {
 
-             var path = mount == null ? item.Path : mount.MountedPath;
 
-             var dvd = new Dvd(path, _fileSystem);
 
-             var primaryTitle = dvd.Titles.OrderByDescending(GetRuntime).FirstOrDefault();
 
-             byte? titleNumber = null;
 
-             if (primaryTitle != null)
 
-             {
 
-                 titleNumber = primaryTitle.VideoTitleSetNumber;
 
-                 item.RunTimeTicks = GetRuntime(primaryTitle);
 
-             }
 
-             return GetPrimaryPlaylistVobFiles(item, mount, titleNumber)
 
-                 .Select(Path.GetFileName)
 
-                 .ToList();
 
-         }
 
-         private long GetRuntime(Title title)
 
-         {
 
-             return title.ProgramChains
 
-                     .Select(i => (TimeSpan)i.PlaybackTime)
 
-                     .Select(i => i.Ticks)
 
-                     .Sum();
 
-         }
 
-         /// <summary>
 
-         /// Mounts the iso if needed.
 
-         /// </summary>
 
-         /// <param name="item">The item.</param>
 
-         /// <param name="cancellationToken">The cancellation token.</param>
 
-         /// <returns>IsoMount.</returns>
 
-         protected Task<IIsoMount> MountIsoIfNeeded(Video item, CancellationToken cancellationToken)
 
-         {
 
-             if (item.VideoType == VideoType.Iso)
 
-             {
 
-                 return _isoManager.Mount(item.Path, cancellationToken);
 
-             }
 
-             return Task.FromResult<IIsoMount>(null);
 
-         }
 
-         /// <summary>
 
-         /// Determines the type of the iso.
 
-         /// </summary>
 
-         /// <param name="isoMount">The iso mount.</param>
 
-         /// <returns>System.Nullable{IsoType}.</returns>
 
-         private IsoType? DetermineIsoType(IIsoMount isoMount)
 
-         {
 
-             var fileSystemEntries = _fileSystem.GetFileSystemEntryPaths(isoMount.MountedPath).Select(Path.GetFileName).ToList();
 
-             if (fileSystemEntries.Contains("video_ts", StringComparer.OrdinalIgnoreCase) ||
 
-                 fileSystemEntries.Contains("VIDEO_TS.IFO", StringComparer.OrdinalIgnoreCase))
 
-             {
 
-                 return IsoType.Dvd;
 
-             }
 
-             if (fileSystemEntries.Contains("bdmv", StringComparer.OrdinalIgnoreCase))
 
-             {
 
-                 return IsoType.BluRay;
 
-             }
 
-             return null;
 
-         }
 
-         private IEnumerable<string> GetPrimaryPlaylistVobFiles(Video video, IIsoMount isoMount, uint? titleNumber)
 
-         {
 
-             // min size 300 mb
 
-             const long minPlayableSize = 314572800;
 
-             var root = isoMount != null ? isoMount.MountedPath : video.Path;
 
-             // Try to eliminate menus and intros by skipping all files at the front of the list that are less than the minimum size
 
-             // Once we reach a file that is at least the minimum, return all subsequent ones
 
-             var allVobs = _fileSystem.GetFiles(root, new[] { ".vob" }, false, true)
 
-                 .OrderBy(i => i.FullName)
 
-                 .ToList();
 
-             // If we didn't find any satisfying the min length, just take them all
 
-             if (allVobs.Count == 0)
 
-             {
 
-                 _logger.Error("No vobs found in dvd structure.");
 
-                 return new List<string>();
 
-             }
 
-             if (titleNumber.HasValue)
 
-             {
 
-                 var prefix = string.Format("VTS_0{0}_", titleNumber.Value.ToString(_usCulture));
 
-                 var vobs = allVobs.Where(i => i.Name.StartsWith(prefix, StringComparison.OrdinalIgnoreCase)).ToList();
 
-                 if (vobs.Count > 0)
 
-                 {
 
-                     var minSizeVobs = vobs
 
-                         .SkipWhile(f => f.Length < minPlayableSize)
 
-                         .ToList();
 
-                     return minSizeVobs.Count == 0 ? vobs.Select(i => i.FullName) : minSizeVobs.Select(i => i.FullName);
 
-                 }
 
-                 _logger.Info("Could not determine vob file list for {0} using DvdLib. Will scan using file sizes.", video.Path);
 
-             }
 
-             var files = allVobs
 
-                 .SkipWhile(f => f.Length < minPlayableSize)
 
-                 .ToList();
 
-             // If we didn't find any satisfying the min length, just take them all
 
-             if (files.Count == 0)
 
-             {
 
-                 _logger.Warn("Vob size filter resulted in zero matches. Taking all vobs.");
 
-                 files = allVobs;
 
-             }
 
-             // Assuming they're named "vts_05_01", take all files whose second part matches that of the first file
 
-             if (files.Count > 0)
 
-             {
 
-                 var parts = _fileSystem.GetFileNameWithoutExtension(files[0]).Split('_');
 
-                 if (parts.Length == 3)
 
-                 {
 
-                     var title = parts[1];
 
-                     files = files.TakeWhile(f =>
 
-                     {
 
-                         var fileParts = _fileSystem.GetFileNameWithoutExtension(f).Split('_');
 
-                         return fileParts.Length == 3 && string.Equals(title, fileParts[1], StringComparison.OrdinalIgnoreCase);
 
-                     }).ToList();
 
-                     // If this resulted in not getting any vobs, just take them all
 
-                     if (files.Count == 0)
 
-                     {
 
-                         _logger.Warn("Vob filename filter resulted in zero matches. Taking all vobs.");
 
-                         files = allVobs;
 
-                     }
 
-                 }
 
-             }
 
-             return files.Select(i => i.FullName);
 
-         }
 
-     }
 
- }
 
 
  |