Video.cs 11 KB

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