Video.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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
  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. /// Should be overridden to return the proper folder where metadata lives
  60. /// </summary>
  61. /// <value>The meta location.</value>
  62. [IgnoreDataMember]
  63. public override string MetaLocation
  64. {
  65. get
  66. {
  67. return VideoType == VideoType.VideoFile || VideoType == VideoType.Iso || IsMultiPart ? System.IO.Path.GetDirectoryName(Path) : Path;
  68. }
  69. }
  70. /// <summary>
  71. /// Needed because the resolver stops at the movie folder and we find the video inside.
  72. /// </summary>
  73. /// <value><c>true</c> if [use parent path to create resolve args]; otherwise, <c>false</c>.</value>
  74. protected override bool UseParentPathToCreateResolveArgs
  75. {
  76. get
  77. {
  78. if (IsInMixedFolder)
  79. {
  80. return false;
  81. }
  82. return VideoType == VideoType.VideoFile || VideoType == VideoType.Iso || IsMultiPart;
  83. }
  84. }
  85. public string MainFeaturePlaylistName { get; set; }
  86. /// <summary>
  87. /// Gets the playable stream files.
  88. /// </summary>
  89. /// <param name="rootPath">The root path.</param>
  90. /// <returns>List{System.String}.</returns>
  91. public List<string> GetPlayableStreamFiles(string rootPath)
  92. {
  93. if (PlayableStreamFileNames == null)
  94. {
  95. return null;
  96. }
  97. var allFiles = Directory.EnumerateFiles(rootPath, "*", SearchOption.AllDirectories).ToList();
  98. return PlayableStreamFileNames.Select(name => allFiles.FirstOrDefault(f => string.Equals(System.IO.Path.GetFileName(f), name, StringComparison.OrdinalIgnoreCase)))
  99. .Where(f => !string.IsNullOrEmpty(f))
  100. .ToList();
  101. }
  102. /// <summary>
  103. /// The default video stream for this video. Use this to determine media info for this item.
  104. /// </summary>
  105. /// <value>The default video stream.</value>
  106. [IgnoreDataMember]
  107. public MediaStream DefaultVideoStream
  108. {
  109. get { return MediaStreams != null ? MediaStreams.FirstOrDefault(s => s.Type == MediaStreamType.Video) : null; }
  110. }
  111. /// <summary>
  112. /// Gets a value indicating whether [is3 D].
  113. /// </summary>
  114. /// <value><c>true</c> if [is3 D]; otherwise, <c>false</c>.</value>
  115. [IgnoreDataMember]
  116. public bool Is3D
  117. {
  118. get { return Video3DFormat.HasValue; }
  119. }
  120. public bool IsHD
  121. {
  122. get { return MediaStreams != null && MediaStreams.Any(i => i.Type == MediaStreamType.Video && i.Width.HasValue && i.Width.Value >= 1280); }
  123. }
  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. /// <summary>
  136. /// Overrides the base implementation to refresh metadata for local trailers
  137. /// </summary>
  138. /// <param name="cancellationToken">The cancellation token.</param>
  139. /// <param name="forceSave">if set to <c>true</c> [is new item].</param>
  140. /// <param name="forceRefresh">if set to <c>true</c> [force].</param>
  141. /// <param name="allowSlowProviders">if set to <c>true</c> [allow slow providers].</param>
  142. /// <param name="resetResolveArgs">The reset resolve args.</param>
  143. /// <returns>true if a provider reports we changed</returns>
  144. public override async Task<bool> RefreshMetadata(CancellationToken cancellationToken, bool forceSave = false, bool forceRefresh = false, bool allowSlowProviders = true, bool resetResolveArgs = true)
  145. {
  146. // Kick off a task to refresh the main item
  147. var result = await base.RefreshMetadata(cancellationToken, forceSave, forceRefresh, allowSlowProviders, resetResolveArgs).ConfigureAwait(false);
  148. var additionalPartsChanged = false;
  149. // Must have a parent to have additional parts
  150. // In other words, it must be part of the Parent/Child tree
  151. // The additional parts won't have additional parts themselves
  152. if (IsMultiPart && LocationType == LocationType.FileSystem && Parent != null)
  153. {
  154. try
  155. {
  156. additionalPartsChanged = await RefreshAdditionalParts(cancellationToken, forceSave, forceRefresh, allowSlowProviders).ConfigureAwait(false);
  157. }
  158. catch (IOException ex)
  159. {
  160. Logger.ErrorException("Error loading additional parts for {0}.", ex, Name);
  161. }
  162. }
  163. return additionalPartsChanged || result;
  164. }
  165. /// <summary>
  166. /// Refreshes the additional parts.
  167. /// </summary>
  168. /// <param name="cancellationToken">The cancellation token.</param>
  169. /// <param name="forceSave">if set to <c>true</c> [force save].</param>
  170. /// <param name="forceRefresh">if set to <c>true</c> [force refresh].</param>
  171. /// <param name="allowSlowProviders">if set to <c>true</c> [allow slow providers].</param>
  172. /// <returns>Task{System.Boolean}.</returns>
  173. private async Task<bool> RefreshAdditionalParts(CancellationToken cancellationToken, bool forceSave = false, bool forceRefresh = false, bool allowSlowProviders = true)
  174. {
  175. var newItems = LoadAdditionalParts().ToList();
  176. var newItemIds = newItems.Select(i => i.Id).ToList();
  177. var itemsChanged = !AdditionalPartIds.SequenceEqual(newItemIds);
  178. var tasks = newItems.Select(i => i.RefreshMetadata(cancellationToken, forceSave, forceRefresh, allowSlowProviders));
  179. var results = await Task.WhenAll(tasks).ConfigureAwait(false);
  180. AdditionalPartIds = newItemIds;
  181. return itemsChanged || results.Contains(true);
  182. }
  183. /// <summary>
  184. /// Loads the additional parts.
  185. /// </summary>
  186. /// <returns>IEnumerable{Video}.</returns>
  187. private IEnumerable<Video> LoadAdditionalParts()
  188. {
  189. IEnumerable<FileSystemInfo> files;
  190. if (VideoType == VideoType.BluRay || VideoType == VideoType.Dvd)
  191. {
  192. files = new DirectoryInfo(System.IO.Path.GetDirectoryName(Path))
  193. .EnumerateDirectories()
  194. .Where(i => !string.Equals(i.FullName, Path, StringComparison.OrdinalIgnoreCase) && EntityResolutionHelper.IsMultiPartFile(i.Name));
  195. }
  196. else
  197. {
  198. files = ResolveArgs.FileSystemChildren.Where(i =>
  199. {
  200. if ((i.Attributes & FileAttributes.Directory) == FileAttributes.Directory)
  201. {
  202. return false;
  203. }
  204. return !string.Equals(i.FullName, Path, StringComparison.OrdinalIgnoreCase) && EntityResolutionHelper.IsVideoFile(i.FullName) && EntityResolutionHelper.IsMultiPartFile(i.Name);
  205. });
  206. }
  207. return LibraryManager.ResolvePaths<Video>(files, null).Select(video =>
  208. {
  209. // Try to retrieve it from the db. If we don't find it, use the resolved version
  210. var dbItem = LibraryManager.RetrieveItem(video.Id) as Video;
  211. if (dbItem != null)
  212. {
  213. dbItem.ResetResolveArgs(video.ResolveArgs);
  214. video = dbItem;
  215. }
  216. return video;
  217. }).ToList();
  218. }
  219. }
  220. }