PhotoProvider.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. var file = File.Create(item.Path);
  30. var image = file as TagLib.Image.File;
  31. var tag = file.GetTag(TagTypes.TiffIFD) as IFDTag;
  32. if (tag != null)
  33. {
  34. var structure = tag.Structure;
  35. if (structure != null)
  36. {
  37. var exif = structure.GetEntry(0, (ushort)IFDEntryTag.ExifIFD) as SubIFDEntry;
  38. if (exif != null)
  39. {
  40. var exifStructure = exif.Structure;
  41. if (exifStructure != null)
  42. {
  43. var entry = exifStructure.GetEntry(0, (ushort)ExifEntryTag.ApertureValue) as RationalIFDEntry;
  44. if (entry != null)
  45. {
  46. double val = entry.Value.Numerator;
  47. val /= entry.Value.Denominator;
  48. item.Aperture = val;
  49. }
  50. entry = exifStructure.GetEntry(0, (ushort)ExifEntryTag.ShutterSpeedValue) as RationalIFDEntry;
  51. if (entry != null)
  52. {
  53. double val = entry.Value.Numerator;
  54. val /= entry.Value.Denominator;
  55. item.ShutterSpeed = val;
  56. }
  57. }
  58. }
  59. }
  60. }
  61. item.CameraMake = image.ImageTag.Make;
  62. item.CameraModel = image.ImageTag.Model;
  63. item.Width = image.Properties.PhotoWidth;
  64. item.Height = image.Properties.PhotoHeight;
  65. var rating = image.ImageTag.Rating;
  66. if (rating.HasValue)
  67. {
  68. item.CommunityRating = rating;
  69. }
  70. else
  71. {
  72. item.CommunityRating = null;
  73. }
  74. item.Overview = image.ImageTag.Comment;
  75. if (!string.IsNullOrWhiteSpace(image.ImageTag.Title))
  76. {
  77. item.Name = image.ImageTag.Title;
  78. }
  79. var dateTaken = image.ImageTag.DateTime;
  80. if (dateTaken.HasValue)
  81. {
  82. item.DateCreated = dateTaken.Value;
  83. item.PremiereDate = dateTaken.Value;
  84. item.ProductionYear = dateTaken.Value.Year;
  85. }
  86. item.Genres = image.ImageTag.Genres.ToList();
  87. item.Tags = image.ImageTag.Keywords.ToList();
  88. item.Software = image.ImageTag.Software;
  89. if (image.ImageTag.Orientation == TagLib.Image.ImageOrientation.None)
  90. {
  91. item.Orientation = null;
  92. }
  93. else
  94. {
  95. Model.Drawing.ImageOrientation orientation;
  96. if (Enum.TryParse(image.ImageTag.Orientation.ToString(), true, out orientation))
  97. {
  98. item.Orientation = orientation;
  99. }
  100. }
  101. item.ExposureTime = image.ImageTag.ExposureTime;
  102. item.FocalLength = image.ImageTag.FocalLength;
  103. item.Latitude = image.ImageTag.Latitude;
  104. item.Longitude = image.ImageTag.Longitude;
  105. item.Altitude = image.ImageTag.Altitude;
  106. if (image.ImageTag.ISOSpeedRatings.HasValue)
  107. {
  108. item.IsoSpeedRating = Convert.ToInt32(image.ImageTag.ISOSpeedRatings.Value);
  109. }
  110. else
  111. {
  112. item.IsoSpeedRating = null;
  113. }
  114. }
  115. catch (Exception e)
  116. {
  117. _logger.ErrorException("Image Provider - Error reading image tag for {0}", e, item.Path);
  118. }
  119. const ItemUpdateType result = ItemUpdateType.ImageUpdate | ItemUpdateType.MetadataImport;
  120. return Task.FromResult(result);
  121. }
  122. public string Name
  123. {
  124. get { return "Embedded Information"; }
  125. }
  126. public bool HasChanged(IHasMetadata item, MetadataStatus status, IDirectoryService directoryService)
  127. {
  128. if (status.ItemDateModified.HasValue)
  129. {
  130. return status.ItemDateModified.Value != item.DateModified;
  131. }
  132. return false;
  133. }
  134. }
  135. }