2014-02-09 22:11:11 +01:00
|
|
|
|
using MediaBrowser.Common.Configuration;
|
2013-02-21 02:33:05 +01:00
|
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
|
using MediaBrowser.Controller.Entities.Audio;
|
2014-02-06 05:39:16 +01:00
|
|
|
|
using MediaBrowser.Controller.Library;
|
2014-02-20 17:37:41 +01:00
|
|
|
|
using MediaBrowser.Controller.MediaEncoding;
|
2013-12-06 04:39:44 +01:00
|
|
|
|
using MediaBrowser.Controller.Persistence;
|
2015-04-05 17:01:57 +02:00
|
|
|
|
using MediaBrowser.Model.Dlna;
|
2013-02-21 02:33:05 +01:00
|
|
|
|
using MediaBrowser.Model.Entities;
|
2014-06-17 03:56:23 +02:00
|
|
|
|
using MediaBrowser.Model.MediaInfo;
|
2014-02-09 22:11:11 +01:00
|
|
|
|
using MediaBrowser.Model.Serialization;
|
2015-06-28 19:00:36 +02:00
|
|
|
|
using System.Collections.Generic;
|
2014-02-06 05:39:16 +01:00
|
|
|
|
using System.Globalization;
|
2014-02-09 22:11:11 +01:00
|
|
|
|
using System.IO;
|
2013-02-21 02:33:05 +01:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
2013-08-15 17:25:51 +02:00
|
|
|
|
using System.Threading.Tasks;
|
2013-02-21 02:33:05 +01:00
|
|
|
|
|
2013-06-09 18:47:28 +02:00
|
|
|
|
namespace MediaBrowser.Providers.MediaInfo
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
2014-02-06 05:39:16 +01:00
|
|
|
|
class FFProbeAudioInfo
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
2014-02-06 05:39:16 +01:00
|
|
|
|
private readonly IMediaEncoder _mediaEncoder;
|
2013-12-06 04:39:44 +01:00
|
|
|
|
private readonly IItemRepository _itemRepo;
|
2014-02-09 22:11:11 +01:00
|
|
|
|
private readonly IApplicationPaths _appPaths;
|
|
|
|
|
private readonly IJsonSerializer _json;
|
2015-06-28 19:00:36 +02:00
|
|
|
|
private readonly ILibraryManager _libraryManager;
|
2013-12-06 04:39:44 +01:00
|
|
|
|
|
2014-02-06 05:39:16 +01:00
|
|
|
|
private readonly CultureInfo _usCulture = new CultureInfo("en-US");
|
2014-02-09 05:12:04 +01:00
|
|
|
|
|
2015-06-28 19:00:36 +02:00
|
|
|
|
public FFProbeAudioInfo(IMediaEncoder mediaEncoder, IItemRepository itemRepo, IApplicationPaths appPaths, IJsonSerializer json, ILibraryManager libraryManager)
|
2013-03-02 18:59:15 +01:00
|
|
|
|
{
|
2014-02-06 05:39:16 +01:00
|
|
|
|
_mediaEncoder = mediaEncoder;
|
2013-12-06 04:39:44 +01:00
|
|
|
|
_itemRepo = itemRepo;
|
2014-02-09 22:11:11 +01:00
|
|
|
|
_appPaths = appPaths;
|
|
|
|
|
_json = json;
|
2015-06-28 19:00:36 +02:00
|
|
|
|
_libraryManager = libraryManager;
|
2013-03-02 18:59:15 +01:00
|
|
|
|
}
|
|
|
|
|
|
2014-02-06 05:39:16 +01:00
|
|
|
|
public async Task<ItemUpdateType> Probe<T>(T item, CancellationToken cancellationToken)
|
|
|
|
|
where T : Audio
|
2013-06-18 21:16:27 +02:00
|
|
|
|
{
|
2014-12-29 21:18:48 +01:00
|
|
|
|
if (item.IsArchive)
|
|
|
|
|
{
|
|
|
|
|
var ext = Path.GetExtension(item.Path) ?? string.Empty;
|
|
|
|
|
item.Container = ext.TrimStart('.');
|
|
|
|
|
return ItemUpdateType.MetadataImport;
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-06 05:39:16 +01:00
|
|
|
|
var result = await GetMediaInfo(item, cancellationToken).ConfigureAwait(false);
|
2013-06-18 21:16:27 +02:00
|
|
|
|
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
|
2014-02-06 05:39:16 +01:00
|
|
|
|
await Fetch(item, cancellationToken, result).ConfigureAwait(false);
|
2013-06-18 21:16:27 +02:00
|
|
|
|
|
2014-02-06 05:39:16 +01:00
|
|
|
|
return ItemUpdateType.MetadataImport;
|
|
|
|
|
}
|
2013-06-18 21:16:27 +02:00
|
|
|
|
|
2015-05-10 06:29:04 +02:00
|
|
|
|
private const string SchemaVersion = "3";
|
2014-03-26 22:05:31 +01:00
|
|
|
|
|
2015-04-05 17:01:57 +02:00
|
|
|
|
private async Task<Model.MediaInfo.MediaInfo> GetMediaInfo(BaseItem item, CancellationToken cancellationToken)
|
2014-02-06 05:39:16 +01:00
|
|
|
|
{
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
|
2015-07-31 03:52:11 +02:00
|
|
|
|
//var idString = item.Id.ToString("N");
|
|
|
|
|
//var cachePath = Path.Combine(_appPaths.CachePath,
|
|
|
|
|
// "ffprobe-audio",
|
|
|
|
|
// idString.Substring(0, 2), idString, "v" + SchemaVersion + _mediaEncoder.Version + item.DateModified.Ticks.ToString(_usCulture) + ".json");
|
|
|
|
|
|
|
|
|
|
//try
|
|
|
|
|
//{
|
|
|
|
|
// return _json.DeserializeFromFile<Model.MediaInfo.MediaInfo>(cachePath);
|
|
|
|
|
//}
|
|
|
|
|
//catch (FileNotFoundException)
|
|
|
|
|
//{
|
|
|
|
|
|
|
|
|
|
//}
|
|
|
|
|
//catch (DirectoryNotFoundException)
|
|
|
|
|
//{
|
|
|
|
|
//}
|
2014-02-09 22:11:11 +01:00
|
|
|
|
|
2015-04-05 17:01:57 +02:00
|
|
|
|
var result = await _mediaEncoder.GetMediaInfo(new MediaInfoRequest
|
|
|
|
|
{
|
|
|
|
|
InputPath = item.Path,
|
|
|
|
|
MediaType = DlnaProfileType.Audio,
|
|
|
|
|
Protocol = MediaProtocol.File
|
2014-02-06 05:39:16 +01:00
|
|
|
|
|
2015-04-05 17:01:57 +02:00
|
|
|
|
}, cancellationToken).ConfigureAwait(false);
|
2014-02-09 22:11:11 +01:00
|
|
|
|
|
2015-07-31 03:52:11 +02:00
|
|
|
|
//Directory.CreateDirectory(Path.GetDirectoryName(cachePath));
|
|
|
|
|
//_json.SerializeToFile(result, cachePath);
|
2014-02-09 22:11:11 +01:00
|
|
|
|
|
|
|
|
|
return result;
|
2013-06-18 21:16:27 +02:00
|
|
|
|
}
|
|
|
|
|
|
2013-02-21 02:33:05 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Fetches the specified audio.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="audio">The audio.</param>
|
|
|
|
|
/// <param name="cancellationToken">The cancellation token.</param>
|
2015-04-04 21:35:29 +02:00
|
|
|
|
/// <param name="mediaInfo">The media information.</param>
|
2013-02-21 02:33:05 +01:00
|
|
|
|
/// <returns>Task.</returns>
|
2015-06-28 19:00:36 +02:00
|
|
|
|
protected async Task Fetch(Audio audio, CancellationToken cancellationToken, Model.MediaInfo.MediaInfo mediaInfo)
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
2014-04-22 19:25:54 +02:00
|
|
|
|
var mediaStreams = mediaInfo.MediaStreams;
|
2013-02-21 02:33:05 +01:00
|
|
|
|
|
2015-04-04 21:35:29 +02:00
|
|
|
|
audio.FormatName = mediaInfo.Container;
|
|
|
|
|
audio.TotalBitrate = mediaInfo.Bitrate;
|
2014-06-30 19:40:46 +02:00
|
|
|
|
audio.HasEmbeddedImage = mediaStreams.Any(i => i.Type == MediaStreamType.EmbeddedImage);
|
2013-12-06 04:39:44 +01:00
|
|
|
|
|
2015-04-04 21:35:29 +02:00
|
|
|
|
audio.RunTimeTicks = mediaInfo.RunTimeTicks;
|
|
|
|
|
audio.Size = mediaInfo.Size;
|
2013-05-15 06:05:52 +02:00
|
|
|
|
|
2015-04-04 21:35:29 +02:00
|
|
|
|
var extension = (Path.GetExtension(audio.Path) ?? string.Empty).TrimStart('.');
|
|
|
|
|
audio.Container = extension;
|
2014-01-12 00:07:56 +01:00
|
|
|
|
|
2015-06-28 19:00:36 +02:00
|
|
|
|
await FetchDataFromTags(audio, mediaInfo).ConfigureAwait(false);
|
2013-12-06 04:39:44 +01:00
|
|
|
|
|
2015-06-28 19:00:36 +02:00
|
|
|
|
await _itemRepo.SaveMediaStreams(audio.Id, mediaStreams, cancellationToken).ConfigureAwait(false);
|
2013-02-21 02:33:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Fetches data from the tags dictionary
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="audio">The audio.</param>
|
2015-04-04 21:35:29 +02:00
|
|
|
|
/// <param name="data">The data.</param>
|
2015-06-28 19:00:36 +02:00
|
|
|
|
private async Task FetchDataFromTags(Audio audio, Model.MediaInfo.MediaInfo data)
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
|
|
|
|
// Only set Name if title was found in the dictionary
|
2015-04-04 21:35:29 +02:00
|
|
|
|
if (!string.IsNullOrEmpty(data.Title))
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
2015-04-04 21:35:29 +02:00
|
|
|
|
audio.Name = data.Title;
|
2013-02-21 02:33:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
2013-08-04 02:59:23 +02:00
|
|
|
|
if (!audio.LockedFields.Contains(MetadataFields.Cast))
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
2015-06-28 19:00:36 +02:00
|
|
|
|
var people = new List<PersonInfo>();
|
2013-08-29 23:00:27 +02:00
|
|
|
|
|
2015-04-04 21:35:29 +02:00
|
|
|
|
foreach (var person in data.People)
|
2013-08-04 02:59:23 +02:00
|
|
|
|
{
|
2015-06-28 19:00:36 +02:00
|
|
|
|
PeopleHelper.AddPerson(people, new PersonInfo
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
2015-04-04 21:35:29 +02:00
|
|
|
|
Name = person.Name,
|
|
|
|
|
Type = person.Type,
|
|
|
|
|
Role = person.Role
|
|
|
|
|
});
|
2015-01-27 23:45:59 +01:00
|
|
|
|
}
|
2015-06-28 19:00:36 +02:00
|
|
|
|
|
|
|
|
|
await _libraryManager.UpdatePeople(audio, people).ConfigureAwait(false);
|
2015-03-13 02:55:22 +01:00
|
|
|
|
}
|
2014-06-23 18:05:19 +02:00
|
|
|
|
|
2015-04-04 21:35:29 +02:00
|
|
|
|
audio.Album = data.Album;
|
|
|
|
|
audio.Artists = data.Artists;
|
|
|
|
|
audio.AlbumArtists = data.AlbumArtists;
|
|
|
|
|
audio.IndexNumber = data.IndexNumber;
|
2015-04-09 19:30:18 +02:00
|
|
|
|
audio.ParentIndexNumber = data.ParentIndexNumber;
|
2015-04-04 21:35:29 +02:00
|
|
|
|
audio.ProductionYear = data.ProductionYear;
|
|
|
|
|
audio.PremiereDate = data.PremiereDate;
|
2013-02-21 02:33:05 +01:00
|
|
|
|
|
|
|
|
|
// If we don't have a ProductionYear try and get it from PremiereDate
|
|
|
|
|
if (audio.PremiereDate.HasValue && !audio.ProductionYear.HasValue)
|
|
|
|
|
{
|
2013-04-12 20:22:40 +02:00
|
|
|
|
audio.ProductionYear = audio.PremiereDate.Value.ToLocalTime().Year;
|
2013-02-21 02:33:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
2013-08-04 02:59:23 +02:00
|
|
|
|
if (!audio.LockedFields.Contains(MetadataFields.Genres))
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
2013-04-28 07:44:45 +02:00
|
|
|
|
audio.Genres.Clear();
|
|
|
|
|
|
2015-04-04 21:35:29 +02:00
|
|
|
|
foreach (var genre in data.Genres)
|
2013-04-28 07:44:45 +02:00
|
|
|
|
{
|
2013-09-06 00:59:07 +02:00
|
|
|
|
audio.AddGenre(genre);
|
2013-04-28 07:44:45 +02:00
|
|
|
|
}
|
2013-02-21 02:33:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
2015-04-04 21:35:29 +02:00
|
|
|
|
if (!audio.LockedFields.Contains(MetadataFields.Studios))
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
2015-04-04 21:35:29 +02:00
|
|
|
|
audio.Studios.Clear();
|
2013-02-21 02:33:05 +01:00
|
|
|
|
|
2015-04-04 21:35:29 +02:00
|
|
|
|
foreach (var studio in data.Studios)
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
2015-04-04 21:35:29 +02:00
|
|
|
|
audio.AddStudio(studio);
|
2013-02-21 02:33:05 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-04 21:35:29 +02:00
|
|
|
|
audio.SetProviderId(MetadataProviders.MusicBrainzAlbumArtist, data.GetProviderId(MetadataProviders.MusicBrainzAlbumArtist));
|
|
|
|
|
audio.SetProviderId(MetadataProviders.MusicBrainzArtist, data.GetProviderId(MetadataProviders.MusicBrainzArtist));
|
|
|
|
|
audio.SetProviderId(MetadataProviders.MusicBrainzAlbum, data.GetProviderId(MetadataProviders.MusicBrainzAlbum));
|
|
|
|
|
audio.SetProviderId(MetadataProviders.MusicBrainzReleaseGroup, data.GetProviderId(MetadataProviders.MusicBrainzReleaseGroup));
|
|
|
|
|
audio.SetProviderId(MetadataProviders.MusicBrainzTrack, data.GetProviderId(MetadataProviders.MusicBrainzTrack));
|
2013-02-21 02:33:05 +01:00
|
|
|
|
}
|
2014-02-06 05:39:16 +01:00
|
|
|
|
}
|
2013-02-21 02:33:05 +01:00
|
|
|
|
}
|