Video.cs 19 KB

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