PhotoProvider.cs 7.2 KB

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