本文介绍一个最简单的基于FFMPEG的视频编码器。该编码器实现了YUV420P的像素数据编码为H.264的压缩编码数据。编码器代码十分简单,可是每一行代码都非常重要,适合好好研究一下。弄清楚了本代码也就基本弄清楚了FFMPEG的编码流程。眼下我尽管已经调通了程序,可是还是有些地方没有全然搞明确,须要下一步继续探究然后补充内容。

本程序使用最新版的类库(编译时间为2014.5.6),开发平台为VC2010。全部的配置都已经做好,仅仅须要执行就能够了。

流程图(2014.9.29更新)

以下附一张使用FFmpeg编码视频的流程图。使用该流程,不仅能够编码H.264的视频,并且能够编码MPEG4/MPEG2/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)。

代码

贴上代码:

/*
*最简单的基于FFmpeg的视频编码器
*Simplest FFmpeg Video Encoder
*
*雷霄骅 Lei Xiaohua
*leixiaohua1020@126.com
*中国传媒大学/数字电视技术
*Communication University of China / Digital TV Technology
*http://blog.csdn.net/leixiaohua1020
*
*本程序实现了YUV像素数据编码为视频码流(H264,MPEG2,VP8等等)。
*是最简单的FFmpeg视频编码方面的教程。
*通过学习本样例能够了解FFmpeg的编码流程。
*This software encode YUV420P data to H.264 bitstream.
*It's the simplest video encoding software based on FFmpeg.
*Suitable for beginner of FFmpeg
*/ #include "stdafx.h" extern "C"
{
#include "libavcodec\avcodec.h"
#include "libavformat\avformat.h"
#include "libswscale\swscale.h"
}; int flush_encoder(AVFormatContext *fmt_ctx,unsigned int stream_index)
{
int ret;
int got_frame;
AVPacket enc_pkt;
if (!(fmt_ctx->streams[stream_index]->codec->codec->capabilities &
CODEC_CAP_DELAY))
return 0;
while (1) {
printf("Flushing stream #%u encoder\n", stream_index);
//ret = encode_write_frame(NULL, stream_index, &got_frame);
enc_pkt.data = NULL;
enc_pkt.size = 0;
av_init_packet(&enc_pkt);
ret = avcodec_encode_video2 (fmt_ctx->streams[stream_index]->codec, &enc_pkt,
NULL, &got_frame);
av_frame_free(NULL);
if (ret < 0)
break;
if (!got_frame)
{ret=0;break;}
printf("编码成功1帧!\n");
/* mux encoded frame */
ret = av_write_frame(fmt_ctx, &enc_pkt);
if (ret < 0)
break;
}
return ret;
} int _tmain(int argc, _TCHAR* argv[])
{
AVFormatContext* pFormatCtx;
AVOutputFormat* fmt;
AVStream* video_st;
AVCodecContext* pCodecCtx;
AVCodec* pCodec; uint8_t* picture_buf;
AVFrame* picture;
int size; FILE *in_file = fopen("src01_480x272.yuv", "rb"); //视频YUV源文件
int in_w=480,in_h=272;//宽高
int framenum=50;
const char* out_file = "src01.h264"; //输出文件路径 av_register_all();
//方法1.组合使用几个函数
pFormatCtx = avformat_alloc_context();
//猜格式
fmt = av_guess_format(NULL, out_file, NULL);
pFormatCtx->oformat = fmt; //方法2.更加自己主动化一些
//avformat_alloc_output_context2(&pFormatCtx, NULL, NULL, out_file);
//fmt = pFormatCtx->oformat; //注意输出路径
if (avio_open(&pFormatCtx->pb,out_file, AVIO_FLAG_READ_WRITE) < 0)
{
printf("输出文件打开失败");
return -1;
} video_st = av_new_stream(pFormatCtx, 0);
if (video_st==NULL)
{
return -1;
}
pCodecCtx = video_st->codec;
pCodecCtx->codec_id = fmt->video_codec;
pCodecCtx->codec_type = AVMEDIA_TYPE_VIDEO;
pCodecCtx->pix_fmt = 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=250;
//H264
//pCodecCtx->me_range = 16;
//pCodecCtx->max_qdiff = 4;
pCodecCtx->qmin = 10;
pCodecCtx->qmax = 51;
//pCodecCtx->qcompress = 0.6;
//输出格式信息
av_dump_format(pFormatCtx, 0, out_file, 1); pCodec = avcodec_find_encoder(pCodecCtx->codec_id);
if (!pCodec)
{
printf("没有找到合适的编码器!\n");
return -1;
}
if (avcodec_open2(pCodecCtx, pCodec,NULL) < 0)
{
printf("编码器打开失败!\n");
return -1;
}
picture = avcodec_alloc_frame();
size = avpicture_get_size(pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height);
picture_buf = (uint8_t *)av_malloc(size);
avpicture_fill((AVPicture *)picture, picture_buf, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height); //写文件头
avformat_write_header(pFormatCtx,NULL); AVPacket pkt;
int y_size = pCodecCtx->width * pCodecCtx->height;
av_new_packet(&pkt,y_size*3); for (int i=0; i<framenum; i++){
//读入YUV
if (fread(picture_buf, 1, y_size*3/2, in_file) < 0)
{
printf("文件读取错误\n");
return -1;
}else if(feof(in_file)){
break;
}
picture->data[0] = picture_buf; // 亮度Y
picture->data[1] = picture_buf+ y_size; // U
picture->data[2] = picture_buf+ y_size*5/4; // V
//PTS
picture->pts=i;
int got_picture=0;
//编码
int ret = avcodec_encode_video2(pCodecCtx, &pkt,picture, &got_picture);
if(ret < 0)
{
printf("编码错误!\n");
return -1;
}
if (got_picture==1)
{
printf("编码成功1帧!\n");
pkt.stream_index = video_st->index;
ret = av_write_frame(pFormatCtx, &pkt);
av_free_packet(&pkt);
}
}
//Flush Encoder
int ret = flush_encoder(pFormatCtx,0);
if (ret < 0) {
printf("Flushing encoder failed\n");
return -1;
} //写文件尾
av_write_trailer(pFormatCtx); //清理
if (video_st)
{
avcodec_close(video_st->codec);
av_free(picture);
av_free(picture_buf);
}
avio_close(pFormatCtx->pb);
avformat_free_context(pFormatCtx); fclose(in_file); return 0;
}

