流程

下面附上一张FFmpeg编码视频的流程图。通过该流程,不仅可以编码H.264/H.265的码流,而且可以编码MPEG4/MPEG2/VP9/VP8等多种码流。实际上使用FFmpeg编码视频的方式都是一样的。图中蓝色背景的函数是实际输出数据的函数。浅绿色的函数是视频编码的函数。

简单介绍一下流程中各个函数的意义

av_register_all():注册FFmpeg所有编解码器。
avformat_alloc_output_context2():初始化输出码流的AVFormatContext。
avio_open():打开输出文件。
av_new_stream():创建输出码流的AVStream。
avcodec_find_encoder():查找编码器。
avcodec_open2():打开编码器。
avformat_write_header():写文件头(对于某些没有文件头的封装格式,不需要此函数。比如说MPEG2TS)。
avcodec_encode_video2():编码一帧视频。即将AVFrame(存储YUV像素数据)编码为AVPacket(存储H.264等格式的码流数据)。
av_write_frame():将编码后的视频码流写入文件。
flush_encoder():输入的像素数据读取完成后调用此函数。用于输出编码器中剩余的AVPacket。
av_write_trailer():写文件尾(对于某些没有文件头的封装格式,不需要此函数。比如说MPEG2TS)。

代码

下面直接贴上代码

  1. *
  2. * 本程序实现了YUV像素数据编码为视频码流(HEVC(H.265),H264,MPEG2,VP8等等)。
  3. * 是最简单的FFmpeg视频编码方面的教程。
  4. * 通过学习本例子可以了解FFmpeg的编码流程。
  5. * This software encode YUV420P data to HEVC(H.265) bitstream (or
  6. * H.264, MPEG2, VP8 etc.).
  7. * It's the simplest video encoding software based on FFmpeg.
  8. * Suitable for beginner of FFmpeg
  9. */
  10. #include <stdio.h>
  11. extern "C"
  12. {
  13. #include "libavutil\opt.h"
  14. #include "libavcodec\avcodec.h"
  15. #include "libavformat\avformat.h"
  16. #include "libswscale\swscale.h"
  17. };
  18. int flush_encoder(AVFormatContext *fmt_ctx,unsigned int stream_index)
  19. {
  20. int ret;
  21. int got_frame;
  22. AVPacket enc_pkt;
  23. if (!(fmt_ctx->streams[stream_index]->codec->codec->capabilities &
  24. CODEC_CAP_DELAY))
  25. return 0;
  26. while (1) {
  27. printf("Flushing stream #%u encoder\n", stream_index);
  28. //ret = encode_write_frame(NULL, stream_index, &got_frame);
  29. enc_pkt.data = NULL;
  30. enc_pkt.size = 0;
  31. av_init_packet(&enc_pkt);
  32. ret = avcodec_encode_video2 (fmt_ctx->streams[stream_index]->codec, &enc_pkt,
  33. NULL, &got_frame);
  34. av_frame_free(NULL);
  35. if (ret < 0)
  36. break;
  37. if (!got_frame){
  38. ret=0;
  39. break;
  40. }
  41. printf("Succeed to encode 1 frame! 编码成功1帧!\n");
  42. /* mux encoded frame */
  43. ret = av_write_frame(fmt_ctx, &enc_pkt);
  44. if (ret < 0)
  45. break;
  46. }
  47. return ret;
  48. }
  49. int main(int argc, char* argv[])
  50. {
  51. AVFormatContext* pFormatCtx;
  52. AVOutputFormat* fmt;
  53. AVStream* video_st;
  54. AVCodecContext* pCodecCtx;
  55. AVCodec* pCodec;
  56. uint8_t* picture_buf;
  57. AVFrame* picture;
  58. int size;
  59. //FILE *in_file = fopen("src01_480x272.yuv", "rb"); //Input YUV data 视频YUV源文件
  60. FILE *in_file = fopen("ds_480x272.yuv", "rb");  //Input YUV data 视频YUV源文件
  61. int in_w=480,in_h=272;//宽高
  62. //Frames to encode
  63. int framenum=100;
  64. //const char* out_file = "src01.h264";  //Output Filepath 输出文件路径
  65. //const char* out_file = "src01.ts";
  66. //const char* out_file = "src01.hevc";
  67. const char* out_file = "ds.hevc";
  68. av_register_all();
  69. //Method1 方法1.组合使用几个函数
  70. pFormatCtx = avformat_alloc_context();
  71. //Guess Format 猜格式
  72. fmt = av_guess_format(NULL, out_file, NULL);
  73. pFormatCtx->oformat = fmt;
  74. //Method 2 方法2.更加自动化一些
  75. //avformat_alloc_output_context2(&pFormatCtx, NULL, NULL, out_file);
  76. //fmt = pFormatCtx->oformat;
  77. //Output Format 注意输出路径
  78. if (avio_open(&pFormatCtx->pb,out_file, AVIO_FLAG_READ_WRITE) < 0)
  79. {
  80. printf("Failed to open output file! 输出文件打开失败");
  81. return -1;
  82. }
  83. video_st = avformat_new_stream(pFormatCtx, 0);
  84. video_st->time_base.num = 1;
  85. video_st->time_base.den = 25;
  86. if (video_st==NULL)
  87. {
  88. return -1;
  89. }
  90. //Param that must set
  91. pCodecCtx = video_st->codec;
  92. //pCodecCtx->codec_id =AV_CODEC_ID_HEVC;
  93. pCodecCtx->codec_id = fmt->video_codec;
  94. pCodecCtx->codec_type = AVMEDIA_TYPE_VIDEO;
  95. pCodecCtx->pix_fmt = PIX_FMT_YUV420P;
  96. pCodecCtx->width = in_w;
  97. pCodecCtx->height = in_h;
  98. pCodecCtx->time_base.num = 1;
  99. pCodecCtx->time_base.den = 25;
  100. pCodecCtx->bit_rate = 400000;
  101. pCodecCtx->gop_size=250;
  102. //H264
  103. //pCodecCtx->me_range = 16;
  104. //pCodecCtx->max_qdiff = 4;
  105. //pCodecCtx->qcompress = 0.6;
  106. pCodecCtx->qmin = 10;
  107. pCodecCtx->qmax = 51;
  108. //Optional Param
  109. pCodecCtx->max_b_frames=3;
  110. // Set Option
  111. AVDictionary *param = 0;
  112. //H.264
  113. if(pCodecCtx->codec_id == AV_CODEC_ID_H264) {
  114. av_dict_set(?m, "preset", "slow", 0);
  115. av_dict_set(?m, "tune", "zerolatency", 0);
  116. }
  117. //H.265
  118. if(pCodecCtx->codec_id == AV_CODEC_ID_H265){
  119. av_dict_set(?m, "x265-params", "qp=20", 0);
  120. av_dict_set(?m, "preset", "ultrafast", 0);
  121. av_dict_set(?m, "tune", "zero-latency", 0);
  122. }
  123. //Dump Information 输出格式信息
  124. av_dump_format(pFormatCtx, 0, out_file, 1);
  125. pCodec = avcodec_find_encoder(pCodecCtx->codec_id);
  126. if (!pCodec){
  127. printf("Can not find encoder! 没有找到合适的编码器!\n");
  128. return -1;
  129. }
  130. if (avcodec_open2(pCodecCtx, pCodec,?m) < 0){
  131. printf("Failed to open encoder! 编码器打开失败!\n");
  132. return -1;
  133. }
  134. picture = avcodec_alloc_frame();
  135. size = avpicture_get_size(pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height);
  136. picture_buf = (uint8_t *)av_malloc(size);
  137. avpicture_fill((AVPicture *)picture, picture_buf, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height);
  138. //Write File Header 写文件头
  139. avformat_write_header(pFormatCtx,NULL);
  140. AVPacket pkt;
  141. int y_size = pCodecCtx->width * pCodecCtx->height;
  142. av_new_packet(&pkt,y_size*3);
  143. for (int i=0; i<framenum; i++){
  144. //Read YUV 读入YUV
  145. if (fread(picture_buf, 1, y_size*3/2, in_file) < 0){
  146. printf("Failed to read YUV data! 文件读取错误\n");
  147. return -1;
  148. }else if(feof(in_file)){
  149. break;
  150. }
  151. picture->data[0] = picture_buf;  // 亮度Y
  152. picture->data[1] = picture_buf+ y_size;  // U
  153. picture->data[2] = picture_buf+ y_size*5/4; // V
  154. //PTS
  155. picture->pts=i;
  156. int got_picture=0;
  157. //Encode 编码
  158. int ret = avcodec_encode_video2(pCodecCtx, &pkt,picture, &got_picture);
  159. if(ret < 0){
  160. printf("Failed to encode! 编码错误!\n");
  161. return -1;
  162. }
  163. if (got_picture==1){
  164. printf("Succeed to encode 1 frame! 编码成功1帧!\n");
  165. pkt.stream_index = video_st->index;
  166. ret = av_write_frame(pFormatCtx, &pkt);
  167. av_free_packet(&pkt);
  168. }
  169. }
  170. //Flush Encoder
  171. int ret = flush_encoder(pFormatCtx,0);
  172. if (ret < 0) {
  173. printf("Flushing encoder failed\n");
  174. return -1;
  175. }
  176. //Write file trailer 写文件尾
  177. av_write_trailer(pFormatCtx);
  178. //Clean 清理
  179. if (video_st){
  180. avcodec_close(video_st->codec);
  181. av_free(picture);
  182. av_free(picture_buf);
  183. }
  184. avio_close(pFormatCtx->pb);
  185. avformat_free_context(pFormatCtx);
  186. fclose(in_file);
  187. return 0;
  188. }
  189. 雷霄骅 (Lei Xiaohua)
    leixiaohua1020@126.com
    http://blog.csdn.net/leixiaohua1020

    版权声明:本文为博主原创文章,未经博主允许不得转载。

