Video.cs 9.9 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 (VideoType == VideoType.BluRay || VideoType == VideoType.Dvd ||
  97. VideoType == VideoType.HdDvd)
  98. {
  99. return Path;
  100. }
  101. return base.ContainingFolderPath;
  102. }
  103. }
  104. public string MainFeaturePlaylistName { get; set; }
  105. /// <summary>
  106. /// Gets the playable stream files.
  107. /// </summary>
  108. /// <param name="rootPath">The root path.</param>
  109. /// <returns>List{System.String}.</returns>
  110. public List<string> GetPlayableStreamFiles(string rootPath)
  111. {
  112. var allFiles = Directory.EnumerateFiles(rootPath, "*", SearchOption.AllDirectories).ToList();
  113. return PlayableStreamFileNames.Select(name => allFiles.FirstOrDefault(f => string.Equals(System.IO.Path.GetFileName(f), name, StringComparison.OrdinalIgnoreCase)))
  114. .Where(f => !string.IsNullOrEmpty(f))
  115. .ToList();
  116. }
  117. /// <summary>
  118. /// Gets a value indicating whether [is3 D].
  119. /// </summary>
  120. /// <value><c>true</c> if [is3 D]; otherwise, <c>false</c>.</value>
  121. [IgnoreDataMember]
  122. public bool Is3D
  123. {
  124. get { return Video3DFormat.HasValue; }
  125. }
  126. public bool IsHD { get; set; }
  127. /// <summary>
  128. /// Gets the type of the media.
  129. /// </summary>
  130. /// <value>The type of the media.</value>
  131. public override string MediaType
  132. {
  133. get
  134. {
  135. return Model.Entities.MediaType.Video;
  136. }
  137. }
  138. protected override async Task<bool> RefreshedOwnedItems(MetadataRefreshOptions options, List<FileSystemInfo> fileSystemChildren, CancellationToken cancellationToken)
  139. {
  140. var hasChanges = await base.RefreshedOwnedItems(options, fileSystemChildren, cancellationToken).ConfigureAwait(false);
  141. // Must have a parent to have additional parts
  142. // In other words, it must be part of the Parent/Child tree
  143. // The additional parts won't have additional parts themselves
  144. if (IsMultiPart && LocationType == LocationType.FileSystem && Parent != null)
  145. {
  146. var additionalPartsChanged = await RefreshAdditionalParts(options, fileSystemChildren, cancellationToken).ConfigureAwait(false);
  147. if (additionalPartsChanged)
  148. {
  149. hasChanges = true;
  150. }
  151. }
  152. return hasChanges;
  153. }
  154. /// <summary>
  155. /// Refreshes the additional parts.
  156. /// </summary>
  157. /// <param name="options">The options.</param>
  158. /// <param name="fileSystemChildren">The file system children.</param>
  159. /// <param name="cancellationToken">The cancellation token.</param>
  160. /// <returns>Task{System.Boolean}.</returns>
  161. private async Task<bool> RefreshAdditionalParts(MetadataRefreshOptions options, IEnumerable<FileSystemInfo> fileSystemChildren, CancellationToken cancellationToken)
  162. {
  163. var newItems = LoadAdditionalParts(fileSystemChildren, options.DirectoryService).ToList();
  164. var newItemIds = newItems.Select(i => i.Id).ToList();
  165. var itemsChanged = !AdditionalPartIds.SequenceEqual(newItemIds);
  166. var tasks = newItems.Select(i => i.RefreshMetadata(options, cancellationToken));
  167. await Task.WhenAll(tasks).ConfigureAwait(false);
  168. AdditionalPartIds = newItemIds;
  169. return itemsChanged;
  170. }
  171. /// <summary>
  172. /// Loads the additional parts.
  173. /// </summary>
  174. /// <returns>IEnumerable{Video}.</returns>
  175. private IEnumerable<Video> LoadAdditionalParts(IEnumerable<FileSystemInfo> fileSystemChildren, IDirectoryService directoryService)
  176. {
  177. IEnumerable<FileSystemInfo> files;
  178. var path = Path;
  179. if (VideoType == VideoType.BluRay || VideoType == VideoType.Dvd)
  180. {
  181. files = fileSystemChildren.Where(i =>
  182. {
  183. if ((i.Attributes & FileAttributes.Directory) == FileAttributes.Directory)
  184. {
  185. return !string.Equals(i.FullName, path, StringComparison.OrdinalIgnoreCase) && EntityResolutionHelper.IsVideoFile(i.FullName) && EntityResolutionHelper.IsMultiPartFile(i.Name);
  186. }
  187. return false;
  188. });
  189. }
  190. else
  191. {
  192. files = fileSystemChildren.Where(i =>
  193. {
  194. if ((i.Attributes & FileAttributes.Directory) == FileAttributes.Directory)
  195. {
  196. return false;
  197. }
  198. return !string.Equals(i.FullName, path, StringComparison.OrdinalIgnoreCase) && EntityResolutionHelper.IsVideoFile(i.FullName) && EntityResolutionHelper.IsMultiPartFile(i.Name);
  199. });
  200. }
  201. return LibraryManager.ResolvePaths<Video>(files, directoryService, null).Select(video =>
  202. {
  203. // Try to retrieve it from the db. If we don't find it, use the resolved version
  204. var dbItem = LibraryManager.GetItemById(video.Id) as Video;
  205. if (dbItem != null)
  206. {
  207. video = dbItem;
  208. }
  209. return video;
  210. // Sort them so that the list can be easily compared for changes
  211. }).OrderBy(i => i.Path).ToList();
  212. }
  213. public override IEnumerable<string> GetDeletePaths()
  214. {
  215. if (!IsInMixedFolder)
  216. {
  217. if (VideoType == VideoType.VideoFile || VideoType == VideoType.Iso)
  218. {
  219. return new[] { System.IO.Path.GetDirectoryName(Path) };
  220. }
  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. }