FFProbeAudioInfoProvider.cs 7.3 KB

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