Video.cs 8.5 KB

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