C++音视频04:音视频编码、生成图片

视频编码

#include <libavutil/log.h>
#include <libavutil/opt.h>
#include <libavcodec/avcodec.h>

static int encode(AVCodecContext *ctx, AVFrame *frame, AVPacket *pkt, FILE *out) {
   
   
    int ret = -1;
    ret = avcodec_send_frame(ctx, frame);
    if (ret < 0) {
   
   
        av_log(NULL, AV_LOG_ERROR, "Failed to send frame to encoder\n");
        goto _END;
    }
    while (ret >= 0) {
   
   
        ret = avcodec_receive_packet(ctx, pkt);
        // AVERROR(EAGAIN)表示出错了
        if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
   
   
            return 0;
        } else if(ret < 0) {
   
   
            return -1;
        }
        fwrite(pkt->data, 1, pkt->size, out);
        av_packet_unref(pkt);
    }
_END:
    return 0;
}

int main(int argc, char *argv[]) {
   
   

    char *dst = NULL;
    char *codecName = NULL;

    const AVCodec *codec = NULL;
    AVCodecContext *ctx = NULL;
    int ret = -1;
    FILE *f = NULL;
    AVFrame *frame = NULL;
    AVPacket *pkt = NULL;

    av_log_set_level(AV_LOG_DEBUG);
    
    // 1. 输入参数
    if (argc < 3) {
   
   
        av_log(NULL, AV_LOG_ERROR, "arguments must more than 3\n");
        goto _ERROR;
    }

    dst = argv[1];
    codecName = argv[2];
    

    // 2. 查找编码器
    codec = avcodec_find_encoder_by_name(codecName);
    if (!codec) {
   
   
        av_log(NULL, AV_LOG_ERROR, "don't find codec name %s\n", codecName);
        goto _ERROR;
    }
    // 3. 创建编码器上下文
    ctx = avcodec_alloc_context3(codec);
    if (!ctx) {
   
   
        av_log(NULL, AV_LOG_ERROR, "No memory\n");
        goto _ERROR;
    }
    // 4. 设置编码器参数
    ctx->width = 640;
    ctx->height = 480;
    ctx->bit_rate = 500000;

    // 时间基和帧率
    ctx->time_base = (AVRational){
   
   1, 25};
    ctx->framerate = (AVRational){
   
   25, 1};
    
    // 设置每10帧一个gop
    ctx->gop_size = 10;
    // 设置一个gop中最多有一个b帧
    ctx->max_b_frames = 1;
    // 设置yuv格式
    ctx->pix_fmt = AV_PIX_FMT_YUV422P;

    if (codec->id == AV_CODEC_ID_H264) {
   
   
        av_opt_set(ctx->priv_data, "preset", "slow", 0);
    }

    // 5. 编码器与编码器上下文绑定
    ret = avcodec_open2(ctx, codec, NULL);
    if (ret < 0) {
   
   
        av_log(ctx, AV_LOG_ERROR, "Don't open codec: %s", av_err2str(ret));
        goto _ERROR;
    }
    // 6. 创建输出文件
    f = fopen(dst, "wb");
    if (!f) {
   
   
        av_log(NULL, AV_LOG_ERROR, "Don't open file %s\n", dst);
        goto _ERROR;
    }

    // 7. 创建AVFrame
    frame = av_frame_alloc();
    if (!frame) {
   
   
        av_log(NULL, AV_LOG_ERROR, "No memory\n");
        goto _ERROR;
    }
    // 必须赋值才能获取到buffer
    frame->width = ctx->width;
    frame->height = ctx->height;
    frame->format = ctx->pix_fmt;

    // frame中真正存储数据的位置不会被av_frame_alloc生成,还需要调用av_frame_get_buffer获取
    ret = av_frame_get_buffer(frame, 0);
    if (ret < 0) {
   
   
        av_log(NULL, AV_LOG_ERROR, "Could't not allocate the video frame\n");
        goto _ERROR;
    }
    // 8. 创建AVPacket
    pkt = av_packet_alloc();
    if (!pkt) {
   
   
        av_log(NULL, AV_LOG_ERROR, "No memory\n");
        goto _ERROR;
    }
    // 9. 生成视频内容
    // 这里是虚拟生成25帧,即一秒钟的数据
    for (int i = 0; i < 25; ++i) {
   
   
        // 确保av_frame中的空间data可用,如果当前data域被锁定了,就会自动指定一个新的data
        ret = av_frame_make_writable(frame);
        if (ret < 0) {
   
   
            break;
        }

        // Y分量
        for (int y = 0; y < ctx->height; y++) {
   
   
            for (int x = 0; x < ctx->width; x++) {
   
   
                // data[0] 指的是yuv中的y,[frame->linesize[0] 指的是行的大小, x表示横轴位移]
                // x + y + i * 3表示Y分量随着x和y变化而渐变
                frame->data[0][y * frame->linesize[0] + x] = x + y + i * 3;
            }
        }

        // UV分量
        for (int y = 0; y < ctx->height; y++) {
   
   
            // 对应uv分量 422中都是y分量的一半,所以除以2
            for (int x = 0; x < ctx->width / 2; x++) {
   
   
                // U分量 128在U分量代表黑色, 后面的2是任意的
                frame->data[1][y * frame->linesize[1] + x] = 128 + y + i * 2;
                // V分量 64在V分量中代表黑色, 后面的5是任意的
                frame->data[2][y * frame->linesize[2] + x] = 64 + x + i * 5;
            }
        }

        frame->pts = i;

        // 10. 编码
        ret = encode(ctx, frame, pkt, f
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值