从零开始学习音视频编程技术(十六) 采集屏幕编码H264

来源:互联网 发布:司法淘宝拍卖网房产 编辑:程序博客网 时间:2024/06/01 17:21

前面介绍了采集屏幕图像并转换成YUV420p。传送门


YUV420p数据是原始的图像数据,100张1920x1080的图像 总大小就达到了300M。太可怕了! 可见,这样直接写入文件是行不通的。因此我们需要把它编码成h264之后在写入文件。至于什么是h264,前面有介绍,这里就不介绍了。


废话不多说,直接进入正文吧。


编码h264还是一样使用ffmpeg,方法如下:


1.打开编码器

1
2
3
4
    AVCodecContext* pCodecCtx;    AVCodec* pCodec;
 
    uint8_t* picture_buf;
    AVFrame* picture;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
bool H264Encoder::openEncoder()
{
 
    int size;
    int in_w = mWidth;
    int in_h = mHeight;//宽高
 
    //查找h264编码器
    pCodec = avcodec_find_encoder(AV_CODEC_ID_H264);
    if(!pCodec)
    {
      fprintf(stderr, "h264 codec not found
");
      exit(1);
    }
 
    pCodecCtx = avcodec_alloc_context3(pCodec);
 
    pCodecCtx->codec_id = AV_CODEC_ID_H264;
    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 = 15;//帧率(既一秒钟多少张图片)
    pCodecCtx->bit_rate = mBitRate; //比特率(调节这个大小可以改变编码后视频的质量)
    pCodecCtx->gop_size=12;
 
    // some formats want stream headers to be separate
    if (pCodecCtx->flags & AVFMT_GLOBALHEADER)
        pCodecCtx->flags |= CODEC_FLAG_GLOBAL_HEADER;
 
    // Set Option
    AVDictionary *param = 0;
    //H.264
    //av_dict_set(&param, "preset", "slow", 0);
    av_dict_set(&param, "preset""superfast", 0);
    av_dict_set(&param, "tune""zerolatency", 0);  //实现实时编码
 
 
    pCodec = avcodec_find_encoder(pCodecCtx->codec_id);
    if (!pCodec){
      printf("Can not find video encoder! 没有找到合适的编码器!
");
      return false;
    }
 
    if (avcodec_open2(pCodecCtx, pCodec,&param) < 0){
      printf("Failed to open video encoder! 编码器打开失败!
");
      return false;
    }
 
    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);
 
    return true;
 
}


2.编码

编码之前的数据必须是Yuv420p格式,我们前面已经得到了这样的数据。编码使用avcodec_encode_video2来实现:

1
2
3
4
5
6
7
8
9
10
11
12
13
picture->data[0] = node.buffer;     // 亮度Y            
picture->data[1] = node.buffer + y_size;  // U
picture->data[2] = node.buffer + y_size*5/4; // V
 
int got_picture=0;
//编码
int ret = avcodec_encode_video2(pCodecCtx, &pkt,picture, &got_picture);
 
if (got_picture==1)            
{
//   bool isKeyFrame = pkt.flags & AV_PKT_FLAG_KEY; //判断是否关键帧
     int w = fwrite(pkt.data,1,pkt.size,h264Fp); //写入文件中 (h264的裸数据 直接写入文件 也可以播放  因为这里包含H264关键帧)
}



最近时间比较少,代码就不做太多的解释了,直接上完整的工程吧。


完整工程下载地址:http://download.csdn.net/detail/qq214517703/9654163


最后生成的out.h264可以直接用普通播放器打开播放。

保存后的文件比起之前可真是小了几百倍啊!

注:由于h264没有时间戳,只有帧率,这里设定的是15,然而我们采集桌面的时候,一秒却采集不到15张,所以用播放器播放的时候 速度上会快很多 是正常现象。



1 0
原创粉丝点击