如何获取 lua 中的 webp 图像 EXIF 元数据?

How to get webp image EXIF metadata in lua?

我可以用下面的代码得到这个数据。但是运行速度太慢:

local handle = io.popen("exiftool image.webp")
local result = handle:read("*a")
handle:close()

是否有更优雅的方式获取元数据?

更新:

我使用这个软件:

这是一个带有 UserComment 字段的图片示例:link

Exiftool 看到这个 属性:

$ exiftool -EXIF:UserComment Johnrogershousemay2020.webp  
User Comment                    : {"foo":"bar"}  

如果您只需要 'UserComment',则在调用 popen 时将其作为参数传递:

local handle = io.popen("exiftool -EXIF:UserComment image.webp")

这种方法不太优雅,但不需要运行外部应用:-)

function get_webp_user_comment(file_name)
   local file = io.open(file_name, "rb")
   local exif_offset, exif_found, is_big_endian, user_comment = 12

   local function read_string(offset, size)
      file:seek("set", offset)
      return file:read(size)
   end

   local function read_uint(offset, size)
      local n, s = 0, read_string(offset, size)
      for j = 1, size do
         n = n * 256 + s:byte(is_big_endian and j or size + 1 - j)
      end
      return n
   end

   local function read_uint32(disp)
      return read_uint(exif_offset + disp, 4)
   end

   local function read_uint16(disp)
      return read_uint(exif_offset + disp, 2)
   end

   local function search_for_tag(ifd_disp, tag)
      if ifd_disp ~= 0 then
         local entry_disp = ifd_disp + 2
         for j = 1, read_uint16(ifd_disp) do
            if read_uint16(entry_disp) == tag then
               return read_uint32(entry_disp + 8), read_uint32(entry_disp + 4)
            end
            entry_disp = entry_disp + 12
         end
         return search_for_tag(read_uint32(entry_disp), tag)
      end
   end

   if read_string(0, 4) == "RIFF" and read_string(8, 4) == "WEBP" then
      local max_offset = read_uint32(-8)
      while exif_offset < max_offset do
         local section_name = read_string(exif_offset, 4)
         exif_offset = exif_offset + 8
         if section_name == "EXIF" then
            local endianness = read_string(exif_offset, 2)
            is_big_endian = endianness == "MM"
            exif_found = is_big_endian or endianness == "II"
            if exif_found then
               break
            end
         end
         exif_offset = exif_offset + read_uint32(-4)
         exif_offset = exif_offset + exif_offset % 2
      end
   end
   if exif_found then
      local exif_ifd = search_for_tag(read_uint32(4), 0x8769)
      if exif_ifd then
         local disp, count = search_for_tag(exif_ifd, 0x9286)
         user_comment = read_string(exif_offset + disp + 8, count - 8)
      end
   end
   file:close()
   return user_comment
end

用法示例:

local file_name = "path/to/Johnrogershousemay2020.webp"
print(get_webp_user_comment(file_name))