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

699 lines
22 KiB
C#
Raw Normal View History

using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Persistence;
using MediaBrowser.Controller.Providers;
2014-06-03 02:01:30 +00:00
using MediaBrowser.Model.Dto;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.MediaInfo;
using System;
2013-02-21 01:33:05 +00:00
using System.Collections.Generic;
2016-03-19 15:38:05 +00:00
using System.Globalization;
2013-02-21 01:33:05 +00:00
using System.Linq;
using System.Runtime.Serialization;
using System.Threading;
using System.Threading.Tasks;
2015-10-04 04:23:11 +00:00
using CommonIO;
2016-03-19 15:38:05 +00:00
using MediaBrowser.Controller.Channels;
2013-02-21 01:33:05 +00:00
namespace MediaBrowser.Controller.Entities
{
/// <summary>
/// Class Video
/// </summary>
2014-12-03 03:13:03 +00:00
public class Video : BaseItem,
IHasAspectRatio,
IHasTags,
2014-06-03 02:01:30 +00:00
ISupportsPlaceHolders,
2014-06-24 21:45:21 +00:00
IHasMediaSources,
2014-08-26 02:30:52 +00:00
IHasShortOverview,
2015-03-17 02:46:21 +00:00
IThemeMedia,
IArchivable
2013-02-21 01:33:05 +00:00
{
public Guid? PrimaryVersionId { get; set; }
2014-12-03 03:13:03 +00:00
public List<string> AdditionalParts { get; set; }
public List<string> LocalAlternateVersions { get; set; }
public List<LinkedChild> LinkedAlternateVersions { get; set; }
2016-03-19 15:38:05 +00:00
public List<ChannelMediaInfo> ChannelMediaSources { get; set; }
2015-09-20 02:06:56 +00:00
[IgnoreDataMember]
public bool IsThemeMedia
{
get
{
return ExtraType.HasValue && ExtraType.Value == Model.Entities.ExtraType.ThemeVideo;
}
}
2014-12-03 03:13:03 +00:00
2014-04-18 05:03:01 +00:00
public long? Size { get; set; }
public string Container { get; set; }
2014-04-22 17:25:54 +00:00
public int? TotalBitrate { get; set; }
2014-06-24 21:45:21 +00:00
public string ShortOverview { get; set; }
2015-09-20 02:06:56 +00:00
public ExtraType? ExtraType { get; set; }
2014-04-18 05:03:01 +00:00
/// <summary>
/// Gets or sets the timestamp.
/// </summary>
/// <value>The timestamp.</value>
2014-04-25 02:45:06 +00:00
public TransportStreamTimestamp? Timestamp { get; set; }
2014-12-03 03:13:03 +00:00
2013-05-02 02:44:52 +00:00
public Video()
{
PlayableStreamFileNames = new List<string>();
2014-12-03 03:13:03 +00:00
AdditionalParts = new List<string>();
LocalAlternateVersions = new List<string>();
2014-02-08 20:02:35 +00:00
Tags = new List<string>();
2014-02-10 18:39:41 +00:00
SubtitleFiles = new List<string>();
LinkedAlternateVersions = new List<LinkedChild>();
}
2015-02-06 05:39:07 +00:00
public override bool CanDownload()
{
if (VideoType == VideoType.HdDvd || VideoType == VideoType.Dvd ||
VideoType == VideoType.BluRay)
{
return false;
}
var locationType = LocationType;
return locationType != LocationType.Remote &&
locationType != LocationType.Virtual;
}
2016-03-19 15:38:05 +00:00
[IgnoreDataMember]
public override LocationType LocationType
{
get
{
if (SourceType == SourceType.Channel)
{
if (string.IsNullOrEmpty(Path))
{
return LocationType.Remote;
}
}
return base.LocationType;
}
}
2014-08-30 14:26:29 +00:00
[IgnoreDataMember]
public override bool SupportsAddingToPlaylist
{
get { return LocationType == LocationType.FileSystem && RunTimeTicks.HasValue; }
}
[IgnoreDataMember]
2014-03-22 16:16:43 +00:00
public int MediaSourceCount
{
get
{
2014-12-03 03:13:03 +00:00
return LinkedAlternateVersions.Count + LocalAlternateVersions.Count + 1;
}
}
2014-12-03 03:13:03 +00:00
[IgnoreDataMember]
public bool IsStacked
{
get { return AdditionalParts.Count > 0; }
}
[IgnoreDataMember]
public bool HasLocalAlternateVersions
{
get { return LocalAlternateVersions.Count > 0; }
}
2014-12-29 20:18:48 +00:00
[IgnoreDataMember]
public bool IsArchive
{
get
{
if (string.IsNullOrWhiteSpace(Path))
{
return false;
}
var ext = System.IO.Path.GetExtension(Path) ?? string.Empty;
return new[] { ".zip", ".rar", ".7z" }.Contains(ext, StringComparer.OrdinalIgnoreCase);
}
}
2014-12-03 03:13:03 +00:00
public IEnumerable<Guid> GetAdditionalPartIds()
{
return AdditionalParts.Select(i => LibraryManager.GetNewItemId(i, typeof(Video)));
}
public IEnumerable<Guid> GetLocalAlternateVersionIds()
{
return LocalAlternateVersions.Select(i => LibraryManager.GetNewItemId(i, typeof(Video)));
}
2016-03-19 15:38:05 +00:00
protected override string CreateUserDataKey()
{
if (ExtraType.HasValue)
{
var key = this.GetProviderId(MetadataProviders.Imdb) ?? this.GetProviderId(MetadataProviders.Tmdb);
if (!string.IsNullOrWhiteSpace(key))
{
key = key + "-" + ExtraType.ToString().ToLower();
// Make sure different trailers have their own data.
if (RunTimeTicks.HasValue)
{
key += "-" + RunTimeTicks.Value.ToString(CultureInfo.InvariantCulture);
}
return key;
}
}
return base.CreateUserDataKey();
}
/// <summary>
/// Gets the linked children.
/// </summary>
/// <returns>IEnumerable{BaseItem}.</returns>
2014-03-19 01:35:40 +00:00
public IEnumerable<Video> GetAlternateVersions()
{
2014-12-03 03:13:03 +00:00
var filesWithinSameDirectory = GetLocalAlternateVersionIds()
.Select(i => LibraryManager.GetItemById(i))
.Where(i => i != null)
.OfType<Video>();
2014-03-19 01:35:40 +00:00
return filesWithinSameDirectory.Concat(GetLinkedAlternateVersions())
.OrderBy(i => i.SortName);
}
public IEnumerable<Video> GetLinkedAlternateVersions()
{
var linkedVersions = LinkedAlternateVersions
.Select(GetLinkedChild)
.Where(i => i != null)
.OfType<Video>();
2014-03-19 01:35:40 +00:00
return linkedVersions
.OrderBy(i => i.SortName);
}
/// <summary>
/// Gets the additional parts.
/// </summary>
/// <returns>IEnumerable{Video}.</returns>
public IEnumerable<Video> GetAdditionalParts()
{
2014-12-03 03:13:03 +00:00
return GetAdditionalPartIds()
.Select(i => LibraryManager.GetItemById(i))
.Where(i => i != null)
.OfType<Video>()
.OrderBy(i => i.SortName);
2013-05-02 02:44:52 +00:00
}
2014-02-10 18:39:41 +00:00
/// <summary>
/// Gets or sets the subtitle paths.
/// </summary>
/// <value>The subtitle paths.</value>
public List<string> SubtitleFiles { get; set; }
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; }
public bool IsPlaceHolder { get; set; }
2014-10-14 04:22:17 +00:00
public bool IsShortcut { get; set; }
public string ShortcutPath { 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
{
2014-12-03 03:13:03 +00:00
if (IsStacked)
{
return System.IO.Path.GetDirectoryName(Path);
}
2013-07-16 18:47:05 +00:00
2014-03-05 02:59:23 +00:00
if (!IsPlaceHolder)
{
2014-03-05 02:59:23 +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
}
}
2014-12-09 04:57:18 +00:00
[IgnoreDataMember]
public override string FileNameWithoutExtension
{
get
{
if (LocationType == LocationType.FileSystem)
{
if (VideoType == VideoType.BluRay || VideoType == VideoType.Dvd || VideoType == VideoType.HdDvd)
{
return System.IO.Path.GetFileName(Path);
}
2014-12-29 20:18:48 +00:00
2014-12-09 04:57:18 +00:00
return System.IO.Path.GetFileNameWithoutExtension(Path);
}
return null;
}
}
2014-12-03 03:13:03 +00:00
internal override bool IsValidFromResolver(BaseItem newItem)
{
var current = this;
var newAsVideo = newItem as Video;
if (newAsVideo != null)
{
if (!current.AdditionalParts.SequenceEqual(newAsVideo.AdditionalParts, StringComparer.OrdinalIgnoreCase))
{
return false;
}
if (!current.LocalAlternateVersions.SequenceEqual(newAsVideo.LocalAlternateVersions, StringComparer.OrdinalIgnoreCase))
{
return false;
}
}
return base.IsValidFromResolver(newItem);
}
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)
{
2015-09-24 17:50:49 +00:00
var allFiles = FileSystem.GetFilePaths(rootPath, true).ToList();
2013-02-21 01:33:05 +00:00
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
}
/// <summary>
/// Gets the type of the media.
/// </summary>
/// <value>The type of the media.</value>
2014-08-30 14:26:29 +00:00
[IgnoreDataMember]
2013-02-21 01:33:05 +00:00
public override string MediaType
{
get
{
return Model.Entities.MediaType.Video;
}
}
2015-10-04 03:38:46 +00:00
protected override async Task<bool> RefreshedOwnedItems(MetadataRefreshOptions options, List<FileSystemMetadata> fileSystemChildren, CancellationToken cancellationToken)
{
2014-02-10 18:39:41 +00:00
var hasChanges = await base.RefreshedOwnedItems(options, fileSystemChildren, cancellationToken).ConfigureAwait(false);
2014-12-03 03:13:03 +00:00
if (IsStacked)
{
var tasks = AdditionalParts
.Select(i => RefreshMetadataForOwnedVideo(options, i, cancellationToken));
await Task.WhenAll(tasks).ConfigureAwait(false);
}
// Must have a parent to have additional parts or alternate versions
2013-06-17 20:35:43 +00:00
// In other words, it must be part of the Parent/Child tree
// The additional parts won't have additional parts themselves
2015-11-11 14:56:31 +00:00
if (LocationType == LocationType.FileSystem && GetParent() != null)
{
2014-12-03 03:13:03 +00:00
if (!IsStacked)
{
RefreshLinkedAlternateVersions();
2014-12-03 03:13:03 +00:00
var tasks = LocalAlternateVersions
.Select(i => RefreshMetadataForOwnedVideo(options, i, cancellationToken));
2014-12-03 03:13:03 +00:00
await Task.WhenAll(tasks).ConfigureAwait(false);
}
}
2014-02-10 18:39:41 +00:00
return hasChanges;
}
2014-12-03 03:13:03 +00:00
private void RefreshLinkedAlternateVersions()
{
foreach (var child in LinkedAlternateVersions)
{
// Reset the cached value
if (child.ItemId.HasValue && child.ItemId.Value == Guid.Empty)
{
child.ItemId = null;
}
}
}
public override async Task UpdateToRepository(ItemUpdateType updateReason, CancellationToken cancellationToken)
{
await base.UpdateToRepository(updateReason, cancellationToken).ConfigureAwait(false);
2014-12-17 05:30:31 +00:00
var localAlternates = GetLocalAlternateVersionIds()
.Select(i => LibraryManager.GetItemById(i))
.Where(i => i != null);
foreach (var item in localAlternates)
{
item.ImageInfos = ImageInfos;
item.Overview = Overview;
item.ProductionYear = ProductionYear;
item.PremiereDate = PremiereDate;
item.CommunityRating = CommunityRating;
item.OfficialRating = OfficialRating;
item.Genres = Genres;
item.ProviderIds = ProviderIds;
2014-04-08 04:17:18 +00:00
await item.UpdateToRepository(ItemUpdateType.MetadataDownload, cancellationToken).ConfigureAwait(false);
}
}
public override IEnumerable<string> GetDeletePaths()
{
if (!IsInMixedFolder)
{
2014-03-05 02:59:23 +00:00
return new[] { ContainingFolderPath };
}
return base.GetDeletePaths();
}
2015-03-03 07:03:17 +00:00
public IEnumerable<MediaStream> GetMediaStreams()
{
2015-03-03 07:03:17 +00:00
var mediaSource = GetMediaSources(false)
.FirstOrDefault();
if (mediaSource == null)
{
2015-03-03 07:03:17 +00:00
return new List<MediaStream>();
}
return mediaSource.MediaStreams;
}
2013-12-08 01:42:15 +00:00
public virtual MediaStream GetDefaultVideoStream()
{
if (!DefaultVideoStreamIndex.HasValue)
{
return null;
}
return MediaSourceManager.GetMediaStreams(new MediaStreamQuery
{
ItemId = Id,
Index = DefaultVideoStreamIndex.Value
}).FirstOrDefault();
}
2014-06-03 02:01:30 +00:00
public virtual IEnumerable<MediaSourceInfo> GetMediaSources(bool enablePathSubstitution)
{
2016-03-19 15:38:05 +00:00
if (SourceType == SourceType.Channel)
{
var sources = ChannelManager.GetStaticMediaSources(this, false, CancellationToken.None)
.Result.ToList();
if (sources.Count > 0)
{
return sources;
}
return new List<MediaSourceInfo>
{
GetVersionInfo(enablePathSubstitution, this, MediaSourceType.Placeholder)
};
}
2014-06-03 02:01:30 +00:00
var item = this;
var result = item.GetAlternateVersions()
.Select(i => GetVersionInfo(enablePathSubstitution, i, MediaSourceType.Grouping))
.ToList();
result.Add(GetVersionInfo(enablePathSubstitution, item, MediaSourceType.Default));
return result.OrderBy(i =>
{
2015-03-07 22:43:53 +00:00
if (i.VideoType == VideoType.VideoFile)
2014-06-03 02:01:30 +00:00
{
return 0;
}
return 1;
}).ThenBy(i => i.Video3DFormat.HasValue ? 1 : 0)
.ThenByDescending(i =>
{
var stream = i.VideoStream;
return stream == null || stream.Width == null ? 0 : stream.Width.Value;
})
.ToList();
}
private static MediaSourceInfo GetVersionInfo(bool enablePathSubstitution, Video i, MediaSourceType type)
{
2015-03-03 07:03:17 +00:00
var mediaStreams = MediaSourceManager.GetMediaStreams(i.Id)
.ToList();
2014-06-03 02:01:30 +00:00
var locationType = i.LocationType;
2014-12-03 03:13:03 +00:00
2014-06-03 02:01:30 +00:00
var info = new MediaSourceInfo
{
Id = i.Id.ToString("N"),
IsoType = i.IsoType,
Protocol = locationType == LocationType.Remote ? MediaProtocol.Http : MediaProtocol.File,
2014-06-03 02:01:30 +00:00
MediaStreams = mediaStreams,
Name = GetMediaSourceName(i, mediaStreams),
Path = enablePathSubstitution ? GetMappedPath(i.Path, locationType) : i.Path,
RunTimeTicks = i.RunTimeTicks,
Video3DFormat = i.Video3DFormat,
VideoType = i.VideoType,
Container = i.Container,
Size = i.Size,
Timestamp = i.Timestamp,
2014-06-12 02:38:40 +00:00
Type = type,
PlayableStreamFileNames = i.PlayableStreamFileNames.ToList(),
SupportsDirectStream = i.VideoType == VideoType.VideoFile
2014-06-03 02:01:30 +00:00
};
if (i.IsShortcut)
{
info.Path = i.ShortcutPath;
if (info.Path.StartsWith("Http", StringComparison.OrdinalIgnoreCase))
{
info.Protocol = MediaProtocol.Http;
}
else if (info.Path.StartsWith("Rtmp", StringComparison.OrdinalIgnoreCase))
{
info.Protocol = MediaProtocol.Rtmp;
}
else if (info.Path.StartsWith("Rtsp", StringComparison.OrdinalIgnoreCase))
{
info.Protocol = MediaProtocol.Rtsp;
}
else
{
info.Protocol = MediaProtocol.File;
}
}
2014-06-03 02:01:30 +00:00
if (string.IsNullOrEmpty(info.Container))
{
if (i.VideoType == VideoType.VideoFile || i.VideoType == VideoType.Iso)
{
if (!string.IsNullOrWhiteSpace(i.Path) && locationType != LocationType.Remote && locationType != LocationType.Virtual)
{
info.Container = System.IO.Path.GetExtension(i.Path).TrimStart('.');
}
}
}
try
{
var bitrate = i.TotalBitrate ??
info.MediaStreams.Where(m => m.Type != MediaStreamType.Subtitle && !string.Equals(m.Codec, "mjpeg", StringComparison.OrdinalIgnoreCase))
.Select(m => m.BitRate ?? 0)
.Sum();
if (bitrate > 0)
{
info.Bitrate = bitrate;
}
}
catch (OverflowException ex)
{
Logger.ErrorException("Error calculating total bitrate", ex);
}
return info;
}
private static string GetMediaSourceName(Video video, List<MediaStream> mediaStreams)
{
var terms = new List<string>();
var videoStream = mediaStreams.FirstOrDefault(i => i.Type == MediaStreamType.Video);
var audioStream = mediaStreams.FirstOrDefault(i => i.Type == MediaStreamType.Audio);
if (video.Video3DFormat.HasValue)
{
terms.Add("3D");
}
if (video.VideoType == VideoType.BluRay)
{
terms.Add("Bluray");
}
else if (video.VideoType == VideoType.Dvd)
{
terms.Add("DVD");
}
else if (video.VideoType == VideoType.HdDvd)
{
terms.Add("HD-DVD");
}
else if (video.VideoType == VideoType.Iso)
{
if (video.IsoType.HasValue)
{
if (video.IsoType.Value == Model.Entities.IsoType.BluRay)
{
terms.Add("Bluray");
}
else if (video.IsoType.Value == Model.Entities.IsoType.Dvd)
{
terms.Add("DVD");
}
}
else
{
terms.Add("ISO");
}
}
if (videoStream != null)
{
if (videoStream.Width.HasValue)
{
if (videoStream.Width.Value >= 3800)
{
terms.Add("4K");
}
else if (videoStream.Width.Value >= 1900)
{
terms.Add("1080P");
}
else if (videoStream.Width.Value >= 1270)
{
terms.Add("720P");
}
else if (videoStream.Width.Value >= 700)
{
terms.Add("480P");
}
else
{
terms.Add("SD");
}
}
}
if (videoStream != null && !string.IsNullOrWhiteSpace(videoStream.Codec))
{
terms.Add(videoStream.Codec.ToUpper());
}
if (audioStream != null)
{
var audioCodec = string.Equals(audioStream.Codec, "dca", StringComparison.OrdinalIgnoreCase)
? audioStream.Profile
: audioStream.Codec;
if (!string.IsNullOrEmpty(audioCodec))
{
terms.Add(audioCodec.ToUpper());
}
}
return string.Join("/", terms.ToArray());
}
2013-02-21 01:33:05 +00:00
}
}