FFProbeAudioInfo.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. using MediaBrowser.Common.Configuration;
  2. using MediaBrowser.Model.Extensions;
  3. using MediaBrowser.Controller.Entities;
  4. using MediaBrowser.Controller.Entities.Audio;
  5. using MediaBrowser.Controller.Library;
  6. using MediaBrowser.Controller.MediaEncoding;
  7. using MediaBrowser.Controller.Persistence;
  8. using MediaBrowser.Model.Entities;
  9. using MediaBrowser.Model.MediaInfo;
  10. using MediaBrowser.Model.Serialization;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Globalization;
  14. using System.IO;
  15. using System.Linq;
  16. using System.Threading;
  17. using System.Threading.Tasks;
  18. namespace MediaBrowser.Providers.MediaInfo
  19. {
  20. class FFProbeAudioInfo
  21. {
  22. private readonly IMediaEncoder _mediaEncoder;
  23. private readonly IItemRepository _itemRepo;
  24. private readonly IApplicationPaths _appPaths;
  25. private readonly IJsonSerializer _json;
  26. private readonly CultureInfo _usCulture = new CultureInfo("en-US");
  27. public FFProbeAudioInfo(IMediaEncoder mediaEncoder, IItemRepository itemRepo, IApplicationPaths appPaths, IJsonSerializer json)
  28. {
  29. _mediaEncoder = mediaEncoder;
  30. _itemRepo = itemRepo;
  31. _appPaths = appPaths;
  32. _json = json;
  33. }
  34. public async Task<ItemUpdateType> Probe<T>(T item, CancellationToken cancellationToken)
  35. where T : Audio
  36. {
  37. if (item.IsArchive)
  38. {
  39. var ext = Path.GetExtension(item.Path) ?? string.Empty;
  40. item.Container = ext.TrimStart('.');
  41. return ItemUpdateType.MetadataImport;
  42. }
  43. var result = await GetMediaInfo(item, cancellationToken).ConfigureAwait(false);
  44. cancellationToken.ThrowIfCancellationRequested();
  45. FFProbeHelpers.NormalizeFFProbeResult(result);
  46. cancellationToken.ThrowIfCancellationRequested();
  47. await Fetch(item, cancellationToken, result).ConfigureAwait(false);
  48. return ItemUpdateType.MetadataImport;
  49. }
  50. private const string SchemaVersion = "1";
  51. private async Task<InternalMediaInfoResult> GetMediaInfo(BaseItem item, CancellationToken cancellationToken)
  52. {
  53. cancellationToken.ThrowIfCancellationRequested();
  54. var idString = item.Id.ToString("N");
  55. var cachePath = Path.Combine(_appPaths.CachePath,
  56. "ffprobe-audio",
  57. idString.Substring(0, 2), idString, "v" + SchemaVersion + _mediaEncoder.Version + item.DateModified.Ticks.ToString(_usCulture) + ".json");
  58. try
  59. {
  60. return _json.DeserializeFromFile<InternalMediaInfoResult>(cachePath);
  61. }
  62. catch (FileNotFoundException)
  63. {
  64. }
  65. catch (DirectoryNotFoundException)
  66. {
  67. }
  68. var inputPath = new[] { item.Path };
  69. var result = await _mediaEncoder.GetMediaInfo(inputPath, MediaProtocol.File, false, cancellationToken).ConfigureAwait(false);
  70. Directory.CreateDirectory(Path.GetDirectoryName(cachePath));
  71. _json.SerializeToFile(result, cachePath);
  72. return result;
  73. }
  74. /// <summary>
  75. /// Fetches the specified audio.
  76. /// </summary>
  77. /// <param name="audio">The audio.</param>
  78. /// <param name="cancellationToken">The cancellation token.</param>
  79. /// <param name="data">The data.</param>
  80. /// <returns>Task.</returns>
  81. protected Task Fetch(Audio audio, CancellationToken cancellationToken, InternalMediaInfoResult data)
  82. {
  83. var mediaInfo = MediaEncoderHelpers.GetMediaInfo(data);
  84. var mediaStreams = mediaInfo.MediaStreams;
  85. audio.FormatName = mediaInfo.Format;
  86. audio.TotalBitrate = mediaInfo.TotalBitrate;
  87. audio.HasEmbeddedImage = mediaStreams.Any(i => i.Type == MediaStreamType.EmbeddedImage);
  88. if (data.streams != null)
  89. {
  90. // Get the first audio stream
  91. var stream = data.streams.FirstOrDefault(s => string.Equals(s.codec_type, "audio", StringComparison.OrdinalIgnoreCase));
  92. if (stream != null)
  93. {
  94. // Get duration from stream properties
  95. var duration = stream.duration;
  96. // If it's not there go into format properties
  97. if (string.IsNullOrEmpty(duration))
  98. {
  99. duration = data.format.duration;
  100. }
  101. // If we got something, parse it
  102. if (!string.IsNullOrEmpty(duration))
  103. {
  104. audio.RunTimeTicks = TimeSpan.FromSeconds(double.Parse(duration, _usCulture)).Ticks;
  105. }
  106. }
  107. }
  108. if (data.format != null)
  109. {
  110. var extension = (Path.GetExtension(audio.Path) ?? string.Empty).TrimStart('.');
  111. audio.Container = extension;
  112. if (!string.IsNullOrEmpty(data.format.size))
  113. {
  114. audio.Size = long.Parse(data.format.size, _usCulture);
  115. }
  116. else
  117. {
  118. audio.Size = null;
  119. }
  120. if (data.format.tags != null)
  121. {
  122. FetchDataFromTags(audio, data.format.tags);
  123. }
  124. }
  125. return _itemRepo.SaveMediaStreams(audio.Id, mediaStreams, cancellationToken);
  126. }
  127. /// <summary>
  128. /// Fetches data from the tags dictionary
  129. /// </summary>
  130. /// <param name="audio">The audio.</param>
  131. /// <param name="tags">The tags.</param>
  132. private void FetchDataFromTags(Audio audio, Dictionary<string, string> tags)
  133. {
  134. var title = FFProbeHelpers.GetDictionaryValue(tags, "title");
  135. // Only set Name if title was found in the dictionary
  136. if (!string.IsNullOrEmpty(title))
  137. {
  138. audio.Name = title;
  139. }
  140. if (!audio.LockedFields.Contains(MetadataFields.Cast))
  141. {
  142. audio.People.Clear();
  143. var composer = FFProbeHelpers.GetDictionaryValue(tags, "composer");
  144. if (!string.IsNullOrWhiteSpace(composer))
  145. {
  146. foreach (var person in Split(composer, false))
  147. {
  148. audio.AddPerson(new PersonInfo { Name = person, Type = PersonType.Composer });
  149. }
  150. }
  151. }
  152. audio.Album = FFProbeHelpers.GetDictionaryValue(tags, "album");
  153. var artists = FFProbeHelpers.GetDictionaryValue(tags, "artists");
  154. if (!string.IsNullOrWhiteSpace(artists))
  155. {
  156. audio.Artists = artists.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries)
  157. .Distinct(StringComparer.OrdinalIgnoreCase)
  158. .ToList();
  159. }
  160. else
  161. {
  162. var artist = FFProbeHelpers.GetDictionaryValue(tags, "artist");
  163. if (string.IsNullOrWhiteSpace(artist))
  164. {
  165. audio.Artists.Clear();
  166. }
  167. else
  168. {
  169. audio.Artists = SplitArtists(artist)
  170. .Distinct(StringComparer.OrdinalIgnoreCase)
  171. .ToList();
  172. }
  173. }
  174. var albumArtist = FFProbeHelpers.GetDictionaryValue(tags, "albumartist");
  175. if (string.IsNullOrWhiteSpace(albumArtist))
  176. {
  177. albumArtist = FFProbeHelpers.GetDictionaryValue(tags, "album artist");
  178. }
  179. if (string.IsNullOrWhiteSpace(albumArtist))
  180. {
  181. albumArtist = FFProbeHelpers.GetDictionaryValue(tags, "album_artist");
  182. }
  183. if (string.IsNullOrWhiteSpace(albumArtist))
  184. {
  185. audio.AlbumArtists = new List<string>();
  186. }
  187. else
  188. {
  189. audio.AlbumArtists = SplitArtists(albumArtist)
  190. .Distinct(StringComparer.OrdinalIgnoreCase)
  191. .ToList();
  192. }
  193. // Track number
  194. audio.IndexNumber = GetDictionaryDiscValue(tags, "track");
  195. // Disc number
  196. audio.ParentIndexNumber = GetDictionaryDiscValue(tags, "disc");
  197. audio.ProductionYear = FFProbeHelpers.GetDictionaryNumericValue(tags, "date");
  198. // Several different forms of retaildate
  199. audio.PremiereDate = FFProbeHelpers.GetDictionaryDateTime(tags, "retaildate") ??
  200. FFProbeHelpers.GetDictionaryDateTime(tags, "retail date") ??
  201. FFProbeHelpers.GetDictionaryDateTime(tags, "retail_date") ??
  202. FFProbeHelpers.GetDictionaryDateTime(tags, "date");
  203. // If we don't have a ProductionYear try and get it from PremiereDate
  204. if (audio.PremiereDate.HasValue && !audio.ProductionYear.HasValue)
  205. {
  206. audio.ProductionYear = audio.PremiereDate.Value.ToLocalTime().Year;
  207. }
  208. if (!audio.LockedFields.Contains(MetadataFields.Genres))
  209. {
  210. FetchGenres(audio, tags);
  211. }
  212. if (!audio.LockedFields.Contains(MetadataFields.Studios))
  213. {
  214. audio.Studios.Clear();
  215. // There's several values in tags may or may not be present
  216. FetchStudios(audio, tags, "organization");
  217. FetchStudios(audio, tags, "ensemble");
  218. FetchStudios(audio, tags, "publisher");
  219. }
  220. // These support mulitple values, but for now we only store the first.
  221. audio.SetProviderId(MetadataProviders.MusicBrainzAlbumArtist, GetMultipleMusicBrainzId(FFProbeHelpers.GetDictionaryValue(tags, "MusicBrainz Album Artist Id")));
  222. audio.SetProviderId(MetadataProviders.MusicBrainzArtist, GetMultipleMusicBrainzId(FFProbeHelpers.GetDictionaryValue(tags, "MusicBrainz Artist Id")));
  223. audio.SetProviderId(MetadataProviders.MusicBrainzAlbum, GetMultipleMusicBrainzId(FFProbeHelpers.GetDictionaryValue(tags, "MusicBrainz Album Id")));
  224. audio.SetProviderId(MetadataProviders.MusicBrainzReleaseGroup, GetMultipleMusicBrainzId(FFProbeHelpers.GetDictionaryValue(tags, "MusicBrainz Release Group Id")));
  225. audio.SetProviderId(MetadataProviders.MusicBrainzTrack, GetMultipleMusicBrainzId(FFProbeHelpers.GetDictionaryValue(tags, "MusicBrainz Release Track Id")));
  226. }
  227. private string GetMultipleMusicBrainzId(string value)
  228. {
  229. if (string.IsNullOrWhiteSpace(value))
  230. {
  231. return null;
  232. }
  233. return value.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries)
  234. .Select(i => i.Trim())
  235. .FirstOrDefault(i => !string.IsNullOrWhiteSpace(i));
  236. }
  237. private readonly char[] _nameDelimiters = { '/', '|', ';', '\\' };
  238. /// <summary>
  239. /// Splits the specified val.
  240. /// </summary>
  241. /// <param name="val">The val.</param>
  242. /// <param name="allowCommaDelimiter">if set to <c>true</c> [allow comma delimiter].</param>
  243. /// <returns>System.String[][].</returns>
  244. private IEnumerable<string> Split(string val, bool allowCommaDelimiter)
  245. {
  246. // Only use the comma as a delimeter if there are no slashes or pipes.
  247. // We want to be careful not to split names that have commas in them
  248. var delimeter = !allowCommaDelimiter || _nameDelimiters.Any(i => val.IndexOf(i) != -1) ?
  249. _nameDelimiters :
  250. new[] { ',' };
  251. return val.Split(delimeter, StringSplitOptions.RemoveEmptyEntries)
  252. .Where(i => !string.IsNullOrWhiteSpace(i))
  253. .Select(i => i.Trim());
  254. }
  255. private const string ArtistReplaceValue = " | ";
  256. private IEnumerable<string> SplitArtists(string val)
  257. {
  258. val = val.Replace(" featuring ", ArtistReplaceValue, StringComparison.OrdinalIgnoreCase)
  259. .Replace(" feat. ", ArtistReplaceValue, StringComparison.OrdinalIgnoreCase);
  260. var artistsFound = new List<string>();
  261. foreach (var whitelistArtist in GetSplitWhitelist())
  262. {
  263. var originalVal = val;
  264. val = val.Replace(whitelistArtist, "|", StringComparison.OrdinalIgnoreCase);
  265. if (!string.Equals(originalVal, val, StringComparison.OrdinalIgnoreCase))
  266. {
  267. artistsFound.Add(whitelistArtist);
  268. }
  269. }
  270. // Only use the comma as a delimeter if there are no slashes or pipes.
  271. // We want to be careful not to split names that have commas in them
  272. var delimeter = _nameDelimiters;
  273. var artists = val.Split(delimeter, StringSplitOptions.RemoveEmptyEntries)
  274. .Where(i => !string.IsNullOrWhiteSpace(i))
  275. .Select(i => i.Trim());
  276. artistsFound.AddRange(artists);
  277. return artistsFound;
  278. }
  279. private List<string> _splitWhiteList = null;
  280. private IEnumerable<string> GetSplitWhitelist()
  281. {
  282. if (_splitWhiteList == null)
  283. {
  284. var file = GetType().Namespace + ".whitelist.txt";
  285. using (var stream = GetType().Assembly.GetManifestResourceStream(file))
  286. {
  287. using (var reader = new StreamReader(stream))
  288. {
  289. var list = new List<string>();
  290. while (!reader.EndOfStream)
  291. {
  292. var val = reader.ReadLine();
  293. if (!string.IsNullOrWhiteSpace(val))
  294. {
  295. list.Add(val);
  296. }
  297. }
  298. _splitWhiteList = list;
  299. }
  300. }
  301. }
  302. return _splitWhiteList;
  303. }
  304. /// <summary>
  305. /// Gets the studios from the tags collection
  306. /// </summary>
  307. /// <param name="audio">The audio.</param>
  308. /// <param name="tags">The tags.</param>
  309. /// <param name="tagName">Name of the tag.</param>
  310. private void FetchStudios(Audio audio, Dictionary<string, string> tags, string tagName)
  311. {
  312. var val = FFProbeHelpers.GetDictionaryValue(tags, tagName);
  313. if (!string.IsNullOrEmpty(val))
  314. {
  315. // Sometimes the artist name is listed here, account for that
  316. var studios = Split(val, true).Where(i => !audio.HasArtist(i));
  317. foreach (var studio in studios)
  318. {
  319. audio.AddStudio(studio);
  320. }
  321. }
  322. }
  323. /// <summary>
  324. /// Gets the genres from the tags collection
  325. /// </summary>
  326. /// <param name="audio">The audio.</param>
  327. /// <param name="tags">The tags.</param>
  328. private void FetchGenres(Audio audio, Dictionary<string, string> tags)
  329. {
  330. var val = FFProbeHelpers.GetDictionaryValue(tags, "genre");
  331. if (!string.IsNullOrEmpty(val))
  332. {
  333. audio.Genres.Clear();
  334. foreach (var genre in Split(val, true))
  335. {
  336. audio.AddGenre(genre);
  337. }
  338. }
  339. }
  340. /// <summary>
  341. /// Gets the disc number, which is sometimes can be in the form of '1', or '1/3'
  342. /// </summary>
  343. /// <param name="tags">The tags.</param>
  344. /// <param name="tagName">Name of the tag.</param>
  345. /// <returns>System.Nullable{System.Int32}.</returns>
  346. private int? GetDictionaryDiscValue(Dictionary<string, string> tags, string tagName)
  347. {
  348. var disc = FFProbeHelpers.GetDictionaryValue(tags, tagName);
  349. if (!string.IsNullOrEmpty(disc))
  350. {
  351. disc = disc.Split('/')[0];
  352. int num;
  353. if (int.TryParse(disc, out num))
  354. {
  355. return num;
  356. }
  357. }
  358. return null;
  359. }
  360. }
  361. }