Video.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. #nullable disable
  2. #pragma warning disable CS1591
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Globalization;
  6. using System.Linq;
  7. using System.Text.Json.Serialization;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. using Jellyfin.Data.Enums;
  11. using Jellyfin.Extensions;
  12. using MediaBrowser.Controller.Library;
  13. using MediaBrowser.Controller.LiveTv;
  14. using MediaBrowser.Controller.Persistence;
  15. using MediaBrowser.Controller.Providers;
  16. using MediaBrowser.Model.Dto;
  17. using MediaBrowser.Model.Entities;
  18. using MediaBrowser.Model.IO;
  19. using MediaBrowser.Model.MediaInfo;
  20. namespace MediaBrowser.Controller.Entities
  21. {
  22. /// <summary>
  23. /// Class Video.
  24. /// </summary>
  25. public class Video : BaseItem,
  26. IHasAspectRatio,
  27. ISupportsPlaceHolders,
  28. IHasMediaSources
  29. {
  30. public Video()
  31. {
  32. AdditionalParts = Array.Empty<string>();
  33. LocalAlternateVersions = Array.Empty<string>();
  34. SubtitleFiles = Array.Empty<string>();
  35. AudioFiles = Array.Empty<string>();
  36. LinkedAlternateVersions = Array.Empty<LinkedChild>();
  37. }
  38. [JsonIgnore]
  39. public string PrimaryVersionId { get; set; }
  40. public string[] AdditionalParts { get; set; }
  41. public string[] LocalAlternateVersions { get; set; }
  42. public LinkedChild[] LinkedAlternateVersions { get; set; }
  43. [JsonIgnore]
  44. public override bool SupportsPlayedStatus => true;
  45. [JsonIgnore]
  46. public override bool SupportsPeople => true;
  47. [JsonIgnore]
  48. public override bool SupportsInheritedParentImages => true;
  49. [JsonIgnore]
  50. public override bool SupportsPositionTicksResume
  51. {
  52. get
  53. {
  54. var extraType = ExtraType;
  55. if (extraType.HasValue)
  56. {
  57. if (extraType.Value == Model.Entities.ExtraType.Sample)
  58. {
  59. return false;
  60. }
  61. if (extraType.Value == Model.Entities.ExtraType.ThemeVideo)
  62. {
  63. return false;
  64. }
  65. if (extraType.Value == Model.Entities.ExtraType.Trailer)
  66. {
  67. return false;
  68. }
  69. }
  70. return true;
  71. }
  72. }
  73. [JsonIgnore]
  74. public override bool SupportsThemeMedia => true;
  75. /// <summary>
  76. /// Gets or sets the timestamp.
  77. /// </summary>
  78. /// <value>The timestamp.</value>
  79. public TransportStreamTimestamp? Timestamp { get; set; }
  80. /// <summary>
  81. /// Gets or sets the subtitle paths.
  82. /// </summary>
  83. /// <value>The subtitle paths.</value>
  84. public string[] SubtitleFiles { get; set; }
  85. /// <summary>
  86. /// Gets or sets the audio paths.
  87. /// </summary>
  88. /// <value>The audio paths.</value>
  89. public string[] AudioFiles { get; set; }
  90. /// <summary>
  91. /// Gets or sets a value indicating whether this instance has subtitles.
  92. /// </summary>
  93. /// <value><c>true</c> if this instance has subtitles; otherwise, <c>false</c>.</value>
  94. public bool HasSubtitles { get; set; }
  95. public bool IsPlaceHolder { get; set; }
  96. /// <summary>
  97. /// Gets or sets the default index of the video stream.
  98. /// </summary>
  99. /// <value>The default index of the video stream.</value>
  100. public int? DefaultVideoStreamIndex { get; set; }
  101. /// <summary>
  102. /// Gets or sets the type of the video.
  103. /// </summary>
  104. /// <value>The type of the video.</value>
  105. public VideoType VideoType { get; set; }
  106. /// <summary>
  107. /// Gets or sets the type of the iso.
  108. /// </summary>
  109. /// <value>The type of the iso.</value>
  110. public IsoType? IsoType { get; set; }
  111. /// <summary>
  112. /// Gets or sets the video3 D format.
  113. /// </summary>
  114. /// <value>The video3 D format.</value>
  115. public Video3DFormat? Video3DFormat { get; set; }
  116. /// <summary>
  117. /// Gets or sets the aspect ratio.
  118. /// </summary>
  119. /// <value>The aspect ratio.</value>
  120. public string AspectRatio { get; set; }
  121. [JsonIgnore]
  122. public override bool SupportsAddingToPlaylist => true;
  123. [JsonIgnore]
  124. public int MediaSourceCount
  125. {
  126. get
  127. {
  128. if (!string.IsNullOrEmpty(PrimaryVersionId))
  129. {
  130. var item = LibraryManager.GetItemById(PrimaryVersionId);
  131. if (item is Video video)
  132. {
  133. return video.MediaSourceCount;
  134. }
  135. }
  136. return LinkedAlternateVersions.Length + LocalAlternateVersions.Length + 1;
  137. }
  138. }
  139. [JsonIgnore]
  140. public bool IsStacked => AdditionalParts.Length > 0;
  141. [JsonIgnore]
  142. public override bool HasLocalAlternateVersions => LocalAlternateVersions.Length > 0;
  143. public static IRecordingsManager RecordingsManager { get; set; }
  144. [JsonIgnore]
  145. public override SourceType SourceType
  146. {
  147. get
  148. {
  149. if (IsActiveRecording())
  150. {
  151. return SourceType.LiveTV;
  152. }
  153. return base.SourceType;
  154. }
  155. }
  156. [JsonIgnore]
  157. public bool IsCompleteMedia
  158. {
  159. get
  160. {
  161. if (SourceType == SourceType.Channel)
  162. {
  163. return !Tags.Contains("livestream", StringComparison.OrdinalIgnoreCase);
  164. }
  165. return !IsActiveRecording();
  166. }
  167. }
  168. [JsonIgnore]
  169. protected virtual bool EnableDefaultVideoUserDataKeys => true;
  170. [JsonIgnore]
  171. public override string ContainingFolderPath
  172. {
  173. get
  174. {
  175. if (IsStacked)
  176. {
  177. return System.IO.Path.GetDirectoryName(Path);
  178. }
  179. if (!IsPlaceHolder)
  180. {
  181. if (VideoType == VideoType.BluRay || VideoType == VideoType.Dvd)
  182. {
  183. return Path;
  184. }
  185. }
  186. return base.ContainingFolderPath;
  187. }
  188. }
  189. [JsonIgnore]
  190. public override string FileNameWithoutExtension
  191. {
  192. get
  193. {
  194. if (IsFileProtocol)
  195. {
  196. if (VideoType == VideoType.BluRay || VideoType == VideoType.Dvd)
  197. {
  198. return System.IO.Path.GetFileName(Path);
  199. }
  200. return System.IO.Path.GetFileNameWithoutExtension(Path);
  201. }
  202. return null;
  203. }
  204. }
  205. /// <summary>
  206. /// Gets a value indicating whether [is3 D].
  207. /// </summary>
  208. /// <value><c>true</c> if [is3 D]; otherwise, <c>false</c>.</value>
  209. [JsonIgnore]
  210. public bool Is3D => Video3DFormat.HasValue;
  211. /// <summary>
  212. /// Gets the type of the media.
  213. /// </summary>
  214. /// <value>The type of the media.</value>
  215. [JsonIgnore]
  216. public override MediaType MediaType => MediaType.Video;
  217. public override List<string> GetUserDataKeys()
  218. {
  219. var list = base.GetUserDataKeys();
  220. if (EnableDefaultVideoUserDataKeys)
  221. {
  222. if (ExtraType.HasValue)
  223. {
  224. var key = this.GetProviderId(MetadataProvider.Tmdb);
  225. if (!string.IsNullOrEmpty(key))
  226. {
  227. list.Insert(0, GetUserDataKey(key));
  228. }
  229. key = this.GetProviderId(MetadataProvider.Imdb);
  230. if (!string.IsNullOrEmpty(key))
  231. {
  232. list.Insert(0, GetUserDataKey(key));
  233. }
  234. }
  235. else
  236. {
  237. var key = this.GetProviderId(MetadataProvider.Imdb);
  238. if (!string.IsNullOrEmpty(key))
  239. {
  240. list.Insert(0, key);
  241. }
  242. key = this.GetProviderId(MetadataProvider.Tmdb);
  243. if (!string.IsNullOrEmpty(key))
  244. {
  245. list.Insert(0, key);
  246. }
  247. }
  248. }
  249. return list;
  250. }
  251. public void SetPrimaryVersionId(string id)
  252. {
  253. if (string.IsNullOrEmpty(id))
  254. {
  255. PrimaryVersionId = null;
  256. }
  257. else
  258. {
  259. PrimaryVersionId = id;
  260. }
  261. PresentationUniqueKey = CreatePresentationUniqueKey();
  262. }
  263. public override string CreatePresentationUniqueKey()
  264. {
  265. if (!string.IsNullOrEmpty(PrimaryVersionId))
  266. {
  267. return PrimaryVersionId;
  268. }
  269. return base.CreatePresentationUniqueKey();
  270. }
  271. public override bool CanDownload()
  272. {
  273. if (VideoType == VideoType.Dvd || VideoType == VideoType.BluRay)
  274. {
  275. return false;
  276. }
  277. return IsFileProtocol;
  278. }
  279. protected override bool IsActiveRecording()
  280. {
  281. return RecordingsManager.GetActiveRecordingInfo(Path) is not null;
  282. }
  283. public override bool CanDelete()
  284. {
  285. if (IsActiveRecording())
  286. {
  287. return false;
  288. }
  289. return base.CanDelete();
  290. }
  291. public IEnumerable<Guid> GetAdditionalPartIds()
  292. {
  293. return AdditionalParts.Select(i => LibraryManager.GetNewItemId(i, typeof(Video)));
  294. }
  295. public IEnumerable<Guid> GetLocalAlternateVersionIds()
  296. {
  297. return LocalAlternateVersions.Select(i => LibraryManager.GetNewItemId(i, typeof(Video)));
  298. }
  299. private string GetUserDataKey(string providerId)
  300. {
  301. var key = providerId + "-" + ExtraType.ToString().ToLowerInvariant();
  302. // Make sure different trailers have their own data.
  303. if (RunTimeTicks.HasValue)
  304. {
  305. key += "-" + RunTimeTicks.Value.ToString(CultureInfo.InvariantCulture);
  306. }
  307. return key;
  308. }
  309. public IEnumerable<Video> GetLinkedAlternateVersions()
  310. {
  311. return LinkedAlternateVersions
  312. .Select(GetLinkedChild)
  313. .Where(i => i is not null)
  314. .OfType<Video>()
  315. .OrderBy(i => i.SortName);
  316. }
  317. /// <summary>
  318. /// Gets the additional parts.
  319. /// </summary>
  320. /// <returns>IEnumerable{Video}.</returns>
  321. public IOrderedEnumerable<Video> GetAdditionalParts()
  322. {
  323. return GetAdditionalPartIds()
  324. .Select(i => LibraryManager.GetItemById(i))
  325. .Where(i => i is not null)
  326. .OfType<Video>()
  327. .OrderBy(i => i.SortName);
  328. }
  329. internal override ItemUpdateType UpdateFromResolvedItem(BaseItem newItem)
  330. {
  331. var updateType = base.UpdateFromResolvedItem(newItem);
  332. if (newItem is Video newVideo)
  333. {
  334. if (!AdditionalParts.SequenceEqual(newVideo.AdditionalParts, StringComparer.Ordinal))
  335. {
  336. AdditionalParts = newVideo.AdditionalParts;
  337. updateType |= ItemUpdateType.MetadataImport;
  338. }
  339. if (!LocalAlternateVersions.SequenceEqual(newVideo.LocalAlternateVersions, StringComparer.Ordinal))
  340. {
  341. LocalAlternateVersions = newVideo.LocalAlternateVersions;
  342. updateType |= ItemUpdateType.MetadataImport;
  343. }
  344. if (VideoType != newVideo.VideoType)
  345. {
  346. VideoType = newVideo.VideoType;
  347. updateType |= ItemUpdateType.MetadataImport;
  348. }
  349. }
  350. return updateType;
  351. }
  352. protected override async Task<bool> RefreshedOwnedItems(MetadataRefreshOptions options, IReadOnlyList<FileSystemMetadata> fileSystemChildren, CancellationToken cancellationToken)
  353. {
  354. var hasChanges = await base.RefreshedOwnedItems(options, fileSystemChildren, cancellationToken).ConfigureAwait(false);
  355. if (IsStacked)
  356. {
  357. var tasks = AdditionalParts
  358. .Select(i => RefreshMetadataForOwnedVideo(options, true, i, cancellationToken));
  359. await Task.WhenAll(tasks).ConfigureAwait(false);
  360. }
  361. // Must have a parent to have additional parts or alternate versions
  362. // In other words, it must be part of the Parent/Child tree
  363. // The additional parts won't have additional parts themselves
  364. if (IsFileProtocol && SupportsOwnedItems)
  365. {
  366. if (!IsStacked)
  367. {
  368. RefreshLinkedAlternateVersions();
  369. var tasks = LocalAlternateVersions
  370. .Select(i => RefreshMetadataForOwnedVideo(options, false, i, cancellationToken));
  371. await Task.WhenAll(tasks).ConfigureAwait(false);
  372. }
  373. }
  374. return hasChanges;
  375. }
  376. private void RefreshLinkedAlternateVersions()
  377. {
  378. foreach (var child in LinkedAlternateVersions)
  379. {
  380. // Reset the cached value
  381. if (child.ItemId.IsNullOrEmpty())
  382. {
  383. child.ItemId = null;
  384. }
  385. }
  386. }
  387. /// <inheritdoc />
  388. public override async Task UpdateToRepositoryAsync(ItemUpdateType updateReason, CancellationToken cancellationToken)
  389. {
  390. await base.UpdateToRepositoryAsync(updateReason, cancellationToken).ConfigureAwait(false);
  391. var localAlternates = GetLocalAlternateVersionIds()
  392. .Select(i => LibraryManager.GetItemById(i))
  393. .Where(i => i is not null);
  394. foreach (var item in localAlternates)
  395. {
  396. item.ImageInfos = ImageInfos;
  397. item.Overview = Overview;
  398. item.ProductionYear = ProductionYear;
  399. item.PremiereDate = PremiereDate;
  400. item.CommunityRating = CommunityRating;
  401. item.OfficialRating = OfficialRating;
  402. item.Genres = Genres;
  403. item.ProviderIds = ProviderIds;
  404. await item.UpdateToRepositoryAsync(ItemUpdateType.MetadataDownload, cancellationToken).ConfigureAwait(false);
  405. }
  406. }
  407. public override IEnumerable<FileSystemMetadata> GetDeletePaths()
  408. {
  409. if (!IsInMixedFolder)
  410. {
  411. return new[]
  412. {
  413. new FileSystemMetadata
  414. {
  415. FullName = ContainingFolderPath,
  416. IsDirectory = true
  417. }
  418. };
  419. }
  420. return base.GetDeletePaths();
  421. }
  422. public virtual MediaStream GetDefaultVideoStream()
  423. {
  424. if (!DefaultVideoStreamIndex.HasValue)
  425. {
  426. return null;
  427. }
  428. return MediaSourceManager.GetMediaStreams(new MediaStreamQuery
  429. {
  430. ItemId = Id,
  431. Index = DefaultVideoStreamIndex.Value
  432. }).FirstOrDefault();
  433. }
  434. protected override IEnumerable<(BaseItem Item, MediaSourceType MediaSourceType)> GetAllItemsForMediaSources()
  435. {
  436. var list = new List<(BaseItem, MediaSourceType)>
  437. {
  438. (this, MediaSourceType.Default)
  439. };
  440. list.AddRange(GetLinkedAlternateVersions().Select(i => ((BaseItem)i, MediaSourceType.Grouping)));
  441. if (!string.IsNullOrEmpty(PrimaryVersionId))
  442. {
  443. if (LibraryManager.GetItemById(PrimaryVersionId) is Video primary)
  444. {
  445. var existingIds = list.Select(i => i.Item1.Id).ToList();
  446. list.Add((primary, MediaSourceType.Grouping));
  447. list.AddRange(primary.GetLinkedAlternateVersions().Where(i => !existingIds.Contains(i.Id)).Select(i => ((BaseItem)i, MediaSourceType.Grouping)));
  448. }
  449. }
  450. var localAlternates = list
  451. .SelectMany(i =>
  452. {
  453. return i.Item1 is Video video ? video.GetLocalAlternateVersionIds() : Enumerable.Empty<Guid>();
  454. })
  455. .Select(LibraryManager.GetItemById)
  456. .Where(i => i is not null)
  457. .ToList();
  458. list.AddRange(localAlternates.Select(i => (i, MediaSourceType.Default)));
  459. return list;
  460. }
  461. }
  462. }