PhotoProvider.cs 7.9 KB

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