AudioInfoProvider.cs 8.2 KB

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