How to Convert GIF to APNG with FFmpeg
Converting an animated GIF to an Animated Portable Network Graphics (APNG) file using FFmpeg is an effective way to improve image quality, reduce file size, and retain alpha transparency. This guide demonstrates the fundamental FFmpeg commands required to execute this conversion, configure loop settings, and preserve proper color formatting.
The Basic Conversion Command
To convert an animated GIF into an APNG file using default settings, open your terminal or command prompt and run the following command:
ffmpeg -i input.gif output.apngFFmpeg automatically detects the input format, decodes the GIF frames, and re-encodes them into the APNG container.
Maintaining Transparency and Color Accuracy
GIFs often have harsh edges around transparent areas because they
only support 1-bit transparency (a pixel is either fully transparent or
fully opaque). APNG supports full 8-bit alpha channels. To ensure FFmpeg
preserves or properly translates the color profile and alpha
transparency, specify the rgba pixel format:
ffmpeg -i input.gif -pix_fmt rgba output.apngControlling Loop Count
By default, an APNG file may not loop continuously unless specified.
You can control the number of repetitions using the -plays
option:
- Infinite loop: Set
-plays 0 - Play once and stop: Set
-plays 1 - Custom loop count: Replace
0with the desired number of loops
Example for an infinitely looping APNG:
ffmpeg -i input.gif -plays 0 -pix_fmt rgba output.apngAdjusting Frame Rate and Speed
If you want to alter the playback frame rate during conversion, use
the -r option:
ffmpeg -i input.gif -r 30 -plays 0 -pix_fmt rgba output.apngThis sets the output frame rate to 30 frames per second.
Batch Converting Multiple GIFs
To convert all GIF files in a directory to APNG format, use a shell loop:
On Linux or macOS (Bash/Zsh):
for f in *.gif; do
ffmpeg -i "$f" -plays 0 -pix_fmt rgba "${f%.gif}.apng"
doneOn Windows (Command Prompt):
for %i in (*.gif) do ffmpeg -i "%i" -plays 0 -pix_fmt rgba "%~ni.apng"