jellyfin-server/MediaBrowser.Controller/Entities/Video.cs

289 lines
9.6 KiB
C#
Raw Normal View History

using MediaBrowser.Controller.Persistence;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Controller.Resolvers;
using MediaBrowser.Model.Entities;
using System;
2013-06-18 19:16:27 +00:00
using System.Collections;
2013-02-21 01:33:05 +00:00
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Threading;
using System.Threading.Tasks;
2013-02-21 01:33:05 +00:00
namespace MediaBrowser.Controller.Entities
{
/// <summary>
/// Class Video
/// </summary>
2014-02-08 20:02:35 +00:00
public class Video : BaseItem, IHasMediaStreams, IHasAspectRatio, IHasTags
2013-02-21 01:33:05 +00:00
{
public bool IsMultiPart { get; set; }
public List<Guid> AdditionalPartIds { get; set; }
2013-05-02 02:44:52 +00:00
public Video()
{
PlayableStreamFileNames = new List<string>();
AdditionalPartIds = new List<Guid>();
2014-02-08 20:02:35 +00:00
Tags = new List<string>();
2013-05-02 02:44:52 +00:00
}
2013-12-06 03:39:44 +00:00
/// <summary>
/// Gets or sets a value indicating whether this instance has subtitles.
/// </summary>
/// <value><c>true</c> if this instance has subtitles; otherwise, <c>false</c>.</value>
public bool HasSubtitles { get; set; }
2014-02-08 20:02:35 +00:00
/// <summary>
/// Gets or sets the tags.
/// </summary>
/// <value>The tags.</value>
public List<string> Tags { get; set; }
2013-12-06 03:39:44 +00:00
/// <summary>
/// Gets or sets the video bit rate.
/// </summary>
/// <value>The video bit rate.</value>
public int? VideoBitRate { get; set; }
/// <summary>
/// Gets or sets the default index of the video stream.
/// </summary>
/// <value>The default index of the video stream.</value>
public int? DefaultVideoStreamIndex { get; set; }
2013-02-21 01:33:05 +00:00
/// <summary>
/// Gets or sets the type of the video.
/// </summary>
/// <value>The type of the video.</value>
public VideoType VideoType { get; set; }
/// <summary>
/// Gets or sets the type of the iso.
/// </summary>
/// <value>The type of the iso.</value>
public IsoType? IsoType { get; set; }
2013-06-25 18:10:39 +00:00
/// <summary>
/// Gets or sets the video3 D format.
/// </summary>
/// <value>The video3 D format.</value>
public Video3DFormat? Video3DFormat { get; set; }
2013-02-21 01:33:05 +00:00
/// <summary>
/// If the video is a folder-rip, this will hold the file list for the largest playlist
/// </summary>
public List<string> PlayableStreamFileNames { get; set; }
/// <summary>
/// Gets the playable stream files.
/// </summary>
/// <returns>List{System.String}.</returns>
public List<string> GetPlayableStreamFiles()
{
return GetPlayableStreamFiles(Path);
}
/// <summary>
/// Gets or sets the aspect ratio.
/// </summary>
/// <value>The aspect ratio.</value>
public string AspectRatio { get; set; }
2013-12-06 03:39:44 +00:00
2013-07-16 18:47:05 +00:00
[IgnoreDataMember]
public override string ContainingFolderPath
2013-07-16 18:47:05 +00:00
{
get
{
if (IsMultiPart)
{
return System.IO.Path.GetDirectoryName(Path);
}
2013-07-16 18:47:05 +00:00
if (VideoType == VideoType.BluRay || VideoType == VideoType.Dvd ||
VideoType == VideoType.HdDvd)
{
return Path;
}
return base.ContainingFolderPath;
2013-07-16 18:47:05 +00:00
}
}
2013-09-02 01:35:57 +00:00
public string MainFeaturePlaylistName { get; set; }
2013-02-21 01:33:05 +00:00
/// <summary>
/// Gets the playable stream files.
/// </summary>
/// <param name="rootPath">The root path.</param>
/// <returns>List{System.String}.</returns>
public List<string> GetPlayableStreamFiles(string rootPath)
{
if (PlayableStreamFileNames == null)
{
return null;
}
var allFiles = Directory.EnumerateFiles(rootPath, "*", SearchOption.AllDirectories).ToList();
2013-06-16 19:02:57 +00:00
return PlayableStreamFileNames.Select(name => allFiles.FirstOrDefault(f => string.Equals(System.IO.Path.GetFileName(f), name, StringComparison.OrdinalIgnoreCase)))
2013-02-21 01:33:05 +00:00
.Where(f => !string.IsNullOrEmpty(f))
.ToList();
}
/// <summary>
/// Gets a value indicating whether [is3 D].
/// </summary>
/// <value><c>true</c> if [is3 D]; otherwise, <c>false</c>.</value>
[IgnoreDataMember]
public bool Is3D
{
2013-06-25 18:10:39 +00:00
get { return Video3DFormat.HasValue; }
2013-02-21 01:33:05 +00:00
}
2013-10-03 14:14:40 +00:00
public bool IsHD { get; set; }
2013-02-21 01:33:05 +00:00
/// <summary>
/// Gets the type of the media.
/// </summary>
/// <value>The type of the media.</value>
public override string MediaType
{
get
{
return Model.Entities.MediaType.Video;
}
}
protected override async Task BeforeRefreshMetadata(MetadataRefreshOptions options, List<FileSystemInfo> fileSystemChildren, CancellationToken cancellationToken)
{
await base.BeforeRefreshMetadata(options, fileSystemChildren, cancellationToken).ConfigureAwait(false);
2013-06-17 20:35:43 +00:00
// Must have a parent to have additional parts
// In other words, it must be part of the Parent/Child tree
// The additional parts won't have additional parts themselves
if (IsMultiPart && LocationType == LocationType.FileSystem && Parent != null)
{
var additionalPartsChanged = await RefreshAdditionalParts(options, fileSystemChildren, cancellationToken).ConfigureAwait(false);
if (additionalPartsChanged)
{
options.ForceSave = true;
}
}
}
/// <summary>
/// Refreshes the additional parts.
/// </summary>
/// <param name="options">The options.</param>
/// <param name="fileSystemChildren">The file system children.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>Task{System.Boolean}.</returns>
2014-02-08 22:38:02 +00:00
private async Task<bool> RefreshAdditionalParts(MetadataRefreshOptions options, IEnumerable<FileSystemInfo> fileSystemChildren, CancellationToken cancellationToken)
{
var newItems = LoadAdditionalParts(fileSystemChildren).ToList();
var newItemIds = newItems.Select(i => i.Id).ToList();
var itemsChanged = !AdditionalPartIds.SequenceEqual(newItemIds);
var tasks = newItems.Select(i => i.RefreshMetadata(options, cancellationToken));
await Task.WhenAll(tasks).ConfigureAwait(false);
AdditionalPartIds = newItemIds;
return itemsChanged;
}
/// <summary>
/// Loads the additional parts.
/// </summary>
/// <returns>IEnumerable{Video}.</returns>
private IEnumerable<Video> LoadAdditionalParts(IEnumerable<FileSystemInfo> fileSystemChildren)
{
2013-06-16 19:02:57 +00:00
IEnumerable<FileSystemInfo> files;
2014-01-02 21:21:47 +00:00
var path = Path;
2013-06-16 19:02:57 +00:00
if (VideoType == VideoType.BluRay || VideoType == VideoType.Dvd)
{
files = fileSystemChildren.Where(i =>
2014-01-02 21:21:47 +00:00
{
if ((i.Attributes & FileAttributes.Directory) == FileAttributes.Directory)
{
return !string.Equals(i.FullName, path, StringComparison.OrdinalIgnoreCase) && EntityResolutionHelper.IsVideoFile(i.FullName) && EntityResolutionHelper.IsMultiPartFile(i.Name);
}
2014-01-02 21:21:47 +00:00
return false;
});
}
2013-06-16 19:02:57 +00:00
else
{
files = fileSystemChildren.Where(i =>
2013-06-16 19:02:57 +00:00
{
if ((i.Attributes & FileAttributes.Directory) == FileAttributes.Directory)
{
return false;
}
2014-01-02 21:21:47 +00:00
return !string.Equals(i.FullName, path, StringComparison.OrdinalIgnoreCase) && EntityResolutionHelper.IsVideoFile(i.FullName) && EntityResolutionHelper.IsMultiPartFile(i.Name);
2013-06-16 19:02:57 +00:00
});
}
return LibraryManager.ResolvePaths<Video>(files, null).Select(video =>
{
// Try to retrieve it from the db. If we don't find it, use the resolved version
2014-01-14 20:03:35 +00:00
var dbItem = LibraryManager.GetItemById(video.Id) as Video;
if (dbItem != null)
{
video = dbItem;
}
return video;
// Sort them so that the list can be easily compared for changes
}).OrderBy(i => i.Path).ToList();
}
public override IEnumerable<string> GetDeletePaths()
{
if (!IsInMixedFolder)
{
2013-12-02 02:24:14 +00:00
if (VideoType == VideoType.VideoFile || VideoType == VideoType.Iso)
{
return new[] { System.IO.Path.GetDirectoryName(Path) };
}
}
return base.GetDeletePaths();
}
2013-12-08 01:42:15 +00:00
public virtual IEnumerable<MediaStream> GetMediaStreams()
{
return ItemRepository.GetMediaStreams(new MediaStreamQuery
{
ItemId = Id
});
}
2013-12-08 01:42:15 +00:00
public virtual MediaStream GetDefaultVideoStream()
{
if (!DefaultVideoStreamIndex.HasValue)
{
return null;
}
return ItemRepository.GetMediaStreams(new MediaStreamQuery
{
ItemId = Id,
Index = DefaultVideoStreamIndex.Value
}).FirstOrDefault();
}
2013-02-21 01:33:05 +00:00
}
}