jellyfin/MediaBrowser.Controller/Providers/AudioInfoProvider.cs

266 lines
8.0 KiB
C#
Raw Normal View History

using MediaBrowser.Controller.FFMpeg;
using MediaBrowser.Controller.Library;
using MediaBrowser.Model.Entities;
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Threading.Tasks;
namespace MediaBrowser.Controller.Providers
{
[Export(typeof(BaseMetadataProvider))]
public class AudioInfoProvider : BaseMediaInfoProvider<Audio>
{
public override MetadataProviderPriority Priority
{
get { return MetadataProviderPriority.First; }
}
protected override string CacheDirectory
{
get { return Kernel.Instance.ApplicationPaths.FFProbeAudioCacheDirectory; }
2012-08-21 19:50:59 -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;
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;
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;
}
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 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))
{
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))
{
var list = audio.Studios ?? new List<string>();
2012-08-20 05:20:51 -07:00
list.AddRange(val.Split('/'));
audio.Studios = list;
}
}
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))
{
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;
}
}
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
public override async Task FetchAsync(BaseEntity item, ItemResolveEventArgs args)
2012-08-19 19:05:55 -07:00
{
await Task.Run(() =>
2012-08-22 05:56:44 -07:00
{
/*T myItem = item as T;
2012-08-22 05:56:44 -07:00
if (CanSkipFFProbe(myItem))
{
return;
}
2012-08-19 19:05:55 -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)
{
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-25 08:30:37 -07:00
if (stream.tags != null)
{
stream.tags = ConvertDictionaryToCaseInSensitive(stream.tags);
}
}
2012-08-19 19:05:55 -07:00
}
Fetch(myItem, result);*/
});
}
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
}
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;
}
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;
}
private Dictionary<string, string> ConvertDictionaryToCaseInSensitive(Dictionary<string, string> dict)
{
Dictionary<string, string> newDict = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
foreach (string key in dict.Keys)
{
newDict[key] = dict[key];
}
return newDict;
}
}
}