源码:

/*** 最简单的基于FFmpeg的图像编码器* Simplest FFmpeg Picture Encoder** 雷霄骅 Lei Xiaohua* leixiaohua1020@126.com* 中国传媒大学/数字电视技术* Communication University of China / Digital TV Technology* http://blog.csdn.net/leixiaohua1020** 本程序实现了YUV420P像素数据编码为JPEG图片。是最简单的FFmpeg编码方面的教程。* 通过学习本例子可以了解FFmpeg的编码流程。*/#include <stdio.h>#define __STDC_CONSTANT_MACROS#ifdef _WIN32//Windowsextern "C"{#include "libavcodec/avcodec.h"#include "libavformat/avformat.h"};#else//Linux...#ifdef __cplusplusextern "C"{#endif#include <libavcodec/avcodec.h>#include <libavformat/avformat.h>#ifdef __cplusplus};#endif#endif#define IMG_WIDTH 640
#define IMG_HEIGHT 480int main(int argc, char* argv[]){AVFormatContext* pFormatCtx;AVOutputFormat* fmt;AVStream* video_st;AVCodecContext* pCodecCtx;AVCodec* pCodec;uint8_t* picture_buf;AVFrame* picture;AVPacket pkt;int y_size;int got_picture=0;int size;int ret=0;FILE *in_file = NULL;                            //YUV sourceint in_w=IMG_WIDTH,in_h=IMG_HEIGHT;                           //YUV's width and heightconst char* out_file = "yuv422.jpg";    //Output filein_file = fopen("/home/jon/yuv422.yuv", "rb");av_register_all();//Method 1pFormatCtx = avformat_alloc_context();//Guess formatfmt = av_guess_format("mjpeg", NULL, NULL);pFormatCtx->oformat = fmt;//Output URLif (avio_open(&pFormatCtx->pb,out_file, AVIO_FLAG_READ_WRITE) < 0){printf("Couldn't open output file.");return -1;}//Method 2. More simple//avformat_alloc_output_context2(&pFormatCtx, NULL, NULL, out_file);//fmt = pFormatCtx->oformat;video_st = avformat_new_stream(pFormatCtx, 0);if (video_st==NULL){return -1;}pCodecCtx = video_st->codec;pCodecCtx->codec_id = fmt->video_codec;pCodecCtx->codec_type = AVMEDIA_TYPE_VIDEO;pCodecCtx->pix_fmt = AV_PIX_FMT_YUVJ422P;pCodecCtx->width = in_w;pCodecCtx->height = in_h;pCodecCtx->time_base.num = 1;pCodecCtx->time_base.den = 25;//Output some informationav_dump_format(pFormatCtx, 0, out_file, 1);pCodec = avcodec_find_encoder(pCodecCtx->codec_id);if (!pCodec){printf("Codec not found.");return -1;}if (avcodec_open2(pCodecCtx, pCodec,NULL) < 0){printf("Could not open codec.");return -1;}picture = av_frame_alloc();size = avpicture_get_size(pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height);picture_buf = (uint8_t *)av_malloc(size);if (!picture_buf){return -1;}avpicture_fill((AVPicture *)picture, picture_buf, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height);//Write Headeravformat_write_header(pFormatCtx,NULL);y_size = pCodecCtx->width * pCodecCtx->height;av_new_packet(&pkt,y_size*3);//Read YUVif (fread(picture_buf, 1, y_size*2, in_file) <=0){printf("Could not read input file.");return -1;}picture->data[0] = picture_buf;              // Ypicture->data[1] = picture_buf+ y_size;      // Upicture->data[2] = picture_buf+ y_size*3/2;  // V//Encoderet = avcodec_encode_video2(pCodecCtx, &pkt,picture, &got_picture);if(ret < 0){printf("Encode Error.\n");return -1;}if (got_picture==1){pkt.stream_index = video_st->index;ret = av_write_frame(pFormatCtx, &pkt);}av_free_packet(&pkt);//Write Trailerav_write_trailer(pFormatCtx);printf("Encode Successful.\n");if (video_st){avcodec_close(video_st->codec);av_free(picture);av_free(picture_buf);}avio_close(pFormatCtx->pb);avformat_free_context(pFormatCtx);fclose(in_file);return 0;}

生成的图片(yuv422.jpg):

