BaseFFProbeProvider.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. using MediaBrowser.Controller.Configuration;
  2. using MediaBrowser.Controller.Entities;
  3. using MediaBrowser.Controller.MediaInfo;
  4. using MediaBrowser.Controller.Providers;
  5. using MediaBrowser.Model.Entities;
  6. using MediaBrowser.Model.IO;
  7. using MediaBrowser.Model.Logging;
  8. using MediaBrowser.Model.Serialization;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Globalization;
  12. using System.Linq;
  13. using System.Threading;
  14. using System.Threading.Tasks;
  15. namespace MediaBrowser.Providers.MediaInfo
  16. {
  17. /// <summary>
  18. /// Provides a base class for extracting media information through ffprobe
  19. /// </summary>
  20. /// <typeparam name="T"></typeparam>
  21. public abstract class BaseFFProbeProvider<T> : BaseMetadataProvider
  22. where T : BaseItem, IHasMediaStreams
  23. {
  24. protected BaseFFProbeProvider(ILogManager logManager, IServerConfigurationManager configurationManager, IMediaEncoder mediaEncoder, IJsonSerializer jsonSerializer)
  25. : base(logManager, configurationManager)
  26. {
  27. JsonSerializer = jsonSerializer;
  28. MediaEncoder = mediaEncoder;
  29. }
  30. protected readonly IMediaEncoder MediaEncoder;
  31. protected readonly IJsonSerializer JsonSerializer;
  32. /// <summary>
  33. /// Gets the priority.
  34. /// </summary>
  35. /// <value>The priority.</value>
  36. public override MetadataProviderPriority Priority
  37. {
  38. get { return MetadataProviderPriority.First; }
  39. }
  40. protected readonly CultureInfo UsCulture = new CultureInfo("en-US");
  41. /// <summary>
  42. /// Supportses the specified item.
  43. /// </summary>
  44. /// <param name="item">The item.</param>
  45. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
  46. public override bool Supports(BaseItem item)
  47. {
  48. return item.LocationType == LocationType.FileSystem && item is T;
  49. }
  50. /// <summary>
  51. /// Override this to return the date that should be compared to the last refresh date
  52. /// to determine if this provider should be re-fetched.
  53. /// </summary>
  54. /// <param name="item">The item.</param>
  55. /// <returns>DateTime.</returns>
  56. protected override DateTime CompareDate(BaseItem item)
  57. {
  58. return item.DateModified;
  59. }
  60. /// <summary>
  61. /// The null mount task result
  62. /// </summary>
  63. protected readonly Task<IIsoMount> NullMountTaskResult = Task.FromResult<IIsoMount>(null);
  64. /// <summary>
  65. /// Gets the provider version.
  66. /// </summary>
  67. /// <value>The provider version.</value>
  68. protected override string ProviderVersion
  69. {
  70. get
  71. {
  72. return "ffmpeg20131209";
  73. }
  74. }
  75. /// <summary>
  76. /// Gets a value indicating whether [refresh on version change].
  77. /// </summary>
  78. /// <value><c>true</c> if [refresh on version change]; otherwise, <c>false</c>.</value>
  79. protected override bool RefreshOnVersionChange
  80. {
  81. get
  82. {
  83. return true;
  84. }
  85. }
  86. /// <summary>
  87. /// Gets the media info.
  88. /// </summary>
  89. /// <param name="item">The item.</param>
  90. /// <param name="isoMount">The iso mount.</param>
  91. /// <param name="cancellationToken">The cancellation token.</param>
  92. /// <returns>Task{MediaInfoResult}.</returns>
  93. /// <exception cref="System.ArgumentNullException">inputPath
  94. /// or
  95. /// cache</exception>
  96. protected async Task<InternalMediaInfoResult> GetMediaInfo(BaseItem item, IIsoMount isoMount, CancellationToken cancellationToken)
  97. {
  98. cancellationToken.ThrowIfCancellationRequested();
  99. var type = InputType.File;
  100. var inputPath = isoMount == null ? new[] { item.Path } : new[] { isoMount.MountedPath };
  101. var video = item as Video;
  102. if (video != null)
  103. {
  104. inputPath = MediaEncoderHelpers.GetInputArgument(video.Path, video.LocationType == LocationType.Remote, video.VideoType, video.IsoType, isoMount, video.PlayableStreamFileNames, out type);
  105. }
  106. return await MediaEncoder.GetMediaInfo(inputPath, type, cancellationToken).ConfigureAwait(false);
  107. }
  108. /// <summary>
  109. /// Mounts the iso if needed.
  110. /// </summary>
  111. /// <param name="item">The item.</param>
  112. /// <param name="cancellationToken">The cancellation token.</param>
  113. /// <returns>IsoMount.</returns>
  114. protected virtual Task<IIsoMount> MountIsoIfNeeded(T item, CancellationToken cancellationToken)
  115. {
  116. return NullMountTaskResult;
  117. }
  118. /// <summary>
  119. /// Called when [pre fetch].
  120. /// </summary>
  121. /// <param name="item">The item.</param>
  122. /// <param name="mount">The mount.</param>
  123. protected virtual void OnPreFetch(T item, IIsoMount mount)
  124. {
  125. }
  126. /// <summary>
  127. /// Normalizes the FF probe result.
  128. /// </summary>
  129. /// <param name="result">The result.</param>
  130. protected void NormalizeFFProbeResult(InternalMediaInfoResult result)
  131. {
  132. if (result.format != null && result.format.tags != null)
  133. {
  134. result.format.tags = ConvertDictionaryToCaseInSensitive(result.format.tags);
  135. }
  136. if (result.streams != null)
  137. {
  138. // Convert all dictionaries to case insensitive
  139. foreach (var stream in result.streams)
  140. {
  141. if (stream.tags != null)
  142. {
  143. stream.tags = ConvertDictionaryToCaseInSensitive(stream.tags);
  144. }
  145. if (stream.disposition != null)
  146. {
  147. stream.disposition = ConvertDictionaryToCaseInSensitive(stream.disposition);
  148. }
  149. }
  150. }
  151. }
  152. /// <summary>
  153. /// Gets a string from an FFProbeResult tags dictionary
  154. /// </summary>
  155. /// <param name="tags">The tags.</param>
  156. /// <param name="key">The key.</param>
  157. /// <returns>System.String.</returns>
  158. protected string GetDictionaryValue(Dictionary<string, string> tags, string key)
  159. {
  160. if (tags == null)
  161. {
  162. return null;
  163. }
  164. string val;
  165. tags.TryGetValue(key, out val);
  166. return val;
  167. }
  168. /// <summary>
  169. /// Gets an int from an FFProbeResult tags dictionary
  170. /// </summary>
  171. /// <param name="tags">The tags.</param>
  172. /// <param name="key">The key.</param>
  173. /// <returns>System.Nullable{System.Int32}.</returns>
  174. protected int? GetDictionaryNumericValue(Dictionary<string, string> tags, string key)
  175. {
  176. var val = GetDictionaryValue(tags, key);
  177. if (!string.IsNullOrEmpty(val))
  178. {
  179. int i;
  180. if (int.TryParse(val, out i))
  181. {
  182. return i;
  183. }
  184. }
  185. return null;
  186. }
  187. /// <summary>
  188. /// Gets a DateTime from an FFProbeResult tags dictionary
  189. /// </summary>
  190. /// <param name="tags">The tags.</param>
  191. /// <param name="key">The key.</param>
  192. /// <returns>System.Nullable{DateTime}.</returns>
  193. protected DateTime? GetDictionaryDateTime(Dictionary<string, string> tags, string key)
  194. {
  195. var val = GetDictionaryValue(tags, key);
  196. if (!string.IsNullOrEmpty(val))
  197. {
  198. DateTime i;
  199. if (DateTime.TryParse(val, out i))
  200. {
  201. return i.ToUniversalTime();
  202. }
  203. }
  204. return null;
  205. }
  206. /// <summary>
  207. /// Converts a dictionary to case insensitive
  208. /// </summary>
  209. /// <param name="dict">The dict.</param>
  210. /// <returns>Dictionary{System.StringSystem.String}.</returns>
  211. private Dictionary<string, string> ConvertDictionaryToCaseInSensitive(Dictionary<string, string> dict)
  212. {
  213. return new Dictionary<string, string>(dict, StringComparer.OrdinalIgnoreCase);
  214. }
  215. }
  216. }