# FFmpeg PNGs to High Quality GIF ###### tags: `ffmpeg` ###### Version: ffmpeg 4.2.2 :::danger ffmpeg 以前的版本會有殘影的 bug ([fixed on 12 Dec 2018](https://superuser.com/questions/1413308/banding-trailing-while-resizing-a-transparent-gif-through-ffmpeg)) ::: ## Input ###### `frames/image-%09d.png` A sequence of high quality png with transparent background like this: ![](https://i.imgur.com/j5mduMn.png) ## Simple Solution ```bash ffmpeg -i frames/image-%09d.png output.gif ``` ###### Output ![](https://i.imgur.com/nDNxmLL.gif) ###### Problems - Transparent background become black - Image quality is not good ## High Quality Solution ```bash ffmpeg -i frames/image-%09d.png -filter_complex \ "split[s0][s1]; [s0]palettegen= stats_mode=single: transparency_color=000000[p]; [s1][p]paletteuse= new=1: alpha_threshold=10" output.gif ``` ![](https://i.imgur.com/wt3GFJB.gif) ### Filter Graph ``` [s0] [p] input --> split -----> palettegen ----> paletteuse --> output | ^ | [s1] | +--------------------------------+ ``` ### Filters GIF 檔案使用 256 色的調色盤來代表顏色,沒有「透明度」的概念,但是可以指定「透明色」(完全透明)。除了有一個 global 的調色盤以外,每個 frame 都可以有一個調色盤。 ###### `pallettegen` Generate pallete by the colors of source images. - **`stats_mode=single`**: generate a single pallette for each frame. - **`transparency_color=000000`**: 額外要指定成透明色的顏色 ###### `palleteuse` `[s][p]palleteuse` means to use `p` as the pallete of `s`. - **`new=1`**: use different pallete for each frame. 搭配 `palletegen=stats_mode=single` 使用。 - **`alpha_threshold=10`**: 原圖 alpha 值在多少以上時視為透明色。