Today I found some PNG format animated emoticon packs online~~ I won't tell you that I stole them from LINE~~, so 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 am going to briefly introduce APNG and provide an online tool that can batch convert APNG to GIF. However, this tool cannot achieve infinite looping. Therefore, I shared a method to batch modify the loop count of GIF, using two different implementations: Node.js and batch processing script. This is convenient for Node developers and regular Windows users 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. 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:
-
Image quality: GIF supports up to 256 colors and does not support Alpha transparency channel, which leads to poor quality in color-rich images and images with semi-transparent effects. APNG can support higher quality images, including more colors and Alpha transparency channels, making the animation effects more delicate.
-
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.
-
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: https://xtaolink.cn/268.html
Batch Conversion of APNG to GIF#
This tool can batch convert APNG to GIF, but it cannot achieve infinite looping.
https://cdkm.com/cn/png-to-gif
Batch Modification of GIF for Infinite Looping#
bat (for regular 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
:modifyGif
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!"
echo.!modifiedData!>"%filePath%"
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 traverse 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 contents 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);
return;
}
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';
batchModifyGifFilesInDirectory(directoryPath);
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 traverse 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 the information of each modified file or any error messages that may occur.
For more information, please refer to: https://www.b612.me/golang/232.html
Better Tools#
This batch processing tool can batch convert multiple APNG files to GIF files and set them to loop infinitely.