Video.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. if (IsMultiPart && LocationType == LocationType.FileSystem)
  124. {
  125. try
  126. {
  127. additionalPartsChanged = await RefreshAdditionalParts(cancellationToken, forceSave, forceRefresh, allowSlowProviders).ConfigureAwait(false);
  128. }
  129. catch (IOException ex)
  130. {
  131. Logger.ErrorException("Error loading additional parts for {0}.", ex, Name);
  132. }
  133. }
  134. return additionalPartsChanged || result;
  135. }
  136. /// <summary>
  137. /// Refreshes the additional parts.
  138. /// </summary>
  139. /// <param name="cancellationToken">The cancellation token.</param>
  140. /// <param name="forceSave">if set to <c>true</c> [force save].</param>
  141. /// <param name="forceRefresh">if set to <c>true</c> [force refresh].</param>
  142. /// <param name="allowSlowProviders">if set to <c>true</c> [allow slow providers].</param>
  143. /// <returns>Task{System.Boolean}.</returns>
  144. private async Task<bool> RefreshAdditionalParts(CancellationToken cancellationToken, bool forceSave = false, bool forceRefresh = false, bool allowSlowProviders = true)
  145. {
  146. if (!IsMultiPart || LocationType != LocationType.FileSystem)
  147. {
  148. return false;
  149. }
  150. var newItems = LoadAdditionalParts().ToList();
  151. var newItemIds = newItems.Select(i => i.Id).ToList();
  152. var itemsChanged = !AdditionalPartIds.SequenceEqual(newItemIds);
  153. var tasks = newItems.Select(i => i.RefreshMetadata(cancellationToken, forceSave, forceRefresh, allowSlowProviders));
  154. var results = await Task.WhenAll(tasks).ConfigureAwait(false);
  155. AdditionalPartIds = newItemIds;
  156. return itemsChanged || results.Contains(true);
  157. }
  158. /// <summary>
  159. /// Loads the additional parts.
  160. /// </summary>
  161. /// <returns>IEnumerable{Video}.</returns>
  162. private IEnumerable<Video> LoadAdditionalParts()
  163. {
  164. IEnumerable<FileSystemInfo> files;
  165. if (VideoType == VideoType.BluRay || VideoType == VideoType.Dvd)
  166. {
  167. files = new DirectoryInfo(System.IO.Path.GetDirectoryName(Path))
  168. .EnumerateDirectories()
  169. .Where(i => !string.Equals(i.FullName, Path, StringComparison.OrdinalIgnoreCase) && EntityResolutionHelper.IsMultiPartFile(i.Name));
  170. }
  171. else
  172. {
  173. files = ResolveArgs.FileSystemChildren.Where(i =>
  174. {
  175. if ((i.Attributes & FileAttributes.Directory) == FileAttributes.Directory)
  176. {
  177. return false;
  178. }
  179. return !string.Equals(i.FullName, Path, StringComparison.OrdinalIgnoreCase) && EntityResolutionHelper.IsVideoFile(i.FullName) && EntityResolutionHelper.IsMultiPartFile(i.Name);
  180. });
  181. }
  182. return LibraryManager.ResolvePaths<Video>(files, null).Select(video =>
  183. {
  184. // Try to retrieve it from the db. If we don't find it, use the resolved version
  185. var dbItem = LibraryManager.RetrieveItem(video.Id) as Video;
  186. if (dbItem != null)
  187. {
  188. dbItem.ResolveArgs = video.ResolveArgs;
  189. video = dbItem;
  190. }
  191. return video;
  192. }).ToList();
  193. }
  194. }
  195. }