Video.cs 9.6 KB

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