Video.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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 List<Guid> AdditionalPartIds { get; set; }
  22. public Video()
  23. {
  24. PlayableStreamFileNames = new List<string>();
  25. AdditionalPartIds = new List<Guid>();
  26. Tags = new List<string>();
  27. SubtitleFiles = new List<string>();
  28. }
  29. /// <summary>
  30. /// Gets or sets the subtitle paths.
  31. /// </summary>
  32. /// <value>The subtitle paths.</value>
  33. public List<string> SubtitleFiles { get; set; }
  34. /// <summary>
  35. /// Gets or sets a value indicating whether this instance has subtitles.
  36. /// </summary>
  37. /// <value><c>true</c> if this instance has subtitles; otherwise, <c>false</c>.</value>
  38. public bool HasSubtitles { get; set; }
  39. public bool IsPlaceHolder { get; set; }
  40. /// <summary>
  41. /// Gets or sets the tags.
  42. /// </summary>
  43. /// <value>The tags.</value>
  44. public List<string> Tags { get; set; }
  45. /// <summary>
  46. /// Gets or sets the video bit rate.
  47. /// </summary>
  48. /// <value>The video bit rate.</value>
  49. public int? VideoBitRate { get; set; }
  50. /// <summary>
  51. /// Gets or sets the default index of the video stream.
  52. /// </summary>
  53. /// <value>The default index of the video stream.</value>
  54. public int? DefaultVideoStreamIndex { get; set; }
  55. /// <summary>
  56. /// Gets or sets the type of the video.
  57. /// </summary>
  58. /// <value>The type of the video.</value>
  59. public VideoType VideoType { get; set; }
  60. /// <summary>
  61. /// Gets or sets the type of the iso.
  62. /// </summary>
  63. /// <value>The type of the iso.</value>
  64. public IsoType? IsoType { get; set; }
  65. /// <summary>
  66. /// Gets or sets the video3 D format.
  67. /// </summary>
  68. /// <value>The video3 D format.</value>
  69. public Video3DFormat? Video3DFormat { get; set; }
  70. /// <summary>
  71. /// If the video is a folder-rip, this will hold the file list for the largest playlist
  72. /// </summary>
  73. public List<string> PlayableStreamFileNames { get; set; }
  74. /// <summary>
  75. /// Gets the playable stream files.
  76. /// </summary>
  77. /// <returns>List{System.String}.</returns>
  78. public List<string> GetPlayableStreamFiles()
  79. {
  80. return GetPlayableStreamFiles(Path);
  81. }
  82. /// <summary>
  83. /// Gets or sets the aspect ratio.
  84. /// </summary>
  85. /// <value>The aspect ratio.</value>
  86. public string AspectRatio { get; set; }
  87. [IgnoreDataMember]
  88. public override string ContainingFolderPath
  89. {
  90. get
  91. {
  92. if (IsMultiPart)
  93. {
  94. return System.IO.Path.GetDirectoryName(Path);
  95. }
  96. if (!IsPlaceHolder)
  97. {
  98. if (VideoType == VideoType.BluRay || VideoType == VideoType.Dvd ||
  99. VideoType == VideoType.HdDvd)
  100. {
  101. return Path;
  102. }
  103. }
  104. return base.ContainingFolderPath;
  105. }
  106. }
  107. public string MainFeaturePlaylistName { get; set; }
  108. /// <summary>
  109. /// Gets the playable stream files.
  110. /// </summary>
  111. /// <param name="rootPath">The root path.</param>
  112. /// <returns>List{System.String}.</returns>
  113. public List<string> GetPlayableStreamFiles(string rootPath)
  114. {
  115. var allFiles = Directory.EnumerateFiles(rootPath, "*", SearchOption.AllDirectories).ToList();
  116. return PlayableStreamFileNames.Select(name => allFiles.FirstOrDefault(f => string.Equals(System.IO.Path.GetFileName(f), name, StringComparison.OrdinalIgnoreCase)))
  117. .Where(f => !string.IsNullOrEmpty(f))
  118. .ToList();
  119. }
  120. /// <summary>
  121. /// Gets a value indicating whether [is3 D].
  122. /// </summary>
  123. /// <value><c>true</c> if [is3 D]; otherwise, <c>false</c>.</value>
  124. [IgnoreDataMember]
  125. public bool Is3D
  126. {
  127. get { return Video3DFormat.HasValue; }
  128. }
  129. public bool IsHD { get; set; }
  130. /// <summary>
  131. /// Gets the type of the media.
  132. /// </summary>
  133. /// <value>The type of the media.</value>
  134. public override string MediaType
  135. {
  136. get
  137. {
  138. return Model.Entities.MediaType.Video;
  139. }
  140. }
  141. protected override async Task<bool> RefreshedOwnedItems(MetadataRefreshOptions options, List<FileSystemInfo> fileSystemChildren, CancellationToken cancellationToken)
  142. {
  143. var hasChanges = await base.RefreshedOwnedItems(options, fileSystemChildren, cancellationToken).ConfigureAwait(false);
  144. // Must have a parent to have additional parts
  145. // In other words, it must be part of the Parent/Child tree
  146. // The additional parts won't have additional parts themselves
  147. if (IsMultiPart && LocationType == LocationType.FileSystem && Parent != null)
  148. {
  149. var additionalPartsChanged = await RefreshAdditionalParts(options, fileSystemChildren, cancellationToken).ConfigureAwait(false);
  150. if (additionalPartsChanged)
  151. {
  152. hasChanges = true;
  153. }
  154. }
  155. return hasChanges;
  156. }
  157. /// <summary>
  158. /// Refreshes the additional parts.
  159. /// </summary>
  160. /// <param name="options">The options.</param>
  161. /// <param name="fileSystemChildren">The file system children.</param>
  162. /// <param name="cancellationToken">The cancellation token.</param>
  163. /// <returns>Task{System.Boolean}.</returns>
  164. private async Task<bool> RefreshAdditionalParts(MetadataRefreshOptions options, IEnumerable<FileSystemInfo> fileSystemChildren, CancellationToken cancellationToken)
  165. {
  166. var newItems = LoadAdditionalParts(fileSystemChildren, options.DirectoryService).ToList();
  167. var newItemIds = newItems.Select(i => i.Id).ToList();
  168. var itemsChanged = !AdditionalPartIds.SequenceEqual(newItemIds);
  169. var tasks = newItems.Select(i => i.RefreshMetadata(options, cancellationToken));
  170. await Task.WhenAll(tasks).ConfigureAwait(false);
  171. AdditionalPartIds = newItemIds;
  172. return itemsChanged;
  173. }
  174. /// <summary>
  175. /// Loads the additional parts.
  176. /// </summary>
  177. /// <returns>IEnumerable{Video}.</returns>
  178. private IEnumerable<Video> LoadAdditionalParts(IEnumerable<FileSystemInfo> fileSystemChildren, IDirectoryService directoryService)
  179. {
  180. IEnumerable<FileSystemInfo> files;
  181. var path = Path;
  182. if (VideoType == VideoType.BluRay || VideoType == VideoType.Dvd)
  183. {
  184. files = fileSystemChildren.Where(i =>
  185. {
  186. if ((i.Attributes & FileAttributes.Directory) == FileAttributes.Directory)
  187. {
  188. return !string.Equals(i.FullName, path, StringComparison.OrdinalIgnoreCase) && EntityResolutionHelper.IsVideoFile(i.FullName) && EntityResolutionHelper.IsMultiPartFile(i.Name);
  189. }
  190. return false;
  191. });
  192. }
  193. else
  194. {
  195. files = fileSystemChildren.Where(i =>
  196. {
  197. if ((i.Attributes & FileAttributes.Directory) == FileAttributes.Directory)
  198. {
  199. return false;
  200. }
  201. return !string.Equals(i.FullName, path, StringComparison.OrdinalIgnoreCase) && EntityResolutionHelper.IsVideoFile(i.FullName) && EntityResolutionHelper.IsMultiPartFile(i.Name);
  202. });
  203. }
  204. return LibraryManager.ResolvePaths<Video>(files, directoryService, null).Select(video =>
  205. {
  206. // Try to retrieve it from the db. If we don't find it, use the resolved version
  207. var dbItem = LibraryManager.GetItemById(video.Id) as Video;
  208. if (dbItem != null)
  209. {
  210. video = dbItem;
  211. }
  212. return video;
  213. // Sort them so that the list can be easily compared for changes
  214. }).OrderBy(i => i.Path).ToList();
  215. }
  216. public override IEnumerable<string> GetDeletePaths()
  217. {
  218. if (!IsInMixedFolder)
  219. {
  220. return new[] { ContainingFolderPath };
  221. }
  222. return base.GetDeletePaths();
  223. }
  224. public virtual IEnumerable<MediaStream> GetMediaStreams()
  225. {
  226. return ItemRepository.GetMediaStreams(new MediaStreamQuery
  227. {
  228. ItemId = Id
  229. });
  230. }
  231. public virtual MediaStream GetDefaultVideoStream()
  232. {
  233. if (!DefaultVideoStreamIndex.HasValue)
  234. {
  235. return null;
  236. }
  237. return ItemRepository.GetMediaStreams(new MediaStreamQuery
  238. {
  239. ItemId = Id,
  240. Index = DefaultVideoStreamIndex.Value
  241. }).FirstOrDefault();
  242. }
  243. }
  244. }