Convert Animated PNG (APNG) to GIF with infinite loop

Today, I found some PNG format animated emojis online I won't tell you that I stole them from LINE, and then I learned about this format called APNG. Since WeChat and QQ do not support APNG, I converted APNG to GIF. After converting APNG to GIF, I found that it can only play once on WeChat, which led to the problem of how to batch modify the loop count of GIF.

So I'm going to give a brief introduction to APNG. And I provided an online tool that can batch convert APNG to GIF, but this tool cannot achieve infinite looping. So I shared a method to batch modify the loop count of GIF, which uses two different implementations: Node.js and batch processing scripts. It is convenient for Node developers and ordinary users using Windows to process in batches directly.

What is APNG?#

APNG (Animated Portable Network Graphics) is a bitmap animation extension of PNG, which can achieve dynamic image effects in PNG format. APNG has advantages over GIF in terms of image quality and detail representation, and with the increasing support for APNG in more and more browsers, it is expected to become one of the standards for the next generation of dynamic images. The main differences are as follows:

  1. Image quality: GIF supports up to 256 colors at most and does not support the Alpha transparency channel, which leads to poor quality in colorful images and images with semi-transparent effects. APNG can support higher quality images, including more colors and Alpha transparency channels, making the animation effect more delicate.

  2. Composition principle: APNG and GIF are both animations composed of multiple frames, but the composition principle of APNG allows for supernatural downward compatibility. The first frame of APNG is a standard PNG image, so even if the browser does not recognize the animation data after APNG, it can display the first frame without obstacles. If the browser supports APNG, it can play the subsequent frames to achieve animation effects.

  3. Browser support: Starting from Chrome 59, Chrome browser began to support APNG, making most browsers able to display APNG animations. Only IE browser does not support APNG.

For more information, please refer to:

Batch Convert APNG to GIF#

This tool can batch convert APNG to GIF, but it cannot loop infinitely.

Batch Modify GIF to Loop Infinitely#

bat (Please use this method for ordinary users)#

Below is the batch processing script (.bat) to achieve the same function:

@echo off
setlocal enabledelayedexpansion

set "directoryPath=C:\path\to\directory"

for /r "%directoryPath%" %%f in (*.gif) do (
    echo Modifying %%~nxf
    call :modifyGif "%%f"

exit /b

set "filePath=%~1"

set /p data=<"%filePath%"
set "index=!data:~0,16!"
set "modifiedData=!data:~0,16!!data:~16,1!!data:~17,1!!data:~19!"


exit /b

Please replace C:\path\to\directory with the actual directory path. Save the above code as a .bat file and double-click to run it. The script will iterate through all .gif files in the specified directory and modify them.

Please note that the functionality of batch processing scripts is relatively limited and cannot directly read binary files. The above script simulates reading file content by reading the first line of the file. When modifying the file, it directly writes the modified data to the file without binary operations. This method may not be suitable for all situations, especially when dealing with large files, there may be performance issues. If you need more complex binary file processing, consider using other programming languages or tools to implement it.

Node (Used by Nexmoe)#


const fs = require('fs');
const path = require('path');

function unlimitedGifRepetitions(path) {
  const data = fs.readFileSync(path);

  const index = data.indexOf(Buffer.from([0x21, 0xFF, 0x0B]));
  if (index < 0) {
    throw new Error(`Cannot find Gif Application Extension in ${path}`);

  data[index + 16] = 255;
  data[index + 17] = 255;

  return data;

function batchModifyGifFilesInDirectory(directoryPath) {
  fs.readdir(directoryPath, (err, files) => {
    if (err) {
      console.error('Error reading directory:', err);

    files.forEach(file => {
      const filePath = path.join(directoryPath, file);
      const fileExtension = path.extname(file);

      if (fileExtension === '.gif') {
        try {
          const modifiedData = unlimitedGifRepetitions(filePath);
          fs.writeFileSync(filePath, modifiedData);
          console.log(`Modified ${file}`);
        } catch (error) {
          console.error(`Error modifying ${file}:`, error);

const directoryPath = './path/to/directory';

Please note that the above code uses the file system module (fs) of Node.js to read and write files. In addition, you need to replace ./path/to/directory with the actual directory path. Before executing this script, make sure that Node.js is installed.

This script will batch iterate through all files in the specified directory and call the unlimitedGifRepetitions function to modify files with the .gif extension. The modified data will be written back to the original file. In the console output, you can see information about each modified file or any error messages that occur.

For more information, please refer to:

Better Tools#

This batch processing tool can batch convert multiple APNG files to GIF files and can set them to loop infinitely after conversion.

Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.