FFProbeAudioInfoProvider.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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)).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, UsCulture)).Ticks;
  64. }
  65. if (data.format.tags != null)
  66. {
  67. FetchDataFromTags(audio, data.format.tags);
  68. }
  69. }
  70. /// <summary>
  71. /// Fetches data from the tags dictionary
  72. /// </summary>
  73. /// <param name="audio">The audio.</param>
  74. /// <param name="tags">The tags.</param>
  75. private void FetchDataFromTags(Audio audio, Dictionary<string, string> tags)
  76. {
  77. var title = GetDictionaryValue(tags, "title");
  78. // Only set Name if title was found in the dictionary
  79. if (!string.IsNullOrEmpty(title))
  80. {
  81. audio.Name = title;
  82. }
  83. var composer = GetDictionaryValue(tags, "composer");
  84. if (!string.IsNullOrWhiteSpace(composer))
  85. {
  86. foreach (var person in Split(composer))
  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. // Several different forms of albumartist
  98. audio.AlbumArtist = GetDictionaryValue(tags, "albumartist") ?? GetDictionaryValue(tags, "album artist") ?? GetDictionaryValue(tags, "album_artist");
  99. // Track number
  100. audio.IndexNumber = GetDictionaryNumericValue(tags, "track");
  101. // Disc number
  102. audio.ParentIndexNumber = GetDictionaryDiscValue(tags);
  103. audio.Language = GetDictionaryValue(tags, "language");
  104. audio.ProductionYear = GetDictionaryNumericValue(tags, "date");
  105. // Several different forms of retaildate
  106. audio.PremiereDate = GetDictionaryDateTime(tags, "retaildate") ?? GetDictionaryDateTime(tags, "retail date") ?? GetDictionaryDateTime(tags, "retail_date");
  107. // If we don't have a ProductionYear try and get it from PremiereDate
  108. if (audio.PremiereDate.HasValue && !audio.ProductionYear.HasValue)
  109. {
  110. audio.ProductionYear = audio.PremiereDate.Value.ToLocalTime().Year;
  111. }
  112. FetchGenres(audio, tags);
  113. // There's several values in tags may or may not be present
  114. FetchStudios(audio, tags, "organization");
  115. FetchStudios(audio, tags, "ensemble");
  116. FetchStudios(audio, tags, "publisher");
  117. }
  118. /// <summary>
  119. /// Splits the specified val.
  120. /// </summary>
  121. /// <param name="val">The val.</param>
  122. /// <returns>System.String[][].</returns>
  123. private string[] Split(string val)
  124. {
  125. // Only use the comma as a delimeter if there are no slashes or pipes.
  126. // We want to be careful not to split names that have commas in them
  127. var delimeter = val.IndexOf('/') == -1 && val.IndexOf('|') == -1 ? new[] { ',' } : new[] { '/', '|' };
  128. return val.Split(delimeter, StringSplitOptions.RemoveEmptyEntries);
  129. }
  130. /// <summary>
  131. /// Gets the studios from the tags collection
  132. /// </summary>
  133. /// <param name="audio">The audio.</param>
  134. /// <param name="tags">The tags.</param>
  135. /// <param name="tagName">Name of the tag.</param>
  136. private void FetchStudios(Audio audio, Dictionary<string, string> tags, string tagName)
  137. {
  138. var val = GetDictionaryValue(tags, tagName);
  139. if (!string.IsNullOrEmpty(val))
  140. {
  141. var studios =
  142. val.Split(new[] {'/', '|'}, StringSplitOptions.RemoveEmptyEntries)
  143. .Where(i => !string.Equals(i, audio.Artist, StringComparison.OrdinalIgnoreCase) && !string.Equals(i, audio.AlbumArtist, StringComparison.OrdinalIgnoreCase));
  144. audio.AddStudios(studios);
  145. }
  146. }
  147. /// <summary>
  148. /// Gets the genres from the tags collection
  149. /// </summary>
  150. /// <param name="audio">The audio.</param>
  151. /// <param name="tags">The tags.</param>
  152. private void FetchGenres(Audio audio, Dictionary<string, string> tags)
  153. {
  154. var val = GetDictionaryValue(tags, "genre");
  155. if (!string.IsNullOrEmpty(val))
  156. {
  157. audio.AddGenres(val.Split(new[] { '/', '|' }, StringSplitOptions.RemoveEmptyEntries));
  158. }
  159. }
  160. /// <summary>
  161. /// Gets the disc number, which is sometimes can be in the form of '1', or '1/3'
  162. /// </summary>
  163. /// <param name="tags">The tags.</param>
  164. /// <returns>System.Nullable{System.Int32}.</returns>
  165. private int? GetDictionaryDiscValue(Dictionary<string, string> tags)
  166. {
  167. var disc = GetDictionaryValue(tags, "disc");
  168. if (!string.IsNullOrEmpty(disc))
  169. {
  170. disc = disc.Split('/')[0];
  171. int num;
  172. if (int.TryParse(disc, out num))
  173. {
  174. return num;
  175. }
  176. }
  177. return null;
  178. }
  179. }
  180. }