Video.cs 7.9 KB

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