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

我可以用以下代码获取数据。 但运行速度太慢:

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

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

UPD:

我使用以下软件:

  • docker (20.10.7)
  • openresty/openresty:xenial (1.15.8.3)
  • luarocks (3.2.1)
  • LuaJIT (2.1.0-beta3)

这是带有UserComment字段的图片示例:[链接](https://drive.google.com/file/d/1Ie7lTBvc1BhbsUiiBRdDXwnOVdIGmcf8/view?usp=sharing)

Exiftool看到了这个属性:

$ exiftool -EXIF:UserComment Johnrogershousemay2020.webp
User Comment                    :{"foo":"bar"}
点赞
用户3342050
用户3342050

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

local handle = io.popen(“exiftool -EXIF:UserComment image.webp”)
2021-06-11 16:07:01
用户1847592
用户1847592

这个方法不是很优雅,但它不需要运行外部应用程序:-)

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))
2021-06-12 12:44:01