2013-09-17 07:32:31 -07:00
|
|
|
|
using MediaBrowser.Common.Extensions;
|
|
|
|
|
using MediaBrowser.Common.MediaInfo;
|
2013-03-03 22:43:06 -07:00
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
2013-02-20 18:33:05 -07:00
|
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
|
using MediaBrowser.Controller.Entities.Audio;
|
|
|
|
|
using MediaBrowser.Model.Entities;
|
2013-04-07 13:55:05 -07:00
|
|
|
|
using MediaBrowser.Model.Logging;
|
|
|
|
|
using MediaBrowser.Model.Serialization;
|
2013-02-20 18:33:05 -07:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
2013-08-15 08:25:51 -07:00
|
|
|
|
using System.Threading.Tasks;
|
2013-02-20 18:33:05 -07:00
|
|
|
|
|
2013-06-09 09:47:28 -07:00
|
|
|
|
namespace MediaBrowser.Providers.MediaInfo
|
2013-02-20 18:33:05 -07:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Extracts audio information using ffprobe
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class FFProbeAudioInfoProvider : BaseFFProbeProvider<Audio>
|
|
|
|
|
{
|
2013-04-07 15:09:48 -07:00
|
|
|
|
public FFProbeAudioInfoProvider(ILogManager logManager, IServerConfigurationManager configurationManager, IMediaEncoder mediaEncoder, IJsonSerializer jsonSerializer)
|
|
|
|
|
: base(logManager, configurationManager, mediaEncoder, jsonSerializer)
|
2013-03-02 10:59:15 -07:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-18 12:16:27 -07:00
|
|
|
|
public override async Task<bool> FetchAsync(BaseItem item, bool force, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
var myItem = (Audio)item;
|
|
|
|
|
|
|
|
|
|
OnPreFetch(myItem, null);
|
|
|
|
|
|
|
|
|
|
var result = await GetMediaInfo(item, null, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
|
|
|
|
|
NormalizeFFProbeResult(result);
|
|
|
|
|
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
|
|
|
|
|
Fetch(myItem, cancellationToken, result);
|
|
|
|
|
|
|
|
|
|
SetLastRefreshed(item, DateTime.UtcNow);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-20 18:33:05 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Fetches the specified audio.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="audio">The audio.</param>
|
|
|
|
|
/// <param name="cancellationToken">The cancellation token.</param>
|
|
|
|
|
/// <param name="data">The data.</param>
|
|
|
|
|
/// <param name="isoMount">The iso mount.</param>
|
|
|
|
|
/// <returns>Task.</returns>
|
2013-06-18 12:16:27 -07:00
|
|
|
|
protected void Fetch(Audio audio, CancellationToken cancellationToken, MediaInfoResult data)
|
2013-02-20 18:33:05 -07:00
|
|
|
|
{
|
2013-04-05 18:03:38 -07:00
|
|
|
|
if (data.streams == null)
|
2013-02-20 18:33:05 -07:00
|
|
|
|
{
|
2013-04-05 18:03:38 -07:00
|
|
|
|
Logger.Error("Audio item has no streams: " + audio.Path);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2013-02-20 18:33:05 -07:00
|
|
|
|
|
2013-04-29 08:06:31 -07:00
|
|
|
|
audio.MediaStreams = data.streams.Select(s => GetMediaStream(s, data.format))
|
|
|
|
|
.Where(i => i != null)
|
|
|
|
|
.ToList();
|
2013-02-20 18:33:05 -07:00
|
|
|
|
|
2013-04-05 18:03:38 -07:00
|
|
|
|
// Get the first audio stream
|
2013-05-14 21:05:52 -07:00
|
|
|
|
var stream = data.streams.FirstOrDefault(s => s.codec_type.Equals("audio", StringComparison.OrdinalIgnoreCase));
|
2013-02-20 18:33:05 -07:00
|
|
|
|
|
2013-05-14 21:05:52 -07:00
|
|
|
|
if (stream != null)
|
2013-04-05 18:03:38 -07:00
|
|
|
|
{
|
2013-05-14 21:05:52 -07:00
|
|
|
|
// Get duration from stream properties
|
|
|
|
|
var duration = stream.duration;
|
2013-02-20 18:33:05 -07:00
|
|
|
|
|
2013-05-14 21:05:52 -07:00
|
|
|
|
// If it's not there go into format properties
|
|
|
|
|
if (string.IsNullOrEmpty(duration))
|
|
|
|
|
{
|
|
|
|
|
duration = data.format.duration;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If we got something, parse it
|
|
|
|
|
if (!string.IsNullOrEmpty(duration))
|
|
|
|
|
{
|
|
|
|
|
audio.RunTimeTicks = TimeSpan.FromSeconds(double.Parse(duration, UsCulture)).Ticks;
|
|
|
|
|
}
|
2013-04-05 18:03:38 -07:00
|
|
|
|
}
|
2013-02-20 18:33:05 -07:00
|
|
|
|
|
2013-04-05 18:03:38 -07:00
|
|
|
|
if (data.format.tags != null)
|
|
|
|
|
{
|
|
|
|
|
FetchDataFromTags(audio, data.format.tags);
|
|
|
|
|
}
|
2013-02-20 18:33:05 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Fetches data from the tags dictionary
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="audio">The audio.</param>
|
|
|
|
|
/// <param name="tags">The tags.</param>
|
|
|
|
|
private void FetchDataFromTags(Audio audio, Dictionary<string, string> tags)
|
|
|
|
|
{
|
|
|
|
|
var title = GetDictionaryValue(tags, "title");
|
|
|
|
|
|
|
|
|
|
// Only set Name if title was found in the dictionary
|
|
|
|
|
if (!string.IsNullOrEmpty(title))
|
|
|
|
|
{
|
|
|
|
|
audio.Name = title;
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-03 17:59:23 -07:00
|
|
|
|
if (!audio.LockedFields.Contains(MetadataFields.Cast))
|
2013-02-20 18:33:05 -07:00
|
|
|
|
{
|
2013-08-29 14:00:27 -07:00
|
|
|
|
audio.People.Clear();
|
|
|
|
|
|
2013-08-03 17:59:23 -07:00
|
|
|
|
var composer = GetDictionaryValue(tags, "composer");
|
2013-02-20 18:33:05 -07:00
|
|
|
|
|
2013-08-03 17:59:23 -07:00
|
|
|
|
if (!string.IsNullOrWhiteSpace(composer))
|
|
|
|
|
{
|
2013-09-17 07:32:31 -07:00
|
|
|
|
foreach (var person in Split(composer))
|
2013-02-20 18:33:05 -07:00
|
|
|
|
{
|
2013-09-05 15:59:07 -07:00
|
|
|
|
audio.AddPerson(new PersonInfo { Name = person, Type = PersonType.Composer });
|
2013-02-20 18:33:05 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
audio.Album = GetDictionaryValue(tags, "album");
|
|
|
|
|
|
2013-09-05 12:00:50 -07:00
|
|
|
|
var artist = GetDictionaryValue(tags, "artist");
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(artist))
|
|
|
|
|
{
|
|
|
|
|
audio.Artists.Clear();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2013-09-17 07:32:31 -07:00
|
|
|
|
audio.Artists = SplitArtists(artist)
|
2013-09-05 12:00:50 -07:00
|
|
|
|
.Distinct(StringComparer.OrdinalIgnoreCase)
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
}
|
2013-02-20 18:33:05 -07:00
|
|
|
|
|
|
|
|
|
// Several different forms of albumartist
|
|
|
|
|
audio.AlbumArtist = GetDictionaryValue(tags, "albumartist") ?? GetDictionaryValue(tags, "album artist") ?? GetDictionaryValue(tags, "album_artist");
|
|
|
|
|
|
|
|
|
|
// Track number
|
2013-09-08 09:49:05 -07:00
|
|
|
|
audio.IndexNumber = GetDictionaryDiscValue(tags, "track");
|
2013-02-20 18:33:05 -07:00
|
|
|
|
|
|
|
|
|
// Disc number
|
2013-09-08 09:49:05 -07:00
|
|
|
|
audio.ParentIndexNumber = GetDictionaryDiscValue(tags, "disc");
|
2013-02-20 18:33:05 -07:00
|
|
|
|
|
|
|
|
|
audio.Language = GetDictionaryValue(tags, "language");
|
|
|
|
|
|
|
|
|
|
audio.ProductionYear = GetDictionaryNumericValue(tags, "date");
|
|
|
|
|
|
|
|
|
|
// Several different forms of retaildate
|
|
|
|
|
audio.PremiereDate = GetDictionaryDateTime(tags, "retaildate") ?? GetDictionaryDateTime(tags, "retail date") ?? GetDictionaryDateTime(tags, "retail_date");
|
|
|
|
|
|
|
|
|
|
// If we don't have a ProductionYear try and get it from PremiereDate
|
|
|
|
|
if (audio.PremiereDate.HasValue && !audio.ProductionYear.HasValue)
|
|
|
|
|
{
|
2013-04-12 11:22:40 -07:00
|
|
|
|
audio.ProductionYear = audio.PremiereDate.Value.ToLocalTime().Year;
|
2013-02-20 18:33:05 -07:00
|
|
|
|
}
|
|
|
|
|
|
2013-08-03 17:59:23 -07:00
|
|
|
|
if (!audio.LockedFields.Contains(MetadataFields.Genres))
|
|
|
|
|
{
|
|
|
|
|
FetchGenres(audio, tags);
|
|
|
|
|
}
|
2013-02-20 18:33:05 -07:00
|
|
|
|
|
2013-08-03 17:59:23 -07:00
|
|
|
|
if (!audio.LockedFields.Contains(MetadataFields.Studios))
|
|
|
|
|
{
|
2013-09-05 12:00:50 -07:00
|
|
|
|
audio.Studios.Clear();
|
|
|
|
|
|
2013-08-03 17:59:23 -07:00
|
|
|
|
// There's several values in tags may or may not be present
|
|
|
|
|
FetchStudios(audio, tags, "organization");
|
|
|
|
|
FetchStudios(audio, tags, "ensemble");
|
|
|
|
|
FetchStudios(audio, tags, "publisher");
|
|
|
|
|
}
|
2013-02-20 18:33:05 -07:00
|
|
|
|
}
|
|
|
|
|
|
2013-09-05 12:00:50 -07:00
|
|
|
|
private readonly char[] _nameDelimiters = new[] { '/', '|', ';', '\\' };
|
|
|
|
|
|
2013-04-20 18:02:16 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Splits the specified val.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="val">The val.</param>
|
|
|
|
|
/// <returns>System.String[][].</returns>
|
2013-09-17 07:32:31 -07:00
|
|
|
|
private IEnumerable<string> Split(string val)
|
2013-04-20 18:02:16 -07:00
|
|
|
|
{
|
|
|
|
|
// Only use the comma as a delimeter if there are no slashes or pipes.
|
|
|
|
|
// We want to be careful not to split names that have commas in them
|
2013-09-17 07:32:31 -07:00
|
|
|
|
var delimeter = _nameDelimiters.Any(i => val.IndexOf(i) != -1) ? _nameDelimiters : new[] { ',' };
|
|
|
|
|
|
|
|
|
|
return val.Split(delimeter, StringSplitOptions.RemoveEmptyEntries)
|
|
|
|
|
.Where(i => !string.IsNullOrWhiteSpace(i))
|
|
|
|
|
.Select(i => i.Trim());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private const string ArtistReplaceValue = " | ";
|
|
|
|
|
|
|
|
|
|
private IEnumerable<string> SplitArtists(string val)
|
|
|
|
|
{
|
|
|
|
|
val = val.Replace(" featuring ", ArtistReplaceValue, StringComparison.OrdinalIgnoreCase)
|
|
|
|
|
.Replace(" feat. ", ArtistReplaceValue, StringComparison.OrdinalIgnoreCase);
|
|
|
|
|
|
|
|
|
|
// Only use the comma as a delimeter if there are no slashes or pipes.
|
|
|
|
|
// We want to be careful not to split names that have commas in them
|
|
|
|
|
var delimeter = _nameDelimiters;
|
2013-04-20 18:02:16 -07:00
|
|
|
|
|
2013-09-05 12:00:50 -07:00
|
|
|
|
return val.Split(delimeter, StringSplitOptions.RemoveEmptyEntries)
|
2013-09-05 15:59:07 -07:00
|
|
|
|
.Where(i => !string.IsNullOrWhiteSpace(i))
|
|
|
|
|
.Select(i => i.Trim());
|
2013-04-20 18:02:16 -07:00
|
|
|
|
}
|
|
|
|
|
|
2013-02-20 18:33:05 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the studios from the tags collection
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="audio">The audio.</param>
|
|
|
|
|
/// <param name="tags">The tags.</param>
|
|
|
|
|
/// <param name="tagName">Name of the tag.</param>
|
|
|
|
|
private void FetchStudios(Audio audio, Dictionary<string, string> tags, string tagName)
|
|
|
|
|
{
|
|
|
|
|
var val = GetDictionaryValue(tags, tagName);
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(val))
|
|
|
|
|
{
|
2013-06-11 09:10:41 -07:00
|
|
|
|
// Sometimes the artist name is listed here, account for that
|
2013-09-17 07:32:31 -07:00
|
|
|
|
var studios = Split(val).Where(i => !audio.HasArtist(i));
|
2013-04-27 22:44:45 -07:00
|
|
|
|
|
|
|
|
|
foreach (var studio in studios)
|
|
|
|
|
{
|
2013-09-05 15:59:07 -07:00
|
|
|
|
audio.AddStudio(studio);
|
2013-04-27 22:44:45 -07:00
|
|
|
|
}
|
2013-02-20 18:33:05 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the genres from the tags collection
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="audio">The audio.</param>
|
|
|
|
|
/// <param name="tags">The tags.</param>
|
|
|
|
|
private void FetchGenres(Audio audio, Dictionary<string, string> tags)
|
|
|
|
|
{
|
|
|
|
|
var val = GetDictionaryValue(tags, "genre");
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(val))
|
|
|
|
|
{
|
2013-04-27 22:44:45 -07:00
|
|
|
|
audio.Genres.Clear();
|
|
|
|
|
|
2013-09-17 07:32:31 -07:00
|
|
|
|
foreach (var genre in Split(val))
|
2013-04-27 22:44:45 -07:00
|
|
|
|
{
|
2013-09-05 15:59:07 -07:00
|
|
|
|
audio.AddGenre(genre);
|
2013-04-27 22:44:45 -07:00
|
|
|
|
}
|
2013-02-20 18:33:05 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the disc number, which is sometimes can be in the form of '1', or '1/3'
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="tags">The tags.</param>
|
2013-09-08 09:49:05 -07:00
|
|
|
|
/// <param name="tagName">Name of the tag.</param>
|
2013-02-20 18:33:05 -07:00
|
|
|
|
/// <returns>System.Nullable{System.Int32}.</returns>
|
2013-09-08 09:49:05 -07:00
|
|
|
|
private int? GetDictionaryDiscValue(Dictionary<string, string> tags, string tagName)
|
2013-02-20 18:33:05 -07:00
|
|
|
|
{
|
2013-09-08 09:49:05 -07:00
|
|
|
|
var disc = GetDictionaryValue(tags, tagName);
|
2013-02-20 18:33:05 -07:00
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(disc))
|
|
|
|
|
{
|
|
|
|
|
disc = disc.Split('/')[0];
|
|
|
|
|
|
|
|
|
|
int num;
|
|
|
|
|
|
|
|
|
|
if (int.TryParse(disc, out num))
|
|
|
|
|
{
|
|
|
|
|
return num;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|