Video.cs 19 KB

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