AudioInfoProvider.cs 8.0 KB

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