FFProbeAudioInfoProvider.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. // Only use the comma as a delimeter if there are no slashes or pipes.
  87. // We want to be careful not to split names that have commas in them
  88. var delimeter = composer.IndexOf('/') == -1 && composer.IndexOf('|') == -1 ? new[] { ',' } : new[] { '/', '|' };
  89. foreach (var person in composer.Split(delimeter, StringSplitOptions.RemoveEmptyEntries))
  90. {
  91. var name = person.Trim();
  92. if (!string.IsNullOrEmpty(name))
  93. {
  94. audio.AddPerson(new PersonInfo { Name = name, Type = PersonType.Composer });
  95. }
  96. }
  97. }
  98. audio.Album = GetDictionaryValue(tags, "album");
  99. audio.Artist = GetDictionaryValue(tags, "artist");
  100. if (!string.IsNullOrWhiteSpace(audio.Artist))
  101. {
  102. // Add to people too
  103. audio.AddPerson(new PersonInfo {Name = audio.Artist, Type = PersonType.MusicArtist});
  104. }
  105. // Several different forms of albumartist
  106. audio.AlbumArtist = GetDictionaryValue(tags, "albumartist") ?? GetDictionaryValue(tags, "album artist") ?? GetDictionaryValue(tags, "album_artist");
  107. // Track number
  108. audio.IndexNumber = GetDictionaryNumericValue(tags, "track");
  109. // Disc number
  110. audio.ParentIndexNumber = GetDictionaryDiscValue(tags);
  111. audio.Language = GetDictionaryValue(tags, "language");
  112. audio.ProductionYear = GetDictionaryNumericValue(tags, "date");
  113. // Several different forms of retaildate
  114. audio.PremiereDate = GetDictionaryDateTime(tags, "retaildate") ?? GetDictionaryDateTime(tags, "retail date") ?? GetDictionaryDateTime(tags, "retail_date");
  115. // If we don't have a ProductionYear try and get it from PremiereDate
  116. if (audio.PremiereDate.HasValue && !audio.ProductionYear.HasValue)
  117. {
  118. audio.ProductionYear = audio.PremiereDate.Value.ToLocalTime().Year;
  119. }
  120. FetchGenres(audio, tags);
  121. // There's several values in tags may or may not be present
  122. FetchStudios(audio, tags, "organization");
  123. FetchStudios(audio, tags, "ensemble");
  124. FetchStudios(audio, tags, "publisher");
  125. }
  126. /// <summary>
  127. /// Gets the studios from the tags collection
  128. /// </summary>
  129. /// <param name="audio">The audio.</param>
  130. /// <param name="tags">The tags.</param>
  131. /// <param name="tagName">Name of the tag.</param>
  132. private void FetchStudios(Audio audio, Dictionary<string, string> tags, string tagName)
  133. {
  134. var val = GetDictionaryValue(tags, tagName);
  135. if (!string.IsNullOrEmpty(val))
  136. {
  137. audio.AddStudios(val.Split(new[] { '/', '|' }, StringSplitOptions.RemoveEmptyEntries));
  138. }
  139. }
  140. /// <summary>
  141. /// Gets the genres from the tags collection
  142. /// </summary>
  143. /// <param name="audio">The audio.</param>
  144. /// <param name="tags">The tags.</param>
  145. private void FetchGenres(Audio audio, Dictionary<string, string> tags)
  146. {
  147. var val = GetDictionaryValue(tags, "genre");
  148. if (!string.IsNullOrEmpty(val))
  149. {
  150. audio.AddGenres(val.Split(new[] { '/', '|' }, StringSplitOptions.RemoveEmptyEntries));
  151. }
  152. }
  153. /// <summary>
  154. /// Gets the disc number, which is sometimes can be in the form of '1', or '1/3'
  155. /// </summary>
  156. /// <param name="tags">The tags.</param>
  157. /// <returns>System.Nullable{System.Int32}.</returns>
  158. private int? GetDictionaryDiscValue(Dictionary<string, string> tags)
  159. {
  160. var disc = GetDictionaryValue(tags, "disc");
  161. if (!string.IsNullOrEmpty(disc))
  162. {
  163. disc = disc.Split('/')[0];
  164. int num;
  165. if (int.TryParse(disc, out num))
  166. {
  167. return num;
  168. }
  169. }
  170. return null;
  171. }
  172. }
  173. }