AudioInfoProvider.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel.Composition;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Threading.Tasks;
  7. using MediaBrowser.Common.Logging;
  8. using MediaBrowser.Controller.FFMpeg;
  9. using MediaBrowser.Controller.Library;
  10. using MediaBrowser.Model.Entities;
  11. namespace MediaBrowser.Controller.Providers
  12. {
  13. [Export(typeof(BaseMetadataProvider))]
  14. public class AudioInfoProvider : BaseMediaInfoProvider<Audio>
  15. {
  16. public override MetadataProviderPriority Priority
  17. {
  18. get { return MetadataProviderPriority.First; }
  19. }
  20. protected override string CacheDirectory
  21. {
  22. get { return Kernel.Instance.ApplicationPaths.FFProbeAudioCacheDirectory; }
  23. }
  24. protected override void Fetch(Audio audio, FFProbeResult data)
  25. {
  26. MediaStream stream = data.streams.First(s => s.codec_type.Equals("audio", StringComparison.OrdinalIgnoreCase));
  27. string bitrate = null;
  28. string duration = null;
  29. audio.Channels = stream.channels;
  30. if (!string.IsNullOrEmpty(stream.sample_rate))
  31. {
  32. audio.SampleRate = int.Parse(stream.sample_rate);
  33. }
  34. bitrate = stream.bit_rate;
  35. duration = stream.duration;
  36. if (string.IsNullOrEmpty(bitrate))
  37. {
  38. bitrate = data.format.bit_rate;
  39. }
  40. if (string.IsNullOrEmpty(duration))
  41. {
  42. duration = data.format.duration;
  43. }
  44. if (!string.IsNullOrEmpty(bitrate))
  45. {
  46. audio.BitRate = int.Parse(bitrate);
  47. }
  48. if (!string.IsNullOrEmpty(duration))
  49. {
  50. audio.RunTimeTicks = TimeSpan.FromSeconds(double.Parse(duration)).Ticks;
  51. }
  52. if (data.format.tags != null)
  53. {
  54. FetchDataFromTags(audio, data.format.tags);
  55. }
  56. }
  57. private void FetchDataFromTags(Audio audio, Dictionary<string, string> tags)
  58. {
  59. string title = GetDictionaryValue(tags, "title");
  60. if (!string.IsNullOrEmpty(title))
  61. {
  62. audio.Name = title;
  63. }
  64. string composer = GetDictionaryValue(tags, "composer");
  65. if (!string.IsNullOrEmpty(composer))
  66. {
  67. audio.AddPerson(new PersonInfo() { Name = composer, Type = "Composer" });
  68. }
  69. audio.Album = GetDictionaryValue(tags, "album");
  70. audio.Artist = GetDictionaryValue(tags, "artist");
  71. audio.AlbumArtist = GetDictionaryValue(tags, "albumartist") ?? GetDictionaryValue(tags, "album artist") ?? GetDictionaryValue(tags, "album_artist");
  72. audio.IndexNumber = GetDictionaryNumericValue(tags, "track");
  73. audio.ParentIndexNumber = GetDictionaryDiscValue(tags);
  74. audio.Language = GetDictionaryValue(tags, "language");
  75. audio.ProductionYear = GetDictionaryNumericValue(tags, "date");
  76. audio.PremiereDate = GetDictionaryDateTime(tags, "retaildate") ?? GetDictionaryDateTime(tags, "retail date") ?? GetDictionaryDateTime(tags, "retail_date");
  77. FetchGenres(audio, tags);
  78. FetchStudios(audio, tags, "organization");
  79. FetchStudios(audio, tags, "ensemble");
  80. FetchStudios(audio, tags, "publisher");
  81. }
  82. private void FetchStudios(Audio audio, Dictionary<string, string> tags, string tagName)
  83. {
  84. string val = GetDictionaryValue(tags, tagName);
  85. if (!string.IsNullOrEmpty(val))
  86. {
  87. var list = audio.Studios ?? new List<string>();
  88. list.AddRange(val.Split('/'));
  89. audio.Studios = list;
  90. }
  91. }
  92. private void FetchGenres(Audio audio, Dictionary<string, string> tags)
  93. {
  94. string val = GetDictionaryValue(tags, "genre");
  95. if (!string.IsNullOrEmpty(val))
  96. {
  97. var list = audio.Genres ?? new List<string>();
  98. list.AddRange(val.Split('/'));
  99. audio.Genres = list;
  100. }
  101. }
  102. private int? GetDictionaryDiscValue(Dictionary<string, string> tags)
  103. {
  104. string disc = GetDictionaryValue(tags, "disc");
  105. if (!string.IsNullOrEmpty(disc))
  106. {
  107. disc = disc.Split('/')[0];
  108. int num;
  109. if (int.TryParse(disc, out num))
  110. {
  111. return num;
  112. }
  113. }
  114. return null;
  115. }
  116. }
  117. public abstract class BaseMediaInfoProvider<T> : BaseMetadataProvider
  118. where T : BaseItem
  119. {
  120. protected abstract string CacheDirectory { get; }
  121. public override bool Supports(BaseEntity item)
  122. {
  123. return item is T;
  124. }
  125. public override async Task FetchAsync(BaseEntity item, ItemResolveEventArgs args)
  126. {
  127. await Task.Run(() =>
  128. {
  129. T myItem = item as T;
  130. if (CanSkipFFProbe(myItem))
  131. {
  132. return;
  133. }
  134. FFProbeResult result = FFProbe.Run(myItem, CacheDirectory);
  135. if (result == null)
  136. {
  137. Logger.LogInfo("Null FFProbeResult for {0} {1}", item.Id, item.Name);
  138. return;
  139. }
  140. if (result.format != null && result.format.tags != null)
  141. {
  142. result.format.tags = ConvertDictionaryToCaseInSensitive(result.format.tags);
  143. }
  144. if (result.streams != null)
  145. {
  146. foreach (MediaStream stream in result.streams)
  147. {
  148. if (stream.tags != null)
  149. {
  150. stream.tags = ConvertDictionaryToCaseInSensitive(stream.tags);
  151. }
  152. }
  153. }
  154. Fetch(myItem, result);
  155. });
  156. }
  157. protected abstract void Fetch(T item, FFProbeResult result);
  158. public override void Init()
  159. {
  160. base.Init();
  161. EnsureCacheSubFolders(CacheDirectory);
  162. }
  163. private void EnsureCacheSubFolders(string root)
  164. {
  165. // Do this now so that we don't have to do this on every operation, which would require us to create a lock in order to maintain thread-safety
  166. for (int i = 0; i <= 9; i++)
  167. {
  168. EnsureDirectory(Path.Combine(root, i.ToString()));
  169. }
  170. EnsureDirectory(Path.Combine(root, "a"));
  171. EnsureDirectory(Path.Combine(root, "b"));
  172. EnsureDirectory(Path.Combine(root, "c"));
  173. EnsureDirectory(Path.Combine(root, "d"));
  174. EnsureDirectory(Path.Combine(root, "e"));
  175. EnsureDirectory(Path.Combine(root, "f"));
  176. }
  177. private void EnsureDirectory(string path)
  178. {
  179. if (!Directory.Exists(path))
  180. {
  181. Directory.CreateDirectory(path);
  182. }
  183. }
  184. protected virtual bool CanSkipFFProbe(T item)
  185. {
  186. return false;
  187. }
  188. protected string GetDictionaryValue(Dictionary<string, string> tags, string key)
  189. {
  190. if (tags == null)
  191. {
  192. return null;
  193. }
  194. if (!tags.ContainsKey(key))
  195. {
  196. return null;
  197. }
  198. return tags[key];
  199. }
  200. protected int? GetDictionaryNumericValue(Dictionary<string, string> tags, string key)
  201. {
  202. string val = GetDictionaryValue(tags, key);
  203. if (!string.IsNullOrEmpty(val))
  204. {
  205. int i;
  206. if (int.TryParse(val, out i))
  207. {
  208. return i;
  209. }
  210. }
  211. return null;
  212. }
  213. protected DateTime? GetDictionaryDateTime(Dictionary<string, string> tags, string key)
  214. {
  215. string val = GetDictionaryValue(tags, key);
  216. if (!string.IsNullOrEmpty(val))
  217. {
  218. DateTime i;
  219. if (DateTime.TryParse(val, out i))
  220. {
  221. return i;
  222. }
  223. }
  224. return null;
  225. }
  226. private Dictionary<string, string> ConvertDictionaryToCaseInSensitive(Dictionary<string, string> dict)
  227. {
  228. Dictionary<string, string> newDict = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
  229. foreach (string key in dict.Keys)
  230. {
  231. newDict[key] = dict[key];
  232. }
  233. return newDict;
  234. }
  235. }
  236. }