Video.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. MediaStreams = new List<MediaStream>();
  23. PlayableStreamFileNames = new List<string>();
  24. AdditionalPartIds = new List<Guid>();
  25. }
  26. /// <summary>
  27. /// Gets or sets the type of the video.
  28. /// </summary>
  29. /// <value>The type of the video.</value>
  30. public VideoType VideoType { get; set; }
  31. /// <summary>
  32. /// Gets or sets the type of the iso.
  33. /// </summary>
  34. /// <value>The type of the iso.</value>
  35. public IsoType? IsoType { get; set; }
  36. /// <summary>
  37. /// Gets or sets the video3 D format.
  38. /// </summary>
  39. /// <value>The video3 D format.</value>
  40. public Video3DFormat? Video3DFormat { get; set; }
  41. /// <summary>
  42. /// Gets or sets the media streams.
  43. /// </summary>
  44. /// <value>The media streams.</value>
  45. public List<MediaStream> MediaStreams { get; set; }
  46. /// <summary>
  47. /// If the video is a folder-rip, this will hold the file list for the largest playlist
  48. /// </summary>
  49. public List<string> PlayableStreamFileNames { get; set; }
  50. /// <summary>
  51. /// Gets the playable stream files.
  52. /// </summary>
  53. /// <returns>List{System.String}.</returns>
  54. public List<string> GetPlayableStreamFiles()
  55. {
  56. return GetPlayableStreamFiles(Path);
  57. }
  58. /// <summary>
  59. /// Gets or sets the aspect ratio.
  60. /// </summary>
  61. /// <value>The aspect ratio.</value>
  62. public string AspectRatio { get; set; }
  63. /// <summary>
  64. /// Should be overridden to return the proper folder where metadata lives
  65. /// </summary>
  66. /// <value>The meta location.</value>
  67. [IgnoreDataMember]
  68. public override string MetaLocation
  69. {
  70. get
  71. {
  72. return VideoType == VideoType.VideoFile || VideoType == VideoType.Iso || IsMultiPart ? System.IO.Path.GetDirectoryName(Path) : Path;
  73. }
  74. }
  75. /// <summary>
  76. /// Needed because the resolver stops at the movie folder and we find the video inside.
  77. /// </summary>
  78. /// <value><c>true</c> if [use parent path to create resolve args]; otherwise, <c>false</c>.</value>
  79. protected override bool UseParentPathToCreateResolveArgs
  80. {
  81. get
  82. {
  83. if (IsInMixedFolder)
  84. {
  85. return false;
  86. }
  87. return VideoType == VideoType.VideoFile || VideoType == VideoType.Iso || IsMultiPart;
  88. }
  89. }
  90. public string MainFeaturePlaylistName { get; set; }
  91. /// <summary>
  92. /// Gets the playable stream files.
  93. /// </summary>
  94. /// <param name="rootPath">The root path.</param>
  95. /// <returns>List{System.String}.</returns>
  96. public List<string> GetPlayableStreamFiles(string rootPath)
  97. {
  98. if (PlayableStreamFileNames == null)
  99. {
  100. return null;
  101. }
  102. var allFiles = Directory.EnumerateFiles(rootPath, "*", SearchOption.AllDirectories).ToList();
  103. return PlayableStreamFileNames.Select(name => allFiles.FirstOrDefault(f => string.Equals(System.IO.Path.GetFileName(f), name, StringComparison.OrdinalIgnoreCase)))
  104. .Where(f => !string.IsNullOrEmpty(f))
  105. .ToList();
  106. }
  107. /// <summary>
  108. /// The default video stream for this video. Use this to determine media info for this item.
  109. /// </summary>
  110. /// <value>The default video stream.</value>
  111. [IgnoreDataMember]
  112. public MediaStream DefaultVideoStream
  113. {
  114. get { return MediaStreams != null ? MediaStreams.FirstOrDefault(s => s.Type == MediaStreamType.Video) : null; }
  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. }