AudioInfoProvider.cs 7.9 KB

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