Video.cs 18 KB

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