AudioInfoProvider.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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[] keys = tags.Keys.ToArray();
  116. for (int i = 0; i < keys.Length; i++)
  117. {
  118. string currentKey = keys[i];
  119. if ("disc".Equals(currentKey, StringComparison.OrdinalIgnoreCase))
  120. {
  121. string disc = tags[currentKey];
  122. if (!string.IsNullOrEmpty(disc))
  123. {
  124. disc = disc.Split('/')[0];
  125. int num;
  126. if (int.TryParse(disc, out num))
  127. {
  128. return num;
  129. }
  130. }
  131. break;
  132. }
  133. }
  134. return null;
  135. }
  136. private string GetDictionaryValue(Dictionary<string, string> tags, string key)
  137. {
  138. string[] keys = tags.Keys.ToArray();
  139. for (int i = 0; i < keys.Length; i++)
  140. {
  141. string currentKey = keys[i];
  142. if (key.Equals(currentKey, StringComparison.OrdinalIgnoreCase))
  143. {
  144. return tags[currentKey];
  145. }
  146. }
  147. return null;
  148. }
  149. private int? GetDictionaryNumericValue(Dictionary<string, string> tags, string key)
  150. {
  151. string val = GetDictionaryValue(tags, key);
  152. if (!string.IsNullOrEmpty(val))
  153. {
  154. int i;
  155. if (int.TryParse(val, out i))
  156. {
  157. return i;
  158. }
  159. }
  160. return null;
  161. }
  162. private DateTime? GetDictionaryDateTime(Dictionary<string, string> tags, string key)
  163. {
  164. string val = GetDictionaryValue(tags, key);
  165. if (!string.IsNullOrEmpty(val))
  166. {
  167. DateTime i;
  168. if (DateTime.TryParse(val, out i))
  169. {
  170. return i;
  171. }
  172. }
  173. return null;
  174. }
  175. private string GetOutputCachePath(BaseItem item)
  176. {
  177. string outputDirectory = Path.Combine(Kernel.Instance.ApplicationPaths.FFProbeAudioCacheDirectory, item.Id.ToString().Substring(0, 1));
  178. return Path.Combine(outputDirectory, item.Id + "-" + item.DateModified.Ticks + ".js");
  179. }
  180. public override void Init()
  181. {
  182. base.Init();
  183. EnsureCacheSubFolders(Kernel.Instance.ApplicationPaths.FFProbeAudioCacheDirectory);
  184. }
  185. internal static void EnsureCacheSubFolders(string root)
  186. {
  187. // 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
  188. for (int i = 0; i <= 9; i++)
  189. {
  190. EnsureDirectory(Path.Combine(root, i.ToString()));
  191. }
  192. EnsureDirectory(Path.Combine(root, "a"));
  193. EnsureDirectory(Path.Combine(root, "b"));
  194. EnsureDirectory(Path.Combine(root, "c"));
  195. EnsureDirectory(Path.Combine(root, "d"));
  196. EnsureDirectory(Path.Combine(root, "e"));
  197. EnsureDirectory(Path.Combine(root, "f"));
  198. }
  199. private static void EnsureDirectory(string path)
  200. {
  201. if (!Directory.Exists(path))
  202. {
  203. Directory.CreateDirectory(path);
  204. }
  205. }
  206. }
  207. }