Video.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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 { get; set; }
  121. /// <summary>
  122. /// Gets the type of the media.
  123. /// </summary>
  124. /// <value>The type of the media.</value>
  125. public override string MediaType
  126. {
  127. get
  128. {
  129. return Model.Entities.MediaType.Video;
  130. }
  131. }
  132. /// <summary>
  133. /// Overrides the base implementation to refresh metadata for local trailers
  134. /// </summary>
  135. /// <param name="cancellationToken">The cancellation token.</param>
  136. /// <param name="forceSave">if set to <c>true</c> [is new item].</param>
  137. /// <param name="forceRefresh">if set to <c>true</c> [force].</param>
  138. /// <param name="allowSlowProviders">if set to <c>true</c> [allow slow providers].</param>
  139. /// <param name="resetResolveArgs">The reset resolve args.</param>
  140. /// <returns>true if a provider reports we changed</returns>
  141. public override async Task<bool> RefreshMetadata(CancellationToken cancellationToken, bool forceSave = false, bool forceRefresh = false, bool allowSlowProviders = true, bool resetResolveArgs = true)
  142. {
  143. // Kick off a task to refresh the main item
  144. var result = await base.RefreshMetadata(cancellationToken, forceSave, forceRefresh, allowSlowProviders, resetResolveArgs).ConfigureAwait(false);
  145. var additionalPartsChanged = false;
  146. // Must have a parent to have additional parts
  147. // In other words, it must be part of the Parent/Child tree
  148. // The additional parts won't have additional parts themselves
  149. if (IsMultiPart && LocationType == LocationType.FileSystem && Parent != null)
  150. {
  151. try
  152. {
  153. additionalPartsChanged = await RefreshAdditionalParts(cancellationToken, forceSave, forceRefresh, allowSlowProviders).ConfigureAwait(false);
  154. }
  155. catch (IOException ex)
  156. {
  157. Logger.ErrorException("Error loading additional parts for {0}.", ex, Name);
  158. }
  159. }
  160. return additionalPartsChanged || result;
  161. }
  162. /// <summary>
  163. /// Refreshes the additional parts.
  164. /// </summary>
  165. /// <param name="cancellationToken">The cancellation token.</param>
  166. /// <param name="forceSave">if set to <c>true</c> [force save].</param>
  167. /// <param name="forceRefresh">if set to <c>true</c> [force refresh].</param>
  168. /// <param name="allowSlowProviders">if set to <c>true</c> [allow slow providers].</param>
  169. /// <returns>Task{System.Boolean}.</returns>
  170. private async Task<bool> RefreshAdditionalParts(CancellationToken cancellationToken, bool forceSave = false, bool forceRefresh = false, bool allowSlowProviders = true)
  171. {
  172. var newItems = LoadAdditionalParts().ToList();
  173. var newItemIds = newItems.Select(i => i.Id).ToList();
  174. var itemsChanged = !AdditionalPartIds.SequenceEqual(newItemIds);
  175. var tasks = newItems.Select(i => i.RefreshMetadata(cancellationToken, forceSave, forceRefresh, allowSlowProviders));
  176. var results = await Task.WhenAll(tasks).ConfigureAwait(false);
  177. AdditionalPartIds = newItemIds;
  178. return itemsChanged || results.Contains(true);
  179. }
  180. /// <summary>
  181. /// Loads the additional parts.
  182. /// </summary>
  183. /// <returns>IEnumerable{Video}.</returns>
  184. private IEnumerable<Video> LoadAdditionalParts()
  185. {
  186. IEnumerable<FileSystemInfo> files;
  187. if (VideoType == VideoType.BluRay || VideoType == VideoType.Dvd)
  188. {
  189. files = new DirectoryInfo(System.IO.Path.GetDirectoryName(Path))
  190. .EnumerateDirectories()
  191. .Where(i => !string.Equals(i.FullName, Path, StringComparison.OrdinalIgnoreCase) && EntityResolutionHelper.IsMultiPartFile(i.Name));
  192. }
  193. else
  194. {
  195. files = ResolveArgs.FileSystemChildren.Where(i =>
  196. {
  197. if ((i.Attributes & FileAttributes.Directory) == FileAttributes.Directory)
  198. {
  199. return false;
  200. }
  201. return !string.Equals(i.FullName, Path, StringComparison.OrdinalIgnoreCase) && EntityResolutionHelper.IsVideoFile(i.FullName) && EntityResolutionHelper.IsMultiPartFile(i.Name);
  202. });
  203. }
  204. return LibraryManager.ResolvePaths<Video>(files, null).Select(video =>
  205. {
  206. // Try to retrieve it from the db. If we don't find it, use the resolved version
  207. var dbItem = LibraryManager.RetrieveItem(video.Id) as Video;
  208. if (dbItem != null)
  209. {
  210. dbItem.ResetResolveArgs(video.ResolveArgs);
  211. video = dbItem;
  212. }
  213. return video;
  214. }).ToList();
  215. }
  216. }
  217. }