FFmpeg———encode_video(学习)

本文详细描述了如何使用C++实现一个针对图片的encode_video函数,通过AVCodecContext和AVFrame等结构,进行H.264编码,同时记录了在编译和编码过程中遇到的问题及解决方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言

encode_video:实现了对图片使用指定编码进行编码,生成可播放的视频流,编译时出现了一些错误,做了一些调整。
基本流程:
1、获取指定的编码器
2、编码器内存申请
3、编码器上下文内容参数设置
4、打开编码器
5、申请数据帧内存
6、模拟图片
7、编码

源码

测试代码,做了部分修改

#include <iostream>

using namespace std;


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

extern"C"
{
   
   
#include "libavcodec/avcodec.h"
#include "libavutil/opt.h"
#include "libavutil/imgutils.h"
#include "libavutil/error.h"
}

static void encode(AVCodecContext* enc_ctx, AVFrame* frame, AVPacket* pkt,
    FILE* outfile)
{
   
   
    int ret;

    /* send the frame to the encoder */
    if (frame)
        printf("Send frame %lld\n", frame->pts);  //修改 lld 替换 PRId64

    /*avcodec_send_frame 与 avcodec_receive_packet 配合使用*/
    ret = avcodec_send_frame(enc_ctx, frame);
    if (ret < 0) {
   
   
        fprintf(stderr, "Error sending a frame for encoding\n");
        exit(1);
    }

    while (ret >= 0) {
   
   
        ret = avcodec_receive_packet(enc_ctx, pkt);
        if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
            return;
        else if (ret < 0) {
   
   
            fprintf(stderr, "Error during encoding\n");
            exit(1);
        }

        
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Liu Zz

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值