ffmpeg 编码 png apng图片

1. 搭建环境
首先需要搭建ubuntu下,ffmpeg开发环境,这个网上有很多在这里就不多叙述了
2.  定义编码器相关的结构体
typedef struct Encode_PNG_Key{AVFormatContext* pFormatCtx;  AVOutputFormat* fmt;  AVStream* video_st; AVCodecContext* pCodecCtx;    AVCodec* pCodec;
} Encode_Png_key;

3. 初始化编码器

这个需要你特别注意,再找编码器格式的千万不要av_guess_format(NULL,  "out.png", NULL);
中间的png找到的是JPEG的编码器,通过阅读源码很容易发现,所以如果你确定是编码png,切记要下边方式找到输出的编码器
int encode_png_init(Encode_Png_key *args, char *filename, int width, int height, const enum AVPixelFormat pix_fmt)
{int ret = 0;if (args == NULL || filename == NULL )return -1;args->pFormatCtx = avformat_alloc_context();//Guess formatargs->fmt = av_guess_format(NULL, NULL, "image/png");if (args->fmt == NULL){ret = -1;dm_printf("av_guess_format fail.");goto end;}args->pFormatCtx->oformat = args->fmt;//Output URLif (avio_open(&args->pFormatCtx->pb, filename, AVIO_FLAG_READ_WRITE) < 0){dm_printf("Couldn't open output file.");ret = -1;goto end;}//Method 2. More simple//avformat_alloc_output_context2(&pFormatCtx, NULL, NULL, out_file);//fmt = pFormatCtx->oformat;args->video_st = avformat_new_stream(args->pFormatCtx, 0);if (args->video_st == NULL){ret = -1;goto end;}args->pCodecCtx = args->video_st->codec;args->pCodecCtx->codec_id = args->fmt->video_codec;args->pCodecCtx->codec_type = AVMEDIA_TYPE_VIDEO;args->pCodecCtx->pix_fmt = pix_fmt;args->pCodecCtx->width = width;args->pCodecCtx->height = height;args->pCodecCtx->time_base.num = 1;args->pCodecCtx->time_base.den = 25;//Output some informationav_dump_format(args->pFormatCtx, 0, filename, 1);args->pCodec = avcodec_find_encoder(args->pCodecCtx->codec_id);if (!args->pCodec){dm_printf("Codec not found.");ret = -1;goto end;}if (avcodec_open2(args->pCodecCtx, args->pCodec, NULL) < 0){dm_printf("Could not open codec.");ret = -1;goto end;}//Write Headeravformat_write_header(args->pFormatCtx, NULL);
end:if (ret < 0){if (args->video_st){avcodec_close(args->video_st->codec);}if (args->pFormatCtx != NULL && args->pFormatCtx->pb != NULL){avio_close(args->pFormatCtx->pb);}if (args->pFormatCtx != NULL)avformat_free_context(args->pFormatCtx);memset(args, 0, sizeof(Encode_Png_key));}return ret;
}

4,编码一桢图片

