AudioInfoProvider.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 : BaseMetadataProvider
  15. {
  16. public override bool Supports(BaseEntity item)
  17. {
  18. return item is Audio;
  19. }
  20. public override MetadataProviderPriority Priority
  21. {
  22. get { return MetadataProviderPriority.First; }
  23. }
  24. public override async Task FetchAsync(BaseEntity item, ItemResolveEventArgs args)
  25. {
  26. await Task.Run(() =>
  27. {
  28. Audio audio = item as Audio;
  29. Fetch(audio, FFProbe.Run(audio));
  30. });
  31. }
  32. private void Fetch(Audio audio, FFProbeResult data)
  33. {
  34. if (data == null)
  35. {
  36. Logger.LogInfo("Null FFProbeResult for {0} {1}", audio.Id, audio.Name);
  37. return;
  38. }
  39. MediaStream stream = data.streams.First(s => s.codec_type.Equals("audio", StringComparison.OrdinalIgnoreCase));
  40. string bitrate = null;
  41. string duration = null;
  42. audio.Channels = stream.channels;
  43. if (!string.IsNullOrEmpty(stream.sample_rate))
  44. {
  45. audio.SampleRate = int.Parse(stream.sample_rate);
  46. }
  47. bitrate = stream.bit_rate;
  48. duration = stream.duration;
  49. if (string.IsNullOrEmpty(bitrate))
  50. {
  51. bitrate = data.format.bit_rate;
  52. }
  53. if (string.IsNullOrEmpty(duration))
  54. {
  55. duration = data.format.duration;
  56. }
  57. if (!string.IsNullOrEmpty(bitrate))
  58. {
  59. audio.BitRate = int.Parse(bitrate);
  60. }
  61. if (!string.IsNullOrEmpty(duration))
  62. {
  63. audio.RunTimeTicks = TimeSpan.FromSeconds(double.Parse(duration)).Ticks;
  64. }
  65. if (data.format.tags != null)
  66. {
  67. FetchDataFromTags(audio, data.format.tags);
  68. }
  69. }
  70. private void FetchDataFromTags(Audio audio, Dictionary<string, string> tags)
  71. {
  72. string title = GetDictionaryValue(tags, "title");
  73. if (!string.IsNullOrEmpty(title))
  74. {
  75. audio.Name = title;
  76. }
  77. string composer = GetDictionaryValue(tags, "composer");
  78. if (!string.IsNullOrEmpty(composer))
  79. {
  80. var list = audio.People ?? new List<PersonInfo>();
  81. list.Add(new PersonInfo() { Name = composer, Type = "Composer" });
  82. audio.People = list;
  83. }
  84. audio.Album = GetDictionaryValue(tags, "album");
  85. audio.Artist = GetDictionaryValue(tags, "artist");
  86. audio.AlbumArtist = GetDictionaryValue(tags, "albumartist") ?? GetDictionaryValue(tags, "album artist") ?? GetDictionaryValue(tags, "album_artist");
  87. audio.IndexNumber = GetDictionaryNumericValue(tags, "track");
  88. audio.ParentIndexNumber = GetDictionaryDiscValue(tags);
  89. audio.Language = GetDictionaryValue(tags, "language");
  90. audio.ProductionYear = GetDictionaryNumericValue(tags, "date");
  91. audio.PremiereDate = GetDictionaryDateTime(tags, "retaildate") ?? GetDictionaryDateTime(tags, "retail date") ?? GetDictionaryDateTime(tags, "retail_date");
  92. FetchGenres(audio, tags);
  93. FetchStudios(audio, tags, "organization");
  94. FetchStudios(audio, tags, "ensemble");
  95. FetchStudios(audio, tags, "publisher");
  96. }
  97. private void FetchStudios(Audio audio, Dictionary<string, string> tags, string tagName)
  98. {
  99. string val = GetDictionaryValue(tags, tagName);
  100. if (!string.IsNullOrEmpty(val))
  101. {
  102. var list = audio.Studios ?? new List<string>();
  103. list.AddRange(val.Split('/'));
  104. audio.Studios = list;
  105. }
  106. }
  107. private void FetchGenres(Audio audio, Dictionary<string, string> tags)
  108. {
  109. string val = GetDictionaryValue(tags, "genre");
  110. if (!string.IsNullOrEmpty(val))
  111. {
  112. var list = audio.Genres ?? new List<string>();
  113. list.AddRange(val.Split('/'));
  114. audio.Genres = list;
  115. }
  116. }
  117. private int? GetDictionaryDiscValue(Dictionary<string, string> tags)
  118. {
  119. string disc = GetDictionaryValue(tags, "disc");
  120. if (!string.IsNullOrEmpty(disc))
  121. {
  122. disc = disc.Split('/')[0];
  123. int num;
  124. if (int.TryParse(disc, out num))
  125. {
  126. return num;
  127. }
  128. }
  129. return null;
  130. }
  131. internal static string GetDictionaryValue(Dictionary<string, string> tags, string key)
  132. {
  133. if (tags == null)
  134. {
  135. return null;
  136. }
  137. string[] keys = tags.Keys.ToArray();
  138. for (int i = 0; i < keys.Length; i++)
  139. {
  140. string currentKey = keys[i];
  141. if (key.Equals(currentKey, StringComparison.OrdinalIgnoreCase))
  142. {
  143. return tags[currentKey];
  144. }
  145. }
  146. return null;
  147. }
  148. private int? GetDictionaryNumericValue(Dictionary<string, string> tags, string key)
  149. {
  150. string val = GetDictionaryValue(tags, key);
  151. if (!string.IsNullOrEmpty(val))
  152. {
  153. int i;
  154. if (int.TryParse(val, out i))
  155. {
  156. return i;
  157. }
  158. }
  159. return null;
  160. }
  161. private DateTime? GetDictionaryDateTime(Dictionary<string, string> tags, string key)
  162. {
  163. string val = GetDictionaryValue(tags, key);
  164. if (!string.IsNullOrEmpty(val))
  165. {
  166. DateTime i;
  167. if (DateTime.TryParse(val, out i))
  168. {
  169. return i;
  170. }
  171. }
  172. return null;
  173. }
  174. private string GetOutputCachePath(BaseItem item)
  175. {
  176. string outputDirectory = Path.Combine(Kernel.Instance.ApplicationPaths.FFProbeAudioCacheDirectory, item.Id.ToString().Substring(0, 1));
  177. return Path.Combine(outputDirectory, item.Id + "-" + item.DateModified.Ticks + ".js");
  178. }
  179. public override void Init()
  180. {
  181. base.Init();
  182. EnsureCacheSubFolders(Kernel.Instance.ApplicationPaths.FFProbeAudioCacheDirectory);
  183. }
  184. internal static void EnsureCacheSubFolders(string root)
  185. {
  186. // 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
  187. for (int i = 0; i <= 9; i++)
  188. {
  189. EnsureDirectory(Path.Combine(root, i.ToString()));
  190. }
  191. EnsureDirectory(Path.Combine(root, "a"));
  192. EnsureDirectory(Path.Combine(root, "b"));
  193. EnsureDirectory(Path.Combine(root, "c"));
  194. EnsureDirectory(Path.Combine(root, "d"));
  195. EnsureDirectory(Path.Combine(root, "e"));
  196. EnsureDirectory(Path.Combine(root, "f"));
  197. }
  198. private static void EnsureDirectory(string path)
  199. {
  200. if (!Directory.Exists(path))
  201. {
  202. Directory.CreateDirectory(path);
  203. }
  204. }
  205. }
  206. }