2012-08-19 17:58:35 +02:00
|
|
|
|
using System;
|
2012-08-21 01:53:32 +02:00
|
|
|
|
using System.Collections.Generic;
|
2012-08-19 17:58:35 +02:00
|
|
|
|
using System.ComponentModel.Composition;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using MediaBrowser.Controller.Events;
|
|
|
|
|
using MediaBrowser.Controller.FFMpeg;
|
|
|
|
|
using MediaBrowser.Model.Entities;
|
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Controller.Providers
|
|
|
|
|
{
|
|
|
|
|
[Export(typeof(BaseMetadataProvider))]
|
|
|
|
|
public class AudioInfoProvider : BaseMetadataProvider
|
|
|
|
|
{
|
2012-08-19 22:38:31 +02:00
|
|
|
|
public override bool Supports(BaseEntity item)
|
2012-08-19 17:58:35 +02:00
|
|
|
|
{
|
|
|
|
|
return item is Audio;
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-20 17:55:05 +02:00
|
|
|
|
public override MetadataProviderPriority Priority
|
|
|
|
|
{
|
|
|
|
|
get { return MetadataProviderPriority.First; }
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-22 22:48:18 +02:00
|
|
|
|
public override async Task FetchAsync(BaseEntity item, ItemResolveEventArgs args)
|
2012-08-19 17:58:35 +02:00
|
|
|
|
{
|
2012-08-22 22:48:18 +02:00
|
|
|
|
await Task.Run(() =>
|
|
|
|
|
{
|
|
|
|
|
Audio audio = item as Audio;
|
2012-08-19 17:58:35 +02:00
|
|
|
|
|
2012-08-22 22:48:18 +02:00
|
|
|
|
Fetch(audio, FFProbe.Run(audio));
|
|
|
|
|
});
|
2012-08-22 04:50:59 +02:00
|
|
|
|
}
|
2012-08-19 17:58:35 +02:00
|
|
|
|
|
2012-08-22 04:50:59 +02:00
|
|
|
|
private void Fetch(Audio audio, FFProbeResult data)
|
|
|
|
|
{
|
2012-08-20 04:05:55 +02:00
|
|
|
|
MediaStream stream = data.streams.First(s => s.codec_type.Equals("audio", StringComparison.OrdinalIgnoreCase));
|
|
|
|
|
|
|
|
|
|
string bitrate = null;
|
|
|
|
|
string duration = null;
|
2012-08-19 17:58:35 +02:00
|
|
|
|
|
|
|
|
|
audio.Channels = stream.channels;
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(stream.sample_rate))
|
|
|
|
|
{
|
|
|
|
|
audio.SampleRate = int.Parse(stream.sample_rate);
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-20 04:05:55 +02:00
|
|
|
|
bitrate = stream.bit_rate;
|
|
|
|
|
duration = stream.duration;
|
|
|
|
|
|
2012-08-19 17:58:35 +02:00
|
|
|
|
if (string.IsNullOrEmpty(bitrate))
|
|
|
|
|
{
|
|
|
|
|
bitrate = data.format.bit_rate;
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-20 04:05:55 +02:00
|
|
|
|
if (string.IsNullOrEmpty(duration))
|
|
|
|
|
{
|
|
|
|
|
duration = data.format.duration;
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-19 17:58:35 +02:00
|
|
|
|
if (!string.IsNullOrEmpty(bitrate))
|
|
|
|
|
{
|
|
|
|
|
audio.BitRate = int.Parse(bitrate);
|
|
|
|
|
}
|
2012-08-20 04:05:55 +02: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 17:58:35 +02:00
|
|
|
|
}
|
|
|
|
|
|
2012-08-20 04:05:55 +02: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 14:20:51 +02:00
|
|
|
|
string composer = GetDictionaryValue(tags, "composer");
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(composer))
|
|
|
|
|
{
|
|
|
|
|
var list = (audio.People ?? new PersonInfo[] { }).ToList();
|
|
|
|
|
list.Add(new PersonInfo() { Name = composer, Type = "Composer" });
|
|
|
|
|
audio.People = list;
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-20 04:05:55 +02: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 14:20:51 +02: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))
|
|
|
|
|
{
|
|
|
|
|
var list = (audio.Studios ?? new string[] { }).ToList();
|
|
|
|
|
list.AddRange(val.Split('/'));
|
|
|
|
|
audio.Studios = list;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void FetchGenres(Audio audio, Dictionary<string, string> tags)
|
|
|
|
|
{
|
|
|
|
|
string val = GetDictionaryValue(tags, "genre");
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(val))
|
|
|
|
|
{
|
|
|
|
|
var list = (audio.Genres ?? new string[] { }).ToList();
|
|
|
|
|
list.AddRange(val.Split('/'));
|
|
|
|
|
audio.Genres = list;
|
|
|
|
|
}
|
2012-08-20 04:05:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private int? GetDictionaryDiscValue(Dictionary<string, string> tags)
|
|
|
|
|
{
|
2012-08-22 14:56:44 +02:00
|
|
|
|
string disc = GetDictionaryValue(tags, "disc");
|
2012-08-20 04:05:55 +02:00
|
|
|
|
|
2012-08-22 14:56:44 +02:00
|
|
|
|
if (!string.IsNullOrEmpty(disc))
|
2012-08-20 04:05:55 +02:00
|
|
|
|
{
|
2012-08-22 14:56:44 +02:00
|
|
|
|
disc = disc.Split('/')[0];
|
2012-08-20 04:05:55 +02:00
|
|
|
|
|
2012-08-22 14:56:44 +02:00
|
|
|
|
int num;
|
2012-08-20 04:05:55 +02:00
|
|
|
|
|
2012-08-22 14:56:44 +02:00
|
|
|
|
if (int.TryParse(disc, out num))
|
|
|
|
|
{
|
|
|
|
|
return num;
|
2012-08-20 04:05:55 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2012-08-20 14:20:51 +02:00
|
|
|
|
|
2012-08-22 14:56:44 +02:00
|
|
|
|
internal static string GetDictionaryValue(Dictionary<string, string> tags, string key)
|
2012-08-20 04:05:55 +02:00
|
|
|
|
{
|
2012-08-22 14:56:44 +02:00
|
|
|
|
if (tags == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-20 04:05:55 +02:00
|
|
|
|
string[] keys = tags.Keys.ToArray();
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < keys.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
string currentKey = keys[i];
|
|
|
|
|
|
|
|
|
|
if (key.Equals(currentKey, StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
return tags[currentKey];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private int? GetDictionaryNumericValue(Dictionary<string, string> tags, string key)
|
|
|
|
|
{
|
|
|
|
|
string val = GetDictionaryValue(tags, key);
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(val))
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
if (int.TryParse(val, out i))
|
|
|
|
|
{
|
|
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private DateTime? GetDictionaryDateTime(Dictionary<string, string> tags, string key)
|
|
|
|
|
{
|
|
|
|
|
string val = GetDictionaryValue(tags, key);
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(val))
|
|
|
|
|
{
|
|
|
|
|
DateTime i;
|
|
|
|
|
|
|
|
|
|
if (DateTime.TryParse(val, out i))
|
|
|
|
|
{
|
|
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2012-08-20 14:20:51 +02:00
|
|
|
|
|
2012-08-19 17:58:35 +02:00
|
|
|
|
private string GetOutputCachePath(BaseItem item)
|
|
|
|
|
{
|
|
|
|
|
string outputDirectory = Path.Combine(Kernel.Instance.ApplicationPaths.FFProbeAudioCacheDirectory, item.Id.ToString().Substring(0, 1));
|
|
|
|
|
|
|
|
|
|
return Path.Combine(outputDirectory, item.Id + "-" + item.DateModified.Ticks + ".js");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void Init()
|
|
|
|
|
{
|
|
|
|
|
base.Init();
|
|
|
|
|
|
2012-08-20 21:16:51 +02:00
|
|
|
|
EnsureCacheSubFolders(Kernel.Instance.ApplicationPaths.FFProbeAudioCacheDirectory);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal static void EnsureCacheSubFolders(string root)
|
|
|
|
|
{
|
2012-08-19 22:38:31 +02:00
|
|
|
|
// Do this now so that we don't have to do this on every operation, which would require us to create a lock in order to maintain thread-safety
|
2012-08-19 17:58:35 +02:00
|
|
|
|
for (int i = 0; i <= 9; i++)
|
|
|
|
|
{
|
2012-08-20 21:16:51 +02:00
|
|
|
|
EnsureDirectory(Path.Combine(root, i.ToString()));
|
2012-08-19 17:58:35 +02:00
|
|
|
|
}
|
|
|
|
|
|
2012-08-20 21:16:51 +02:00
|
|
|
|
EnsureDirectory(Path.Combine(root, "a"));
|
|
|
|
|
EnsureDirectory(Path.Combine(root, "b"));
|
|
|
|
|
EnsureDirectory(Path.Combine(root, "c"));
|
|
|
|
|
EnsureDirectory(Path.Combine(root, "d"));
|
|
|
|
|
EnsureDirectory(Path.Combine(root, "e"));
|
|
|
|
|
EnsureDirectory(Path.Combine(root, "f"));
|
2012-08-19 17:58:35 +02:00
|
|
|
|
}
|
|
|
|
|
|
2012-08-20 21:16:51 +02:00
|
|
|
|
private static void EnsureDirectory(string path)
|
2012-08-19 17:58:35 +02:00
|
|
|
|
{
|
|
|
|
|
if (!Directory.Exists(path))
|
|
|
|
|
{
|
|
|
|
|
Directory.CreateDirectory(path);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|