PhotoProvider.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using Jellyfin.Extensions;
  7. using MediaBrowser.Controller.Drawing;
  8. using MediaBrowser.Controller.Entities;
  9. using MediaBrowser.Controller.Library;
  10. using MediaBrowser.Controller.Providers;
  11. using MediaBrowser.Model.Drawing;
  12. using MediaBrowser.Model.Entities;
  13. using Microsoft.Extensions.Logging;
  14. using TagLib;
  15. using TagLib.IFD;
  16. using TagLib.IFD.Entries;
  17. using TagLib.IFD.Tags;
  18. namespace Emby.Photos;
  19. /// <summary>
  20. /// Metadata provider for photos.
  21. /// </summary>
  22. public class PhotoProvider : ICustomMetadataProvider<Photo>, IForcedProvider, IHasItemChangeMonitor
  23. {
  24. private readonly ILogger<PhotoProvider> _logger;
  25. private readonly IImageProcessor _imageProcessor;
  26. // Other extensions might cause taglib to hang
  27. private readonly string[] _includeExtensions = [".jpg", ".jpeg", ".png", ".tiff", ".cr2", ".webp", ".avif"];
  28. /// <summary>
  29. /// Initializes a new instance of the <see cref="PhotoProvider" /> class.
  30. /// </summary>
  31. /// <param name="logger">The logger.</param>
  32. /// <param name="imageProcessor">The image processor.</param>
  33. public PhotoProvider(ILogger<PhotoProvider> logger, IImageProcessor imageProcessor)
  34. {
  35. _logger = logger;
  36. _imageProcessor = imageProcessor;
  37. }
  38. /// <inheritdoc />
  39. public string Name => "Embedded Information";
  40. /// <inheritdoc />
  41. public bool HasChanged(BaseItem item, IDirectoryService directoryService)
  42. {
  43. if (item.IsFileProtocol)
  44. {
  45. var file = directoryService.GetFile(item.Path);
  46. return file is not null && file.LastWriteTimeUtc != item.DateModified;
  47. }
  48. return false;
  49. }
  50. /// <inheritdoc />
  51. public Task<ItemUpdateType> FetchAsync(Photo item, MetadataRefreshOptions options, CancellationToken cancellationToken)
  52. {
  53. item.SetImagePath(ImageType.Primary, item.Path);
  54. // Examples: https://github.com/mono/taglib-sharp/blob/a5f6949a53d09ce63ee7495580d6802921a21f14/tests/fixtures/TagLib.Tests.Images/NullOrientationTest.cs
  55. if (_includeExtensions.Contains(Path.GetExtension(item.Path.AsSpan()), StringComparison.OrdinalIgnoreCase))
  56. {
  57. try
  58. {
  59. using var file = TagLib.File.Create(item.Path);
  60. if (file.GetTag(TagTypes.TiffIFD) is IFDTag tag)
  61. {
  62. var structure = tag.Structure;
  63. if (structure?.GetEntry(0, (ushort)IFDEntryTag.ExifIFD) is SubIFDEntry exif)
  64. {
  65. var exifStructure = exif.Structure;
  66. if (exifStructure is not null)
  67. {
  68. if (exifStructure.GetEntry(0, (ushort)ExifEntryTag.ApertureValue) is RationalIFDEntry apertureEntry)
  69. {
  70. item.Aperture = (double)apertureEntry.Value.Numerator / apertureEntry.Value.Denominator;
  71. }
  72. if (exifStructure.GetEntry(0, (ushort)ExifEntryTag.ShutterSpeedValue) is RationalIFDEntry shutterSpeedEntry)
  73. {
  74. item.ShutterSpeed = (double)shutterSpeedEntry.Value.Numerator / shutterSpeedEntry.Value.Denominator;
  75. }
  76. }
  77. }
  78. }
  79. if (file is TagLib.Image.File image)
  80. {
  81. item.CameraMake = image.ImageTag.Make;
  82. item.CameraModel = image.ImageTag.Model;
  83. item.Width = image.Properties.PhotoWidth;
  84. item.Height = image.Properties.PhotoHeight;
  85. item.CommunityRating = image.ImageTag.Rating;
  86. item.Overview = image.ImageTag.Comment;
  87. if (!string.IsNullOrWhiteSpace(image.ImageTag.Title)
  88. && !item.LockedFields.Contains(MetadataField.Name))
  89. {
  90. item.Name = image.ImageTag.Title;
  91. }
  92. var dateTaken = image.ImageTag.DateTime;
  93. if (dateTaken.HasValue)
  94. {
  95. item.DateCreated = dateTaken.Value;
  96. item.PremiereDate = dateTaken.Value;
  97. item.ProductionYear = dateTaken.Value.Year;
  98. }
  99. item.Genres = image.ImageTag.Genres;
  100. item.Tags = image.ImageTag.Keywords;
  101. item.Software = image.ImageTag.Software;
  102. if (image.ImageTag.Orientation == TagLib.Image.ImageOrientation.None)
  103. {
  104. item.Orientation = null;
  105. }
  106. else if (Enum.TryParse(image.ImageTag.Orientation.ToString(), true, out ImageOrientation orientation))
  107. {
  108. item.Orientation = orientation;
  109. }
  110. item.ExposureTime = image.ImageTag.ExposureTime;
  111. item.FocalLength = image.ImageTag.FocalLength;
  112. item.Latitude = image.ImageTag.Latitude;
  113. item.Longitude = image.ImageTag.Longitude;
  114. item.Altitude = image.ImageTag.Altitude;
  115. if (image.ImageTag.ISOSpeedRatings.HasValue)
  116. {
  117. item.IsoSpeedRating = Convert.ToInt32(image.ImageTag.ISOSpeedRatings.Value);
  118. }
  119. else
  120. {
  121. item.IsoSpeedRating = null;
  122. }
  123. }
  124. }
  125. catch (Exception ex)
  126. {
  127. _logger.LogError(ex, "Image Provider - Error reading image tag for {0}", item.Path);
  128. }
  129. }
  130. if (item.Width <= 0 || item.Height <= 0)
  131. {
  132. var img = item.GetImageInfo(ImageType.Primary, 0);
  133. try
  134. {
  135. var size = _imageProcessor.GetImageDimensions(item, img);
  136. if (size.Width > 0 && size.Height > 0)
  137. {
  138. item.Width = size.Width;
  139. item.Height = size.Height;
  140. }
  141. }
  142. catch (ArgumentException)
  143. {
  144. // format not supported
  145. }
  146. }
  147. const ItemUpdateType Result = ItemUpdateType.ImageUpdate | ItemUpdateType.MetadataImport;
  148. return Task.FromResult(Result);
  149. }
  150. }