Last active
April 10, 2022 21:30
-
-
Save runeb/4587530 to your computer and use it in GitHub Desktop.
Adobe XMP parser for JPEG files, in Ruby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def xmp(filename) | |
xap = "http://ns.adobe.com/xap/1.0/\0".freeze | |
f = File.open(filename, 'r') | |
xml = nil | |
begin | |
return nil unless f.readbyte == 0xFF | |
return nil unless f.readbyte == 0xD8 | |
while !f.eof | |
case f.readbyte | |
when 0xFF | |
case f.readbyte | |
when 0xE1 | |
size = f.read(2).unpack('H*').first.hex | |
next unless size > xap.length | |
ns = f.read(xap.length) | |
if ns == xap | |
xml = f.read(size - xap.length - 2) | |
break | |
end | |
end | |
end | |
end | |
ensure | |
f.close | |
end | |
xml | |
end | |
require 'nokogiri' | |
puts Nokogiri::XML.parse xmp('yeah.jpg') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment