1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
|
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using MediaBrowser.Common.Logging;
using MediaBrowser.Controller.FFMpeg;
using MediaBrowser.Controller.Library;
using MediaBrowser.Model.Entities;
namespace MediaBrowser.Controller.Providers
{
[Export(typeof(BaseMetadataProvider))]
public class AudioInfoProvider : BaseMetadataProvider
{
public override bool Supports(BaseEntity item)
{
return item is Audio;
}
public override MetadataProviderPriority Priority
{
get { return MetadataProviderPriority.First; }
}
public override async Task FetchAsync(BaseEntity item, ItemResolveEventArgs args)
{
await Task.Run(() =>
{
Audio audio = item as Audio;
Fetch(audio, FFProbe.Run(audio));
});
}
private void Fetch(Audio audio, FFProbeResult data)
{
if (data == null)
{
Logger.LogInfo("Null FFProbeResult for {0} {1}", audio.Id, audio.Name);
return;
}
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);
}
bitrate = stream.bit_rate;
duration = stream.duration;
if (string.IsNullOrEmpty(bitrate))
{
bitrate = data.format.bit_rate;
}
if (string.IsNullOrEmpty(duration))
{
duration = data.format.duration;
}
if (!string.IsNullOrEmpty(bitrate))
{
audio.BitRate = int.Parse(bitrate);
}
if (!string.IsNullOrEmpty(duration))
{
audio.RunTimeTicks = TimeSpan.FromSeconds(double.Parse(duration)).Ticks;
}
if (data.format.tags != null)
{
FetchDataFromTags(audio, data.format.tags);
}
}
private void FetchDataFromTags(Audio audio, Dictionary<string, string> tags)
{
string title = GetDictionaryValue(tags, "title");
if (!string.IsNullOrEmpty(title))
{
audio.Name = title;
}
string composer = GetDictionaryValue(tags, "composer");
if (!string.IsNullOrEmpty(composer))
{
var list = audio.People ?? new List<PersonInfo>();
list.Add(new PersonInfo() { Name = composer, Type = "Composer" });
audio.People = list;
}
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");
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>();
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 List<string>();
list.AddRange(val.Split('/'));
audio.Genres = list;
}
}
private int? GetDictionaryDiscValue(Dictionary<string, string> tags)
{
string disc = GetDictionaryValue(tags, "disc");
if (!string.IsNullOrEmpty(disc))
{
disc = disc.Split('/')[0];
int num;
if (int.TryParse(disc, out num))
{
return num;
}
}
return null;
}
internal static string GetDictionaryValue(Dictionary<string, string> tags, string key)
{
if (tags == null)
{
return null;
}
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;
}
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();
EnsureCacheSubFolders(Kernel.Instance.ApplicationPaths.FFProbeAudioCacheDirectory);
}
internal static void EnsureCacheSubFolders(string root)
{
// 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
for (int i = 0; i <= 9; i++)
{
EnsureDirectory(Path.Combine(root, i.ToString()));
}
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"));
}
private static void EnsureDirectory(string path)
{
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
}
}
}
|