int encode_png(Encode_Png_key *args, AVFrame *frame)
{int ret = -1;int got_frame = 0;AVPacket  packet;memset(&packet, 0, sizeof(packet));if (args == NULL  || args->pCodecCtx == NULL){dm_printf("args == NULL || filt_frame== NULL || args->pCodecCtx == NULL");return -1;}av_init_packet(&packet);packet.data = NULL;//Encoderet = avcodec_encode_video2( args->pCodecCtx, &packet, frame, &got_frame);if(ret < 0){dm_printf("Encode Error.\n");return -1;}if (got_frame == 1){packet.stream_index =  args->video_st->index;ret = av_write_frame( args->pFormatCtx, &packet);}elsevflush_encoder(args->pFormatCtx, 0);av_free_packet(&packet);args->pCodecCtx->frame_number = 0;return ret;
}
5, 释放资源
int encode_png_release(Encode_Png_key *args)
{if (args != NULL){if (args->pFormatCtx != NULL){//Write Trailerav_write_trailer(args->pFormatCtx);}if (args->video_st){avcodec_close(args->video_st->codec);}if (args->pFormatCtx != NULL && args->pFormatCtx->pb != NULL){avio_close(args->pFormatCtx->pb);}if (args->pFormatCtx != NULL)avformat_free_context(args->pFormatCtx);memset(args, 0, sizeof(Encode_Png_key));}return 0;
}

另外apng是无损压缩的png动图,这个比gif动态效果好,非常适合手机端动图开发

ffmpeg 编码 png apng图片相关推荐

  1. 利用FFmpeg编码器将JPG图片进行H.264编码原理

    利用FFmpeg编码器将JPG图片进行H.264编码原理 文章目录 利用FFmpeg编码器将JPG图片进行H.264编码原理 整体的编码流程 将JPG或BMP编码为YUV 利用FFmpeg将YUV格式 ...

  2. 使用ffmpeg 的 filter 给图片添加水印

    使用ffmpeg 的 filter 给图片添加水印. main.c #include <stdio.h>#include <libavfilter/avfilter.h> #i ...

  3. 【FFmpeg编码实战】(2)将YUV420P图片集编码成H.264视频文件(方法二)

    [FFmpeg编码实战](2)将YUV420P图片集编码成H.264视频文件(方法二) 一.编码成 H.264 视频文件,运行结果 二.编码成 MPEG4 视频文件,运行结果 三.编码成 AV_COD ...

  4. buuctf [GKCTF 2021]你知道apng吗 <apng图片格式的考察>

    下载文件得到个 apng的文件 其可以用火狐打开查看 这里需要下载一个工具: apngdis_gui.exe 来分解这个apng图片 打开来看看 1.这张图片是扭曲的需要拉扯一下 用在线ps 扫描得到 ...

  5. 201205阶段二FFmpeg编码

    目录 一.学习的知识点 FFMpeg编码流程 二.上课没有听懂或者没有理解的地方 三.当天学习的收获 一.学习的知识点 FFMpeg编码流程 注册 猜编码器格式 创建视频流 找到对应编码器 打开编码输 ...

  6. 如何强制ffmpeg编码时输出一个关键帧

    原文地址:http://www.ffmpeg.com.cn/index.php/%E5%A6%82%E4%BD%95%E5%BC%BA%E5%88%B6ffmpeg%E7%BC%96%E7%A0%81 ...

  7. 利用 LibWebP-NET 解码与编码 WebP 格式图片

    利用 LibWebP-NET 解码与编码 WebP 格式图片 原文:利用 LibWebP-NET 解码与编码 WebP 格式图片 WebP 格式是谷歌开发并发展的一种最新的网络图片格式,具有压缩率高. ...

  8. FFmpeg编码支持与定制(三)

    1.FFmpeg编码支持与定制 FFmpeg本身支持一些音视频编码格式.文件封装格式与流媒体传输协议,但是支持的数量依然有限,FFmpeg所做的只是提供一套基础的框架,所有的编码格式.文件封装格式与流 ...

  9. ffmpeg delogo滤镜去除图片水印

    之前本人写过ffmpeg movie滤镜添加图片水印,ffmpeg 非movie滤镜添加图片水印 今天用delogo滤镜去掉图片水印,ffmpeg命令行如下: ffmpeg -i in-compute ...

最新文章

  1. c语言游戏call调用,C语言-植物大战僵尸-刷僵尸call;fps游戏CS-方框透视(矩阵+传统)...
  2. javscript对cookie的操作,以及封装
  3. 对团队成员公开感谢博客
  4. wordpress 调用css,WordPress折腾记-精简CSS及JS在插件中的调用
  5. LINUX SHELL中数组的使用
  6. php中合并数组保留键值,如何使用php合并数组并保留键值的方法
  7. 谷歌 draco学习 二 压缩点信息
  8. win32应用程序和win32控制台应用程序
  9. 100/1000BASE-T1多端口测试方案
  10. 深度学习目标检测在游戏领域的应用
  11. 如何免费使用正版win10系统
  12. 助力篇|常见金融风控数据分析内容汇总,助你面试道路畅通无阻
  13. SketchUp安装组件失败“.Net FrameWork 4.5.2”的解决办法
  14. B站网页版播放常用快捷键
  15. day1—ECS阿里云云计算服务
  16. Sanic框架登录验证——Sanic-Auth的使用方法
  17. 项目起名的一些小单词
  18. 【整数与除数】小凯的疑惑
  19. Python(+numpy)实现对9*9数独问题(单解或多解)的快速递归求解
  20. 【钉钉-场景化能力包】出差考勤对接费控报销

热门文章

  1. 英语六级、考研高频词汇
  2. kafka安装基于脚本
  3. 新型公共政务云解决方案
  4. oracle 控制文件丢失或损坏的恢复
  5. linux系统取消报警声,Linux 中警报声的消除
  6. 安利一款功能强大,简单好用的录屏软件!
  7. JavaScript 数组遍历的五种方法(转)
  8. 设置scure CRT记录所有会话
  9. 经典好用的软件,不容错过
  10. matlab三相短路电流计算程序_三相短路短路电流计算(matlab程序)