Video.cs 8.6 KB

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