FFProbeAudioInfoProvider.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. using MediaBrowser.Common.IO;
  2. using MediaBrowser.Common.MediaInfo;
  3. using MediaBrowser.Controller.Configuration;
  4. using MediaBrowser.Controller.Entities;
  5. using MediaBrowser.Controller.Entities.Audio;
  6. using MediaBrowser.Model.Entities;
  7. using MediaBrowser.Model.Logging;
  8. using MediaBrowser.Model.Serialization;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using System.Threading;
  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, IMediaEncoder mediaEncoder, IJsonSerializer jsonSerializer)
  21. : base(logManager, configurationManager, mediaEncoder, jsonSerializer)
  22. {
  23. }
  24. /// <summary>
  25. /// Gets the name of the cache directory.
  26. /// </summary>
  27. /// <value>The name of the cache directory.</value>
  28. protected override string CacheDirectoryName
  29. {
  30. get
  31. {
  32. return "ffmpeg-audio-info";
  33. }
  34. }
  35. /// <summary>
  36. /// Fetches the specified audio.
  37. /// </summary>
  38. /// <param name="audio">The audio.</param>
  39. /// <param name="cancellationToken">The cancellation token.</param>
  40. /// <param name="data">The data.</param>
  41. /// <param name="isoMount">The iso mount.</param>
  42. /// <returns>Task.</returns>
  43. protected override void Fetch(Audio audio, CancellationToken cancellationToken, MediaInfoResult data, IIsoMount isoMount)
  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))
  51. .Where(i => i != null)
  52. .ToList();
  53. // Get the first audio stream
  54. var stream = data.streams.First(s => s.codec_type.Equals("audio", StringComparison.OrdinalIgnoreCase));
  55. // Get duration from stream properties
  56. var duration = stream.duration;
  57. // If it's not there go into format properties
  58. if (string.IsNullOrEmpty(duration))
  59. {
  60. duration = data.format.duration;
  61. }
  62. // If we got something, parse it
  63. if (!string.IsNullOrEmpty(duration))
  64. {
  65. audio.RunTimeTicks = TimeSpan.FromSeconds(double.Parse(duration, UsCulture)).Ticks;
  66. }
  67. if (data.format.tags != null)
  68. {
  69. FetchDataFromTags(audio, data.format.tags);
  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. foreach (var person in Split(composer))
  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. // Several different forms of albumartist
  100. audio.AlbumArtist = GetDictionaryValue(tags, "albumartist") ?? GetDictionaryValue(tags, "album artist") ?? GetDictionaryValue(tags, "album_artist");
  101. // Track number
  102. audio.IndexNumber = GetDictionaryNumericValue(tags, "track");
  103. // Disc number
  104. audio.ParentIndexNumber = GetDictionaryDiscValue(tags);
  105. audio.Language = GetDictionaryValue(tags, "language");
  106. audio.ProductionYear = GetDictionaryNumericValue(tags, "date");
  107. // Several different forms of retaildate
  108. audio.PremiereDate = GetDictionaryDateTime(tags, "retaildate") ?? GetDictionaryDateTime(tags, "retail date") ?? GetDictionaryDateTime(tags, "retail_date");
  109. // If we don't have a ProductionYear try and get it from PremiereDate
  110. if (audio.PremiereDate.HasValue && !audio.ProductionYear.HasValue)
  111. {
  112. audio.ProductionYear = audio.PremiereDate.Value.ToLocalTime().Year;
  113. }
  114. FetchGenres(audio, tags);
  115. // There's several values in tags may or may not be present
  116. FetchStudios(audio, tags, "organization");
  117. FetchStudios(audio, tags, "ensemble");
  118. FetchStudios(audio, tags, "publisher");
  119. }
  120. /// <summary>
  121. /// Splits the specified val.
  122. /// </summary>
  123. /// <param name="val">The val.</param>
  124. /// <returns>System.String[][].</returns>
  125. private IEnumerable<string> Split(string val)
  126. {
  127. // Only use the comma as a delimeter if there are no slashes or pipes.
  128. // We want to be careful not to split names that have commas in them
  129. var delimeter = val.IndexOf('/') == -1 && val.IndexOf('|') == -1 ? new[] { ',' } : new[] { '/', '|' };
  130. return val.Split(delimeter, StringSplitOptions.RemoveEmptyEntries);
  131. }
  132. /// <summary>
  133. /// Gets the studios from the tags collection
  134. /// </summary>
  135. /// <param name="audio">The audio.</param>
  136. /// <param name="tags">The tags.</param>
  137. /// <param name="tagName">Name of the tag.</param>
  138. private void FetchStudios(Audio audio, Dictionary<string, string> tags, string tagName)
  139. {
  140. var val = GetDictionaryValue(tags, tagName);
  141. if (!string.IsNullOrEmpty(val))
  142. {
  143. var studios =
  144. val.Split(new[] { '/', '|' }, StringSplitOptions.RemoveEmptyEntries)
  145. .Where(i => !string.Equals(i, audio.Artist, StringComparison.OrdinalIgnoreCase) && !string.Equals(i, audio.AlbumArtist, StringComparison.OrdinalIgnoreCase));
  146. audio.Studios.Clear();
  147. foreach (var studio in studios)
  148. {
  149. audio.AddStudio(studio);
  150. }
  151. }
  152. }
  153. /// <summary>
  154. /// Gets the genres from the tags collection
  155. /// </summary>
  156. /// <param name="audio">The audio.</param>
  157. /// <param name="tags">The tags.</param>
  158. private void FetchGenres(Audio audio, Dictionary<string, string> tags)
  159. {
  160. var val = GetDictionaryValue(tags, "genre");
  161. if (!string.IsNullOrEmpty(val))
  162. {
  163. audio.Genres.Clear();
  164. foreach (var genre in val.Split(new[] { '/', '|' }, StringSplitOptions.RemoveEmptyEntries))
  165. {
  166. audio.AddGenre(genre);
  167. }
  168. }
  169. }
  170. /// <summary>
  171. /// Gets the disc number, which is sometimes can be in the form of '1', or '1/3'
  172. /// </summary>
  173. /// <param name="tags">The tags.</param>
  174. /// <returns>System.Nullable{System.Int32}.</returns>
  175. private int? GetDictionaryDiscValue(Dictionary<string, string> tags)
  176. {
  177. var disc = GetDictionaryValue(tags, "disc");
  178. if (!string.IsNullOrEmpty(disc))
  179. {
  180. disc = disc.Split('/')[0];
  181. int num;
  182. if (int.TryParse(disc, out num))
  183. {
  184. return num;
  185. }
  186. }
  187. return null;
  188. }
  189. }
  190. }