Video.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. using MediaBrowser.Controller.Persistence;
  2. using MediaBrowser.Controller.Resolvers;
  3. using MediaBrowser.Model.Entities;
  4. using System;
  5. using System.Collections;
  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, IHasAspectRatio
  18. {
  19. public bool IsMultiPart { get; set; }
  20. public List<Guid> AdditionalPartIds { get; set; }
  21. public Video()
  22. {
  23. PlayableStreamFileNames = new List<string>();
  24. AdditionalPartIds = new List<Guid>();
  25. }
  26. /// <summary>
  27. /// Gets or sets a value indicating whether this instance has subtitles.
  28. /// </summary>
  29. /// <value><c>true</c> if this instance has subtitles; otherwise, <c>false</c>.</value>
  30. public bool HasSubtitles { get; set; }
  31. /// <summary>
  32. /// Gets or sets the video bit rate.
  33. /// </summary>
  34. /// <value>The video bit rate.</value>
  35. public int? VideoBitRate { get; set; }
  36. /// <summary>
  37. /// Gets or sets the default index of the video stream.
  38. /// </summary>
  39. /// <value>The default index of the video stream.</value>
  40. public int? DefaultVideoStreamIndex { get; set; }
  41. /// <summary>
  42. /// Gets or sets the type of the video.
  43. /// </summary>
  44. /// <value>The type of the video.</value>
  45. public VideoType VideoType { get; set; }
  46. /// <summary>
  47. /// Gets or sets the type of the iso.
  48. /// </summary>
  49. /// <value>The type of the iso.</value>
  50. public IsoType? IsoType { get; set; }
  51. /// <summary>
  52. /// Gets or sets the video3 D format.
  53. /// </summary>
  54. /// <value>The video3 D format.</value>
  55. public Video3DFormat? Video3DFormat { get; set; }
  56. /// <summary>
  57. /// If the video is a folder-rip, this will hold the file list for the largest playlist
  58. /// </summary>
  59. public List<string> PlayableStreamFileNames { get; set; }
  60. /// <summary>
  61. /// Gets the playable stream files.
  62. /// </summary>
  63. /// <returns>List{System.String}.</returns>
  64. public List<string> GetPlayableStreamFiles()
  65. {
  66. return GetPlayableStreamFiles(Path);
  67. }
  68. /// <summary>
  69. /// Gets or sets the aspect ratio.
  70. /// </summary>
  71. /// <value>The aspect ratio.</value>
  72. public string AspectRatio { get; set; }
  73. /// <summary>
  74. /// Should be overridden to return the proper folder where metadata lives
  75. /// </summary>
  76. /// <value>The meta location.</value>
  77. [IgnoreDataMember]
  78. public override string MetaLocation
  79. {
  80. get
  81. {
  82. return VideoType == VideoType.VideoFile || VideoType == VideoType.Iso || IsMultiPart ? System.IO.Path.GetDirectoryName(Path) : Path;
  83. }
  84. }
  85. /// <summary>
  86. /// Needed because the resolver stops at the movie folder and we find the video inside.
  87. /// </summary>
  88. /// <value><c>true</c> if [use parent path to create resolve args]; otherwise, <c>false</c>.</value>
  89. protected override bool UseParentPathToCreateResolveArgs
  90. {
  91. get
  92. {
  93. if (IsInMixedFolder)
  94. {
  95. return false;
  96. }
  97. return VideoType == VideoType.VideoFile || VideoType == VideoType.Iso || IsMultiPart;
  98. }
  99. }
  100. public string MainFeaturePlaylistName { get; set; }
  101. /// <summary>
  102. /// Gets the playable stream files.
  103. /// </summary>
  104. /// <param name="rootPath">The root path.</param>
  105. /// <returns>List{System.String}.</returns>
  106. public List<string> GetPlayableStreamFiles(string rootPath)
  107. {
  108. if (PlayableStreamFileNames == null)
  109. {
  110. return null;
  111. }
  112. var allFiles = Directory.EnumerateFiles(rootPath, "*", SearchOption.AllDirectories).ToList();
  113. return PlayableStreamFileNames.Select(name => allFiles.FirstOrDefault(f => string.Equals(System.IO.Path.GetFileName(f), name, StringComparison.OrdinalIgnoreCase)))
  114. .Where(f => !string.IsNullOrEmpty(f))
  115. .ToList();
  116. }
  117. /// <summary>
  118. /// Gets a value indicating whether [is3 D].
  119. /// </summary>
  120. /// <value><c>true</c> if [is3 D]; otherwise, <c>false</c>.</value>
  121. [IgnoreDataMember]
  122. public bool Is3D
  123. {
  124. get { return Video3DFormat.HasValue; }
  125. }
  126. public bool IsHD { get; set; }
  127. /// <summary>
  128. /// Gets the type of the media.
  129. /// </summary>
  130. /// <value>The type of the media.</value>
  131. public override string MediaType
  132. {
  133. get
  134. {
  135. return Model.Entities.MediaType.Video;
  136. }
  137. }
  138. /// <summary>
  139. /// Overrides the base implementation to refresh metadata for local trailers
  140. /// </summary>
  141. /// <param name="cancellationToken">The cancellation token.</param>
  142. /// <param name="forceSave">if set to <c>true</c> [is new item].</param>
  143. /// <param name="forceRefresh">if set to <c>true</c> [force].</param>
  144. /// <param name="allowSlowProviders">if set to <c>true</c> [allow slow providers].</param>
  145. /// <param name="resetResolveArgs">The reset resolve args.</param>
  146. /// <returns>true if a provider reports we changed</returns>
  147. public override async Task<bool> RefreshMetadata(CancellationToken cancellationToken, bool forceSave = false, bool forceRefresh = false, bool allowSlowProviders = true, bool resetResolveArgs = true)
  148. {
  149. // Kick off a task to refresh the main item
  150. var result = await base.RefreshMetadata(cancellationToken, forceSave, forceRefresh, allowSlowProviders, resetResolveArgs).ConfigureAwait(false);
  151. var additionalPartsChanged = false;
  152. // Must have a parent to have additional parts
  153. // In other words, it must be part of the Parent/Child tree
  154. // The additional parts won't have additional parts themselves
  155. if (IsMultiPart && LocationType == LocationType.FileSystem && Parent != null)
  156. {
  157. try
  158. {
  159. additionalPartsChanged = await RefreshAdditionalParts(cancellationToken, forceSave, forceRefresh, allowSlowProviders).ConfigureAwait(false);
  160. }
  161. catch (IOException ex)
  162. {
  163. Logger.ErrorException("Error loading additional parts for {0}.", ex, Name);
  164. }
  165. }
  166. return additionalPartsChanged || result;
  167. }
  168. /// <summary>
  169. /// Refreshes the additional parts.
  170. /// </summary>
  171. /// <param name="cancellationToken">The cancellation token.</param>
  172. /// <param name="forceSave">if set to <c>true</c> [force save].</param>
  173. /// <param name="forceRefresh">if set to <c>true</c> [force refresh].</param>
  174. /// <param name="allowSlowProviders">if set to <c>true</c> [allow slow providers].</param>
  175. /// <returns>Task{System.Boolean}.</returns>
  176. private async Task<bool> RefreshAdditionalParts(CancellationToken cancellationToken, bool forceSave = false, bool forceRefresh = false, bool allowSlowProviders = true)
  177. {
  178. var newItems = LoadAdditionalParts().ToList();
  179. var newItemIds = newItems.Select(i => i.Id).ToList();
  180. var itemsChanged = !AdditionalPartIds.SequenceEqual(newItemIds);
  181. var tasks = newItems.Select(i => i.RefreshMetadata(cancellationToken, forceSave, forceRefresh, allowSlowProviders));
  182. var results = await Task.WhenAll(tasks).ConfigureAwait(false);
  183. AdditionalPartIds = newItemIds;
  184. return itemsChanged || results.Contains(true);
  185. }
  186. /// <summary>
  187. /// Loads the additional parts.
  188. /// </summary>
  189. /// <returns>IEnumerable{Video}.</returns>
  190. private IEnumerable<Video> LoadAdditionalParts()
  191. {
  192. IEnumerable<FileSystemInfo> files;
  193. if (VideoType == VideoType.BluRay || VideoType == VideoType.Dvd)
  194. {
  195. files = new DirectoryInfo(System.IO.Path.GetDirectoryName(Path))
  196. .EnumerateDirectories()
  197. .Where(i => !string.Equals(i.FullName, Path, StringComparison.OrdinalIgnoreCase) && EntityResolutionHelper.IsMultiPartFile(i.Name));
  198. }
  199. else
  200. {
  201. files = ResolveArgs.FileSystemChildren.Where(i =>
  202. {
  203. if ((i.Attributes & FileAttributes.Directory) == FileAttributes.Directory)
  204. {
  205. return false;
  206. }
  207. return !string.Equals(i.FullName, Path, StringComparison.OrdinalIgnoreCase) && EntityResolutionHelper.IsVideoFile(i.FullName) && EntityResolutionHelper.IsMultiPartFile(i.Name);
  208. });
  209. }
  210. return LibraryManager.ResolvePaths<Video>(files, null).Select(video =>
  211. {
  212. // Try to retrieve it from the db. If we don't find it, use the resolved version
  213. var dbItem = LibraryManager.RetrieveItem(video.Id) as Video;
  214. if (dbItem != null)
  215. {
  216. dbItem.ResetResolveArgs(video.ResolveArgs);
  217. video = dbItem;
  218. }
  219. return video;
  220. }).ToList();
  221. }
  222. public override IEnumerable<string> GetDeletePaths()
  223. {
  224. if (!IsInMixedFolder)
  225. {
  226. if (VideoType == VideoType.VideoFile || VideoType == VideoType.Iso)
  227. {
  228. return new[] { System.IO.Path.GetDirectoryName(Path) };
  229. }
  230. }
  231. return base.GetDeletePaths();
  232. }
  233. public virtual IEnumerable<MediaStream> GetMediaStreams()
  234. {
  235. return ItemRepository.GetMediaStreams(new MediaStreamQuery
  236. {
  237. ItemId = Id
  238. });
  239. }
  240. public virtual MediaStream GetDefaultVideoStream()
  241. {
  242. if (!DefaultVideoStreamIndex.HasValue)
  243. {
  244. return null;
  245. }
  246. return ItemRepository.GetMediaStreams(new MediaStreamQuery
  247. {
  248. ItemId = Id,
  249. Index = DefaultVideoStreamIndex.Value
  250. }).FirstOrDefault();
  251. }
  252. }
  253. }