/******************************
功能:编码YUV序列为h264视频文件
FFmpeg:4.0.2
******************************/
#include <iostream>
extern "C"
{
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavutil/imgutils.h>
}
using namespace std;static void encode(AVCodecContext *enc_ctx, AVFrame *frame, AVPacket *pkt, FILE *outfile)
{int ret;if (frame)printf("Send frame %3lld\n", frame->pts);ret = avcodec_send_frame(enc_ctx, frame);if (ret < 0){fprintf(stderr, "Error sending a frame for encoding\n");exit(1);}while (ret >= 0){ret = avcodec_receive_packet(enc_ctx, pkt);if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)return;else if (ret < 0){fprintf(stderr, "Error during encoding\n");exit(1);}printf("Write packet %3lld(size=%5d)\n", pkt->pts, pkt->size);fflush(stdout);fwrite(pkt->data, 1, pkt->size, outfile);av_packet_unref(pkt);}
}
int EncodeYUVToH264(const char *input_fileName, const char* out_fileName, int in_w, int in_h, int framenum)
{// 打开输入YUV序列FILE *in_file = NULL;fopen_s(&in_file, input_fileName, "rb");if (!in_file){cout << "can not open file!" << endl;return -1;}// 打开输出视频文件FILE *out_file = NULL;fopen_s(&out_file, out_fileName, "wb");// 初始化AVFormatContext结构体,根据文件名获取到合适的封装格式AVFormatContext *pFormatCtx = avformat_alloc_context();avformat_alloc_output_context2(&pFormatCtx, NULL, NULL, out_fileName);AVOutputFormat *fmt = pFormatCtx->oformat;// 初始化视频码流AVStream *video_st = avformat_new_stream(pFormatCtx, 0);if (video_st == NULL){printf("failed allocating output stream\n");return -1;}video_st->time_base.num = 1;video_st->time_base.den = 25;// 编码器Context设置参数AVCodecContext *pCodecCtx = avcodec_alloc_context3(NULL);avcodec_parameters_to_context(pCodecCtx, pFormatCtx->streams[0]->codecpar);pCodecCtx->codec_id = fmt->video_codec;pCodecCtx->codec_type = AVMEDIA_TYPE_VIDEO;pCodecCtx->pix_fmt = AV_PIX_FMT_YUV420P;pCodecCtx->width = in_w;pCodecCtx->height = in_h;pCodecCtx->time_base.num = 1;pCodecCtx->time_base.den = 25;pCodecCtx->bit_rate = 400000;pCodecCtx->gop_size = 12;if (pCodecCtx->codec_id == AV_CODEC_ID_H264){pCodecCtx->qmin = 10;pCodecCtx->qmax = 51;pCodecCtx->qcompress = 0.6;}if (pCodecCtx->codec_id == AV_CODEC_ID_MPEG2VIDEO)pCodecCtx->max_b_frames = 2;if (pCodecCtx->codec_id == AV_CODEC_ID_MPEG1VIDEO)pCodecCtx->mb_decision = 2;// 寻找编码器 AVCodec *pCodec = avcodec_find_encoder(AV_CODEC_ID_H264);if (!pCodec){cout << "no right encoder!" << endl;return -1;}// 打开编码器if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0){cout << "open encoder fail!" << endl;return -1;}// 输出格式信息av_dump_format(pFormatCtx, 0, out_fileName, 1);// 初始化帧AVFrame *pictureFrame = av_frame_alloc();pictureFrame->width = pCodecCtx->width;pictureFrame->height = pCodecCtx->height;pictureFrame->format = pCodecCtx->pix_fmt;// ffmpeg4.0int size = av_image_get_buffer_size(pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height, 1);uint8_t *picture_buf = (uint8_t *)av_malloc(size);av_image_fill_arrays(pictureFrame->data, pictureFrame->linesize, picture_buf, AV_PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height, 1);// 写头文件avformat_write_header(pFormatCtx, NULL);// 创建已编码帧AVPacket *pkt = av_packet_alloc();if (!pkt){return -1;}// 编码器Context大小int y_size = pCodecCtx->width*pCodecCtx->height;//循环每一帧for (int i = 0; i < framenum; i++){// 读入YUVif (fread(picture_buf, 1, y_size * 3 / 2, in_file) <= 0){cout << "read file fail!" << endl;return -1;}else if (feof(in_file)){break;}pictureFrame->data[0] = picture_buf; // YpictureFrame->data[1] = picture_buf + y_size; // UpictureFrame->data[2] = picture_buf + y_size * 5 / 4;// VpictureFrame->pts = i;// encode the imageencode(pCodecCtx, pictureFrame, pkt, out_file);}// flush the encoder encode(pCodecCtx, NULL, pkt, out_file);av_write_trailer(pFormatCtx);// 释放内存av_frame_free(&pictureFrame);av_packet_free(&pkt);avcodec_free_context(&pCodecCtx);av_free(picture_buf);avformat_free_context(pFormatCtx);fclose(out_file);fclose(in_file);
}
int main(int argc, char *argv[])
{// 输入YUV序列const char *input_fileName = "../YUV/test.yuv";// 输入YUV宽度和高度、帧数int in_w = 352, in_h = 288;int framenum = 300;// 输出H264文件const char* out_fileName = "../H264/test_out.h264";if (EncodeYUVToH264(input_fileName, out_fileName, in_w, in_h, framenum) < 0)cout << "encode failed!" << endl;cout << "encode finished!" << endl;return getchar();
}

