Video.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. using MediaBrowser.Controller.Persistence;
  2. using MediaBrowser.Controller.Providers;
  3. using MediaBrowser.Controller.Resolvers;
  4. using MediaBrowser.Model.Entities;
  5. using System;
  6. using System.Collections;
  7. using System.Collections.Generic;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Runtime.Serialization;
  11. using System.Threading;
  12. using System.Threading.Tasks;
  13. namespace MediaBrowser.Controller.Entities
  14. {
  15. /// <summary>
  16. /// Class Video
  17. /// </summary>
  18. public class Video : BaseItem, IHasMediaStreams, IHasAspectRatio, IHasTags, ISupportsPlaceHolders
  19. {
  20. public bool IsMultiPart { get; set; }
  21. public bool HasLocalAlternateVersions { get; set; }
  22. public List<Guid> AdditionalPartIds { get; set; }
  23. public List<Guid> AlternateVersionIds { get; set; }
  24. public Video()
  25. {
  26. PlayableStreamFileNames = new List<string>();
  27. AdditionalPartIds = new List<Guid>();
  28. AlternateVersionIds = new List<Guid>();
  29. Tags = new List<string>();
  30. SubtitleFiles = new List<string>();
  31. LinkedAlternateVersions = new List<LinkedChild>();
  32. }
  33. [IgnoreDataMember]
  34. public bool HasAlternateVersions
  35. {
  36. get
  37. {
  38. return HasLocalAlternateVersions || LinkedAlternateVersions.Count > 0;
  39. }
  40. }
  41. public List<LinkedChild> LinkedAlternateVersions { get; set; }
  42. /// <summary>
  43. /// Gets the linked children.
  44. /// </summary>
  45. /// <returns>IEnumerable{BaseItem}.</returns>
  46. public IEnumerable<BaseItem> GetAlternateVersions()
  47. {
  48. var filesWithinSameDirectory = AlternateVersionIds
  49. .Select(i => LibraryManager.GetItemById(i))
  50. .Where(i => i != null)
  51. .OfType<Video>();
  52. var linkedVersions = LinkedAlternateVersions
  53. .Select(GetLinkedChild)
  54. .Where(i => i != null)
  55. .OfType<Video>();
  56. return filesWithinSameDirectory.Concat(linkedVersions)
  57. .OrderBy(i => i.SortName);
  58. }
  59. /// <summary>
  60. /// Gets the additional parts.
  61. /// </summary>
  62. /// <returns>IEnumerable{Video}.</returns>
  63. public IEnumerable<Video> GetAdditionalParts()
  64. {
  65. return AdditionalPartIds
  66. .Select(i => LibraryManager.GetItemById(i))
  67. .Where(i => i != null)
  68. .OfType<Video>()
  69. .OrderBy(i => i.SortName);
  70. }
  71. /// <summary>
  72. /// Gets or sets the subtitle paths.
  73. /// </summary>
  74. /// <value>The subtitle paths.</value>
  75. public List<string> SubtitleFiles { get; set; }
  76. /// <summary>
  77. /// Gets or sets a value indicating whether this instance has subtitles.
  78. /// </summary>
  79. /// <value><c>true</c> if this instance has subtitles; otherwise, <c>false</c>.</value>
  80. public bool HasSubtitles { get; set; }
  81. public bool IsPlaceHolder { get; set; }
  82. /// <summary>
  83. /// Gets or sets the tags.
  84. /// </summary>
  85. /// <value>The tags.</value>
  86. public List<string> Tags { get; set; }
  87. /// <summary>
  88. /// Gets or sets the video bit rate.
  89. /// </summary>
  90. /// <value>The video bit rate.</value>
  91. public int? VideoBitRate { get; set; }
  92. /// <summary>
  93. /// Gets or sets the default index of the video stream.
  94. /// </summary>
  95. /// <value>The default index of the video stream.</value>
  96. public int? DefaultVideoStreamIndex { get; set; }
  97. /// <summary>
  98. /// Gets or sets the type of the video.
  99. /// </summary>
  100. /// <value>The type of the video.</value>
  101. public VideoType VideoType { get; set; }
  102. /// <summary>
  103. /// Gets or sets the type of the iso.
  104. /// </summary>
  105. /// <value>The type of the iso.</value>
  106. public IsoType? IsoType { get; set; }
  107. /// <summary>
  108. /// Gets or sets the video3 D format.
  109. /// </summary>
  110. /// <value>The video3 D format.</value>
  111. public Video3DFormat? Video3DFormat { get; set; }
  112. /// <summary>
  113. /// If the video is a folder-rip, this will hold the file list for the largest playlist
  114. /// </summary>
  115. public List<string> PlayableStreamFileNames { get; set; }
  116. /// <summary>
  117. /// Gets the playable stream files.
  118. /// </summary>
  119. /// <returns>List{System.String}.</returns>
  120. public List<string> GetPlayableStreamFiles()
  121. {
  122. return GetPlayableStreamFiles(Path);
  123. }
  124. /// <summary>
  125. /// Gets or sets the aspect ratio.
  126. /// </summary>
  127. /// <value>The aspect ratio.</value>
  128. public string AspectRatio { get; set; }
  129. [IgnoreDataMember]
  130. public override string ContainingFolderPath
  131. {
  132. get
  133. {
  134. if (IsMultiPart)
  135. {
  136. return System.IO.Path.GetDirectoryName(Path);
  137. }
  138. if (!IsPlaceHolder)
  139. {
  140. if (VideoType == VideoType.BluRay || VideoType == VideoType.Dvd ||
  141. VideoType == VideoType.HdDvd)
  142. {
  143. return Path;
  144. }
  145. }
  146. return base.ContainingFolderPath;
  147. }
  148. }
  149. public string MainFeaturePlaylistName { get; set; }
  150. /// <summary>
  151. /// Gets the playable stream files.
  152. /// </summary>
  153. /// <param name="rootPath">The root path.</param>
  154. /// <returns>List{System.String}.</returns>
  155. public List<string> GetPlayableStreamFiles(string rootPath)
  156. {
  157. var allFiles = Directory.EnumerateFiles(rootPath, "*", SearchOption.AllDirectories).ToList();
  158. return PlayableStreamFileNames.Select(name => allFiles.FirstOrDefault(f => string.Equals(System.IO.Path.GetFileName(f), name, StringComparison.OrdinalIgnoreCase)))
  159. .Where(f => !string.IsNullOrEmpty(f))
  160. .ToList();
  161. }
  162. /// <summary>
  163. /// Gets a value indicating whether [is3 D].
  164. /// </summary>
  165. /// <value><c>true</c> if [is3 D]; otherwise, <c>false</c>.</value>
  166. [IgnoreDataMember]
  167. public bool Is3D
  168. {
  169. get { return Video3DFormat.HasValue; }
  170. }
  171. public bool IsHD { get; set; }
  172. /// <summary>
  173. /// Gets the type of the media.
  174. /// </summary>
  175. /// <value>The type of the media.</value>
  176. public override string MediaType
  177. {
  178. get
  179. {
  180. return Model.Entities.MediaType.Video;
  181. }
  182. }
  183. protected override async Task<bool> RefreshedOwnedItems(MetadataRefreshOptions options, List<FileSystemInfo> fileSystemChildren, CancellationToken cancellationToken)
  184. {
  185. var hasChanges = await base.RefreshedOwnedItems(options, fileSystemChildren, cancellationToken).ConfigureAwait(false);
  186. // Must have a parent to have additional parts or alternate versions
  187. // In other words, it must be part of the Parent/Child tree
  188. // The additional parts won't have additional parts themselves
  189. if (LocationType == LocationType.FileSystem && Parent != null)
  190. {
  191. if (IsMultiPart)
  192. {
  193. var additionalPartsChanged = await RefreshAdditionalParts(options, fileSystemChildren, cancellationToken).ConfigureAwait(false);
  194. if (additionalPartsChanged)
  195. {
  196. hasChanges = true;
  197. }
  198. }
  199. else
  200. {
  201. RefreshLinkedAlternateVersions();
  202. if (HasLocalAlternateVersions)
  203. {
  204. var additionalPartsChanged = await RefreshAlternateVersionsWithinSameDirectory(options, fileSystemChildren, cancellationToken).ConfigureAwait(false);
  205. if (additionalPartsChanged)
  206. {
  207. hasChanges = true;
  208. }
  209. }
  210. }
  211. }
  212. return hasChanges;
  213. }
  214. private bool RefreshLinkedAlternateVersions()
  215. {
  216. foreach (var child in LinkedAlternateVersions)
  217. {
  218. // Reset the cached value
  219. if (child.ItemId.HasValue && child.ItemId.Value == Guid.Empty)
  220. {
  221. child.ItemId = null;
  222. }
  223. }
  224. return false;
  225. }
  226. /// <summary>
  227. /// Refreshes the additional parts.
  228. /// </summary>
  229. /// <param name="options">The options.</param>
  230. /// <param name="fileSystemChildren">The file system children.</param>
  231. /// <param name="cancellationToken">The cancellation token.</param>
  232. /// <returns>Task{System.Boolean}.</returns>
  233. private async Task<bool> RefreshAdditionalParts(MetadataRefreshOptions options, IEnumerable<FileSystemInfo> fileSystemChildren, CancellationToken cancellationToken)
  234. {
  235. var newItems = LoadAdditionalParts(fileSystemChildren, options.DirectoryService).ToList();
  236. var newItemIds = newItems.Select(i => i.Id).ToList();
  237. var itemsChanged = !AdditionalPartIds.SequenceEqual(newItemIds);
  238. var tasks = newItems.Select(i => i.RefreshMetadata(options, cancellationToken));
  239. await Task.WhenAll(tasks).ConfigureAwait(false);
  240. AdditionalPartIds = newItemIds;
  241. return itemsChanged;
  242. }
  243. /// <summary>
  244. /// Loads the additional parts.
  245. /// </summary>
  246. /// <returns>IEnumerable{Video}.</returns>
  247. private IEnumerable<Video> LoadAdditionalParts(IEnumerable<FileSystemInfo> fileSystemChildren, IDirectoryService directoryService)
  248. {
  249. IEnumerable<FileSystemInfo> files;
  250. var path = Path;
  251. if (VideoType == VideoType.BluRay || VideoType == VideoType.Dvd)
  252. {
  253. files = fileSystemChildren.Where(i =>
  254. {
  255. if ((i.Attributes & FileAttributes.Directory) == FileAttributes.Directory)
  256. {
  257. return !string.Equals(i.FullName, path, StringComparison.OrdinalIgnoreCase) && EntityResolutionHelper.IsMultiPartFolder(i.FullName) && EntityResolutionHelper.IsMultiPartFile(i.Name);
  258. }
  259. return false;
  260. });
  261. }
  262. else
  263. {
  264. files = fileSystemChildren.Where(i =>
  265. {
  266. if ((i.Attributes & FileAttributes.Directory) == FileAttributes.Directory)
  267. {
  268. return false;
  269. }
  270. return !string.Equals(i.FullName, path, StringComparison.OrdinalIgnoreCase) && EntityResolutionHelper.IsVideoFile(i.FullName) && EntityResolutionHelper.IsMultiPartFile(i.Name);
  271. });
  272. }
  273. return LibraryManager.ResolvePaths<Video>(files, directoryService, null).Select(video =>
  274. {
  275. // Try to retrieve it from the db. If we don't find it, use the resolved version
  276. var dbItem = LibraryManager.GetItemById(video.Id) as Video;
  277. if (dbItem != null)
  278. {
  279. video = dbItem;
  280. }
  281. return video;
  282. // Sort them so that the list can be easily compared for changes
  283. }).OrderBy(i => i.Path).ToList();
  284. }
  285. private async Task<bool> RefreshAlternateVersionsWithinSameDirectory(MetadataRefreshOptions options, IEnumerable<FileSystemInfo> fileSystemChildren, CancellationToken cancellationToken)
  286. {
  287. var newItems = LoadAlternateVersionsWithinSameDirectory(fileSystemChildren, options.DirectoryService).ToList();
  288. var newItemIds = newItems.Select(i => i.Id).ToList();
  289. var itemsChanged = !AlternateVersionIds.SequenceEqual(newItemIds);
  290. var tasks = newItems.Select(i => i.RefreshMetadata(options, cancellationToken));
  291. await Task.WhenAll(tasks).ConfigureAwait(false);
  292. AlternateVersionIds = newItemIds;
  293. return itemsChanged;
  294. }
  295. /// <summary>
  296. /// Loads the additional parts.
  297. /// </summary>
  298. /// <returns>IEnumerable{Video}.</returns>
  299. private IEnumerable<Video> LoadAlternateVersionsWithinSameDirectory(IEnumerable<FileSystemInfo> fileSystemChildren, IDirectoryService directoryService)
  300. {
  301. IEnumerable<FileSystemInfo> files;
  302. var path = Path;
  303. var currentFilename = System.IO.Path.GetFileNameWithoutExtension(path) ?? string.Empty;
  304. // Only support this for video files. For folder rips, they'll have to use the linking feature
  305. if (VideoType == VideoType.VideoFile || VideoType == VideoType.Iso)
  306. {
  307. files = fileSystemChildren.Where(i =>
  308. {
  309. if ((i.Attributes & FileAttributes.Directory) == FileAttributes.Directory)
  310. {
  311. return false;
  312. }
  313. return !string.Equals(i.FullName, path, StringComparison.OrdinalIgnoreCase) &&
  314. EntityResolutionHelper.IsVideoFile(i.FullName) &&
  315. i.Name.StartsWith(currentFilename, StringComparison.OrdinalIgnoreCase);
  316. });
  317. }
  318. else
  319. {
  320. files = new List<FileSystemInfo>();
  321. }
  322. return LibraryManager.ResolvePaths<Video>(files, directoryService, null).Select(video =>
  323. {
  324. // Try to retrieve it from the db. If we don't find it, use the resolved version
  325. var dbItem = LibraryManager.GetItemById(video.Id) as Video;
  326. if (dbItem != null)
  327. {
  328. video = dbItem;
  329. }
  330. video.ImageInfos = ImageInfos;
  331. return video;
  332. // Sort them so that the list can be easily compared for changes
  333. }).OrderBy(i => i.Path).ToList();
  334. }
  335. public override IEnumerable<string> GetDeletePaths()
  336. {
  337. if (!IsInMixedFolder)
  338. {
  339. return new[] { ContainingFolderPath };
  340. }
  341. return base.GetDeletePaths();
  342. }
  343. public virtual IEnumerable<MediaStream> GetMediaStreams()
  344. {
  345. return ItemRepository.GetMediaStreams(new MediaStreamQuery
  346. {
  347. ItemId = Id
  348. });
  349. }
  350. public virtual MediaStream GetDefaultVideoStream()
  351. {
  352. if (!DefaultVideoStreamIndex.HasValue)
  353. {
  354. return null;
  355. }
  356. return ItemRepository.GetMediaStreams(new MediaStreamQuery
  357. {
  358. ItemId = Id,
  359. Index = DefaultVideoStreamIndex.Value
  360. }).FirstOrDefault();
  361. }
  362. }
  363. }