FFMPEG:压缩之H264编码(YUV420P->H264)

简介: <p><strong>720*576@25hz,550帧的yuv420p数据,编码时间13.3秒。</strong></p><p><strong><br></strong></p><img src="http://img.blog.csdn.net/20141126144246140?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbWFvMDUx

720*576@25hz,550帧的yuv420p数据,编码时间13.3秒。



void CTest0Dlg::OnButton5() 
{
// TODO: Add your control notification handler code here
int nWidth = 720;
int nHeight= 576;

av_register_all();
avcodec_register_all();
AVFrame *m_pYUVFrame = new AVFrame[1];;  //YUV帧数据
AVCodecContext *c= NULL;
AVCodec *pCodecH264; //编码器
uint8_t * yuv_buff;//

//查找h264编码器
pCodecH264 = avcodec_find_encoder(CODEC_ID_H264);
if(!pCodecH264)
{
 fprintf(stderr, "h264 codec not found\n");
 exit(1);
}

c= avcodec_alloc_context();
c->bit_rate = 1000000;// put sample parameters 
c->width =720;// 
c->height = 576;// 

// frames per second 
AVRational rate;
rate.num = 1;
rate.den = 25;
c->time_base= rate;//(AVRational){1,25};
c->gop_size = 10; // emit one intra frame every ten frames 
c->max_b_frames=1;
c->thread_count = 1;
c->pix_fmt = PIX_FMT_YUV420P;//PIX_FMT_RGB24;

//av_opt_set(c->priv_data, /*"preset"*/"libvpx-1080p.ffpreset", /*"slow"*/NULL, 0);
//打开编码器
if(avcodec_open(c,pCodecH264)<0)
 TRACE("不能打开编码库");

int size = c->width * c->height;
yuv_buff = (uint8_t *) malloc((size * 3) / 2); // size for YUV 420 

//图象编码
int outbuf_size=100000;
uint8_t * outbuf= (uint8_t*)malloc(outbuf_size); 
int u_size = 0;
FILE *f=NULL; 
char * filename = "e:\\pic\\000.264";
f = fopen(filename, "wb");
if (!f)
{
 TRACE( "could not open %s\n", filename);
 exit(1);
}

AVPacket avpkt;
    FILE *fp;
fp = fopen("d:\\temp\\VIDEO720576.yuv","rb+");

//AVFrame *pTFrame=new AVFrame
while (1)
{
 int len = fread(yuv_buff,1,nWidth* nHeight*3/2,fp);
 if (len==0)
 {
 break;
 }
 avpicture_fill((AVPicture*)m_pYUVFrame, (uint8_t*)yuv_buff, PIX_FMT_YUV420P, nWidth, nHeight);
 int got_packet_ptr = 0;
 av_init_packet(&avpkt);
 avpkt.data = outbuf;
 avpkt.size = outbuf_size;
while(1)
{
 u_size = avcodec_encode_video(c, outbuf, outbuf_size, m_pYUVFrame);


 if (u_size > 0 && u_size<100000)
 {
fwrite(avpkt.data, 1, u_size, f);
break;
 }
}
}

fclose(f); 
fclose(fp); 
delete []m_pYUVFrame;
free(outbuf);
avcodec_close(c);
av_free(c);
MessageBox("over");

}

http://download.csdn.net/detail/mao0514/8202691


相关文章
|
24天前
|
存储 编解码 数据处理
【FFmpeg 视频基本格式】深入理解FFmpeg:从YUV到PCM,解码到编码(三)
【FFmpeg 视频基本格式】深入理解FFmpeg:从YUV到PCM,解码到编码
26 0
|
24天前
|
存储 编解码 数据处理
【FFmpeg 视频基本格式】深入理解FFmpeg:从YUV到PCM,解码到编码(二)
【FFmpeg 视频基本格式】深入理解FFmpeg:从YUV到PCM,解码到编码
33 0
|
3月前
ffmpeg 命令提取音视频数据-ffmpeg导出h265裸流-ffmpeg导出h264裸流
ffmpeg 命令提取音视频数据-ffmpeg导出h265裸流-ffmpeg导出h264裸流
66 0
|
24天前
|
存储 缓存 编解码
【FFmpeg 视频基本格式】深入理解FFmpeg:从YUV到PCM,解码到编码(一)
【FFmpeg 视频基本格式】深入理解FFmpeg:从YUV到PCM,解码到编码
31 0
|
3月前
|
编解码 并行计算
ffmpeg cuda加速 h264->hevc(h265) 缩小存储空间
ffmpeg cuda加速 h264->hevc(h265) 缩小存储空间
46 0
|
8月前
ffmpeg编码报错:more samples than frame size (avcodec_encode_audio2)
ffmpeg编码报错:more samples than frame size (avcodec_encode_audio2)
50 0
ffmpeg编码报错:more samples than frame size (avcodec_encode_audio2)
|
8月前
ffmpeg获取视频大小再压缩视频脚本
ffmpeg获取视频大小再压缩视频脚本
|
12月前
|
存储 编解码 C++
FFmpeg连载5-音视频编码
ffmpeg连载系列
108 0
|
编解码
ffmpeg编码格式转换
ffmpeg编码格式转换
368 0
FFMPEG最简解码H264(NVIDIA硬解)
FFMPEG最简解码H264(NVIDIA硬解)
278 0