当前位置: > 生活 > 

imtoo mpeg encoder

时间:2024-03-09 05:22:02人气:61编辑:用户投稿
前言

本系列文章

FFMPEGDemo分析之muxing.c

FFmpegDemo分析之demuxing_decoding.c

FFmpegDemo分析之decode_video.c

本篇文章来介绍一下单独使用libavcodec库进行编码视频文件。编码音频和编码视频的步骤其实都差不多。先来简单介绍下编码的步骤。步骤如下:

找到编码器->分配编码器上下文->给编码器上下文赋值一些必要的编码参数->打开编码器->分配AVPacket和AVFrame->给AVFrame赋值一些必要参数分配data空间->给AVFrame填充编码数据->进行编码生成输出AVPacket->释放资源。

详细的流程图

imtoo mpeg encoder

C++音视频开发学习资料:点击领取→音视频开发(资料文档+视频教程+面试题)(FFmpeg+WebRTC+RTMP+RTSP+HLS+RTP)

C++程序员必看,抓住音视频开发的大浪潮!冲击年薪60万

imtoo mpeg encoder

代码详细分析

#include<stdio.h>n#include<stdlib.h>n#include<string.h>nn#include<libavcodec/avcodec.h>nn#include<libavutil/opt.h>n#include<libavutil/imgutils.h>nn//编码和音频编码一样。这都是老套路了。nstaticvoidencode(AVCodecContext*enc_ctx,AVFrame*frame,AVPacket*pkt,nFILE*outfile)n{nintret;nn/*sendtheframetotheencoder*/nif(frame)nprintf("Sendframe%3"PRId64"n",frame->pts);nnret=avcodec_send_frame(enc_ctx,frame);nif(ret<0){nfprintf(stderr,"Errorsendingaframeforencodingn");nexit(1);n}nnwhile(ret>=0){nret=avcodec_receive_packet(enc_ctx,pkt);nif(ret==AVERROR(EAGAIN)||ret==AVERROR_EOF)nreturn;nelseif(ret<0){nfprintf(stderr,"Errorduringencodingn");nexit(1);n}nnprintf("Writepacket%3"PRId64"(size=%5d)n",pkt->pts,pkt->size);nfwrite(pkt->data,1,pkt->size,outfile);nav_packet_unref(pkt);n}n}nnintmain(intargc,char**argv)n{nconstchar*filename,*codec_name;nconstAVCodec*codec;nAVCodecContext*c=NULL;ninti,ret,x,y;nFILE*f;nAVFrame*frame;nAVPacket*pkt;nuint8_tendcode[]={0,0,1,0xb7};nnif(argc<=2){nfprintf(stderr,"Usage:%s<outputfile><codecname>n",argv[0]);nexit(0);n}nfilename=argv[1];ncodec_name=argv[2];nn/*findthempeg1videoencoder*/n//找到解码器ncodec=avcodec_find_encoder_by_name(codec_name);nif(!codec){nfprintf(stderr,"Codec'%s'notfoundn",codec_name);nexit(1);n}n//分配解码器上下文nc=avcodec_alloc_context3(codec);nif(!c){nfprintf(stderr,"Couldnotallocatevideocodeccontextn");nexit(1);n}nnn//设置一些编码必要参数比特率、宽度、高度、时间基、帧率、gop最大的b帧、图像格式。这些参数在remuxing.c讲解过了。n/*putsampleparameters*/nc->bit_rate=400000;n/*resolutionmustbeamultipleoftwo*/nc->width=352;nc->height=288;n/*framespersecond*/nc->time_base=(AVRational){1,25};nc->framerate=(AVRational){25,1};nn/*emitoneintraframeeverytenframesn*checkframepict_typebeforepassingframen*toencoder,ifframe->pict_typeisAV_PICTURE_TYPE_In*thengop_sizeisignoredandtheoutputofencodern*willalwaysbeIframeirrespectivetogop_sizen*/nc->gop_size=10;nc->max_b_frames=1;nc->pix_fmt=AV_PIX_FMT_YUV420P;n//如果是h264设置编码的预设参数慢编码nif(codec->id==AV_CODEC_ID_H264)nav_opt_set(c->priv_data,"preset","slow",0);nn/*openit*/n//打开编码器nret=avcodec_open2(c,codec,NULL);nif(ret<0){nfprintf(stderr,"Couldnotopencodec:%sn",av_err2str(ret));nexit(1);n}n//打开文件nf=fopen(filename,"wb");nif(!f){nfprintf(stderr,"Couldnotopen%sn",filename);nexit(1);n}n//分配AVPacketnpkt=av_packet_alloc();nif(!pkt)nexit(1);n//初始化AVFramenframe=av_frame_alloc();nif(!frame){nfprintf(stderr,"Couldnotallocatevideoframen");nexit(1);n}nframe->format=c->pix_fmt;nframe->width=c->width;nframe->height=c->height;n//为framedata分配缓存空间,在调用这个函数前必须为AVFrame设置格式、如果视频要设置宽高、音频要设置每帧的采样个数nb_samples、通道数channel_layoutn//第二个参数设置缓存数据大小对齐最好设置为0,ffmpeg会根据cpu自动设置对齐nret=av_frame_get_buffer(frame,0);nif(ret<0){nfprintf(stderr,"Couldnotallocatethevideoframedatan");nexit(1);n}nn//就编码1秒25帧数据n/*encode1secondofvideo*/nfor(i=0;i<25;i++){nfflush(stdout);n//使可写n/*makesuretheframedataiswritable*/nret=av_frame_make_writable(frame);nif(ret<0)nexit(1);nnn//写入数据yuv420p格式planneryyyyuuvvn/*prepareadummyimage*/n/*Y*/nfor(y=0;y<c->height;y++){nfor(x=0;x<c->width;x++){nframe->data[0][y*frame->linesize[0]+x]=x+y+i*3;n}n}nn/*CbandCr*/nfor(y=0;y<c->height/2;y++){nfor(x=0;x<c->width/2;x++){nframe->data[1][y*frame->linesize[1]+x]=128+y+i*2;nframe->data[2][y*frame->linesize[2]+x]=64+x+i*5;n}n}n//设置ptsnframe->pts=i;nn/*encodetheimage*/nencode(c,frame,pkt,f);n}nn/*flushtheencoder*/nencode(c,NULL,pkt,f);nn/*addsequenceendcodetohavearealMPEGfile*/n//对于mpege1mpeg2添加一个结束序列码nif(codec->id==AV_CODEC_ID_MPEG1VIDEO||codec->id==AV_CODEC_ID_MPEG2VIDEO)nfwrite(endcode,1,sizeof(endcode),f);nn//释放资源nfclose(f);nnavcodec_free_context(&c);nav_frame_free(&frame);nav_packet_free(&pkt);nnreturn0;n}nn

标签:
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至123@。cc举报,一经查实,本站将立刻删除。

显示全部

收起

最新文章
热门推荐

最新更新 | 文章排行 | 滇ICP备2023006777号 | 网站地图

统计代码