YUV编码为HEVC(H.265)相关推荐

  1. 高效视频编码 (HEVC) -H.265(结构解析)

    版本 HEVC (H.265) 规范的第一个版本于 2013 年 4 月发布.该标准的版本如下: ITU-T H.265 (V1) (04/2013) http://handle.itu.int/11 ...

  2. HEVC/H.265编码HM码率控制

    HEVC/H.265编码标准HM平台码率控制流程 研究生期间了解过HM平台的码率控制过程,现在时隔2年多又回顾了一下HM平台的码控流程,发现相较之前多了CpbSaturationEnabled这么个机 ...

  3. 二、对HEVC/H.265视频编解码器进行隐写的基本思路

    二.对HEVC/H.265视频编解码器进行隐写的基本思路 概述 1.视频隐写的基本思路 2.视频隐写的举例说明 3.结尾 概述 其实对视频隐写.图像隐写或是音频隐写,基本的思路都是一样的:读取原始图像 ...

  4. SONY索尼A7S3相机HEVC|H.265视频RSV损坏修复MP4

    继佳能和松下相机HEVC|H.265断电视频文件成功修复后,终于迎来索尼HEVC|H.265编码损坏视频修复.回想以往索尼微单的MP4视频文件,使用的都是H.264视频编码技术,从A7S3微单开始支持 ...

  5. AV1比HEVC/H.265简单对比

    AV1是由开放媒体视频联盟(Alliance of Open Media Video)开发的开放.免版税的下一代视频编码格式.它被设计为取代谷歌的VP9,并与H.265/HEVC竞争.AV1的目标是在 ...

  6. 修改Chromium源码实现HEVC/H.265 4K视频播放

    本文作者:蔡斯杰,字节跳动互娱前端业务负责人 公司内容生产端最近(2019/10)在推广 HEVC/H.265 的使用,这种视频编码格式对比H.264更加先进且节省带宽,虽然先进但是因为专利费的问题, ...

  7. HEVC/H.265(1)——入门初步了解

    一.引子 说到H.265,个人的初始印象就是比H.264高端了那么一些的编码标准,再就是在学校的BT上下的那个好多播放器都播放不了,只有potplayer等一小部分未来播放器能播放的权利的游戏HEVC ...

  8. 【miscellaneous】最新HEVC/H.265 4K视频,显卡解码测试

    转载自:http://bbs.zol.com.cn/diybbs/d34441_76103.html 4K这个概念也在最近几年开始流行了起来,无论是4K显示器.4K电视盒子,还是4K游戏对硬件的要求也 ...

  9. HEVC (H.265)介绍(转)

    [Liupin]: 这是一篇简单介绍H.265文章,我接触和开发H.265二年来,H.265技术在行业内接收速度比H.264快多了,现在国际和国内各大公司都在进行H.265应用,不管是IC设计还是H. ...

  10. HEVC/H.265硬件编码器实现杂谈

    国际视频编码标准HEVC已经发布两年有余,市场上关于支持HEVC的硬件也日益涌现,本文借鉴了各方面资源做了综合与概述,给出了HEVC硬件编码器实现的基本方法等重要网络资源. 一.系统设计要点 对于HE ...

