FFProbeAudioInfoProvider.cs 7.9 KB

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