2019-01-13 19:54:44 +00:00
|
|
|
using System;
|
2016-10-31 07:42:14 +00:00
|
|
|
using System.IO;
|
2016-10-26 18:25:03 +00:00
|
|
|
using System.Linq;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
2021-12-20 12:31:07 +00:00
|
|
|
using Jellyfin.Extensions;
|
2017-09-22 20:33:01 +00:00
|
|
|
using MediaBrowser.Controller.Drawing;
|
2016-10-26 18:25:03 +00:00
|
|
|
using MediaBrowser.Controller.Entities;
|
2014-02-13 05:11:54 +00:00
|
|
|
using MediaBrowser.Controller.Library;
|
|
|
|
using MediaBrowser.Controller.Providers;
|
2019-01-13 20:46:33 +00:00
|
|
|
using MediaBrowser.Model.Drawing;
|
2014-02-13 05:11:54 +00:00
|
|
|
using MediaBrowser.Model.Entities;
|
2018-12-13 13:18:25 +00:00
|
|
|
using Microsoft.Extensions.Logging;
|
2014-08-28 01:27:46 +00:00
|
|
|
using TagLib;
|
|
|
|
using TagLib.IFD;
|
|
|
|
using TagLib.IFD.Entries;
|
|
|
|
using TagLib.IFD.Tags;
|
2014-02-13 05:11:54 +00:00
|
|
|
|
2024-03-07 19:19:00 +00:00
|
|
|
namespace Emby.Photos;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Metadata provider for photos.
|
|
|
|
/// </summary>
|
|
|
|
public class PhotoProvider : ICustomMetadataProvider<Photo>, IForcedProvider, IHasItemChangeMonitor
|
2014-02-13 05:11:54 +00:00
|
|
|
{
|
2024-03-07 19:19:00 +00:00
|
|
|
private readonly ILogger<PhotoProvider> _logger;
|
|
|
|
private readonly IImageProcessor _imageProcessor;
|
|
|
|
|
|
|
|
// These are causing taglib to hang
|
|
|
|
private readonly string[] _includeExtensions = [".jpg", ".jpeg", ".png", ".tiff", ".cr2", ".webp", ".avif"];
|
|
|
|
|
2019-10-09 15:10:16 +00:00
|
|
|
/// <summary>
|
2024-03-07 19:19:00 +00:00
|
|
|
/// Initializes a new instance of the <see cref="PhotoProvider" /> class.
|
2019-10-09 15:10:16 +00:00
|
|
|
/// </summary>
|
2024-03-07 19:19:00 +00:00
|
|
|
/// <param name="logger">The logger.</param>
|
|
|
|
/// <param name="imageProcessor">The image processor.</param>
|
|
|
|
public PhotoProvider(ILogger<PhotoProvider> logger, IImageProcessor imageProcessor)
|
2014-02-13 05:11:54 +00:00
|
|
|
{
|
2024-03-07 19:19:00 +00:00
|
|
|
_logger = logger;
|
|
|
|
_imageProcessor = imageProcessor;
|
|
|
|
}
|
2014-02-13 05:11:54 +00:00
|
|
|
|
2024-03-07 19:19:00 +00:00
|
|
|
/// <inheritdoc />
|
|
|
|
public string Name => "Embedded Information";
|
2019-05-11 09:55:41 +00:00
|
|
|
|
2024-03-07 19:19:00 +00:00
|
|
|
/// <inheritdoc />
|
|
|
|
public bool HasChanged(BaseItem item, IDirectoryService directoryService)
|
|
|
|
{
|
|
|
|
if (item.IsFileProtocol)
|
2018-09-12 17:26:21 +00:00
|
|
|
{
|
2024-03-07 19:19:00 +00:00
|
|
|
var file = directoryService.GetFile(item.Path);
|
|
|
|
return file is not null && file.LastWriteTimeUtc != item.DateModified;
|
2018-09-12 17:26:21 +00:00
|
|
|
}
|
|
|
|
|
2024-03-07 19:19:00 +00:00
|
|
|
return false;
|
|
|
|
}
|
2014-02-13 05:11:54 +00:00
|
|
|
|
2024-03-07 19:19:00 +00:00
|
|
|
/// <inheritdoc />
|
|
|
|
public Task<ItemUpdateType> FetchAsync(Photo item, MetadataRefreshOptions options, CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
item.SetImagePath(ImageType.Primary, item.Path);
|
|
|
|
|
|
|
|
// Examples: https://github.com/mono/taglib-sharp/blob/a5f6949a53d09ce63ee7495580d6802921a21f14/tests/fixtures/TagLib.Tests.Images/NullOrientationTest.cs
|
|
|
|
if (_includeExtensions.Contains(Path.GetExtension(item.Path.AsSpan()), StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
try
|
2014-02-13 05:11:54 +00:00
|
|
|
{
|
2024-03-07 19:19:00 +00:00
|
|
|
using var file = TagLib.File.Create(item.Path);
|
|
|
|
if (file.GetTag(TagTypes.TiffIFD) is IFDTag tag)
|
2014-08-28 01:27:46 +00:00
|
|
|
{
|
2024-03-07 19:19:00 +00:00
|
|
|
var structure = tag.Structure;
|
|
|
|
if (structure?.GetEntry(0, (ushort)IFDEntryTag.ExifIFD) is SubIFDEntry exif)
|
2014-08-28 01:27:46 +00:00
|
|
|
{
|
2024-03-07 19:19:00 +00:00
|
|
|
var exifStructure = exif.Structure;
|
|
|
|
if (exifStructure is not null)
|
2018-12-11 13:55:33 +00:00
|
|
|
{
|
2024-03-07 19:19:00 +00:00
|
|
|
if (exifStructure.GetEntry(0, (ushort)ExifEntryTag.ApertureValue) is RationalIFDEntry apertureEntry)
|
2014-08-28 01:27:46 +00:00
|
|
|
{
|
2024-03-07 19:19:00 +00:00
|
|
|
item.Aperture = (double)apertureEntry.Value.Numerator / apertureEntry.Value.Denominator;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (exifStructure.GetEntry(0, (ushort)ExifEntryTag.ShutterSpeedValue) is RationalIFDEntry shutterSpeedEntry)
|
|
|
|
{
|
|
|
|
item.ShutterSpeed = (double)shutterSpeedEntry.Value.Numerator / shutterSpeedEntry.Value.Denominator;
|
2014-08-28 01:27:46 +00:00
|
|
|
}
|
2018-12-11 13:55:33 +00:00
|
|
|
}
|
2024-03-07 19:19:00 +00:00
|
|
|
}
|
|
|
|
}
|
2014-02-13 05:11:54 +00:00
|
|
|
|
2024-03-07 19:19:00 +00:00
|
|
|
if (file is TagLib.Image.File image)
|
|
|
|
{
|
|
|
|
item.CameraMake = image.ImageTag.Make;
|
|
|
|
item.CameraModel = image.ImageTag.Model;
|
2015-03-12 14:51:41 +00:00
|
|
|
|
2024-03-07 19:19:00 +00:00
|
|
|
item.Width = image.Properties.PhotoWidth;
|
|
|
|
item.Height = image.Properties.PhotoHeight;
|
2014-02-19 16:24:06 +00:00
|
|
|
|
2024-03-07 19:19:00 +00:00
|
|
|
item.CommunityRating = image.ImageTag.Rating;
|
2014-02-13 05:11:54 +00:00
|
|
|
|
2024-03-07 19:19:00 +00:00
|
|
|
item.Overview = image.ImageTag.Comment;
|
2014-08-28 01:27:46 +00:00
|
|
|
|
2024-03-07 19:19:00 +00:00
|
|
|
if (!string.IsNullOrWhiteSpace(image.ImageTag.Title)
|
|
|
|
&& !item.LockedFields.Contains(MetadataField.Name))
|
|
|
|
{
|
|
|
|
item.Name = image.ImageTag.Title;
|
|
|
|
}
|
2014-02-13 05:11:54 +00:00
|
|
|
|
2024-03-07 19:19:00 +00:00
|
|
|
var dateTaken = image.ImageTag.DateTime;
|
|
|
|
if (dateTaken.HasValue)
|
|
|
|
{
|
|
|
|
item.DateCreated = dateTaken.Value;
|
|
|
|
item.PremiereDate = dateTaken.Value;
|
|
|
|
item.ProductionYear = dateTaken.Value.Year;
|
|
|
|
}
|
2014-08-28 01:27:46 +00:00
|
|
|
|
2024-03-07 19:19:00 +00:00
|
|
|
item.Genres = image.ImageTag.Genres;
|
|
|
|
item.Tags = image.ImageTag.Keywords;
|
|
|
|
item.Software = image.ImageTag.Software;
|
2015-09-14 17:39:35 +00:00
|
|
|
|
2024-03-07 19:19:00 +00:00
|
|
|
if (image.ImageTag.Orientation == TagLib.Image.ImageOrientation.None)
|
|
|
|
{
|
|
|
|
item.Orientation = null;
|
|
|
|
}
|
|
|
|
else if (Enum.TryParse(image.ImageTag.Orientation.ToString(), true, out ImageOrientation orientation))
|
|
|
|
{
|
|
|
|
item.Orientation = orientation;
|
|
|
|
}
|
2014-08-28 01:27:46 +00:00
|
|
|
|
2024-03-07 19:19:00 +00:00
|
|
|
item.ExposureTime = image.ImageTag.ExposureTime;
|
|
|
|
item.FocalLength = image.ImageTag.FocalLength;
|
2014-08-30 14:26:29 +00:00
|
|
|
|
2024-03-07 19:19:00 +00:00
|
|
|
item.Latitude = image.ImageTag.Latitude;
|
|
|
|
item.Longitude = image.ImageTag.Longitude;
|
|
|
|
item.Altitude = image.ImageTag.Altitude;
|
2014-08-30 14:26:29 +00:00
|
|
|
|
2024-03-07 19:19:00 +00:00
|
|
|
if (image.ImageTag.ISOSpeedRatings.HasValue)
|
|
|
|
{
|
|
|
|
item.IsoSpeedRating = Convert.ToInt32(image.ImageTag.ISOSpeedRatings.Value);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
item.IsoSpeedRating = null;
|
2015-09-14 17:39:35 +00:00
|
|
|
}
|
2017-09-22 20:33:01 +00:00
|
|
|
}
|
2014-08-28 01:27:46 +00:00
|
|
|
}
|
2024-03-07 19:19:00 +00:00
|
|
|
catch (Exception ex)
|
2014-08-28 01:27:46 +00:00
|
|
|
{
|
2024-03-07 19:19:00 +00:00
|
|
|
_logger.LogError(ex, "Image Provider - Error reading image tag for {0}", item.Path);
|
|
|
|
}
|
|
|
|
}
|
2017-09-22 20:33:01 +00:00
|
|
|
|
2024-03-07 19:19:00 +00:00
|
|
|
if (item.Width <= 0 || item.Height <= 0)
|
|
|
|
{
|
|
|
|
var img = item.GetImageInfo(ImageType.Primary, 0);
|
2018-09-12 17:26:21 +00:00
|
|
|
|
2024-03-07 19:19:00 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
var size = _imageProcessor.GetImageDimensions(item, img);
|
|
|
|
|
|
|
|
if (size.Width > 0 && size.Height > 0)
|
2017-10-22 06:22:43 +00:00
|
|
|
{
|
2024-03-07 19:19:00 +00:00
|
|
|
item.Width = size.Width;
|
|
|
|
item.Height = size.Height;
|
2017-10-22 06:22:43 +00:00
|
|
|
}
|
2014-08-28 01:27:46 +00:00
|
|
|
}
|
2024-03-07 19:19:00 +00:00
|
|
|
catch (ArgumentException)
|
|
|
|
{
|
|
|
|
// format not supported
|
|
|
|
}
|
2014-02-13 05:11:54 +00:00
|
|
|
}
|
2024-03-07 19:19:00 +00:00
|
|
|
|
|
|
|
const ItemUpdateType Result = ItemUpdateType.ImageUpdate | ItemUpdateType.MetadataImport;
|
|
|
|
return Task.FromResult(Result);
|
2014-02-13 05:11:54 +00:00
|
|
|
}
|
|
|
|
}
|