jellyfin-server/MediaBrowser.Providers/Photos/PhotoProvider.cs

145 lines
5.1 KiB
C#
Raw Normal View History

2014-02-19 16:24:06 +00:00
using MediaBrowser.Controller.Drawing;
using MediaBrowser.Controller.Entities;
2014-02-13 05:11:54 +00:00
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Logging;
using System;
using System.Linq;
2014-02-13 05:11:54 +00:00
using System.Threading;
using System.Threading.Tasks;
using TagLib;
using TagLib.IFD;
using TagLib.IFD.Entries;
using TagLib.IFD.Tags;
2014-02-13 05:11:54 +00:00
namespace MediaBrowser.Providers.Photos
{
public class PhotoProvider : ICustomMetadataProvider<Photo>, IHasChangeMonitor
{
private readonly ILogger _logger;
2014-02-19 16:24:06 +00:00
private readonly IImageProcessor _imageProcessor;
2014-02-13 05:11:54 +00:00
2014-02-19 16:24:06 +00:00
public PhotoProvider(ILogger logger, IImageProcessor imageProcessor)
2014-02-13 05:11:54 +00:00
{
_logger = logger;
2014-02-19 16:24:06 +00:00
_imageProcessor = imageProcessor;
2014-02-13 05:11:54 +00:00
}
2014-06-09 19:16:14 +00:00
public Task<ItemUpdateType> FetchAsync(Photo item, MetadataRefreshOptions options, CancellationToken cancellationToken)
2014-02-13 05:11:54 +00:00
{
item.SetImagePath(ImageType.Primary, item.Path);
2014-03-07 21:24:01 +00:00
item.SetImagePath(ImageType.Backdrop, item.Path);
2014-02-13 05:11:54 +00:00
// Examples: https://github.com/mono/taglib-sharp/blob/a5f6949a53d09ce63ee7495580d6802921a21f14/tests/fixtures/TagLib.Tests.Images/NullOrientationTest.cs
try
2014-02-13 05:11:54 +00:00
{
var file = File.Create(item.Path);
2014-02-13 05:11:54 +00:00
var image = file as TagLib.Image.File;
2014-02-13 05:11:54 +00:00
var tag = file.GetTag(TagTypes.TiffIFD) as IFDTag;
2014-02-13 05:11:54 +00:00
if (tag != null)
{
var structure = tag.Structure;
2014-02-13 05:11:54 +00:00
if (structure != null)
{
var exif = structure.GetEntry(0, (ushort)IFDEntryTag.ExifIFD) as SubIFDEntry;
2014-02-13 05:11:54 +00:00
if (exif != null)
2014-02-13 05:11:54 +00:00
{
var exifStructure = exif.Structure;
if (exifStructure != null)
{
var entry = exifStructure.GetEntry(0, (ushort)ExifEntryTag.ApertureValue) as RationalIFDEntry;
if (entry != null)
{
double val = entry.Value.Numerator;
val /= entry.Value.Denominator;
item.Aperture = val;
}
entry = exifStructure.GetEntry(0, (ushort)ExifEntryTag.ShutterSpeedValue) as RationalIFDEntry;
if (entry != null)
{
double val = entry.Value.Numerator;
val /= entry.Value.Denominator;
item.ShutterSpeed = val;
}
}
2014-02-13 05:11:54 +00:00
}
}
}
2014-02-13 05:11:54 +00:00
item.CameraManufacturer = image.ImageTag.Make;
item.CameraModel = image.ImageTag.Model;
2014-02-13 05:11:54 +00:00
var rating = image.ImageTag.Rating;
if (rating.HasValue)
{
item.CommunityRating = rating;
}
else
{
item.CommunityRating = null;
}
2014-02-19 16:24:06 +00:00
item.Overview = image.ImageTag.Comment;
2014-02-13 05:11:54 +00:00
if (!string.IsNullOrWhiteSpace(image.ImageTag.Title))
{
item.Name = image.ImageTag.Title;
2014-02-13 05:11:54 +00:00
}
var dateTaken = image.ImageTag.DateTime;
if (dateTaken.HasValue)
2014-02-13 05:11:54 +00:00
{
item.DateCreated = dateTaken.Value;
item.PremiereDate = dateTaken.Value;
item.ProductionYear = dateTaken.Value.Year;
2014-02-13 05:11:54 +00:00
}
var size = _imageProcessor.GetImageSize(item.Path);
item.Height = Convert.ToInt32(size.Height);
item.Width = Convert.ToInt32(size.Width);
item.Genres = image.ImageTag.Genres.ToList();
item.Tags = image.ImageTag.Keywords.ToList();
item.Software = image.ImageTag.Software;
Model.Drawing.ImageOrientation orientation;
if (Enum.TryParse(image.ImageTag.Orientation.ToString(), true, out orientation))
{
item.Orientation = orientation;
}
item.ExposureTime = image.ImageTag.ExposureTime;
item.FocalLength = image.ImageTag.FocalLength;
}
catch (Exception e)
{
_logger.ErrorException("Image Provider - Error reading image tag for {0}", e, item.Path);
}
2014-02-13 05:11:54 +00:00
const ItemUpdateType result = ItemUpdateType.ImageUpdate | ItemUpdateType.MetadataImport;
return Task.FromResult(result);
}
public string Name
{
get { return "Embedded Information"; }
}
public bool HasChanged(IHasMetadata item, IDirectoryService directoryService, DateTime date)
{
return item.DateModified > date;
}
}
}