Video.cs 9.8 KB

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