jellyfin/MediaBrowser.Providers/MediaInfo/FFProbeVideoInfoProvider.cs

667 lines
25 KiB
C#
Raw Normal View History

2014-01-11 05:49:18 +00:00
using DvdLib.Ifo;
using MediaBrowser.Common.MediaInfo;
2013-03-04 05:43:06 +00:00
using MediaBrowser.Controller.Configuration;
2013-02-21 01:33:05 +00:00
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Localization;
2013-12-15 18:29:34 +00:00
using MediaBrowser.Controller.MediaInfo;
2013-06-23 19:03:20 +00:00
using MediaBrowser.Controller.Persistence;
using MediaBrowser.Controller.Providers;
2013-02-21 01:33:05 +00:00
using MediaBrowser.Model.Entities;
2013-08-10 01:16:31 +00:00
using MediaBrowser.Model.IO;
2013-03-02 17:59:15 +00:00
using MediaBrowser.Model.Logging;
2013-02-21 06:38:23 +00:00
using MediaBrowser.Model.MediaInfo;
2013-02-24 21:53:54 +00:00
using MediaBrowser.Model.Serialization;
2013-02-21 01:33:05 +00:00
using System;
using System.Collections.Generic;
using System.Globalization;
2013-02-21 01:33:05 +00:00
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
2013-06-09 16:47:28 +00:00
namespace MediaBrowser.Providers.MediaInfo
2013-02-21 01:33:05 +00:00
{
/// <summary>
/// Extracts video information using ffprobe
/// </summary>
public class FFProbeVideoInfoProvider : BaseFFProbeProvider<Video>
{
2013-06-23 19:03:20 +00:00
private readonly IItemRepository _itemRepo;
public FFProbeVideoInfoProvider(IIsoManager isoManager, IBlurayExaminer blurayExaminer, IJsonSerializer jsonSerializer, ILogManager logManager, IServerConfigurationManager configurationManager, IMediaEncoder mediaEncoder, ILocalizationManager localization, IItemRepository itemRepo)
2013-04-07 22:09:48 +00:00
: base(logManager, configurationManager, mediaEncoder, jsonSerializer)
2013-03-04 05:43:06 +00:00
{
if (isoManager == null)
{
throw new ArgumentNullException("isoManager");
}
if (blurayExaminer == null)
{
throw new ArgumentNullException("blurayExaminer");
}
_blurayExaminer = blurayExaminer;
_localization = localization;
2013-06-23 19:03:20 +00:00
_itemRepo = itemRepo;
2013-03-04 05:43:06 +00:00
_isoManager = isoManager;
}
2013-02-21 06:38:23 +00:00
/// <summary>
/// Gets or sets the bluray examiner.
/// </summary>
/// <value>The bluray examiner.</value>
2013-02-24 21:53:54 +00:00
private readonly IBlurayExaminer _blurayExaminer;
2013-02-21 06:38:23 +00:00
2013-02-24 21:53:54 +00:00
/// <summary>
/// The _iso manager
/// </summary>
private readonly IIsoManager _isoManager;
private readonly ILocalizationManager _localization;
2013-06-12 00:19:56 +00:00
2013-02-21 01:33:05 +00:00
/// <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
2013-02-21 01:33:05 +00:00
return true;
}
}
/// <summary>
/// Gets the filestamp extensions.
/// </summary>
/// <value>The filestamp extensions.</value>
protected override string[] FilestampExtensions
{
get
{
2013-09-20 00:56:37 +00:00
return new[] { ".srt", ".ssa", ".ass" };
}
}
public override MetadataProviderPriority Priority
{
get
{
return MetadataProviderPriority.Second;
}
}
2013-02-21 01:33:05 +00:00
/// <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)
{
2013-03-09 20:27:35 +00:00
if (item.LocationType != LocationType.FileSystem)
{
return false;
}
2013-02-21 01:33:05 +00:00
var video = item as Video;
if (video != null)
{
if (video.VideoType == VideoType.Iso)
{
return _isoManager.CanMount(item.Path);
2013-02-21 01:33:05 +00:00
}
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))
{
2014-01-11 05:49:18 +00:00
FetchFromDvdLib(item, mount);
2013-02-21 01:33:05 +00:00
}
2013-02-21 01:33:05 +00:00
base.OnPreFetch(item, mount);
}
2014-01-11 05:49:18 +00:00
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();
PopulateDvdStreamFiles(item, mount);
}
private long GetRuntime(Title title)
{
return title.ProgramChains
.Select(i => (TimeSpan)i.PlaybackTime)
.Select(i => i.Ticks)
.Sum();
}
2013-12-06 20:07:34 +00:00
public override async Task<bool> FetchAsync(BaseItem item, bool force, BaseProviderInfo providerInfo, CancellationToken cancellationToken)
2013-06-18 19:16:27 +00:00
{
2013-09-18 02:43:34 +00:00
var video = (Video)item;
2013-06-18 19:16:27 +00:00
2013-09-18 02:43:34 +00:00
var isoMount = await MountIsoIfNeeded(video, cancellationToken).ConfigureAwait(false);
2013-06-18 19:16:27 +00:00
try
{
2013-09-18 02:43:34 +00:00
OnPreFetch(video, isoMount);
2013-06-18 19:16:27 +00:00
2013-09-18 02:43:34 +00:00
// 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.");
2013-12-06 20:07:34 +00:00
SetLastRefreshed(item, DateTime.UtcNow, providerInfo);
2013-09-18 02:43:34 +00:00
return true;
}
}
2013-09-20 00:56:37 +00:00
2013-06-18 19:16:27 +00:00
var result = await GetMediaInfo(item, isoMount, cancellationToken).ConfigureAwait(false);
cancellationToken.ThrowIfCancellationRequested();
NormalizeFFProbeResult(result);
cancellationToken.ThrowIfCancellationRequested();
2013-12-06 20:07:34 +00:00
await Fetch(video, force, providerInfo, cancellationToken, result, isoMount).ConfigureAwait(false);
2013-06-18 19:16:27 +00:00
}
finally
{
if (isoMount != null)
{
isoMount.Dispose();
}
}
2013-12-06 20:07:34 +00:00
SetLastRefreshed(item, DateTime.UtcNow, providerInfo);
2013-06-18 19:16:27 +00:00
return true;
}
2013-02-21 01:33:05 +00:00
/// <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);
2013-02-21 01:33:05 +00:00
}
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;
}
/// <summary>
/// Finds vob files and populates the dvd stream file properties
/// </summary>
/// <param name="video">The video.</param>
/// <param name="isoMount">The iso mount.</param>
private void PopulateDvdStreamFiles(Video video, IIsoMount isoMount)
{
// 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
2013-09-18 02:43:34 +00:00
var allVobs = Directory.EnumerateFiles(root, "*.vob", SearchOption.AllDirectories).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;
}
2013-09-20 00:56:37 +00:00
2013-09-18 02:43:34 +00:00
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();
2013-09-18 02:43:34 +00:00
// 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;
}
}
}
video.PlayableStreamFileNames = files.Select(Path.GetFileName).ToList();
2013-02-21 01:33:05 +00:00
}
/// <summary>
/// Fetches the specified video.
/// </summary>
/// <param name="video">The video.</param>
/// <param name="force">if set to <c>true</c> [force].</param>
2013-02-21 01:33:05 +00:00
/// <param name="cancellationToken">The cancellation token.</param>
/// <param name="data">The data.</param>
/// <param name="isoMount">The iso mount.</param>
/// <returns>Task.</returns>
2013-12-06 20:07:34 +00:00
protected async Task Fetch(Video video, bool force, BaseProviderInfo providerInfo, CancellationToken cancellationToken, MediaInfoResult data, IIsoMount isoMount)
2013-02-21 01:33:05 +00:00
{
if (data.format != null)
2013-02-21 01:33:05 +00:00
{
// For dvd's this may not always be accurate, so don't set the runtime if the item already has one
var needToSetRuntime = video.VideoType != VideoType.Dvd || video.RunTimeTicks == null || video.RunTimeTicks.Value == 0;
2013-02-21 01:33:05 +00:00
if (needToSetRuntime && !string.IsNullOrEmpty(data.format.duration))
2013-02-21 01:33:05 +00:00
{
video.RunTimeTicks = TimeSpan.FromSeconds(double.Parse(data.format.duration, UsCulture)).Ticks;
2013-02-21 01:33:05 +00:00
}
}
2013-02-21 01:33:05 +00:00
2014-01-11 23:07:56 +00:00
var mediaStreams = MediaEncoderHelpers.GetMediaStreams(data).ToList();
2013-02-21 01:33:05 +00:00
2013-06-18 19:16:27 +00:00
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;
2013-12-06 03:39:44 +00:00
FetchBdInfo(video, chapters, mediaStreams, inputPath, cancellationToken);
}
2013-02-21 01:33:05 +00:00
2013-12-06 03:39:44 +00:00
AddExternalSubtitles(video, mediaStreams);
2013-06-12 00:19:56 +00:00
FetchWtvInfo(video, force, data);
2013-06-18 19:16:27 +00:00
2013-12-06 03:39:44 +00:00
video.IsHD = mediaStreams.Any(i => i.Type == MediaStreamType.Video && i.Width.HasValue && i.Width.Value >= 1270);
2013-10-03 14:14:40 +00:00
2013-12-06 03:39:44 +00:00
if (chapters.Count == 0 && mediaStreams.Any(i => i.Type == MediaStreamType.Video))
2013-06-18 19:16:27 +00:00
{
AddDummyChapters(video, chapters);
}
2013-12-06 03:39:44 +00:00
var videoStream = mediaStreams.FirstOrDefault(i => i.Type == MediaStreamType.Video);
video.VideoBitRate = videoStream == null ? null : videoStream.BitRate;
video.DefaultVideoStreamIndex = videoStream == null ? (int?)null : videoStream.Index;
2013-12-06 03:39:44 +00:00
video.HasSubtitles = mediaStreams.Any(i => i.Type == MediaStreamType.Subtitle);
2013-12-15 18:29:34 +00:00
await FFMpegManager.Instance.PopulateChapterImages(video, chapters, false, false, cancellationToken).ConfigureAwait(false);
2013-09-26 15:15:18 +00:00
2013-12-06 20:07:34 +00:00
var videoFileChanged = CompareDate(video) > providerInfo.LastRefreshed;
2013-09-26 15:15:18 +00:00
2013-12-06 03:39:44 +00:00
await _itemRepo.SaveMediaStreams(video.Id, mediaStreams, cancellationToken).ConfigureAwait(false);
2013-09-26 15:15:18 +00:00
// 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);
}
2013-06-12 00:19:56 +00:00
}
/// <summary>
/// Fetches the WTV info.
/// </summary>
/// <param name="video">The video.</param>
/// <param name="force">if set to <c>true</c> [force].</param>
2013-06-12 00:19:56 +00:00
/// <param name="data">The data.</param>
private void FetchWtvInfo(Video video, bool force, MediaInfoResult data)
2013-06-12 00:19:56 +00:00
{
2013-06-17 01:49:30 +00:00
if (data.format == null || data.format.tags == null)
2013-06-12 00:19:56 +00:00
{
return;
}
if (force || video.Genres.Count == 0)
2013-06-12 00:19:56 +00:00
{
if (!video.LockedFields.Contains(MetadataFields.Genres))
2013-08-04 00:59:23 +00:00
{
var genres = GetDictionaryValue(data.format.tags, "genre");
if (!string.IsNullOrEmpty(genres))
{
2013-12-29 17:07:29 +00:00
video.Genres = genres.Split(new[] { ';', '/', ',' }, StringSplitOptions.RemoveEmptyEntries)
.Where(i => !string.IsNullOrWhiteSpace(i))
.Select(i => i.Trim())
.ToList();
}
2013-08-04 00:59:23 +00:00
}
2013-06-12 00:19:56 +00:00
}
if (force || string.IsNullOrEmpty(video.Overview))
2013-06-12 00:19:56 +00:00
{
if (!video.LockedFields.Contains(MetadataFields.Overview))
{
var overview = GetDictionaryValue(data.format.tags, "WM/SubTitleDescription");
2013-06-12 00:19:56 +00:00
if (!string.IsNullOrWhiteSpace(overview))
{
video.Overview = overview;
}
}
}
2013-06-12 00:19:56 +00:00
if (force || string.IsNullOrEmpty(video.OfficialRating))
2013-06-12 00:19:56 +00:00
{
var officialRating = GetDictionaryValue(data.format.tags, "WM/ParentalRating");
2013-06-12 00:19:56 +00:00
if (!string.IsNullOrWhiteSpace(officialRating))
{
if (!video.LockedFields.Contains(MetadataFields.OfficialRating))
{
video.OfficialRating = officialRating;
}
}
}
2013-06-12 00:19:56 +00:00
if (force || video.People.Count == 0)
2013-06-12 00:19:56 +00:00
{
if (!video.LockedFields.Contains(MetadataFields.Cast))
{
var people = GetDictionaryValue(data.format.tags, "WM/MediaCredits");
2013-06-12 00:19:56 +00:00
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();
}
}
}
2013-06-12 00:19:56 +00:00
if (force || !video.ProductionYear.HasValue)
2013-06-12 00:19:56 +00:00
{
var year = GetDictionaryValue(data.format.tags, "WM/OriginalReleaseTime");
2013-06-12 00:19:56 +00:00
if (!string.IsNullOrWhiteSpace(year))
2013-06-12 00:19:56 +00:00
{
int val;
if (int.TryParse(year, NumberStyles.Integer, UsCulture, out val))
{
video.ProductionYear = val;
}
2013-06-12 00:19:56 +00:00
}
}
2013-02-21 01:33:05 +00:00
}
/// <summary>
/// Adds the external subtitles.
/// </summary>
/// <param name="video">The video.</param>
2013-12-06 03:39:44 +00:00
/// <param name="currentStreams">The current streams.</param>
private void AddExternalSubtitles(Video video, List<MediaStream> currentStreams)
2013-02-21 01:33:05 +00:00
{
2013-07-16 18:47:05 +00:00
var useParent = !video.ResolveArgs.IsDirectory;
2013-02-21 01:33:05 +00:00
if (useParent && video.Parent == null)
{
return;
}
var fileSystemChildren = useParent
? video.Parent.ResolveArgs.FileSystemChildren
: video.ResolveArgs.FileSystemChildren;
2013-12-06 03:39:44 +00:00
var startIndex = currentStreams.Count;
2013-02-21 01:33:05 +00:00
var streams = new List<MediaStream>();
var videoFileNameWithoutExtension = Path.GetFileNameWithoutExtension(video.Path);
2013-06-05 14:58:22 +00:00
foreach (var file in fileSystemChildren
2013-09-20 00:56:37 +00:00
.Where(f => !f.Attributes.HasFlag(FileAttributes.Directory) && FilestampExtensions.Contains(Path.GetExtension(f.FullName), StringComparer.OrdinalIgnoreCase)))
2013-02-21 01:33:05 +00:00
{
var fullName = file.FullName;
2013-02-21 01:33:05 +00:00
var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fullName);
// If the subtitle file matches the video file name
if (string.Equals(videoFileNameWithoutExtension, fileNameWithoutExtension, StringComparison.OrdinalIgnoreCase))
2013-02-21 01:33:05 +00:00
{
streams.Add(new MediaStream
{
Index = startIndex++,
Type = MediaStreamType.Subtitle,
IsExternal = true,
Path = fullName,
2013-09-20 00:58:15 +00:00
Codec = Path.GetExtension(fullName).ToLower().TrimStart('.')
});
2013-02-21 01:33:05 +00:00
}
else if (fileNameWithoutExtension.StartsWith(videoFileNameWithoutExtension + ".", StringComparison.OrdinalIgnoreCase))
2013-06-05 14:58:22 +00:00
{
// Support xbmc naming conventions - 300.spanish.srt
var language = fileNameWithoutExtension.Split('.').LastOrDefault();
// Try to translate to three character code
2013-06-06 15:12:29 +00:00
// 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,
2013-09-20 02:03:37 +00:00
Codec = Path.GetExtension(fullName).ToLower().TrimStart('.'),
Language = language
});
}
2013-02-21 01:33:05 +00:00
}
2013-12-06 03:39:44 +00:00
currentStreams.AddRange(streams);
2013-02-21 01:33:05 +00:00
}
/// <summary>
/// The dummy chapter duration
/// </summary>
private readonly long _dummyChapterDuration = TimeSpan.FromMinutes(5).Ticks;
2013-02-21 01:33:05 +00:00
/// <summary>
/// Adds the dummy chapters.
/// </summary>
/// <param name="video">The video.</param>
2013-06-18 19:16:27 +00:00
/// <param name="chapters">The chapters.</param>
private void AddDummyChapters(Video video, List<ChapterInfo> chapters)
2013-02-21 01:33:05 +00:00
{
var runtime = video.RunTimeTicks ?? 0;
2013-08-05 21:19:10 +00:00
if (runtime < 0)
{
throw new ArgumentException(string.Format("{0} has invalid runtime of {1}", video.Name, runtime));
}
if (runtime < _dummyChapterDuration)
2013-02-21 01:33:05 +00:00
{
return;
}
long currentChapterTicks = 0;
var index = 1;
2013-08-05 21:19:10 +00:00
// Limit to 100 chapters just in case there's some incorrect metadata here
while (currentChapterTicks < runtime && index < 100)
2013-02-21 01:33:05 +00:00
{
chapters.Add(new ChapterInfo
{
Name = "Chapter " + index,
StartPositionTicks = currentChapterTicks
});
index++;
currentChapterTicks += _dummyChapterDuration;
2013-02-21 01:33:05 +00:00
}
}
2013-02-21 06:38:23 +00:00
/// <summary>
/// Fetches the bd info.
/// </summary>
/// <param name="item">The item.</param>
2013-06-18 19:16:27 +00:00
/// <param name="chapters">The chapters.</param>
2013-12-06 03:39:44 +00:00
/// <param name="mediaStreams">The media streams.</param>
2013-02-21 06:38:23 +00:00
/// <param name="inputPath">The input path.</param>
/// <param name="cancellationToken">The cancellation token.</param>
2013-12-06 03:39:44 +00:00
private void FetchBdInfo(BaseItem item, List<ChapterInfo> chapters, List<MediaStream> mediaStreams, string inputPath, CancellationToken cancellationToken)
2013-02-21 06:38:23 +00:00
{
var video = (Video)item;
var result = GetBDInfo(inputPath);
2013-02-21 06:38:23 +00:00
cancellationToken.ThrowIfCancellationRequested();
int? currentHeight = null;
int? currentWidth = null;
int? currentBitRate = null;
2013-12-06 03:39:44 +00:00
var videoStream = mediaStreams.FirstOrDefault(s => s.Type == MediaStreamType.Video);
2013-02-21 06:38:23 +00:00
// 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
2013-12-06 03:39:44 +00:00
Fetch(video, mediaStreams, result, chapters);
2013-02-21 06:38:23 +00:00
2013-12-06 03:39:44 +00:00
videoStream = mediaStreams.FirstOrDefault(s => s.Type == MediaStreamType.Video);
2013-02-21 06:38:23 +00:00
// 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>
2013-12-06 03:39:44 +00:00
/// <param name="mediaStreams">The media streams.</param>
2013-02-21 06:38:23 +00:00
/// <param name="stream">The stream.</param>
2013-06-18 19:16:27 +00:00
/// <param name="chapters">The chapters.</param>
2013-12-06 03:39:44 +00:00
private void Fetch(Video video, List<MediaStream> mediaStreams, BlurayDiscInfo stream, List<ChapterInfo> chapters)
2013-02-21 06:38:23 +00:00
{
// Check all input for null/empty/zero
2013-12-06 03:39:44 +00:00
mediaStreams.Clear();
mediaStreams.AddRange(stream.MediaStreams);
2013-02-21 06:38:23 +00:00
2013-09-02 01:35:57 +00:00
video.MainFeaturePlaylistName = stream.PlaylistName;
2013-02-21 06:38:23 +00:00
if (stream.RunTimeTicks.HasValue && stream.RunTimeTicks.Value > 0)
{
video.RunTimeTicks = stream.RunTimeTicks;
}
video.PlayableStreamFileNames = stream.Files.ToList();
if (stream.Chapters != null)
{
2013-06-18 19:16:27 +00:00
chapters.Clear();
chapters.AddRange(stream.Chapters.Select(c => new ChapterInfo
2013-02-21 06:38:23 +00:00
{
StartPositionTicks = TimeSpan.FromSeconds(c).Ticks
2013-06-18 19:16:27 +00:00
}));
2013-02-21 06:38:23 +00:00
}
}
/// <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)
{
2013-02-24 21:53:54 +00:00
return _blurayExaminer.GetDiscInfo(path);
2013-02-21 06:38:23 +00:00
}
2013-02-21 01:33:05 +00:00
}
}