#include "Ffmpeg_Encode.h"
void Ffmpeg_Encoder::Ffmpeg_Encoder_Init()
{
av_register_all();
avcodec_register_all();
m_pYUVFrame = new AVFrame[1];//YUV帧数据赋值
c = NULL;//解码器指针对象赋初值
}
void Ffmpeg_Encoder::Ffmpeg_Encoder_Setpara(AVCodecID mycodeid, int vwidth, int vheight)
{
pCodecH264 = avcodec_find_encoder(mycodeid);//查找h264编码器
if (!pCodecH264)
{
fprintf(stderr, "h264 codec not found\n");
exit(1);
}
width = vwidth;
height = vheight;
c = avcodec_alloc_context3(pCodecH264);//函数用于分配一个AVCodecContext并设置默认值,如果失败返回NULL,并可用av_free()进行释放
c->bit_rate = 1024*1024; //码率,越高视频质量越好
c->width = vwidth;//设置编码视频宽度
c->height = vheight; //设置编码视频高度
c->time_base.den = 25;//设置帧率,num为分子和den为分母,如果是1/25则表示25帧/s
c->time_base.num = 1;
c->gop_size = 10; //设置GOP大小,该值表示每10帧会插入一个I帧
c->max_b_frames = 1;//设置B帧最大数,该值表示在两个非B帧之间,所允许插入的B帧的最大帧数
c->pix_fmt = AV_PIX_FMT_YUV420P;//设置像素格式
imgwidthlen = c->width * 3;
av_opt_set(c->priv_data, "tune", "zerolatency", 0);//设置编码器的延时,解决前面的几十帧不出数据的情况
if (avcodec_open2(c, pCodecH264, NULL) < 0)//打开编码器
return;
nDataLen = vwidth*vheight * 3;//计算图像rgb数据区长度
yuv_buff = new uint8_t[nDataLen/2];//初始化数据区,为yuv图像帧准备填充缓存
rgb_buff = new uint8_t[nDataLen];//初始化数据区,为rgb图像帧准备填充缓存
outbuf_size = 100000;////初始化编码输出数据区
outbuf = new uint8_t[outbuf_size];
scxt = sws_getContext(c->width, c->height, AV_PIX_FMT_BGR24, c->width, c->height, AV_PIX_FMT_YUV420P, SWS_POINT, NULL, NULL, NULL);//初始化格式转换函数
av_image_fill_arrays(m_pYUVFrame->data, m_pYUVFrame->linesize, yuv_buff, AV_PIX_FMT_YUV420P, width, height, 1);
}
void Ffmpeg_Encoder::Ffmpeg_Encoder_Encode(FILE *file, uint8_t *data)
{
av_init_packet(&pkt);
memcpy(rgb_buff, data, nDataLen);//拷贝图像数据到rgb图像帧缓存中准备处理
sws_scale(scxt, &rgb_buff, &imgwidthlen, 0, c->height, m_pYUVFrame->data, m_pYUVFrame->linesize);// 将RGB转化为YUV
int myoutputlen = 0;
int returnvalue = avcodec_encode_video2(c, &pkt, m_pYUVFrame, &myoutputlen);
if (returnvalue == 0)
{
fwrite(pkt.data, 1, pkt.size, file);
}
}
void Ffmpeg_Encoder::Ffmpeg_Encoder_Close()
{
delete[]m_pYUVFrame;
delete[]rgb_buff;
delete[]yuv_buff;
delete[]outbuf;
sws_freeContext(scxt);
avcodec_close(c);//关闭编码器
av_free(c);
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
利用FFmpge进行视频压缩(从图像到H264视频流)新版 (143个子文件)
Ffmpeg_Encode.cpp 2KB
Ffmpeg_Encode_Main.cpp 874B
avutil-55.def 10KB
avutil-54.def 10KB
avcodec-56.def 6KB
avcodec-57.def 5KB
avformat-57.def 4KB
avformat-56.def 4KB
avfilter-5.def 2KB
avfilter-6.def 2KB
swscale-3.def 781B
swscale-4.def 756B
avdevice-56.def 554B
avdevice-57.def 554B
swresample-2.def 457B
swresample-1.def 433B
postproc-54.def 233B
postproc-53.def 233B
avcodec-57.dll 25.6MB
avformat-57.dll 6.08MB
avutil-55.dll 559KB
swscale-4.dll 513KB
swresample-2.dll 331KB
Ffmpeg_Encode.vcxproj.filters 1KB
avcodec.h 173KB
avformat.h 106KB
avfilter.h 39KB
opt.h 35KB
pixfmt.h 30KB
avio.h 26KB
frame.h 22KB
swresample.h 20KB
intreadwrite.h 18KB
avdevice.h 18KB
mem.h 15KB
common.h 15KB
pixdesc.h 14KB
old_pix_fmts.h 14KB
avstring.h 13KB
swscale.h 12KB
log.h 10KB
old_codec_ids.h 10KB
samplefmt.h 10KB
buffer.h 10KB
channel_layout.h 9KB
avutil.h 9KB
imgutils.h 8KB
dict.h 8KB
vdpau.h 8KB
bprint.h 8KB
parseutils.h 7KB
version.h 7KB
xvmc.h 6KB
vda.h 6KB
fifo.h 6KB
error.h 5KB
tree.h 5KB
timecode.h 5KB
buffersink.h 5KB
eval.h 5KB
mathematics.h 5KB
audio_fifo.h 5KB
cpu.h 5KB
attributes.h 4KB
vaapi.h 4KB
rational.h 4KB
videotoolbox.h 4KB
version.h 4KB
buffersrc.h 4KB
qsv.h 4KB
dv_profile.h 4KB
stereo3d.h 4KB
threadmessage.h 4KB
hash.h 4KB
dirac.h 3KB
asrc_abuffer.h 3KB
downmix_info.h 3KB
display.h 3KB
avfft.h 3KB
postprocess.h 3KB
hmac.h 3KB
crc.h 3KB
bswap.h 3KB
d3d11va.h 3KB
version.h 3KB
xtea.h 3KB
timestamp.h 3KB
version.h 3KB
avdct.h 3KB
cast5.h 3KB
file.h 2KB
blowfish.h 2KB
avcodec.h 2KB
dxva2.h 2KB
vorbis_parser.h 2KB
des.h 2KB
twofish.h 2KB
camellia.h 2KB
aes_ctr.h 2KB
avassert.h 2KB
共 143 条
- 1
- 2
_寒潭雁影
- 粉丝: 2425
- 资源: 41
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页