2012-09-08 12:05:18 -07:00
|
|
|
|
using MediaBrowser.Controller.FFMpeg;
|
|
|
|
|
using MediaBrowser.Controller.Library;
|
|
|
|
|
using MediaBrowser.Model.Entities;
|
|
|
|
|
using System;
|
2012-08-20 16:53:32 -07:00
|
|
|
|
using System.Collections.Generic;
|
2012-08-19 08:58:35 -07:00
|
|
|
|
using System.ComponentModel.Composition;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Controller.Providers
|
|
|
|
|
{
|
2012-09-08 12:05:18 -07:00
|
|
|
|
[Export(typeof(BaseMetadataProvider))]
|
2012-08-24 09:24:29 -07:00
|
|
|
|
public class AudioInfoProvider : BaseMediaInfoProvider<Audio>
|
2012-08-19 08:58:35 -07:00
|
|
|
|
{
|
2012-08-20 08:55:05 -07:00
|
|
|
|
public override MetadataProviderPriority Priority
|
|
|
|
|
{
|
|
|
|
|
get { return MetadataProviderPriority.First; }
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-24 09:24:29 -07:00
|
|
|
|
protected override string CacheDirectory
|
2012-08-19 08:58:35 -07:00
|
|
|
|
{
|
2012-08-24 09:24:29 -07:00
|
|
|
|
get { return Kernel.Instance.ApplicationPaths.FFProbeAudioCacheDirectory; }
|
2012-08-21 19:50:59 -07:00
|
|
|
|
}
|
2012-08-19 08:58:35 -07:00
|
|
|
|
|
2012-08-24 09:24:29 -07:00
|
|
|
|
protected override void Fetch(Audio audio, FFProbeResult data)
|
2012-08-21 19:50:59 -07:00
|
|
|
|
{
|
2012-08-19 19:05:55 -07:00
|
|
|
|
MediaStream stream = data.streams.First(s => s.codec_type.Equals("audio", StringComparison.OrdinalIgnoreCase));
|
|
|
|
|
|
|
|
|
|
string bitrate = null;
|
|
|
|
|
string duration = null;
|
2012-08-19 08:58:35 -07:00
|
|
|
|
|
|
|
|
|
audio.Channels = stream.channels;
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(stream.sample_rate))
|
|
|
|
|
{
|
|
|
|
|
audio.SampleRate = int.Parse(stream.sample_rate);
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-19 19:05:55 -07:00
|
|
|
|
bitrate = stream.bit_rate;
|
|
|
|
|
duration = stream.duration;
|
|
|
|
|
|
2012-08-19 08:58:35 -07:00
|
|
|
|
if (string.IsNullOrEmpty(bitrate))
|
|
|
|
|
{
|
|
|
|
|
bitrate = data.format.bit_rate;
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-19 19:05:55 -07:00
|
|
|
|
if (string.IsNullOrEmpty(duration))
|
|
|
|
|
{
|
|
|
|
|
duration = data.format.duration;
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-19 08:58:35 -07:00
|
|
|
|
if (!string.IsNullOrEmpty(bitrate))
|
|
|
|
|
{
|
|
|
|
|
audio.BitRate = int.Parse(bitrate);
|
|
|
|
|
}
|
2012-08-19 19:05:55 -07:00
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(duration))
|
|
|
|
|
{
|
|
|
|
|
audio.RunTimeTicks = TimeSpan.FromSeconds(double.Parse(duration)).Ticks;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (data.format.tags != null)
|
|
|
|
|
{
|
|
|
|
|
FetchDataFromTags(audio, data.format.tags);
|
|
|
|
|
}
|
2012-08-19 08:58:35 -07:00
|
|
|
|
}
|
|
|
|
|
|
2012-08-19 19:05:55 -07:00
|
|
|
|
private void FetchDataFromTags(Audio audio, Dictionary<string, string> tags)
|
|
|
|
|
{
|
|
|
|
|
string title = GetDictionaryValue(tags, "title");
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(title))
|
|
|
|
|
{
|
|
|
|
|
audio.Name = title;
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-20 05:20:51 -07:00
|
|
|
|
string composer = GetDictionaryValue(tags, "composer");
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(composer))
|
|
|
|
|
{
|
2012-08-24 09:24:29 -07:00
|
|
|
|
audio.AddPerson(new PersonInfo() { Name = composer, Type = "Composer" });
|
2012-08-20 05:20:51 -07:00
|
|
|
|
}
|
|
|
|
|
|
2012-08-19 19:05:55 -07:00
|
|
|
|
audio.Album = GetDictionaryValue(tags, "album");
|
|
|
|
|
audio.Artist = GetDictionaryValue(tags, "artist");
|
|
|
|
|
audio.AlbumArtist = GetDictionaryValue(tags, "albumartist") ?? GetDictionaryValue(tags, "album artist") ?? GetDictionaryValue(tags, "album_artist");
|
|
|
|
|
|
|
|
|
|
audio.IndexNumber = GetDictionaryNumericValue(tags, "track");
|
|
|
|
|
audio.ParentIndexNumber = GetDictionaryDiscValue(tags);
|
|
|
|
|
|
|
|
|
|
audio.Language = GetDictionaryValue(tags, "language");
|
|
|
|
|
|
|
|
|
|
audio.ProductionYear = GetDictionaryNumericValue(tags, "date");
|
|
|
|
|
|
|
|
|
|
audio.PremiereDate = GetDictionaryDateTime(tags, "retaildate") ?? GetDictionaryDateTime(tags, "retail date") ?? GetDictionaryDateTime(tags, "retail_date");
|
2012-08-20 05:20:51 -07:00
|
|
|
|
|
|
|
|
|
FetchGenres(audio, tags);
|
|
|
|
|
|
|
|
|
|
FetchStudios(audio, tags, "organization");
|
|
|
|
|
FetchStudios(audio, tags, "ensemble");
|
|
|
|
|
FetchStudios(audio, tags, "publisher");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void FetchStudios(Audio audio, Dictionary<string, string> tags, string tagName)
|
|
|
|
|
{
|
|
|
|
|
string val = GetDictionaryValue(tags, tagName);
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(val))
|
|
|
|
|
{
|
2012-08-24 05:45:54 -07:00
|
|
|
|
var list = audio.Studios ?? new List<string>();
|
2012-08-20 05:20:51 -07:00
|
|
|
|
list.AddRange(val.Split('/'));
|
|
|
|
|
audio.Studios = list;
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-08-24 09:24:29 -07:00
|
|
|
|
|
2012-08-20 05:20:51 -07:00
|
|
|
|
private void FetchGenres(Audio audio, Dictionary<string, string> tags)
|
|
|
|
|
{
|
|
|
|
|
string val = GetDictionaryValue(tags, "genre");
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(val))
|
|
|
|
|
{
|
2012-08-24 05:45:54 -07:00
|
|
|
|
var list = audio.Genres ?? new List<string>();
|
2012-08-20 05:20:51 -07:00
|
|
|
|
list.AddRange(val.Split('/'));
|
|
|
|
|
audio.Genres = list;
|
|
|
|
|
}
|
2012-08-19 19:05:55 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private int? GetDictionaryDiscValue(Dictionary<string, string> tags)
|
|
|
|
|
{
|
2012-08-22 05:56:44 -07:00
|
|
|
|
string disc = GetDictionaryValue(tags, "disc");
|
2012-08-19 19:05:55 -07:00
|
|
|
|
|
2012-08-22 05:56:44 -07:00
|
|
|
|
if (!string.IsNullOrEmpty(disc))
|
2012-08-19 19:05:55 -07:00
|
|
|
|
{
|
2012-08-22 05:56:44 -07:00
|
|
|
|
disc = disc.Split('/')[0];
|
2012-08-19 19:05:55 -07:00
|
|
|
|
|
2012-08-22 05:56:44 -07:00
|
|
|
|
int num;
|
2012-08-19 19:05:55 -07:00
|
|
|
|
|
2012-08-22 05:56:44 -07:00
|
|
|
|
if (int.TryParse(disc, out num))
|
|
|
|
|
{
|
|
|
|
|
return num;
|
2012-08-19 19:05:55 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2012-08-24 09:24:29 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public abstract class BaseMediaInfoProvider<T> : BaseMetadataProvider
|
|
|
|
|
where T : BaseItem
|
|
|
|
|
{
|
|
|
|
|
protected abstract string CacheDirectory { get; }
|
|
|
|
|
|
|
|
|
|
public override bool Supports(BaseEntity item)
|
|
|
|
|
{
|
|
|
|
|
return item is T;
|
|
|
|
|
}
|
2012-08-20 05:20:51 -07:00
|
|
|
|
|
2012-08-24 09:24:29 -07:00
|
|
|
|
public override async Task FetchAsync(BaseEntity item, ItemResolveEventArgs args)
|
2012-08-19 19:05:55 -07:00
|
|
|
|
{
|
2012-08-24 09:24:29 -07:00
|
|
|
|
await Task.Run(() =>
|
2012-08-22 05:56:44 -07:00
|
|
|
|
{
|
2012-09-08 12:05:18 -07:00
|
|
|
|
/*T myItem = item as T;
|
2012-08-22 05:56:44 -07:00
|
|
|
|
|
2012-08-24 09:24:29 -07:00
|
|
|
|
if (CanSkipFFProbe(myItem))
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2012-08-19 19:05:55 -07:00
|
|
|
|
|
2012-08-24 09:24:29 -07:00
|
|
|
|
FFProbeResult result = FFProbe.Run(myItem, CacheDirectory);
|
|
|
|
|
|
2012-08-25 08:30:37 -07:00
|
|
|
|
if (result == null)
|
|
|
|
|
{
|
|
|
|
|
Logger.LogInfo("Null FFProbeResult for {0} {1}", item.Id, item.Name);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (result.format != null && result.format.tags != null)
|
2012-08-24 09:24:29 -07:00
|
|
|
|
{
|
|
|
|
|
result.format.tags = ConvertDictionaryToCaseInSensitive(result.format.tags);
|
|
|
|
|
}
|
2012-08-19 19:05:55 -07:00
|
|
|
|
|
2012-08-25 08:30:37 -07:00
|
|
|
|
if (result.streams != null)
|
2012-08-19 19:05:55 -07:00
|
|
|
|
{
|
2012-08-25 08:30:37 -07:00
|
|
|
|
foreach (MediaStream stream in result.streams)
|
2012-08-24 09:24:29 -07:00
|
|
|
|
{
|
2012-08-25 08:30:37 -07:00
|
|
|
|
if (stream.tags != null)
|
|
|
|
|
{
|
|
|
|
|
stream.tags = ConvertDictionaryToCaseInSensitive(stream.tags);
|
|
|
|
|
}
|
2012-08-24 09:24:29 -07:00
|
|
|
|
}
|
2012-08-19 19:05:55 -07:00
|
|
|
|
}
|
2012-08-24 09:24:29 -07:00
|
|
|
|
|
2012-09-08 12:05:18 -07:00
|
|
|
|
Fetch(myItem, result);*/
|
2012-08-24 09:24:29 -07:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected abstract void Fetch(T item, FFProbeResult result);
|
|
|
|
|
|
|
|
|
|
protected virtual bool CanSkipFFProbe(T item)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected string GetDictionaryValue(Dictionary<string, string> tags, string key)
|
|
|
|
|
{
|
|
|
|
|
if (tags == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!tags.ContainsKey(key))
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return tags[key];
|
2012-08-19 19:05:55 -07:00
|
|
|
|
}
|
|
|
|
|
|
2012-08-24 09:24:29 -07:00
|
|
|
|
protected int? GetDictionaryNumericValue(Dictionary<string, string> tags, string key)
|
2012-08-19 19:05:55 -07:00
|
|
|
|
{
|
|
|
|
|
string val = GetDictionaryValue(tags, key);
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(val))
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
if (int.TryParse(val, out i))
|
|
|
|
|
{
|
|
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-24 09:24:29 -07:00
|
|
|
|
protected DateTime? GetDictionaryDateTime(Dictionary<string, string> tags, string key)
|
2012-08-19 19:05:55 -07:00
|
|
|
|
{
|
|
|
|
|
string val = GetDictionaryValue(tags, key);
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(val))
|
|
|
|
|
{
|
|
|
|
|
DateTime i;
|
|
|
|
|
|
|
|
|
|
if (DateTime.TryParse(val, out i))
|
|
|
|
|
{
|
2012-09-04 12:23:15 -07:00
|
|
|
|
return i.ToUniversalTime();
|
2012-08-19 19:05:55 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2012-08-24 09:24:29 -07:00
|
|
|
|
|
|
|
|
|
private Dictionary<string, string> ConvertDictionaryToCaseInSensitive(Dictionary<string, string> dict)
|
2012-08-19 08:58:35 -07:00
|
|
|
|
{
|
2012-08-24 09:24:29 -07:00
|
|
|
|
Dictionary<string, string> newDict = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
|
2012-08-19 08:58:35 -07:00
|
|
|
|
|
2012-08-24 09:24:29 -07:00
|
|
|
|
foreach (string key in dict.Keys)
|
2012-08-19 08:58:35 -07:00
|
|
|
|
{
|
2012-08-24 09:24:29 -07:00
|
|
|
|
newDict[key] = dict[key];
|
2012-08-19 08:58:35 -07:00
|
|
|
|
}
|
|
|
|
|
|
2012-08-24 09:24:29 -07:00
|
|
|
|
return newDict;
|
2012-08-19 08:58:35 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|