2014-02-04 21:19:29 +01:00
|
|
|
|
using DvdLib.Ifo;
|
2014-02-09 22:11:11 +01:00
|
|
|
|
using MediaBrowser.Common.Configuration;
|
2014-02-04 21:19:29 +01:00
|
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
|
using MediaBrowser.Controller.Library;
|
|
|
|
|
using MediaBrowser.Controller.Localization;
|
2014-02-20 17:37:41 +01:00
|
|
|
|
using MediaBrowser.Controller.MediaEncoding;
|
2014-02-04 21:19:29 +01:00
|
|
|
|
using MediaBrowser.Controller.Persistence;
|
2014-02-10 19:39:41 +01:00
|
|
|
|
using MediaBrowser.Controller.Providers;
|
2014-02-04 21:19:29 +01:00
|
|
|
|
using MediaBrowser.Model.Entities;
|
|
|
|
|
using MediaBrowser.Model.IO;
|
|
|
|
|
using MediaBrowser.Model.Logging;
|
|
|
|
|
using MediaBrowser.Model.MediaInfo;
|
2014-02-09 22:11:11 +01:00
|
|
|
|
using MediaBrowser.Model.Serialization;
|
2014-02-04 21:19:29 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Globalization;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2014-04-18 07:03:01 +02:00
|
|
|
|
using MediaBrowser.Common.Extensions;
|
2014-02-04 21:19:29 +01:00
|
|
|
|
|
|
|
|
|
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;
|
2014-02-09 22:11:11 +01:00
|
|
|
|
private readonly IApplicationPaths _appPaths;
|
|
|
|
|
private readonly IJsonSerializer _json;
|
2014-02-20 17:37:41 +01:00
|
|
|
|
private readonly IEncodingManager _encodingManager;
|
2014-02-04 21:19:29 +01:00
|
|
|
|
|
|
|
|
|
private readonly CultureInfo _usCulture = new CultureInfo("en-US");
|
|
|
|
|
|
2014-02-20 17:37:41 +01:00
|
|
|
|
public FFProbeVideoInfo(ILogger logger, IIsoManager isoManager, IMediaEncoder mediaEncoder, IItemRepository itemRepo, IBlurayExaminer blurayExaminer, ILocalizationManager localization, IApplicationPaths appPaths, IJsonSerializer json, IEncodingManager encodingManager)
|
2014-02-04 21:19:29 +01:00
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_isoManager = isoManager;
|
|
|
|
|
_mediaEncoder = mediaEncoder;
|
|
|
|
|
_itemRepo = itemRepo;
|
|
|
|
|
_blurayExaminer = blurayExaminer;
|
|
|
|
|
_localization = localization;
|
2014-02-09 22:11:11 +01:00
|
|
|
|
_appPaths = appPaths;
|
|
|
|
|
_json = json;
|
2014-02-20 17:37:41 +01:00
|
|
|
|
_encodingManager = encodingManager;
|
2014-02-04 21:19:29 +01:00
|
|
|
|
}
|
|
|
|
|
|
2014-02-10 19:39:41 +01:00
|
|
|
|
public async Task<ItemUpdateType> ProbeVideo<T>(T item, IDirectoryService directoryService, CancellationToken cancellationToken)
|
2014-02-04 21:19:29 +01:00
|
|
|
|
where T : Video
|
|
|
|
|
{
|
|
|
|
|
var isoMount = await MountIsoIfNeeded(item, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
2014-03-05 03:59:59 +01:00
|
|
|
|
BlurayDiscInfo blurayDiscInfo = null;
|
|
|
|
|
|
2014-02-04 21:19:29 +01:00
|
|
|
|
try
|
|
|
|
|
{
|
2014-03-05 03:59:59 +01:00
|
|
|
|
if (item.VideoType == VideoType.BluRay || (item.IsoType.HasValue && item.IsoType == IsoType.BluRay))
|
|
|
|
|
{
|
|
|
|
|
var inputPath = isoMount != null ? isoMount.MountedPath : item.Path;
|
|
|
|
|
|
|
|
|
|
blurayDiscInfo = GetBDInfo(inputPath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
OnPreFetch(item, isoMount, blurayDiscInfo);
|
2014-02-04 21:19:29 +01:00
|
|
|
|
|
|
|
|
|
// If we didn't find any satisfying the min length, just take them all
|
|
|
|
|
if (item.VideoType == VideoType.Dvd || (item.IsoType.HasValue && item.IsoType == IsoType.Dvd))
|
|
|
|
|
{
|
|
|
|
|
if (item.PlayableStreamFileNames.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
_logger.Error("No playable vobs found in dvd structure, skipping ffprobe.");
|
|
|
|
|
return ItemUpdateType.MetadataImport;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-05 03:59:59 +01:00
|
|
|
|
if (item.VideoType == VideoType.BluRay || (item.IsoType.HasValue && item.IsoType == IsoType.BluRay))
|
|
|
|
|
{
|
|
|
|
|
if (item.PlayableStreamFileNames.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
_logger.Error("No playable vobs found in bluray structure, skipping ffprobe.");
|
|
|
|
|
return ItemUpdateType.MetadataImport;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-04 21:19:29 +01:00
|
|
|
|
var result = await GetMediaInfo(item, isoMount, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
|
|
|
|
|
FFProbeHelpers.NormalizeFFProbeResult(result);
|
|
|
|
|
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
|
2014-03-05 03:59:59 +01:00
|
|
|
|
await Fetch(item, cancellationToken, result, isoMount, blurayDiscInfo, directoryService).ConfigureAwait(false);
|
2014-02-04 21:19:29 +01:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
if (isoMount != null)
|
|
|
|
|
{
|
|
|
|
|
isoMount.Dispose();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ItemUpdateType.MetadataImport;
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-26 22:05:31 +01:00
|
|
|
|
private const string SchemaVersion = "1";
|
|
|
|
|
|
2014-02-04 21:19:29 +01:00
|
|
|
|
private async Task<InternalMediaInfoResult> GetMediaInfo(BaseItem item, IIsoMount isoMount, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
|
2014-02-09 22:11:11 +01:00
|
|
|
|
var idString = item.Id.ToString("N");
|
2014-03-26 22:05:31 +01:00
|
|
|
|
var cachePath = Path.Combine(_appPaths.CachePath,
|
|
|
|
|
"ffprobe-video",
|
|
|
|
|
idString.Substring(0, 2), idString, "v" + SchemaVersion + _mediaEncoder.Version + item.DateModified.Ticks.ToString(_usCulture) + ".json");
|
2014-02-09 22:11:11 +01:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return _json.DeserializeFromFile<InternalMediaInfoResult>(cachePath);
|
|
|
|
|
}
|
|
|
|
|
catch (FileNotFoundException)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
catch (DirectoryNotFoundException)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-04 21:19:29 +01:00
|
|
|
|
var type = InputType.File;
|
|
|
|
|
var inputPath = isoMount == null ? new[] { item.Path } : new[] { isoMount.MountedPath };
|
|
|
|
|
|
|
|
|
|
var video = item as Video;
|
|
|
|
|
|
|
|
|
|
if (video != null)
|
|
|
|
|
{
|
|
|
|
|
inputPath = MediaEncoderHelpers.GetInputArgument(video.Path, video.LocationType == LocationType.Remote, video.VideoType, video.IsoType, isoMount, video.PlayableStreamFileNames, out type);
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-09 22:11:11 +01:00
|
|
|
|
var result = await _mediaEncoder.GetMediaInfo(inputPath, type, false, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
Directory.CreateDirectory(Path.GetDirectoryName(cachePath));
|
|
|
|
|
_json.SerializeToFile(result, cachePath);
|
|
|
|
|
|
|
|
|
|
return result;
|
2014-02-04 21:19:29 +01:00
|
|
|
|
}
|
|
|
|
|
|
2014-03-05 03:59:59 +01:00
|
|
|
|
protected async Task Fetch(Video video, CancellationToken cancellationToken, InternalMediaInfoResult data, IIsoMount isoMount, BlurayDiscInfo blurayInfo, IDirectoryService directoryService)
|
2014-02-04 21:19:29 +01:00
|
|
|
|
{
|
2014-04-22 19:25:54 +02:00
|
|
|
|
var mediaInfo = MediaEncoderHelpers.GetMediaInfo(data);
|
|
|
|
|
var mediaStreams = mediaInfo.MediaStreams;
|
|
|
|
|
|
|
|
|
|
video.TotalBitrate = mediaInfo.TotalBitrate;
|
|
|
|
|
video.FormatName = (mediaInfo.Format ?? string.Empty)
|
|
|
|
|
.Replace("matroska", "mkv", StringComparison.OrdinalIgnoreCase);
|
|
|
|
|
|
2014-02-04 21:19:29 +01:00
|
|
|
|
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;
|
|
|
|
|
}
|
2014-04-18 07:03:01 +02:00
|
|
|
|
|
|
|
|
|
if (video.VideoType == VideoType.VideoFile)
|
|
|
|
|
{
|
|
|
|
|
var extension = (Path.GetExtension(video.Path) ?? string.Empty).TrimStart('.');
|
|
|
|
|
|
|
|
|
|
video.Container = extension;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
video.Container = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(data.format.size))
|
|
|
|
|
{
|
|
|
|
|
video.Size = long.Parse(data.format.size, _usCulture);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
video.Size = null;
|
|
|
|
|
}
|
2014-02-04 21:19:29 +01:00
|
|
|
|
}
|
|
|
|
|
|
2014-03-26 22:05:31 +01:00
|
|
|
|
var mediaChapters = (data.Chapters ?? new MediaChapter[] { }).ToList();
|
|
|
|
|
var chapters = mediaChapters.Select(GetChapterInfo).ToList();
|
2014-02-04 21:19:29 +01:00
|
|
|
|
|
|
|
|
|
if (video.VideoType == VideoType.BluRay || (video.IsoType.HasValue && video.IsoType.Value == IsoType.BluRay))
|
|
|
|
|
{
|
2014-03-05 03:59:59 +01:00
|
|
|
|
FetchBdInfo(video, chapters, mediaStreams, blurayInfo);
|
2014-02-04 21:19:29 +01:00
|
|
|
|
}
|
|
|
|
|
|
2014-02-10 19:39:41 +01:00
|
|
|
|
AddExternalSubtitles(video, mediaStreams, directoryService);
|
2014-02-04 21:19:29 +01:00
|
|
|
|
|
|
|
|
|
FetchWtvInfo(video, 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);
|
|
|
|
|
|
2014-02-20 17:37:41 +01:00
|
|
|
|
await _encodingManager.RefreshChapterImages(new ChapterImageRefreshOptions
|
|
|
|
|
{
|
|
|
|
|
Chapters = chapters,
|
|
|
|
|
Video = video,
|
|
|
|
|
ExtractImages = false,
|
|
|
|
|
SaveChapters = false
|
|
|
|
|
|
|
|
|
|
}, cancellationToken).ConfigureAwait(false);
|
2014-02-04 21:19:29 +01:00
|
|
|
|
|
|
|
|
|
await _itemRepo.SaveMediaStreams(video.Id, mediaStreams, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
await _itemRepo.SaveChapters(video.Id, chapters, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-26 22:05:31 +01:00
|
|
|
|
private ChapterInfo GetChapterInfo(MediaChapter chapter)
|
|
|
|
|
{
|
|
|
|
|
var info = new ChapterInfo();
|
|
|
|
|
|
|
|
|
|
if (chapter.tags != null)
|
|
|
|
|
{
|
|
|
|
|
string name;
|
|
|
|
|
if (chapter.tags.TryGetValue("title", out name))
|
|
|
|
|
{
|
|
|
|
|
info.Name = name;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
info.StartPositionTicks = chapter.start/100;
|
|
|
|
|
|
|
|
|
|
return info;
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-05 03:59:59 +01:00
|
|
|
|
private void FetchBdInfo(BaseItem item, List<ChapterInfo> chapters, List<MediaStream> mediaStreams, BlurayDiscInfo blurayInfo)
|
2014-02-04 21:19:29 +01:00
|
|
|
|
{
|
|
|
|
|
var video = (Video)item;
|
|
|
|
|
|
|
|
|
|
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();
|
2014-03-05 03:59:59 +01:00
|
|
|
|
mediaStreams.AddRange(blurayInfo.MediaStreams);
|
2014-02-04 21:19:29 +01:00
|
|
|
|
|
2014-03-05 03:59:59 +01:00
|
|
|
|
video.MainFeaturePlaylistName = blurayInfo.PlaylistName;
|
2014-02-04 21:19:29 +01:00
|
|
|
|
|
2014-03-05 03:59:59 +01:00
|
|
|
|
if (blurayInfo.RunTimeTicks.HasValue && blurayInfo.RunTimeTicks.Value > 0)
|
2014-02-04 21:19:29 +01:00
|
|
|
|
{
|
2014-03-05 03:59:59 +01:00
|
|
|
|
video.RunTimeTicks = blurayInfo.RunTimeTicks;
|
2014-02-04 21:19:29 +01:00
|
|
|
|
}
|
|
|
|
|
|
2014-03-05 03:59:59 +01:00
|
|
|
|
video.PlayableStreamFileNames = blurayInfo.Files.ToList();
|
2014-02-04 21:19:29 +01:00
|
|
|
|
|
2014-03-05 03:59:59 +01:00
|
|
|
|
if (blurayInfo.Chapters != null)
|
2014-02-04 21:19:29 +01:00
|
|
|
|
{
|
|
|
|
|
chapters.Clear();
|
|
|
|
|
|
2014-03-05 03:59:59 +01:00
|
|
|
|
chapters.AddRange(blurayInfo.Chapters.Select(c => new ChapterInfo
|
2014-02-04 21:19:29 +01:00
|
|
|
|
{
|
|
|
|
|
StartPositionTicks = TimeSpan.FromSeconds(c).Ticks
|
|
|
|
|
|
|
|
|
|
}));
|
|
|
|
|
}
|
2014-03-05 03:59:59 +01:00
|
|
|
|
|
|
|
|
|
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;
|
2014-02-04 21:19:29 +01: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)
|
|
|
|
|
{
|
|
|
|
|
return _blurayExaminer.GetDiscInfo(path);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void FetchWtvInfo(Video video, InternalMediaInfoResult data)
|
|
|
|
|
{
|
|
|
|
|
if (data.format == null || data.format.tags == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (video.Genres.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
if (!video.LockedFields.Contains(MetadataFields.Genres))
|
|
|
|
|
{
|
|
|
|
|
var genres = FFProbeHelpers.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 (string.IsNullOrEmpty(video.Overview))
|
|
|
|
|
{
|
|
|
|
|
if (!video.LockedFields.Contains(MetadataFields.Overview))
|
|
|
|
|
{
|
|
|
|
|
var overview = FFProbeHelpers.GetDictionaryValue(data.format.tags, "WM/SubTitleDescription");
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(overview))
|
|
|
|
|
{
|
|
|
|
|
video.Overview = overview;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(video.OfficialRating))
|
|
|
|
|
{
|
|
|
|
|
var officialRating = FFProbeHelpers.GetDictionaryValue(data.format.tags, "WM/ParentalRating");
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(officialRating))
|
|
|
|
|
{
|
|
|
|
|
if (!video.LockedFields.Contains(MetadataFields.OfficialRating))
|
|
|
|
|
{
|
|
|
|
|
video.OfficialRating = officialRating;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (video.People.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
if (!video.LockedFields.Contains(MetadataFields.Cast))
|
|
|
|
|
{
|
|
|
|
|
var people = FFProbeHelpers.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 (!video.ProductionYear.HasValue)
|
|
|
|
|
{
|
|
|
|
|
var year = FFProbeHelpers.GetDictionaryValue(data.format.tags, "WM/OriginalReleaseTime");
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(year))
|
|
|
|
|
{
|
|
|
|
|
int val;
|
|
|
|
|
|
|
|
|
|
if (int.TryParse(year, NumberStyles.Integer, _usCulture, out val))
|
|
|
|
|
{
|
|
|
|
|
video.ProductionYear = val;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private IEnumerable<string> SubtitleExtensions
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return new[] { ".srt", ".ssa", ".ass" };
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-12 20:56:12 +01:00
|
|
|
|
public IEnumerable<FileSystemInfo> GetSubtitleFiles(Video video, IDirectoryService directoryService)
|
2014-02-10 19:39:41 +01:00
|
|
|
|
{
|
|
|
|
|
var containingPath = video.ContainingFolderPath;
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(containingPath))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException(string.Format("Cannot search for items that don't have a path: {0} {1}", video.Name, video.Id));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var files = directoryService.GetFiles(containingPath);
|
|
|
|
|
|
|
|
|
|
var videoFileNameWithoutExtension = Path.GetFileNameWithoutExtension(video.Path);
|
|
|
|
|
|
|
|
|
|
return files.Where(i =>
|
|
|
|
|
{
|
|
|
|
|
if (!i.Attributes.HasFlag(FileAttributes.Directory) &&
|
|
|
|
|
SubtitleExtensions.Contains(i.Extension, StringComparer.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
var fullName = i.FullName;
|
|
|
|
|
|
|
|
|
|
var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fullName);
|
|
|
|
|
|
|
|
|
|
if (string.Equals(videoFileNameWithoutExtension, fileNameWithoutExtension, StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if (fileNameWithoutExtension.StartsWith(videoFileNameWithoutExtension + ".", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-04 21:19:29 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Adds the external subtitles.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="video">The video.</param>
|
|
|
|
|
/// <param name="currentStreams">The current streams.</param>
|
2014-02-10 19:39:41 +01:00
|
|
|
|
private void AddExternalSubtitles(Video video, List<MediaStream> currentStreams, IDirectoryService directoryService)
|
2014-02-04 21:19:29 +01:00
|
|
|
|
{
|
2014-02-10 19:39:41 +01:00
|
|
|
|
var files = GetSubtitleFiles(video, directoryService);
|
|
|
|
|
|
|
|
|
|
var startIndex = currentStreams.Count;
|
|
|
|
|
var streams = new List<MediaStream>();
|
|
|
|
|
|
|
|
|
|
var videoFileNameWithoutExtension = Path.GetFileNameWithoutExtension(video.Path);
|
|
|
|
|
|
|
|
|
|
foreach (var file in files)
|
|
|
|
|
{
|
|
|
|
|
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
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
video.SubtitleFiles = streams.Select(i => i.Path).OrderBy(i => i).ToList();
|
|
|
|
|
|
|
|
|
|
currentStreams.AddRange(streams);
|
2014-02-04 21:19:29 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <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>
|
|
|
|
|
/// Called when [pre fetch].
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="item">The item.</param>
|
|
|
|
|
/// <param name="mount">The mount.</param>
|
2014-03-05 03:59:59 +01:00
|
|
|
|
private void OnPreFetch(Video item, IIsoMount mount, BlurayDiscInfo blurayDiscInfo)
|
2014-02-04 21:19:29 +01:00
|
|
|
|
{
|
|
|
|
|
if (item.VideoType == VideoType.Iso)
|
|
|
|
|
{
|
|
|
|
|
item.IsoType = DetermineIsoType(mount);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (item.VideoType == VideoType.Dvd || (item.IsoType.HasValue && item.IsoType == IsoType.Dvd))
|
|
|
|
|
{
|
|
|
|
|
FetchFromDvdLib(item, mount);
|
|
|
|
|
}
|
2014-03-05 03:59:59 +01:00
|
|
|
|
|
|
|
|
|
if (item.VideoType == VideoType.BluRay || (item.IsoType.HasValue && item.IsoType.Value == IsoType.BluRay))
|
|
|
|
|
{
|
|
|
|
|
item.PlayableStreamFileNames = blurayDiscInfo.Files.ToList();
|
|
|
|
|
}
|
2014-02-04 21:19:29 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void FetchFromDvdLib(Video item, IIsoMount mount)
|
|
|
|
|
{
|
|
|
|
|
var path = mount == null ? item.Path : mount.MountedPath;
|
|
|
|
|
var dvd = new Dvd(path);
|
2014-02-22 23:40:44 +01:00
|
|
|
|
|
2014-02-04 21:19:29 +01:00
|
|
|
|
var primaryTitle = dvd.Titles.OrderByDescending(GetRuntime).FirstOrDefault();
|
|
|
|
|
|
2014-02-22 23:40:44 +01:00
|
|
|
|
byte? titleNumber = null;
|
2014-02-04 21:19:29 +01:00
|
|
|
|
|
|
|
|
|
if (primaryTitle != null)
|
|
|
|
|
{
|
2014-02-22 23:40:44 +01:00
|
|
|
|
titleNumber = primaryTitle.VideoTitleSetNumber;
|
2014-02-22 21:20:22 +01:00
|
|
|
|
item.RunTimeTicks = GetRuntime(primaryTitle);
|
2014-02-04 21:19:29 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <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 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
|
2014-02-24 04:27:13 +01:00
|
|
|
|
var allVobs = new DirectoryInfo(root).EnumerateFiles("*", SearchOption.AllDirectories)
|
|
|
|
|
.Where(file => string.Equals(file.Extension, ".vob", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
.OrderBy(i => i.FullName)
|
2014-02-04 21:19:29 +01:00
|
|
|
|
.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));
|
2014-02-24 04:27:13 +01:00
|
|
|
|
var vobs = allVobs.Where(i => i.Name.StartsWith(prefix, StringComparison.OrdinalIgnoreCase)).ToList();
|
2014-02-04 21:19:29 +01:00
|
|
|
|
|
|
|
|
|
if (vobs.Count > 0)
|
|
|
|
|
{
|
2014-02-24 04:48:27 +01:00
|
|
|
|
var minSizeVobs = vobs
|
2014-02-24 04:27:13 +01:00
|
|
|
|
.SkipWhile(f => f.Length < minPlayableSize)
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
return minSizeVobs.Count == 0 ? vobs.Select(i => i.FullName) : minSizeVobs.Select(i => i.FullName);
|
2014-02-04 21:19:29 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_logger.Debug("Could not determine vob file list for {0} using DvdLib. Will scan using file sizes.", video.Path);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var files = allVobs
|
2014-02-24 04:27:13 +01:00
|
|
|
|
.SkipWhile(f => f.Length < minPlayableSize)
|
2014-02-04 21:19:29 +01:00
|
|
|
|
.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)
|
|
|
|
|
{
|
2014-02-24 04:27:13 +01:00
|
|
|
|
var parts = Path.GetFileNameWithoutExtension(files[0].FullName).Split('_');
|
2014-02-04 21:19:29 +01:00
|
|
|
|
|
|
|
|
|
if (parts.Length == 3)
|
|
|
|
|
{
|
|
|
|
|
var title = parts[1];
|
|
|
|
|
|
|
|
|
|
files = files.TakeWhile(f =>
|
|
|
|
|
{
|
2014-02-24 04:27:13 +01:00
|
|
|
|
var fileParts = Path.GetFileNameWithoutExtension(f.FullName).Split('_');
|
2014-02-04 21:19:29 +01:00
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-24 04:27:13 +01:00
|
|
|
|
return files.Select(i => i.FullName);
|
2014-02-04 21:19:29 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|