个人网站备案需要哪些资料,确定网站建设目的,科技对人类的意义,网站开发软件平台有哪些gif历史部分介绍请参考上一篇《GIF图片格式详解#xff08;一#xff09;》#xff0c;
格式部分详解参考 《GIF图片格式详解#xff08;二#xff09;》
或直接访问博客地址#xff1a;https://blog.whatsroot.xyz/2023/12/16/all-about-gif/
本篇介绍下用于处理gif图…gif历史部分介绍请参考上一篇《GIF图片格式详解一》
格式部分详解参考 《GIF图片格式详解二》
或直接访问博客地址https://blog.whatsroot.xyz/2023/12/16/all-about-gif/
本篇介绍下用于处理gif图片的命令行工具方便集成到代码中。
软件使用
首先是ffmpeg尽管它是一个音视频框架但是由于视频与动态图天然的联系ffmpeg在n2.6版本(2015年)就对GIF做了支持尽管支持图片或者视频转为gif,但是如果原视频或者图片带有透明通道,转换后透明通道会丢失。这个功能直到n4.0(2017年才开始支持。所以ffmpeg版本低于4.0的linux发行版比如ubuntu18.04均无法转换带透明通道的GIF,需要更新ffmpeg版本或者使用更新的linux发行版。不过尽管n4.0支持透明通道但是转换效果并不好会有部分透明边界问题使用时还是推荐升级到最新版本。
在n4.0中palattegen filter中开始添加透明通道
diff --git a/libavfilter/vf_palettegen.c b/libavfilter/vf_palettegen.c
index 03de317348..5ff73e6b2b 100644
--- a/libavfilter/vf_palettegen.cb/libavfilter/vf_palettegen.c-27,6 27,7 #include libavutil/internal.h#include libavutil/opt.h#include libavutil/qsort.h
#include libavutil/intreadwrite.h#include avfilter.h#include internal.h -74,6 75,7 typedef struct PaletteGenContext {struct range_box boxes[256]; // define the segmentation of the colorspace (the final palette)int nb_boxes; // number of boxes (increase will segmenting them)int palette_pushed; // if the palette frame is pushed into the outlink or notuint8_t transparency_color[4]; // background color for transparency} PaletteGenContext;#define OFFSET(x) offsetof(PaletteGenContext, x)-81,6 83,7 typedef struct PaletteGenContext {static const AVOption palettegen_options[] {{ max_colors, set the maximum number of colors to use in the palette, OFFSET(max_colors), AV_OPT_TYPE_INT, {.i64256}, 4, 256, FLAGS },{ reserve_transparent, reserve a palette entry for transparency, OFFSET(reserve_transparent), AV_OPT_TYPE_BOOL, {.i641}, 0, 1, FLAGS },{ transparency_color, set a background color for transparency, OFFSET(transparency_color), AV_OPT_TYPE_COLOR, {.strlime}, CHAR_MIN, CHAR_MAX, FLAGS },{ stats_mode, set statistics mode, OFFSET(stats_mode), AV_OPT_TYPE_INT, {.i64STATS_MODE_ALL_FRAMES}, 0, NB_STATS_MODE-1, FLAGS, mode },{ full, compute full frame histograms, 0, AV_OPT_TYPE_CONST, {.i64STATS_MODE_ALL_FRAMES}, INT_MIN, INT_MAX, FLAGS, mode },{ diff, compute histograms only for the part that differs from previous frame, 0, AV_OPT_TYPE_CONST, {.i64STATS_MODE_DIFF_FRAMES}, INT_MIN, INT_MAX, FLAGS, mode },-250,7 253,7 static void write_palette(AVFilterContext *ctx, AVFrame *out)if (s-reserve_transparent) {av_assert0(s-nb_boxes 256);
- pal[out-width - pal_linesize - 1] 0x0000ff00; // add a green transparent colorpal[out-width - pal_linesize - 1] AV_RB32(s-transparency_color) 8;}}使用ffmpeg转换图片并保留透明通道的方法
ffmpeg -i input.gif -vf split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse -gifflags transdiff -y out.gif#注意只有vf部分是必须的gifflags只是为了提高编码效率其次可以使用imagemagick工具进行转换此工具不仅支持gif动态图还支持webp动态图 比如使用imagemagick 6缩放gif则可简单使用
convert input.gif -resize 300x200 out.gifflagsimagemagic 7版本命令方式有所改变不过改变不大这里不再详细讨论