jellyfin/MediaBrowser.Controller/Entities/Video.cs

564 lines
17 KiB
C#
Raw Normal View History

#nullable disable
#pragma warning disable CS1591
using System;
2018-12-27 18:27:57 -05:00
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
2019-10-15 11:49:49 -04:00
using System.Text.Json.Serialization;
2018-12-27 18:27:57 -05:00
using System.Threading;
using System.Threading.Tasks;
using Jellyfin.Data.Enums;
2021-12-20 07:31:07 -05:00
using Jellyfin.Extensions;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.LiveTv;
using MediaBrowser.Controller.Persistence;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Dto;
using MediaBrowser.Model.Entities;
2018-12-27 18:27:57 -05:00
using MediaBrowser.Model.IO;
using MediaBrowser.Model.MediaInfo;
2018-12-27 18:27:57 -05:00
namespace MediaBrowser.Controller.Entities
{
/// <summary>
/// Class Video.
2018-12-27 18:27:57 -05:00
/// </summary>
public class Video : BaseItem,
IHasAspectRatio,
ISupportsPlaceHolders,
IHasMediaSources
{
public Video()
{
AdditionalParts = Array.Empty<string>();
LocalAlternateVersions = Array.Empty<string>();
SubtitleFiles = Array.Empty<string>();
2021-12-01 15:05:43 -05:00
AudioFiles = Array.Empty<string>();
LinkedAlternateVersions = Array.Empty<LinkedChild>();
}
2019-10-15 11:49:49 -04:00
[JsonIgnore]
2018-12-27 18:27:57 -05:00
public string PrimaryVersionId { get; set; }
public string[] AdditionalParts { get; set; }
2020-06-15 17:43:52 -04:00
2018-12-27 18:27:57 -05:00
public string[] LocalAlternateVersions { get; set; }
2020-06-15 17:43:52 -04:00
2018-12-27 18:27:57 -05:00
public LinkedChild[] LinkedAlternateVersions { get; set; }
2019-10-15 11:49:49 -04:00
[JsonIgnore]
public override bool SupportsPlayedStatus => true;
2018-12-27 18:27:57 -05:00
2019-10-15 11:49:49 -04:00
[JsonIgnore]
public override bool SupportsPeople => true;
2018-12-27 18:27:57 -05:00
2019-10-15 11:49:49 -04:00
[JsonIgnore]
public override bool SupportsInheritedParentImages => true;
2018-12-27 18:27:57 -05:00
2019-10-15 11:49:49 -04:00
[JsonIgnore]
2018-12-27 18:27:57 -05:00
public override bool SupportsPositionTicksResume
{
get
{
var extraType = ExtraType;
if (extraType.HasValue)
{
if (extraType.Value == Model.Entities.ExtraType.Sample)
{
return false;
}
2020-06-15 17:43:52 -04:00
2018-12-27 18:27:57 -05:00
if (extraType.Value == Model.Entities.ExtraType.ThemeVideo)
{
return false;
}
2020-06-15 17:43:52 -04:00
2018-12-27 18:27:57 -05:00
if (extraType.Value == Model.Entities.ExtraType.Trailer)
{
return false;
}
}
2020-06-15 17:43:52 -04:00
2018-12-27 18:27:57 -05:00
return true;
}
}
2019-10-15 11:49:49 -04:00
[JsonIgnore]
public override bool SupportsThemeMedia => true;
2018-12-27 18:27:57 -05:00
/// <summary>
/// Gets or sets the timestamp.
/// </summary>
/// <value>The timestamp.</value>
public TransportStreamTimestamp? Timestamp { get; set; }
/// <summary>
/// Gets or sets the subtitle paths.
/// </summary>
/// <value>The subtitle paths.</value>
public string[] SubtitleFiles { get; set; }
2021-11-22 15:47:52 -05:00
/// <summary>
/// Gets or sets the audio paths.
/// </summary>
/// <value>The audio paths.</value>
public string[] AudioFiles { get; set; }
2018-12-27 18:27:57 -05: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; }
/// <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; }
/// <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; }
/// <summary>
/// Gets or sets the video3 D format.
/// </summary>
/// <value>The video3 D format.</value>
public Video3DFormat? Video3DFormat { get; set; }
/// <summary>
/// Gets or sets the aspect ratio.
/// </summary>
/// <value>The aspect ratio.</value>
public string AspectRatio { get; set; }
2019-10-15 11:49:49 -04:00
[JsonIgnore]
public override bool SupportsAddingToPlaylist => true;
2018-12-27 18:27:57 -05:00
2019-10-15 11:49:49 -04:00
[JsonIgnore]
2018-12-27 18:27:57 -05:00
public int MediaSourceCount
{
get
{
if (!string.IsNullOrEmpty(PrimaryVersionId))
{
2018-12-27 16:43:48 -05:00
var item = LibraryManager.GetItemById(PrimaryVersionId);
if (item is Video video)
2018-12-27 18:27:57 -05:00
{
2018-12-27 16:43:48 -05:00
return video.MediaSourceCount;
2018-12-27 18:27:57 -05:00
}
}
2020-06-15 17:43:52 -04:00
2018-12-27 18:27:57 -05:00
return LinkedAlternateVersions.Length + LocalAlternateVersions.Length + 1;
}
}
2019-10-15 11:49:49 -04:00
[JsonIgnore]
public bool IsStacked => AdditionalParts.Length > 0;
2018-12-27 18:27:57 -05:00
2019-10-15 11:49:49 -04:00
[JsonIgnore]
public override bool HasLocalAlternateVersions => LocalAlternateVersions.Length > 0;
2018-12-27 18:27:57 -05:00
2024-02-09 13:46:28 -05:00
public static IRecordingsManager RecordingsManager { get; set; }
2018-12-27 18:27:57 -05:00
2019-10-15 11:49:49 -04:00
[JsonIgnore]
2018-12-27 18:27:57 -05:00
public override SourceType SourceType
{
get
{
if (IsActiveRecording())
{
return SourceType.LiveTV;
}
return base.SourceType;
}
}
[JsonIgnore]
public bool IsCompleteMedia
2018-12-27 18:27:57 -05:00
{
get
{
if (SourceType == SourceType.Channel)
{
2021-12-20 07:31:07 -05:00
return !Tags.Contains("livestream", StringComparison.OrdinalIgnoreCase);
}
return !IsActiveRecording();
}
2018-12-27 18:27:57 -05:00
}
[JsonIgnore]
protected virtual bool EnableDefaultVideoUserDataKeys => true;
[JsonIgnore]
public override string ContainingFolderPath
2018-12-27 18:27:57 -05:00
{
get
2018-12-27 18:27:57 -05:00
{
if (IsStacked)
{
return System.IO.Path.GetDirectoryName(Path);
}
2018-12-27 18:27:57 -05:00
if (!IsPlaceHolder)
{
if (VideoType == VideoType.BluRay || VideoType == VideoType.Dvd)
{
return Path;
}
}
return base.ContainingFolderPath;
}
2018-12-27 18:27:57 -05:00
}
2019-10-15 11:49:49 -04:00
[JsonIgnore]
public override string FileNameWithoutExtension
2018-12-27 18:27:57 -05:00
{
get
{
if (IsFileProtocol)
2018-12-27 18:27:57 -05:00
{
if (VideoType == VideoType.BluRay || VideoType == VideoType.Dvd)
{
return System.IO.Path.GetFileName(Path);
}
return System.IO.Path.GetFileNameWithoutExtension(Path);
2018-12-27 18:27:57 -05:00
}
return null;
2018-12-27 18:27:57 -05:00
}
}
/// <summary>
/// Gets a value indicating whether [is3 D].
/// </summary>
/// <value><c>true</c> if [is3 D]; otherwise, <c>false</c>.</value>
2019-10-15 11:49:49 -04:00
[JsonIgnore]
public bool Is3D => Video3DFormat.HasValue;
/// <summary>
/// Gets the type of the media.
/// </summary>
/// <value>The type of the media.</value>
[JsonIgnore]
public override MediaType MediaType => MediaType.Video;
2018-12-27 18:27:57 -05:00
public override List<string> GetUserDataKeys()
{
var list = base.GetUserDataKeys();
if (EnableDefaultVideoUserDataKeys)
{
if (ExtraType.HasValue)
{
2020-06-06 15:17:49 -04:00
var key = this.GetProviderId(MetadataProvider.Tmdb);
2018-12-27 18:27:57 -05:00
if (!string.IsNullOrEmpty(key))
{
list.Insert(0, GetUserDataKey(key));
}
2020-06-06 15:17:49 -04:00
key = this.GetProviderId(MetadataProvider.Imdb);
2018-12-27 18:27:57 -05:00
if (!string.IsNullOrEmpty(key))
{
list.Insert(0, GetUserDataKey(key));
}
}
else
{
2020-06-06 15:17:49 -04:00
var key = this.GetProviderId(MetadataProvider.Imdb);
2018-12-27 18:27:57 -05:00
if (!string.IsNullOrEmpty(key))
{
list.Insert(0, key);
}
2020-06-06 15:17:49 -04:00
key = this.GetProviderId(MetadataProvider.Tmdb);
2018-12-27 18:27:57 -05:00
if (!string.IsNullOrEmpty(key))
{
list.Insert(0, key);
}
}
}
return list;
}
public void SetPrimaryVersionId(string id)
{
if (string.IsNullOrEmpty(id))
{
PrimaryVersionId = null;
}
else
{
PrimaryVersionId = id;
}
PresentationUniqueKey = CreatePresentationUniqueKey();
}
public override string CreatePresentationUniqueKey()
{
if (!string.IsNullOrEmpty(PrimaryVersionId))
{
return PrimaryVersionId;
}
return base.CreatePresentationUniqueKey();
}
public override bool CanDownload()
{
if (VideoType == VideoType.Dvd || VideoType == VideoType.BluRay)
{
return false;
}
return IsFileProtocol;
}
protected override bool IsActiveRecording()
{
2024-02-09 13:46:28 -05:00
return RecordingsManager.GetActiveRecordingInfo(Path) is not null;
}
public override bool CanDelete()
{
if (IsActiveRecording())
{
return false;
}
return base.CanDelete();
}
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)));
}
2018-12-27 18:27:57 -05:00
private string GetUserDataKey(string providerId)
{
2019-01-27 06:03:43 -05:00
var key = providerId + "-" + ExtraType.ToString().ToLowerInvariant();
2018-12-27 18:27:57 -05:00
// Make sure different trailers have their own data.
if (RunTimeTicks.HasValue)
{
key += "-" + RunTimeTicks.Value.ToString(CultureInfo.InvariantCulture);
}
return key;
}
public IEnumerable<Video> GetLinkedAlternateVersions()
{
return LinkedAlternateVersions
.Select(GetLinkedChild)
2022-12-05 09:01:13 -05:00
.Where(i => i is not null)
2018-12-27 18:27:57 -05:00
.OfType<Video>()
.OrderBy(i => i.SortName);
}
2023-06-11 15:42:14 -04:00
/// <summary>
/// Gets the MediaStreams for this Video and all alternate Versions (linked and local).
/// </summary>
/// <returns>A list of all MediaStreams which are linked to this Video.</returns>
public IEnumerable<MediaStream> GetAllLinkedMediaStreams()
{
return GetAllItemsForMediaSources().SelectMany(item => item.Item.GetMediaStreams());
}
2018-12-27 18:27:57 -05:00
/// <summary>
/// Gets the additional parts.
/// </summary>
/// <returns>IEnumerable{Video}.</returns>
2018-12-27 16:43:48 -05:00
public IOrderedEnumerable<Video> GetAdditionalParts()
2018-12-27 18:27:57 -05:00
{
return GetAdditionalPartIds()
.Select(i => LibraryManager.GetItemById(i))
2022-12-05 09:01:13 -05:00
.Where(i => i is not null)
2018-12-27 18:27:57 -05:00
.OfType<Video>()
.OrderBy(i => i.SortName);
}
internal override ItemUpdateType UpdateFromResolvedItem(BaseItem newItem)
{
var updateType = base.UpdateFromResolvedItem(newItem);
2018-12-27 16:43:48 -05:00
if (newItem is Video newVideo)
2018-12-27 18:27:57 -05:00
{
if (!AdditionalParts.SequenceEqual(newVideo.AdditionalParts, StringComparer.Ordinal))
{
AdditionalParts = newVideo.AdditionalParts;
updateType |= ItemUpdateType.MetadataImport;
}
2020-06-15 17:43:52 -04:00
2018-12-27 18:27:57 -05:00
if (!LocalAlternateVersions.SequenceEqual(newVideo.LocalAlternateVersions, StringComparer.Ordinal))
{
LocalAlternateVersions = newVideo.LocalAlternateVersions;
updateType |= ItemUpdateType.MetadataImport;
}
2020-06-15 17:43:52 -04:00
2018-12-27 18:27:57 -05:00
if (VideoType != newVideo.VideoType)
{
VideoType = newVideo.VideoType;
updateType |= ItemUpdateType.MetadataImport;
}
}
return updateType;
}
2022-01-28 06:21:40 -05:00
protected override async Task<bool> RefreshedOwnedItems(MetadataRefreshOptions options, IReadOnlyList<FileSystemMetadata> fileSystemChildren, CancellationToken cancellationToken)
2018-12-27 18:27:57 -05:00
{
var hasChanges = await base.RefreshedOwnedItems(options, fileSystemChildren, cancellationToken).ConfigureAwait(false);
if (IsStacked)
{
var tasks = AdditionalParts
.Select(i => RefreshMetadataForOwnedVideo(options, true, i, cancellationToken));
await Task.WhenAll(tasks).ConfigureAwait(false);
}
// Must have a parent to have additional parts or alternate versions
// In other words, it must be part of the Parent/Child tree
// The additional parts won't have additional parts themselves
if (IsFileProtocol && SupportsOwnedItems)
{
if (!IsStacked)
{
RefreshLinkedAlternateVersions();
var tasks = LocalAlternateVersions
.Select(i => RefreshMetadataForOwnedVideo(options, false, i, cancellationToken));
await Task.WhenAll(tasks).ConfigureAwait(false);
}
}
return hasChanges;
}
private void RefreshLinkedAlternateVersions()
{
foreach (var child in LinkedAlternateVersions)
{
// Reset the cached value
if (child.ItemId.IsNullOrEmpty())
2018-12-27 18:27:57 -05:00
{
child.ItemId = null;
}
}
}
2020-08-21 16:01:19 -04:00
/// <inheritdoc />
public override async Task UpdateToRepositoryAsync(ItemUpdateType updateReason, CancellationToken cancellationToken)
2018-12-27 18:27:57 -05:00
{
2020-08-21 16:01:19 -04:00
await base.UpdateToRepositoryAsync(updateReason, cancellationToken).ConfigureAwait(false);
2018-12-27 18:27:57 -05:00
var localAlternates = GetLocalAlternateVersionIds()
.Select(i => LibraryManager.GetItemById(i))
2022-12-05 09:01:13 -05:00
.Where(i => i is not null);
2018-12-27 18:27:57 -05:00
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;
2020-08-21 16:01:19 -04:00
await item.UpdateToRepositoryAsync(ItemUpdateType.MetadataDownload, cancellationToken).ConfigureAwait(false);
2018-12-27 18:27:57 -05:00
}
}
public override IEnumerable<FileSystemMetadata> GetDeletePaths()
{
if (!IsInMixedFolder)
{
return new[]
{
2018-12-27 18:27:57 -05:00
new FileSystemMetadata
{
FullName = ContainingFolderPath,
IsDirectory = true
}
};
}
return base.GetDeletePaths();
}
public virtual MediaStream GetDefaultVideoStream()
{
if (!DefaultVideoStreamIndex.HasValue)
{
return null;
}
return MediaSourceManager.GetMediaStreams(new MediaStreamQuery
{
ItemId = Id,
Index = DefaultVideoStreamIndex.Value
}).FirstOrDefault();
}
2021-12-24 16:18:24 -05:00
protected override IEnumerable<(BaseItem Item, MediaSourceType MediaSourceType)> GetAllItemsForMediaSources()
2018-12-27 18:27:57 -05:00
{
2021-11-16 10:31:57 -05:00
var list = new List<(BaseItem, MediaSourceType)>
{
(this, MediaSourceType.Default)
};
2018-12-27 18:27:57 -05:00
2021-11-16 10:31:57 -05:00
list.AddRange(GetLinkedAlternateVersions().Select(i => ((BaseItem)i, MediaSourceType.Grouping)));
2018-12-27 18:27:57 -05:00
if (!string.IsNullOrEmpty(PrimaryVersionId))
{
2021-11-16 10:31:57 -05:00
if (LibraryManager.GetItemById(PrimaryVersionId) is Video primary)
2018-12-27 18:27:57 -05:00
{
var existingIds = list.Select(i => i.Item1.Id).ToList();
2021-11-16 10:31:57 -05:00
list.Add((primary, MediaSourceType.Grouping));
list.AddRange(primary.GetLinkedAlternateVersions().Where(i => !existingIds.Contains(i.Id)).Select(i => ((BaseItem)i, MediaSourceType.Grouping)));
2018-12-27 18:27:57 -05:00
}
}
var localAlternates = list
.SelectMany(i =>
{
2021-11-16 10:31:57 -05:00
return i.Item1 is Video video ? video.GetLocalAlternateVersionIds() : Enumerable.Empty<Guid>();
2018-12-27 18:27:57 -05:00
})
.Select(LibraryManager.GetItemById)
2022-12-05 09:01:13 -05:00
.Where(i => i is not null)
2018-12-27 18:27:57 -05:00
.ToList();
2021-11-16 10:31:57 -05:00
list.AddRange(localAlternates.Select(i => (i, MediaSourceType.Default)));
2018-12-27 18:27:57 -05:00
return list;
}
}
}