123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686 |
- using DvdLib.Ifo;
- using MediaBrowser.Controller.Configuration;
- using MediaBrowser.Controller.Entities;
- using MediaBrowser.Controller.Localization;
- using MediaBrowser.Controller.MediaInfo;
- using MediaBrowser.Controller.Persistence;
- using MediaBrowser.Controller.Providers;
- using MediaBrowser.Model.Entities;
- using MediaBrowser.Model.IO;
- using MediaBrowser.Model.Logging;
- using MediaBrowser.Model.MediaInfo;
- 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;
- namespace MediaBrowser.Providers.MediaInfo
- {
- /// <summary>
- /// Extracts video information using ffprobe
- /// </summary>
- public class FFProbeVideoInfoProvider : BaseFFProbeProvider<Video>
- {
- private readonly IItemRepository _itemRepo;
- public FFProbeVideoInfoProvider(IIsoManager isoManager, IBlurayExaminer blurayExaminer, IJsonSerializer jsonSerializer, ILogManager logManager, IServerConfigurationManager configurationManager, IMediaEncoder mediaEncoder, ILocalizationManager localization, IItemRepository itemRepo)
- : base(logManager, configurationManager, mediaEncoder, jsonSerializer)
- {
- if (isoManager == null)
- {
- throw new ArgumentNullException("isoManager");
- }
- if (blurayExaminer == null)
- {
- throw new ArgumentNullException("blurayExaminer");
- }
- _blurayExaminer = blurayExaminer;
- _localization = localization;
- _itemRepo = itemRepo;
- _isoManager = isoManager;
- }
- /// <summary>
- /// Gets or sets the bluray examiner.
- /// </summary>
- /// <value>The bluray examiner.</value>
- private readonly IBlurayExaminer _blurayExaminer;
- /// <summary>
- /// The _iso manager
- /// </summary>
- private readonly IIsoManager _isoManager;
- private readonly ILocalizationManager _localization;
- /// <summary>
- /// Returns true or false indicating if the provider should refresh when the contents of it's directory changes
- /// </summary>
- /// <value><c>true</c> if [refresh on file system stamp change]; otherwise, <c>false</c>.</value>
- protected override bool RefreshOnFileSystemStampChange
- {
- get
- {
- // Need this in case external subtitle files change
- return true;
- }
- }
- /// <summary>
- /// Gets the filestamp extensions.
- /// </summary>
- /// <value>The filestamp extensions.</value>
- protected override string[] FilestampExtensions
- {
- get
- {
- return new[] { ".srt", ".ssa", ".ass" };
- }
- }
- public override MetadataProviderPriority Priority
- {
- get
- {
- return MetadataProviderPriority.Second;
- }
- }
- /// <summary>
- /// Supports video files and dvd structures
- /// </summary>
- /// <param name="item">The item.</param>
- /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
- public override bool Supports(BaseItem item)
- {
- if (item.LocationType != LocationType.FileSystem)
- {
- return false;
- }
- var video = item as Video;
- if (video != null)
- {
- if (video.VideoType == VideoType.Iso)
- {
- return _isoManager.CanMount(item.Path);
- }
- return video.VideoType == VideoType.VideoFile || video.VideoType == VideoType.Dvd || video.VideoType == VideoType.BluRay;
- }
- return false;
- }
- /// <summary>
- /// Called when [pre fetch].
- /// </summary>
- /// <param name="item">The item.</param>
- /// <param name="mount">The mount.</param>
- protected override void OnPreFetch(Video item, IIsoMount mount)
- {
- if (item.VideoType == VideoType.Iso)
- {
- item.IsoType = DetermineIsoType(mount);
- }
- if (item.VideoType == VideoType.Dvd || (item.IsoType.HasValue && item.IsoType == IsoType.Dvd))
- {
- FetchFromDvdLib(item, mount);
- }
- base.OnPreFetch(item, mount);
- }
- private void FetchFromDvdLib(Video item, IIsoMount mount)
- {
- var path = mount == null ? item.Path : mount.MountedPath;
- var dvd = new Dvd(path);
- item.RunTimeTicks = dvd.Titles.Select(GetRuntime).Max();
- var primaryTitle = dvd.Titles.OrderByDescending(GetRuntime).FirstOrDefault();
- uint? titleNumber = null;
- if (primaryTitle != null)
- {
- titleNumber = primaryTitle.TitleNumber;
- }
- item.PlayableStreamFileNames = 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();
- }
- public override async Task<bool> FetchAsync(BaseItem item, bool force, BaseProviderInfo providerInfo, CancellationToken cancellationToken)
- {
- var video = (Video)item;
- var isoMount = await MountIsoIfNeeded(video, cancellationToken).ConfigureAwait(false);
- try
- {
- OnPreFetch(video, isoMount);
- // If we didn't find any satisfying the min length, just take them all
- if (video.VideoType == VideoType.Dvd || (video.IsoType.HasValue && video.IsoType == IsoType.Dvd))
- {
- if (video.PlayableStreamFileNames.Count == 0)
- {
- Logger.Error("No playable vobs found in dvd structure, skipping ffprobe.");
- SetLastRefreshed(item, DateTime.UtcNow, providerInfo);
- return true;
- }
- }
- var result = await GetMediaInfo(item, isoMount, cancellationToken).ConfigureAwait(false);
- cancellationToken.ThrowIfCancellationRequested();
- NormalizeFFProbeResult(result);
- cancellationToken.ThrowIfCancellationRequested();
- await Fetch(video, force, providerInfo, cancellationToken, result, isoMount).ConfigureAwait(false);
- }
- finally
- {
- if (isoMount != null)
- {
- isoMount.Dispose();
- }
- }
- SetLastRefreshed(item, DateTime.UtcNow, providerInfo);
- return true;
- }
- /// <summary>
- /// Mounts the iso if needed.
- /// </summary>
- /// <param name="item">The item.</param>
- /// <param name="cancellationToken">The cancellation token.</param>
- /// <returns>IsoMount.</returns>
- protected override Task<IIsoMount> MountIsoIfNeeded(Video item, CancellationToken cancellationToken)
- {
- if (item.VideoType == VideoType.Iso)
- {
- return _isoManager.Mount(item.Path, cancellationToken);
- }
- return base.MountIsoIfNeeded(item, cancellationToken);
- }
- /// <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 folders = Directory.EnumerateDirectories(isoMount.MountedPath).Select(Path.GetFileName).ToList();
- if (folders.Contains("video_ts", StringComparer.OrdinalIgnoreCase))
- {
- return IsoType.Dvd;
- }
- if (folders.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 = Directory.EnumerateFiles(root, "*", SearchOption.AllDirectories)
- .Where(file => string.Equals(Path.GetExtension(file), ".vob", StringComparison.OrdinalIgnoreCase))
- .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 => Path.GetFileName(i).StartsWith(prefix, StringComparison.OrdinalIgnoreCase)).ToList();
- if (vobs.Count > 0)
- {
- return vobs;
- }
- Logger.Debug("Could not determine vob file list for {0} using DvdLib. Will scan using file sizes.", video.Path);
- }
- var files = allVobs
- .SkipWhile(f => new FileInfo(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 = Path.GetFileNameWithoutExtension(files[0]).Split('_');
- if (parts.Length == 3)
- {
- var title = parts[1];
- files = files.TakeWhile(f =>
- {
- var fileParts = Path.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;
- }
- /// <summary>
- /// Fetches the specified video.
- /// </summary>
- /// <param name="video">The video.</param>
- /// <param name="force">if set to <c>true</c> [force].</param>
- /// <param name="cancellationToken">The cancellation token.</param>
- /// <param name="data">The data.</param>
- /// <param name="isoMount">The iso mount.</param>
- /// <returns>Task.</returns>
- protected async Task Fetch(Video video, bool force, BaseProviderInfo providerInfo, CancellationToken cancellationToken, InternalMediaInfoResult data, IIsoMount isoMount)
- {
- if (data.format != null)
- {
- // 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 && !string.IsNullOrEmpty(data.format.duration))
- {
- video.RunTimeTicks = TimeSpan.FromSeconds(double.Parse(data.format.duration, UsCulture)).Ticks;
- }
- }
- var mediaStreams = MediaEncoderHelpers.GetMediaInfo(data).MediaStreams;
- var chapters = data.Chapters ?? new List<ChapterInfo>();
- if (video.VideoType == VideoType.BluRay || (video.IsoType.HasValue && video.IsoType.Value == IsoType.BluRay))
- {
- var inputPath = isoMount != null ? isoMount.MountedPath : video.Path;
- FetchBdInfo(video, chapters, mediaStreams, inputPath, cancellationToken);
- }
- AddExternalSubtitles(video, mediaStreams);
- FetchWtvInfo(video, force, data);
- video.IsHD = mediaStreams.Any(i => i.Type == MediaStreamType.Video && i.Width.HasValue && i.Width.Value >= 1270);
- if (chapters.Count == 0 && mediaStreams.Any(i => i.Type == MediaStreamType.Video))
- {
- AddDummyChapters(video, chapters);
- }
- var videoStream = mediaStreams.FirstOrDefault(i => i.Type == MediaStreamType.Video);
- video.VideoBitRate = videoStream == null ? null : videoStream.BitRate;
- video.DefaultVideoStreamIndex = videoStream == null ? (int?)null : videoStream.Index;
- video.HasSubtitles = mediaStreams.Any(i => i.Type == MediaStreamType.Subtitle);
- await FFMpegManager.Instance.PopulateChapterImages(video, chapters, false, false, cancellationToken).ConfigureAwait(false);
- var videoFileChanged = CompareDate(video) > providerInfo.LastRefreshed;
- await _itemRepo.SaveMediaStreams(video.Id, mediaStreams, cancellationToken).ConfigureAwait(false);
- // Only save chapters if forcing, if the video changed, or if there are not already any saved ones
- if (force || videoFileChanged || _itemRepo.GetChapter(video.Id, 0) == null)
- {
- await _itemRepo.SaveChapters(video.Id, chapters, cancellationToken).ConfigureAwait(false);
- }
- }
- /// <summary>
- /// Fetches the WTV info.
- /// </summary>
- /// <param name="video">The video.</param>
- /// <param name="force">if set to <c>true</c> [force].</param>
- /// <param name="data">The data.</param>
- private void FetchWtvInfo(Video video, bool force, InternalMediaInfoResult data)
- {
- if (data.format == null || data.format.tags == null)
- {
- return;
- }
- if (force || video.Genres.Count == 0)
- {
- if (!video.LockedFields.Contains(MetadataFields.Genres))
- {
- var genres = GetDictionaryValue(data.format.tags, "genre");
- if (!string.IsNullOrEmpty(genres))
- {
- video.Genres = genres.Split(new[] { ';', '/', ',' }, StringSplitOptions.RemoveEmptyEntries)
- .Where(i => !string.IsNullOrWhiteSpace(i))
- .Select(i => i.Trim())
- .ToList();
- }
- }
- }
- if (force || string.IsNullOrEmpty(video.Overview))
- {
- if (!video.LockedFields.Contains(MetadataFields.Overview))
- {
- var overview = GetDictionaryValue(data.format.tags, "WM/SubTitleDescription");
- if (!string.IsNullOrWhiteSpace(overview))
- {
- video.Overview = overview;
- }
- }
- }
- if (force || string.IsNullOrEmpty(video.OfficialRating))
- {
- var officialRating = GetDictionaryValue(data.format.tags, "WM/ParentalRating");
- if (!string.IsNullOrWhiteSpace(officialRating))
- {
- if (!video.LockedFields.Contains(MetadataFields.OfficialRating))
- {
- video.OfficialRating = officialRating;
- }
- }
- }
- if (force || video.People.Count == 0)
- {
- if (!video.LockedFields.Contains(MetadataFields.Cast))
- {
- var people = GetDictionaryValue(data.format.tags, "WM/MediaCredits");
- if (!string.IsNullOrEmpty(people))
- {
- video.People = people.Split(new[] { ';', '/' }, StringSplitOptions.RemoveEmptyEntries)
- .Where(i => !string.IsNullOrWhiteSpace(i))
- .Select(i => new PersonInfo { Name = i.Trim(), Type = PersonType.Actor })
- .ToList();
- }
- }
- }
- if (force || !video.ProductionYear.HasValue)
- {
- var year = GetDictionaryValue(data.format.tags, "WM/OriginalReleaseTime");
- if (!string.IsNullOrWhiteSpace(year))
- {
- int val;
- if (int.TryParse(year, NumberStyles.Integer, UsCulture, out val))
- {
- video.ProductionYear = val;
- }
- }
- }
- }
- /// <summary>
- /// Adds the external subtitles.
- /// </summary>
- /// <param name="video">The video.</param>
- /// <param name="currentStreams">The current streams.</param>
- private void AddExternalSubtitles(Video video, List<MediaStream> currentStreams)
- {
- var useParent = !video.ResolveArgs.IsDirectory;
- if (useParent && video.Parent == null)
- {
- return;
- }
- var fileSystemChildren = useParent
- ? video.Parent.ResolveArgs.FileSystemChildren
- : video.ResolveArgs.FileSystemChildren;
- var startIndex = currentStreams.Count;
- var streams = new List<MediaStream>();
- var videoFileNameWithoutExtension = Path.GetFileNameWithoutExtension(video.Path);
- foreach (var file in fileSystemChildren
- .Where(f => !f.Attributes.HasFlag(FileAttributes.Directory) && FilestampExtensions.Contains(Path.GetExtension(f.FullName), StringComparer.OrdinalIgnoreCase)))
- {
- var fullName = file.FullName;
- var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fullName);
- // If the subtitle file matches the video file name
- if (string.Equals(videoFileNameWithoutExtension, fileNameWithoutExtension, StringComparison.OrdinalIgnoreCase))
- {
- streams.Add(new MediaStream
- {
- Index = startIndex++,
- Type = MediaStreamType.Subtitle,
- IsExternal = true,
- Path = fullName,
- Codec = Path.GetExtension(fullName).ToLower().TrimStart('.')
- });
- }
- else if (fileNameWithoutExtension.StartsWith(videoFileNameWithoutExtension + ".", StringComparison.OrdinalIgnoreCase))
- {
- // Support xbmc naming conventions - 300.spanish.srt
- var language = fileNameWithoutExtension.Split('.').LastOrDefault();
- // Try to translate to three character code
- // Be flexible and check against both the full and three character versions
- var culture = _localization.GetCultures()
- .FirstOrDefault(i => string.Equals(i.DisplayName, language, StringComparison.OrdinalIgnoreCase) || string.Equals(i.Name, language, StringComparison.OrdinalIgnoreCase) || string.Equals(i.ThreeLetterISOLanguageName, language, StringComparison.OrdinalIgnoreCase) || string.Equals(i.TwoLetterISOLanguageName, language, StringComparison.OrdinalIgnoreCase));
- if (culture != null)
- {
- language = culture.ThreeLetterISOLanguageName;
- }
- streams.Add(new MediaStream
- {
- Index = startIndex++,
- Type = MediaStreamType.Subtitle,
- IsExternal = true,
- Path = fullName,
- Codec = Path.GetExtension(fullName).ToLower().TrimStart('.'),
- Language = language
- });
- }
- }
- currentStreams.AddRange(streams);
- }
- /// <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
- {
- Name = "Chapter " + index,
- StartPositionTicks = currentChapterTicks
- });
- index++;
- currentChapterTicks += _dummyChapterDuration;
- }
- }
- /// <summary>
- /// Fetches the bd info.
- /// </summary>
- /// <param name="item">The item.</param>
- /// <param name="chapters">The chapters.</param>
- /// <param name="mediaStreams">The media streams.</param>
- /// <param name="inputPath">The input path.</param>
- /// <param name="cancellationToken">The cancellation token.</param>
- private void FetchBdInfo(BaseItem item, List<ChapterInfo> chapters, List<MediaStream> mediaStreams, string inputPath, CancellationToken cancellationToken)
- {
- var video = (Video)item;
- var result = GetBDInfo(inputPath);
- cancellationToken.ThrowIfCancellationRequested();
- 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
- Fetch(video, mediaStreams, result, chapters);
- 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;
- }
- }
- /// <summary>
- /// Determines whether the specified num is empty.
- /// </summary>
- /// <param name="num">The num.</param>
- /// <returns><c>true</c> if the specified num is empty; otherwise, <c>false</c>.</returns>
- private bool IsEmpty(int? num)
- {
- return !num.HasValue || num.Value == 0;
- }
- /// <summary>
- /// Fills video properties from the VideoStream of the largest playlist
- /// </summary>
- /// <param name="video">The video.</param>
- /// <param name="mediaStreams">The media streams.</param>
- /// <param name="stream">The stream.</param>
- /// <param name="chapters">The chapters.</param>
- private void Fetch(Video video, List<MediaStream> mediaStreams, BlurayDiscInfo stream, List<ChapterInfo> chapters)
- {
- // Check all input for null/empty/zero
- mediaStreams.Clear();
- mediaStreams.AddRange(stream.MediaStreams);
- video.MainFeaturePlaylistName = stream.PlaylistName;
- if (stream.RunTimeTicks.HasValue && stream.RunTimeTicks.Value > 0)
- {
- video.RunTimeTicks = stream.RunTimeTicks;
- }
- video.PlayableStreamFileNames = stream.Files.ToList();
- if (stream.Chapters != null)
- {
- chapters.Clear();
- chapters.AddRange(stream.Chapters.Select(c => new ChapterInfo
- {
- StartPositionTicks = TimeSpan.FromSeconds(c).Ticks
- }));
- }
- }
- /// <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)
- {
- return _blurayExaminer.GetDiscInfo(path);
- }
- }
- }
|