PhotoProvider.cs 6.0 KB

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