FFProbeAudioInfoProvider.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. using MediaBrowser.Common.IO;
  2. using MediaBrowser.Controller.Configuration;
  3. using MediaBrowser.Controller.Entities;
  4. using MediaBrowser.Controller.Entities.Audio;
  5. using MediaBrowser.Controller.MediaInfo;
  6. using MediaBrowser.Model.Entities;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12. using MediaBrowser.Model.Logging;
  13. namespace MediaBrowser.Controller.Providers.MediaInfo
  14. {
  15. /// <summary>
  16. /// Extracts audio information using ffprobe
  17. /// </summary>
  18. public class FFProbeAudioInfoProvider : BaseFFProbeProvider<Audio>
  19. {
  20. public FFProbeAudioInfoProvider(ILogManager logManager, IServerConfigurationManager configurationManager) : base(logManager, configurationManager)
  21. {
  22. }
  23. /// <summary>
  24. /// Gets the name of the cache directory.
  25. /// </summary>
  26. /// <value>The name of the cache directory.</value>
  27. protected override string CacheDirectoryName
  28. {
  29. get
  30. {
  31. return "ffmpeg-audio-info";
  32. }
  33. }
  34. /// <summary>
  35. /// Fetches the specified audio.
  36. /// </summary>
  37. /// <param name="audio">The audio.</param>
  38. /// <param name="cancellationToken">The cancellation token.</param>
  39. /// <param name="data">The data.</param>
  40. /// <param name="isoMount">The iso mount.</param>
  41. /// <returns>Task.</returns>
  42. protected override Task Fetch(Audio audio, CancellationToken cancellationToken, FFProbeResult data, IIsoMount isoMount)
  43. {
  44. return Task.Run(() =>
  45. {
  46. if (data.streams == null)
  47. {
  48. Logger.Error("Audio item has no streams: " + audio.Path);
  49. return;
  50. }
  51. audio.MediaStreams = data.streams.Select(s => GetMediaStream(s, data.format)).ToList();
  52. // Get the first audio stream
  53. var stream = data.streams.First(s => s.codec_type.Equals("audio", StringComparison.OrdinalIgnoreCase));
  54. // Get duration from stream properties
  55. var duration = stream.duration;
  56. // If it's not there go into format properties
  57. if (string.IsNullOrEmpty(duration))
  58. {
  59. duration = data.format.duration;
  60. }
  61. // If we got something, parse it
  62. if (!string.IsNullOrEmpty(duration))
  63. {
  64. audio.RunTimeTicks = TimeSpan.FromSeconds(double.Parse(duration, UsCulture)).Ticks;
  65. }
  66. if (data.format.tags != null)
  67. {
  68. FetchDataFromTags(audio, data.format.tags);
  69. }
  70. });
  71. }
  72. /// <summary>
  73. /// Fetches data from the tags dictionary
  74. /// </summary>
  75. /// <param name="audio">The audio.</param>
  76. /// <param name="tags">The tags.</param>
  77. private void FetchDataFromTags(Audio audio, Dictionary<string, string> tags)
  78. {
  79. var title = GetDictionaryValue(tags, "title");
  80. // Only set Name if title was found in the dictionary
  81. if (!string.IsNullOrEmpty(title))
  82. {
  83. audio.Name = title;
  84. }
  85. var composer = GetDictionaryValue(tags, "composer");
  86. if (!string.IsNullOrWhiteSpace(composer))
  87. {
  88. // Only use the comma as a delimeter if there are no slashes or pipes.
  89. // We want to be careful not to split names that have commas in them
  90. var delimeter = composer.IndexOf('/') == -1 && composer.IndexOf('|') == -1 ? new[] { ',' } : new[] { '/', '|' };
  91. foreach (var person in composer.Split(delimeter, StringSplitOptions.RemoveEmptyEntries))
  92. {
  93. var name = person.Trim();
  94. if (!string.IsNullOrEmpty(name))
  95. {
  96. audio.AddPerson(new PersonInfo { Name = name, Type = PersonType.Composer });
  97. }
  98. }
  99. }
  100. audio.Album = GetDictionaryValue(tags, "album");
  101. audio.Artist = GetDictionaryValue(tags, "artist");
  102. if (!string.IsNullOrWhiteSpace(audio.Artist))
  103. {
  104. // Add to people too
  105. audio.AddPerson(new PersonInfo {Name = audio.Artist, Type = PersonType.MusicArtist});
  106. }
  107. // Several different forms of albumartist
  108. audio.AlbumArtist = GetDictionaryValue(tags, "albumartist") ?? GetDictionaryValue(tags, "album artist") ?? GetDictionaryValue(tags, "album_artist");
  109. // Track number
  110. audio.IndexNumber = GetDictionaryNumericValue(tags, "track");
  111. // Disc number
  112. audio.ParentIndexNumber = GetDictionaryDiscValue(tags);
  113. audio.Language = GetDictionaryValue(tags, "language");
  114. audio.ProductionYear = GetDictionaryNumericValue(tags, "date");
  115. // Several different forms of retaildate
  116. audio.PremiereDate = GetDictionaryDateTime(tags, "retaildate") ?? GetDictionaryDateTime(tags, "retail date") ?? GetDictionaryDateTime(tags, "retail_date");
  117. // If we don't have a ProductionYear try and get it from PremiereDate
  118. if (audio.PremiereDate.HasValue && !audio.ProductionYear.HasValue)
  119. {
  120. audio.ProductionYear = audio.PremiereDate.Value.Year;
  121. }
  122. FetchGenres(audio, tags);
  123. // There's several values in tags may or may not be present
  124. FetchStudios(audio, tags, "organization");
  125. FetchStudios(audio, tags, "ensemble");
  126. FetchPublishers(audio, tags, "publisher");
  127. }
  128. /// <summary>
  129. /// Gets the studios from the tags collection
  130. /// </summary>
  131. /// <param name="audio">The audio.</param>
  132. /// <param name="tags">The tags.</param>
  133. /// <param name="tagName">Name of the tag.</param>
  134. private void FetchStudios(Audio audio, Dictionary<string, string> tags, string tagName)
  135. {
  136. var val = GetDictionaryValue(tags, tagName);
  137. if (!string.IsNullOrEmpty(val))
  138. {
  139. audio.AddStudios(val.Split(new[] { '/', '|' }, StringSplitOptions.RemoveEmptyEntries));
  140. }
  141. }
  142. /// <summary>
  143. /// Fetches the publishers.
  144. /// </summary>
  145. /// <param name="audio">The audio.</param>
  146. /// <param name="tags">The tags.</param>
  147. /// <param name="tagName">Name of the tag.</param>
  148. private void FetchPublishers(Audio audio, Dictionary<string, string> tags, string tagName)
  149. {
  150. var val = GetDictionaryValue(tags, tagName);
  151. if (!string.IsNullOrEmpty(val))
  152. {
  153. audio.AddPublishers(val.Split(new[] { '/', '|' }, StringSplitOptions.RemoveEmptyEntries));
  154. }
  155. }
  156. /// <summary>
  157. /// Gets the genres from the tags collection
  158. /// </summary>
  159. /// <param name="audio">The audio.</param>
  160. /// <param name="tags">The tags.</param>
  161. private void FetchGenres(Audio audio, Dictionary<string, string> tags)
  162. {
  163. var val = GetDictionaryValue(tags, "genre");
  164. if (!string.IsNullOrEmpty(val))
  165. {
  166. audio.AddGenres(val.Split(new[] { '/', '|' }, StringSplitOptions.RemoveEmptyEntries));
  167. }
  168. }
  169. /// <summary>
  170. /// Gets the disc number, which is sometimes can be in the form of '1', or '1/3'
  171. /// </summary>
  172. /// <param name="tags">The tags.</param>
  173. /// <returns>System.Nullable{System.Int32}.</returns>
  174. private int? GetDictionaryDiscValue(Dictionary<string, string> tags)
  175. {
  176. var disc = GetDictionaryValue(tags, "disc");
  177. if (!string.IsNullOrEmpty(disc))
  178. {
  179. disc = disc.Split('/')[0];
  180. int num;
  181. if (int.TryParse(disc, out num))
  182. {
  183. return num;
  184. }
  185. }
  186. return null;
  187. }
  188. }
  189. }