Video.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. using MediaBrowser.Controller.Resolvers;
  2. using MediaBrowser.Model.Entities;
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Runtime.Serialization;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. namespace MediaBrowser.Controller.Entities
  12. {
  13. /// <summary>
  14. /// Class Video
  15. /// </summary>
  16. public class Video : BaseItem, IHasMediaStreams, IHasAspectRatio
  17. {
  18. public bool IsMultiPart { get; set; }
  19. public List<Guid> AdditionalPartIds { get; set; }
  20. public Video()
  21. {
  22. PlayableStreamFileNames = new List<string>();
  23. AdditionalPartIds = new List<Guid>();
  24. }
  25. /// <summary>
  26. /// Gets or sets a value indicating whether this instance has subtitles.
  27. /// </summary>
  28. /// <value><c>true</c> if this instance has subtitles; otherwise, <c>false</c>.</value>
  29. public bool HasSubtitles { get; set; }
  30. /// <summary>
  31. /// Gets or sets the video bit rate.
  32. /// </summary>
  33. /// <value>The video bit rate.</value>
  34. public int? VideoBitRate { get; set; }
  35. /// <summary>
  36. /// Gets or sets the default index of the video stream.
  37. /// </summary>
  38. /// <value>The default index of the video stream.</value>
  39. public int? DefaultVideoStreamIndex { get; set; }
  40. /// <summary>
  41. /// Gets or sets the type of the video.
  42. /// </summary>
  43. /// <value>The type of the video.</value>
  44. public VideoType VideoType { get; set; }
  45. /// <summary>
  46. /// Gets or sets the type of the iso.
  47. /// </summary>
  48. /// <value>The type of the iso.</value>
  49. public IsoType? IsoType { get; set; }
  50. /// <summary>
  51. /// Gets or sets the video3 D format.
  52. /// </summary>
  53. /// <value>The video3 D format.</value>
  54. public Video3DFormat? Video3DFormat { get; set; }
  55. /// <summary>
  56. /// If the video is a folder-rip, this will hold the file list for the largest playlist
  57. /// </summary>
  58. public List<string> PlayableStreamFileNames { get; set; }
  59. /// <summary>
  60. /// Gets the playable stream files.
  61. /// </summary>
  62. /// <returns>List{System.String}.</returns>
  63. public List<string> GetPlayableStreamFiles()
  64. {
  65. return GetPlayableStreamFiles(Path);
  66. }
  67. /// <summary>
  68. /// Gets or sets the aspect ratio.
  69. /// </summary>
  70. /// <value>The aspect ratio.</value>
  71. public string AspectRatio { get; set; }
  72. /// <summary>
  73. /// Should be overridden to return the proper folder where metadata lives
  74. /// </summary>
  75. /// <value>The meta location.</value>
  76. [IgnoreDataMember]
  77. public override string MetaLocation
  78. {
  79. get
  80. {
  81. return VideoType == VideoType.VideoFile || VideoType == VideoType.Iso || IsMultiPart ? System.IO.Path.GetDirectoryName(Path) : Path;
  82. }
  83. }
  84. /// <summary>
  85. /// Needed because the resolver stops at the movie folder and we find the video inside.
  86. /// </summary>
  87. /// <value><c>true</c> if [use parent path to create resolve args]; otherwise, <c>false</c>.</value>
  88. protected override bool UseParentPathToCreateResolveArgs
  89. {
  90. get
  91. {
  92. if (IsInMixedFolder)
  93. {
  94. return false;
  95. }
  96. return VideoType == VideoType.VideoFile || VideoType == VideoType.Iso || IsMultiPart;
  97. }
  98. }
  99. public string MainFeaturePlaylistName { get; set; }
  100. /// <summary>
  101. /// Gets the playable stream files.
  102. /// </summary>
  103. /// <param name="rootPath">The root path.</param>
  104. /// <returns>List{System.String}.</returns>
  105. public List<string> GetPlayableStreamFiles(string rootPath)
  106. {
  107. if (PlayableStreamFileNames == null)
  108. {
  109. return null;
  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. /// <summary>
  138. /// Overrides the base implementation to refresh metadata for local trailers
  139. /// </summary>
  140. /// <param name="cancellationToken">The cancellation token.</param>
  141. /// <param name="forceSave">if set to <c>true</c> [is new item].</param>
  142. /// <param name="forceRefresh">if set to <c>true</c> [force].</param>
  143. /// <param name="allowSlowProviders">if set to <c>true</c> [allow slow providers].</param>
  144. /// <param name="resetResolveArgs">The reset resolve args.</param>
  145. /// <returns>true if a provider reports we changed</returns>
  146. public override async Task<bool> RefreshMetadata(CancellationToken cancellationToken, bool forceSave = false, bool forceRefresh = false, bool allowSlowProviders = true, bool resetResolveArgs = true)
  147. {
  148. // Kick off a task to refresh the main item
  149. var result = await base.RefreshMetadata(cancellationToken, forceSave, forceRefresh, allowSlowProviders, resetResolveArgs).ConfigureAwait(false);
  150. var additionalPartsChanged = false;
  151. // Must have a parent to have additional parts
  152. // In other words, it must be part of the Parent/Child tree
  153. // The additional parts won't have additional parts themselves
  154. if (IsMultiPart && LocationType == LocationType.FileSystem && Parent != null)
  155. {
  156. try
  157. {
  158. additionalPartsChanged = await RefreshAdditionalParts(cancellationToken, forceSave, forceRefresh, allowSlowProviders).ConfigureAwait(false);
  159. }
  160. catch (IOException ex)
  161. {
  162. Logger.ErrorException("Error loading additional parts for {0}.", ex, Name);
  163. }
  164. }
  165. return additionalPartsChanged || result;
  166. }
  167. /// <summary>
  168. /// Refreshes the additional parts.
  169. /// </summary>
  170. /// <param name="cancellationToken">The cancellation token.</param>
  171. /// <param name="forceSave">if set to <c>true</c> [force save].</param>
  172. /// <param name="forceRefresh">if set to <c>true</c> [force refresh].</param>
  173. /// <param name="allowSlowProviders">if set to <c>true</c> [allow slow providers].</param>
  174. /// <returns>Task{System.Boolean}.</returns>
  175. private async Task<bool> RefreshAdditionalParts(CancellationToken cancellationToken, bool forceSave = false, bool forceRefresh = false, bool allowSlowProviders = true)
  176. {
  177. var newItems = LoadAdditionalParts().ToList();
  178. var newItemIds = newItems.Select(i => i.Id).ToList();
  179. var itemsChanged = !AdditionalPartIds.SequenceEqual(newItemIds);
  180. var tasks = newItems.Select(i => i.RefreshMetadata(cancellationToken, forceSave, forceRefresh, allowSlowProviders));
  181. var results = await Task.WhenAll(tasks).ConfigureAwait(false);
  182. AdditionalPartIds = newItemIds;
  183. return itemsChanged || results.Contains(true);
  184. }
  185. /// <summary>
  186. /// Loads the additional parts.
  187. /// </summary>
  188. /// <returns>IEnumerable{Video}.</returns>
  189. private IEnumerable<Video> LoadAdditionalParts()
  190. {
  191. IEnumerable<FileSystemInfo> files;
  192. if (VideoType == VideoType.BluRay || VideoType == VideoType.Dvd)
  193. {
  194. files = new DirectoryInfo(System.IO.Path.GetDirectoryName(Path))
  195. .EnumerateDirectories()
  196. .Where(i => !string.Equals(i.FullName, Path, StringComparison.OrdinalIgnoreCase) && EntityResolutionHelper.IsMultiPartFile(i.Name));
  197. }
  198. else
  199. {
  200. files = ResolveArgs.FileSystemChildren.Where(i =>
  201. {
  202. if ((i.Attributes & FileAttributes.Directory) == FileAttributes.Directory)
  203. {
  204. return false;
  205. }
  206. return !string.Equals(i.FullName, Path, StringComparison.OrdinalIgnoreCase) && EntityResolutionHelper.IsVideoFile(i.FullName) && EntityResolutionHelper.IsMultiPartFile(i.Name);
  207. });
  208. }
  209. return LibraryManager.ResolvePaths<Video>(files, null).Select(video =>
  210. {
  211. // Try to retrieve it from the db. If we don't find it, use the resolved version
  212. var dbItem = LibraryManager.RetrieveItem(video.Id) as Video;
  213. if (dbItem != null)
  214. {
  215. dbItem.ResetResolveArgs(video.ResolveArgs);
  216. video = dbItem;
  217. }
  218. return video;
  219. }).ToList();
  220. }
  221. public override IEnumerable<string> GetDeletePaths()
  222. {
  223. if (!IsInMixedFolder)
  224. {
  225. if (VideoType == VideoType.VideoFile || VideoType == VideoType.Iso)
  226. {
  227. return new[] { System.IO.Path.GetDirectoryName(Path) };
  228. }
  229. }
  230. return base.GetDeletePaths();
  231. }
  232. }
  233. }