Video.cs 8.5 KB

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