软件执行截图(受限于文件体积,原始YUV帧数非常少):

编码前的YUV序列:

编码后的H.264码流:

下载地址:

http://download.csdn.net/detail/leixiaohua1020/7324115

修正记录:

之前发现编码后的H.264码流与YUV输入的帧数不同。经过观察对照其它程序后发现须要调用flush_encoder()将编码器中剩余的视频帧输出。已经将该问题修正。

下载地址(修正后):

http://download.csdn.net/detail/leixiaohua1020/7466649

SourceForge上已经更新。

PUDN源码(修正后):

http://www.pudn.com/downloads644/sourcecode/multimedia/detail2605258.html

SourceForge项目地址:

https://sourceforge.net/projects/simplestffmpegvideoencoder/

最简单的基于FFMPEG的视频编码器(YUV编码为H.264)的更多相关文章

  1. 最简单的基于FFmpeg的视频编码器-更新版(YUV编码为HEVC(H.265))

    ===================================================== 最简单的基于FFmpeg的视频编码器文章列表: 最简单的基于FFMPEG的视频编码器(YUV ...

  2. 最简单的基于FFMPEG的转码程序

    本文介绍一个简单的基于FFmpeg的转码器.它可以将一种视频格式(包括封转格式和编码格式)转换为另一种视频格式.转码器在视音频编解码处理的程序中,属于一个比较复杂的东西.因为它结合了视频的解码和编码. ...

  3. 最简单的基于FFmpeg的编码器-纯净版(不包含libavformat)

    ===================================================== 最简单的基于FFmpeg的视频编码器文章列表: 最简单的基于FFMPEG的视频编码器(YUV ...

  4. 最简单的基于FFmpeg的内存读写的例子:内存转码器

    ===================================================== 最简单的基于FFmpeg的内存读写的例子系列文章列表: 最简单的基于FFmpeg的内存读写的 ...

  5. 最简单的基于FFmpeg的内存读写的例子:内存播放器

    ===================================================== 最简单的基于FFmpeg的内存读写的例子系列文章列表: 最简单的基于FFmpeg的内存读写的 ...

  6. (转)最简单的基于FFmpeg的内存读写的例子:内存播放器

    ffmpeg内存播放解码 目录(?)[+] ===================================================== 最简单的基于FFmpeg的内存读写的例子系列文章 ...

  7. 最简单的基于FFmpeg的libswscale的示例(YUV转RGB)

    ===================================================== 最简单的基于FFmpeg的libswscale的示例系列文章列表: 最简单的基于FFmpeg ...

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

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

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

    原文转自 https://blog.csdn.net/leixiaohua1020/article/details/25346147/ 伴随着毕业论文的完成,这两天终于腾出了空闲,又有时间搞搞FFMP ...

随机推荐

  1. 在ios开发中有多少常用的加密解密方式(备用)

    最常用的是MD5和base64编码,还有DES 3DES AES加密 ios怎么实现RAS加密解密 最近几天折腾了一下如何在iOS上使用RSA来加密.iOS上并没有直接的RSA加密API.但是iOS提 ...

  2. UML 类图的关系

    1.  关联关系 1.1 单向关联 . public class ClassA { private ClassB bVar; } public class ClassB { //... } 1.2  ...

  3. Ubuntu开机自动挂载Windows分区

    转自Ubuntu 12.04开机自动挂载Windows分区 1.查看系统磁盘号 sd2,sd5,sd7分别对应我windows的C,D,F盘,也是本次要添加到开机挂载的,E盘为wubi安装盘. 2.查 ...

  4. 在安全层面,企业如何获得更好的投资回报率 ROI?

    前言 任何企业对投资都有回报的要求,回报可能是直接的「利润」,达到短期.长期的目标,或者通过投资减少损失.因此每个项目的决策者在每笔投资前都要衡量 ROI,证明该投资能达到的效果和收益,以便在项目结束 ...

  5. IE连EXCHANGE的HTTPS时,出现错误

    "There is a problem with this website's security certificate" 这种问题,一般是SSL或CA认证证书的问题. 一般从服务 ...

  6. VS2008编译器编译出来的文件比mingw编译的要几乎小一半

    为什么要在VS2008中使用QT静态编译呢?很简单,因为VS2008编译器编译出来的文件比mingw编译的要几乎小一半. 好了现在我们来做些准备工作,VS2008自然要安装的,然后打上SP1的补丁.然 ...

  7. Github上更新自己Fork的代码

    一.前提本文的前提是你已经在github上fork了别人的分支,并且弄好了跟github的ssh连接.相关配置详情参考:https://help.github.com二.详细操作 检出自己在githu ...

  8. iOS设备下添加button的按下效果

    在触屏上, button的按下效果用:active触发, 但是我发现iOS设备上:active效果并没有生效. :active pseudo-class doesn't work in mobile ...

  9. 江中微型统计分析软件V1.0版本完成

    中文名称:江中微型统计分析软件 英文名称: 项目名称:JXUTCMMSAS 项目地址:保密 在研究生最后历时1年的时间里,完成了江中微型统计分析软件V1.0,后续还在不断更新中,将自己的改进算法.机器 ...

  10. 如何修复在Microsoft Azure中“虚拟机防火墙打开,关闭RDP的连接端口”问题

     注:下列步骤并不一定适用所有场景,提供思路,请灵活应用 我们在使用Microsoft Azure 中Windows 虚拟机,有时会发生错误打开防火墙或一些管家软件错误的关闭了"远程桌面 ...