最新文章

  1. 关于 TApplication 详解 三 ---- TComponent
  2. Linux完全兼容POSIX1.0标准的特性
  3. TensorFlow官方文档中的sub 和mul中的函数已经在API中改名了
  4. Thymeleaf 常用属性
  5. GAN做图像翻译的一点总结
  6. 逆向工程核心原理学习笔记(十三):分析abex' crackme #1 的延伸:将参数压入栈
  7. php读取客户机本地时间,PHP如何获取客户端时区以及准确显示所在地时间
  8. Eclipse中自动创建set、get方法
  9. php include 导航栏,PHP全栈开发(八):CSS Ⅹ 导航栏制作
  10. mvc+EF实现简单的登陆功能
  11. 从CarLife音乐切换回蓝牙音乐音量变小
  12. Excel黑科技——含合并单元格的同行求和并下拉自动填充
  13. 安装Dev c++后,编译文件出现未编译的解决方法
  14. 菲尔博士的三十六交际方式
  15. transform 属性 实现3D立体相册
  16. rsa public key not find与Generate First a serial的解决方案-6.13日摸索总结
  17. [c#]喜马拉雅、蜻蜓、荔枝FM音频批量下载器V1.3 by Levme开发手记
  18. [安洵杯 2019]不是文件上传
  19. CAD杀毒V2.6 正式版
  20. Java自动生成数据库设计文档(Word)

热门文章

  1. vue实现搜索框记录搜索历史_Vue 实现输入框新增搜索历史记录功能
  2. 1302: PIPI的族谱(二叉树)
  3. 单片机C51继电器控制C语言,51单片机对继电器的控制
  4. 无线web认证计费服务器,WEB认证原理
  5. HNOI2015 亚瑟王
  6. C++ VTK VMTK 提取血管中心线
  7. FPC柔性印制电路板学习一
  8. 如何在数据库mysql中储存图片
  9. 147计算机代表啥,147代表什么意思
  10. 互联网思维之用户思维