*【注】如果你觉得此文不错,可以考虑打赏我哦,您的打赏将是我更新的最大动力,非常感谢。(打赏也是基于自愿原则的哦( ̄︶ ̄))

FFmpeg 4.0.2编码YUV序列为H264视频文件相关推荐

  1. ffmpeg —— v4l2录制h264视频文件(边采集边转码)

    相关文章: v4l2采集图像并转换格式 视频编码(yuv编码h264) 目标:使用v4l2采集摄像头图像数据,并实时编码成h264视频文件. 代码: #include <stdio.h> ...

  2. windows使用ffmpeg将mp4文件转变成h264视频文件

    我是直接使用ffmpeg的安装包,win10下 首先下载ffmpeg http://www.ffmpeg.org/download.htmlhttp://www.ffmpeg.org/download ...

  3. 毕设系列之Libx264实时视频流(YUV 420P转H264视频编码篇)

    #PS:要转载请注明出处,本人版权所有 #PS:这个只是 < 我自己 >理解,如果和你的 #原则相冲突,请谅解,勿喷 开发环境:Ubuntu 16.04 LTS 本文的技术实现部分参考雷博 ...

  4. ffmpeg将yuv数据编码为H264格式数据

    1.yuv->H264经过编码后可以明显缩小视频文件的体积,例如我们经常看到的MP4文件其实就是由H264格式的视频文件和aac音频格式文件打包而成. 2.整个编码流程: 网上的一个关于AVFo ...

  5. FFmpeg简单使用:视频编码 ---- YUV转H264

    基本流程 从本地读取YUV数据编码为h264格式的数据,然后再存⼊到本地,编码后的数据有带startcode. 与FFmpeg 示例⾳频编码的流程基本⼀致. 函数说明: avcodec_find_en ...

  6. 使用ffmpeg将BMP图片编码为x264视频文件,将H264视频保存为BMP图片,yuv视频文件保存为图片的代码

    ffmpeg开源库,实现将bmp格式的图片编码成x264文件,并将编码好的H264文件解码保存为BMP文件. 实现将视频文件yuv格式保存的图片格式的测试,图像格式png,jpg, gif等等测试均O ...

  7. 最简单的基于FFMPEG的图像编码器(YUV编码为JPEG)

    伴随着毕业论文的完成,这两天终于腾出了空闲,又有时间搞搞FFMPEG的研究了.想着之前一直搞的都是FFMPEG解码方面的工作,很少涉及到FFMPEG编码方面的东西,于是打算研究一下FFMPEG的编码. ...

  8. NDK开发-Android下摄像头YUV数据获取与H264编码(FFmpeg、x264)总结

    涉及知识点: Camera2 API使用 YUV420P与YUV420SP(NV21)格式转换 h264文件格式 FFmpeg工程 x264解码器 这次就先记录一下开发过程,因为牵涉到的很多技术问题都 ...

  9. FFmpeg浅尝辄止(二)——YUV视频序列编码为视频 ~~ 新版修改版-调通

    文章来源: FFmpeg浅尝辄止(二)--YUV视频序列编码为视频 针对新版FFmpeg进行修改后,VC2010调通 AVFormatContext* oc; AVOutputFormat* fmt; ...

最新文章

  1. centos7配置br0_centos 7.2 网卡配置文件 及 linux bridge的静态配置
  2. Ubuntu 安装 搜狗输入法
  3. yolov3安卓实现_重磅!MobileNet-YOLOv3来了(含三种框架开源代码)
  4. php mysql随机记录_php随机取mysql记录方法小结
  5. Docker搭建WebLogic服务器
  6. vue中v-on指令的使用之Vue知识点归纳(四)
  7. python shutil_Python shutil模块
  8. About_PHP_验证码的生成
  9. java对菜单项的监听_我是新手,请问大神java菜单项和下拉列表添加监听和监听方法???有变量和方法就行了...
  10. 清华“姚班”大佬豆瓣征婚被网暴
  11. 中科呐喊WiFi热点广告机APP使用教程(安卓)
  12. HTML登录注册页面简单实现
  13. java期末考试复习题_java期末考试复习题库 试题题库.doc
  14. 马蜂窝用户内容贡献能力模型构建
  15. 一文看懂Echo和Alexa,亚马逊如何用苹果的玩法在玩语音?
  16. 软件缺陷的定义和判定标准
  17. 比较两个字符串是否相等,strcmp wcscmp stricmp wcsicmp
  18. 手把手教你重装系统,新手必看,最全教程
  19. linux修改snmp团体名称,HP-UX修改MP卡的SNMP团体字
  20. 抖音android 语言英语,老师要求用英文介绍一下抖音这款软件求大神支招

热门文章

  1. 计算机组成与体系结构第二次试验:存储器实验
  2. 面试必备-Java集合框架
  3. Linux字符设备驱动详解四(使用自属的xbus驱动总线)
  4. 2022-04-13 WPF面试题 什么是可冻结对象?
  5. UGC之后,PGC、PUGC内容会是新的突破口吗?
  6. 0593-CDH5与CDH6对比
  7. Sahi案例分享:音乐批量下载
  8. 王道计算机考研机试指南刷题笔记-自用7
  9. Security之注解篇
  10. [学习心得]C# 登录验证码