FFProbeAudioInfoProvider.cs 7.4 KB

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