Video.cs 8.3 KB

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