Video.cs 19 KB

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