最简单的YUV422转jpg的例程相关推荐

  1. Socket编程实践(3) 多连接服务器实现与简单P2P聊天程序例程

    SO_REUSEADDR选项 在上一篇文章的最后我们贴出了一个简单的C/S通信的例程.在该例程序中,使用"Ctrl+c"结束通信后,服务器是无法立即重启的,如果尝试重启服务器,将被 ...

  2. 大疆Onboard SDK 3.2玫瑰线例程浅析

    大疆Onboard SDK 3.2玫瑰线例程浅析 前不久参加2017英飞凌杯无人机竞赛,研究了一下大疆的OnbardSDK,现在比赛结束了,简单写一下其中自带的玫瑰线例程分析,也算对比赛进行一点总结. ...

  3. 新手学习单片机最常见的六大误区,你进坑了吗?

    一.去背寄存器 千万不要去记寄存器,我做开发这么多年了,一个寄存器都记不住. 寄存器一般是我们要使用单片机外设的时候会去配置. 一流的方法是直接参考别人的配置程序. 二流的方法是看数据手册,最傻雕的做 ...

  4. linux Rootkit:x86与ARM的内联内核函数Hooking

    介绍 几个月前,我添加了一个新的项目.(https://github.com/mncoppola/suterusu)         通过我的各种对路由器后门及内核漏洞利用的探险,我最近的兴趣转向Li ...

  5. 谈谈对APC的一点理解

    谈谈对APC的一点理解 异步过程调用(APCs) 是NT异步处理体系结构中的一个基础部分,理解了它,对于了解NT怎样操作和执行几个核心的系统操作很有帮助. 1) APCs允许用户程序和系统元件在一个进 ...

  6. Oracle学习笔记:redo重做日志

    redo重做日志对于oracle来说犹如 肝脏 那么重要,是oracle机能中的排毒恢复健康的重要部件!! 相关的基本概念: 1.scn 2.日志序列号 3.日志组 和 日志 成员,尤其是日志文件的尺 ...

  7. Ardino基础教程 2_LED闪烁实验

    实验二:LED 闪烁实验 LED 小灯实验是比较基础的实验之一,上一个" Hello World!"实 验里已经利用到了Arduino 自带的LED,这次我们利用其他I/O 口和 ...

  8. 【S操作】轻松优雅库移植解决方案,arduino库移植应对方案

    微信关注 "DLGG创客DIY" 设为"星标",重磅干货,第一时间送达. 为啥要用arduino?最重要的一个原因就是因为arduino完美的生态,即可以找到很 ...

  9. MFC应用程序框架入门

    摘要: 本文主要对VC++ 6.0的MFC编程方法及MFC应用程序框架进行简要介绍. 关键词: VC++6.0:MFC:程序框架 1 MFC概述 顾名思意,MFC应用程序框架是以MFC作为框架基础的, ...

  10. c++ vs release没有exe_未来安全 | 第一次Geant4培训总结 | 有没有你关注的问题呢?...

    Geant4简介 Geant4是蒙卡工具包,模拟很多粒子,记录一些统计量,用这些统计量去估计真实的物理实验的结果.蒙卡模拟程序,从最老的MCNP,到PENELOPE,FLUKA等. MCNP是用输入卡 ...

最新文章

  1. 六、springboot整合swagger
  2. Android 图片的帧动画
  3. c语言c99标准_如何成为一名优秀的 C 语言程序员?
  4. 白嫖我常用的 11 个超火的前端必备在线工具,终于有时间上班摸鱼了
  5. Azure Show|第一期 开播啦!嘉宾梁迪李卓恒李佳芮
  6. Qt笔记-获取Windows下目前运行的进程信息
  7. java单例模式实例_Java设计模式之单例模式 通俗易懂 超详细 【内含案例】
  8. 北理乐学c语言基础答案晕,北理乐学C语言答案
  9. PMP知识点总结-自由时差与总时差
  10. 服务器信号满格但网速很慢,信号满格网速太慢是什么原因,网速慢是什么原因-...
  11. 微信公众号配置失败问题解决方法
  12. pyppeteer 报 Execution context was destroyed, most likely because of a navigation
  13. 如何保障微服务架构下的数据一致性
  14. android 静音与振动
  15. 平方逼近matlab,MATLAB上机实验——最佳平方逼近
  16. 含有一般疑问句的歌_一般将来时详解,想学好英语必须要掌握的一个最重要语法...
  17. java每日打卡_“365算法每日学计划”:03打卡-贪心算法
  18. C#中sealed的用法
  19. xUtils3.x的网络请求封装和请求https之单向SSL验证
  20. B - The Pilots Brothers' refrigerator

热门文章

  1. 常见Struts、Hibernate、Spring、J2EE、ibatis、Oracle等开发框架架构图及其简介
  2. C# 中 动态获得或设置一个对象的值
  3. linux gcc下实现简单socket套接字小程序
  4. [翻译]Scott Mitchell的ASP.NET2.0数据教程中文版索引(Canceled!!)
  5. c语言 extern_C语言入门笔记(三)
  6. 设计模式(Design Patterns)
  7. vue学习笔记-vue双向数据绑定
  8. UltraISO v 9.6 单文件版
  9. 如何开发一款高大上的android应用的必备知识
  10. Zabbix安装界面显示PHP time zone 为“红色”的解决办法