Video.cs 9.2 KB

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