Video.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. [IgnoreDataMember]
  75. public override string ContainingFolderPath
  76. {
  77. get
  78. {
  79. if (IsMultiPart)
  80. {
  81. return System.IO.Path.GetDirectoryName(Path);
  82. }
  83. if (VideoType == VideoType.BluRay || VideoType == VideoType.Dvd ||
  84. VideoType == VideoType.HdDvd)
  85. {
  86. return Path;
  87. }
  88. return base.ContainingFolderPath;
  89. }
  90. }
  91. public string MainFeaturePlaylistName { get; set; }
  92. /// <summary>
  93. /// Gets the playable stream files.
  94. /// </summary>
  95. /// <param name="rootPath">The root path.</param>
  96. /// <returns>List{System.String}.</returns>
  97. public List<string> GetPlayableStreamFiles(string rootPath)
  98. {
  99. if (PlayableStreamFileNames == null)
  100. {
  101. return null;
  102. }
  103. var allFiles = Directory.EnumerateFiles(rootPath, "*", SearchOption.AllDirectories).ToList();
  104. return PlayableStreamFileNames.Select(name => allFiles.FirstOrDefault(f => string.Equals(System.IO.Path.GetFileName(f), name, StringComparison.OrdinalIgnoreCase)))
  105. .Where(f => !string.IsNullOrEmpty(f))
  106. .ToList();
  107. }
  108. /// <summary>
  109. /// Gets a value indicating whether [is3 D].
  110. /// </summary>
  111. /// <value><c>true</c> if [is3 D]; otherwise, <c>false</c>.</value>
  112. [IgnoreDataMember]
  113. public bool Is3D
  114. {
  115. get { return Video3DFormat.HasValue; }
  116. }
  117. public bool IsHD { get; set; }
  118. /// <summary>
  119. /// Gets the type of the media.
  120. /// </summary>
  121. /// <value>The type of the media.</value>
  122. public override string MediaType
  123. {
  124. get
  125. {
  126. return Model.Entities.MediaType.Video;
  127. }
  128. }
  129. protected override async Task BeforeRefreshMetadata(MetadataRefreshOptions options, List<FileSystemInfo> fileSystemChildren, CancellationToken cancellationToken)
  130. {
  131. await base.BeforeRefreshMetadata(options, fileSystemChildren, cancellationToken).ConfigureAwait(false);
  132. // Must have a parent to have additional parts
  133. // In other words, it must be part of the Parent/Child tree
  134. // The additional parts won't have additional parts themselves
  135. if (IsMultiPart && LocationType == LocationType.FileSystem && Parent != null)
  136. {
  137. var additionalPartsChanged = await RefreshAdditionalParts(options, fileSystemChildren, cancellationToken).ConfigureAwait(false);
  138. if (additionalPartsChanged)
  139. {
  140. options.ForceSave = true;
  141. }
  142. }
  143. }
  144. /// <summary>
  145. /// Refreshes the additional parts.
  146. /// </summary>
  147. /// <param name="options">The options.</param>
  148. /// <param name="fileSystemChildren">The file system children.</param>
  149. /// <param name="cancellationToken">The cancellation token.</param>
  150. /// <returns>Task{System.Boolean}.</returns>
  151. private async Task<bool> RefreshAdditionalParts(MetadataRefreshOptions options, List<FileSystemInfo> fileSystemChildren, CancellationToken cancellationToken)
  152. {
  153. var newItems = LoadAdditionalParts(fileSystemChildren).ToList();
  154. var newItemIds = newItems.Select(i => i.Id).ToList();
  155. var itemsChanged = !AdditionalPartIds.SequenceEqual(newItemIds);
  156. var tasks = newItems.Select(i => i.RefreshMetadata(options, cancellationToken));
  157. await Task.WhenAll(tasks).ConfigureAwait(false);
  158. AdditionalPartIds = newItemIds;
  159. return itemsChanged;
  160. }
  161. /// <summary>
  162. /// Loads the additional parts.
  163. /// </summary>
  164. /// <returns>IEnumerable{Video}.</returns>
  165. private IEnumerable<Video> LoadAdditionalParts(IEnumerable<FileSystemInfo> fileSystemChildren)
  166. {
  167. IEnumerable<FileSystemInfo> files;
  168. var path = Path;
  169. if (VideoType == VideoType.BluRay || VideoType == VideoType.Dvd)
  170. {
  171. files = fileSystemChildren.Where(i =>
  172. {
  173. if ((i.Attributes & FileAttributes.Directory) == FileAttributes.Directory)
  174. {
  175. return !string.Equals(i.FullName, path, StringComparison.OrdinalIgnoreCase) && EntityResolutionHelper.IsVideoFile(i.FullName) && EntityResolutionHelper.IsMultiPartFile(i.Name);
  176. }
  177. return false;
  178. });
  179. }
  180. else
  181. {
  182. files = fileSystemChildren.Where(i =>
  183. {
  184. if ((i.Attributes & FileAttributes.Directory) == FileAttributes.Directory)
  185. {
  186. return false;
  187. }
  188. return !string.Equals(i.FullName, path, StringComparison.OrdinalIgnoreCase) && EntityResolutionHelper.IsVideoFile(i.FullName) && EntityResolutionHelper.IsMultiPartFile(i.Name);
  189. });
  190. }
  191. return LibraryManager.ResolvePaths<Video>(files, null).Select(video =>
  192. {
  193. // Try to retrieve it from the db. If we don't find it, use the resolved version
  194. var dbItem = LibraryManager.GetItemById(video.Id) as Video;
  195. if (dbItem != null)
  196. {
  197. video = dbItem;
  198. }
  199. return video;
  200. // Sort them so that the list can be easily compared for changes
  201. }).OrderBy(i => i.Path).ToList();
  202. }
  203. public override IEnumerable<string> GetDeletePaths()
  204. {
  205. if (!IsInMixedFolder)
  206. {
  207. if (VideoType == VideoType.VideoFile || VideoType == VideoType.Iso)
  208. {
  209. return new[] { System.IO.Path.GetDirectoryName(Path) };
  210. }
  211. }
  212. return base.GetDeletePaths();
  213. }
  214. public virtual IEnumerable<MediaStream> GetMediaStreams()
  215. {
  216. return ItemRepository.GetMediaStreams(new MediaStreamQuery
  217. {
  218. ItemId = Id
  219. });
  220. }
  221. public virtual MediaStream GetDefaultVideoStream()
  222. {
  223. if (!DefaultVideoStreamIndex.HasValue)
  224. {
  225. return null;
  226. }
  227. return ItemRepository.GetMediaStreams(new MediaStreamQuery
  228. {
  229. ItemId = Id,
  230. Index = DefaultVideoStreamIndex.Value
  231. }).FirstOrDefault();
  232. }
  233. }
  234. }