是否有可以使用现有动画GIF文件以及设置/取消循环计数和循环标志的库?我有几个由FFMPEG产生的GIF文件,似乎无法将循环/循环计数标志设置为FFMPEG。因此需要某种GIF图像的后期处理。

最佳答案

解决方案(在scala中)非常简单:

  val raf = new RandomAccessFile(src, "rw")
  // skip GIF header, 6 bytes. Don't care of it much.
  raf.skipBytes(6)
  // don't need image dimension
  raf.skipBytes(4)
  val flags = raf.readUnsignedByte()
  val headerSize = 3 * (1 << ((flags & 7) + 1)) // 00000111 - size of color table
  val headerExists = flags & 128 // 10000000 - is there a color table at all
  // skip background color and pixel ratio
  raf.skipBytes(2)
  if (headerExists != 0) {
    raf.skipBytes(headerSize)
  }
  val signature = raf.readUnsignedShort()
  require(signature == 0x21ff)
  raf.skipBytes(13) // NETSCAPE 2.0
  raf.skipBytes(1) // GIF animation flag has to be 1
  ctx.loopCount.foreach {
    v =>
      raf.writeByte(v & 0xff)
      raf.writeByte((v >> 8) & 0xff)
  }
  raf.close()

10-05 22:43