FFProbeAudioInfoProvider.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 void Fetch(Audio audio, CancellationToken cancellationToken, FFProbeResult data, IIsoMount isoMount)
  43. {
  44. if (data.streams == null)
  45. {
  46. Logger.Error("Audio item has no streams: " + audio.Path);
  47. return;
  48. }
  49. audio.MediaStreams = data.streams.Select(s => GetMediaStream(s, data.format)).ToList();
  50. // Get the first audio stream
  51. var stream = data.streams.First(s => s.codec_type.Equals("audio", StringComparison.OrdinalIgnoreCase));
  52. // Get duration from stream properties
  53. var duration = stream.duration;
  54. // If it's not there go into format properties
  55. if (string.IsNullOrEmpty(duration))
  56. {
  57. duration = data.format.duration;
  58. }
  59. // If we got something, parse it
  60. if (!string.IsNullOrEmpty(duration))
  61. {
  62. audio.RunTimeTicks = TimeSpan.FromSeconds(double.Parse(duration, UsCulture)).Ticks;
  63. }
  64. if (data.format.tags != null)
  65. {
  66. FetchDataFromTags(audio, data.format.tags);
  67. }
  68. }
  69. /// <summary>
  70. /// Fetches data from the tags dictionary
  71. /// </summary>
  72. /// <param name="audio">The audio.</param>
  73. /// <param name="tags">The tags.</param>
  74. private void FetchDataFromTags(Audio audio, Dictionary<string, string> tags)
  75. {
  76. var title = GetDictionaryValue(tags, "title");
  77. // Only set Name if title was found in the dictionary
  78. if (!string.IsNullOrEmpty(title))
  79. {
  80. audio.Name = title;
  81. }
  82. var composer = GetDictionaryValue(tags, "composer");
  83. if (!string.IsNullOrWhiteSpace(composer))
  84. {
  85. // Only use the comma as a delimeter if there are no slashes or pipes.
  86. // We want to be careful not to split names that have commas in them
  87. var delimeter = composer.IndexOf('/') == -1 && composer.IndexOf('|') == -1 ? new[] { ',' } : new[] { '/', '|' };
  88. foreach (var person in composer.Split(delimeter, StringSplitOptions.RemoveEmptyEntries))
  89. {
  90. var name = person.Trim();
  91. if (!string.IsNullOrEmpty(name))
  92. {
  93. audio.AddPerson(new PersonInfo { Name = name, Type = PersonType.Composer });
  94. }
  95. }
  96. }
  97. audio.Album = GetDictionaryValue(tags, "album");
  98. audio.Artist = GetDictionaryValue(tags, "artist");
  99. if (!string.IsNullOrWhiteSpace(audio.Artist))
  100. {
  101. // Add to people too
  102. audio.AddPerson(new PersonInfo {Name = audio.Artist, Type = PersonType.MusicArtist});
  103. }
  104. // Several different forms of albumartist
  105. audio.AlbumArtist = GetDictionaryValue(tags, "albumartist") ?? GetDictionaryValue(tags, "album artist") ?? GetDictionaryValue(tags, "album_artist");
  106. // Track number
  107. audio.IndexNumber = GetDictionaryNumericValue(tags, "track");
  108. // Disc number
  109. audio.ParentIndexNumber = GetDictionaryDiscValue(tags);
  110. audio.Language = GetDictionaryValue(tags, "language");
  111. audio.ProductionYear = GetDictionaryNumericValue(tags, "date");
  112. // Several different forms of retaildate
  113. audio.PremiereDate = GetDictionaryDateTime(tags, "retaildate") ?? GetDictionaryDateTime(tags, "retail date") ?? GetDictionaryDateTime(tags, "retail_date");
  114. // If we don't have a ProductionYear try and get it from PremiereDate
  115. if (audio.PremiereDate.HasValue && !audio.ProductionYear.HasValue)
  116. {
  117. audio.ProductionYear = audio.PremiereDate.Value.Year;
  118. }
  119. FetchGenres(audio, tags);
  120. // There's several values in tags may or may not be present
  121. FetchStudios(audio, tags, "organization");
  122. FetchStudios(audio, tags, "ensemble");
  123. FetchStudios(audio, tags, "publisher");
  124. }
  125. /// <summary>
  126. /// Gets the studios from the tags collection
  127. /// </summary>
  128. /// <param name="audio">The audio.</param>
  129. /// <param name="tags">The tags.</param>
  130. /// <param name="tagName">Name of the tag.</param>
  131. private void FetchStudios(Audio audio, Dictionary<string, string> tags, string tagName)
  132. {
  133. var val = GetDictionaryValue(tags, tagName);
  134. if (!string.IsNullOrEmpty(val))
  135. {
  136. audio.AddStudios(val.Split(new[] { '/', '|' }, StringSplitOptions.RemoveEmptyEntries));
  137. }
  138. }
  139. /// <summary>
  140. /// Gets the genres from the tags collection
  141. /// </summary>
  142. /// <param name="audio">The audio.</param>
  143. /// <param name="tags">The tags.</param>
  144. private void FetchGenres(Audio audio, Dictionary<string, string> tags)
  145. {
  146. var val = GetDictionaryValue(tags, "genre");
  147. if (!string.IsNullOrEmpty(val))
  148. {
  149. audio.AddGenres(val.Split(new[] { '/', '|' }, StringSplitOptions.RemoveEmptyEntries));
  150. }
  151. }
  152. /// <summary>
  153. /// Gets the disc number, which is sometimes can be in the form of '1', or '1/3'
  154. /// </summary>
  155. /// <param name="tags">The tags.</param>
  156. /// <returns>System.Nullable{System.Int32}.</returns>
  157. private int? GetDictionaryDiscValue(Dictionary<string, string> tags)
  158. {
  159. var disc = GetDictionaryValue(tags, "disc");
  160. if (!string.IsNullOrEmpty(disc))
  161. {
  162. disc = disc.Split('/')[0];
  163. int num;
  164. if (int.TryParse(disc, out num))
  165. {
  166. return num;
  167. }
  168. }
  169. return null;
  170. }
  171. }
  172. }