P10 RV1126推流项目——ffmpeg输出参数初始化

 前言 

                             

从本章开始我们将要学习嵌入式音视频的学习了 ,使用的瑞芯微的开发板

🎬 个人主页:@ChenPi

🐻推荐专栏1: 《C++_@ChenPi的博客-CSDN博客》✨✨✨ 

🔥 推荐专栏2: 《Linux C应用编程(概念类)_@ChenPi的博客-CSDN博客》✨✨✨

🛸推荐专栏3:《嵌入式音视频_@ChenPi的博客-CSDN博客
🌺本篇简介  :本章主要讲解ffmpeg模块输出参数的初始化

 

今天讲解的部分就是继上章未讲完的部分继续讲解

首先我们看上图红框里面的函数,函数内部就是实现ffmpeg初始化,合成复合流以及推流

里面内容比价多,我可能分为三章去分别讲解,今天我们先讲一下ffmpeg的参数初始化

01  ffmpeg输出模块结构体

在FFMPEG输出模块中用到了很多个结构体,分别是:AVFormatContext、AVStream、AVCodec、AVCodecContext、AVIOContext

  1. AVFormatContext:是存储音视频封装格式中包含的信息的结构体,它是FFMPEG中的核心,所有的文件封装、编码都是从它开始
  2.  AVStream:AVStream主要存储了音频流、视频流的具体信息
  3. ​​​​​​​ AVCodec:AVCodec是存储编解码信息的结构体
  4. ​​​​​​​ AVCodecContext:AVCodecContext 是编解码上下文,它主要的功能是除了存储AVCodec的信息外,还保存了额外的编码属性
  5. AVIOContext:其中AVIOContext是FFMPEG管理输入输出数据的结构体

 02 ffmpeg初始化代码分析

 这个代我们要我们要先从这个结构体开始看

这个结构体会有些许复杂

typedef struct
{
    AVStream *stream;  /*主要储存了音视频流的具体信息*/
    AVCodecContext *enc;   //编解码上下文,它主要的功能是除了存储 AVCodec 的信息外,还保存了额外的编码属性
    int64_t next_timestamp;  //
    int samples_count;
    AVPacket *packet;   //压缩后的数据和关于这些数据的一些附加的信息,如显示时间戳(pts),解码时间戳(dts),数据时长(duration),所在流媒体的索引(stream_index)等等。
} OutputStream;

typedef struct
{
    unsigned int config_id;
    int protocol_type;                    //流媒体TYPE
    char network_addr[NETWORK_ADDR_LENGTH];//流媒体地址
    enum AVCodecID video_codec; //视频编码器ID
    enum AVCodecID audio_codec; //音频编码器ID
    OutputStream video_stream; //VIDEO的STREAM配置
    OutputStream audio_stream; //AUDIO的STREAM配置
    AVFormatContext *oc; //是存储音视频封装格式中包含的信息的结构体,也是FFmpeg中统领全局的结构体,对文件的封装、编码操作从这里开始。
} RKMEDIA_FFMPEG_CONFIG; //FFMPEG配置

 

这个结构体很重要,这里面保存着配置相关的所有信息

其中最重要的结构体是AVFormatContext,里面存储音视频封装格式中包含的信息的结构体,

也是FFmpeg中统领全局的结构体,对文件的封装、编码操作从这里开始。

 将一些简单的参数配置完后,我们看一下如何初始化FFmpeg

 ffmpeg输出模块初始化大概就是这八步,我们一步一步解析

2.1 根据需要配置码流属性avformat_alloc_output_context2()

函数原型:

int avformat_alloc_output_context2(AVFormatContext **ctx, 
AVOutputFormat *oformat, const char *format_name, const char *filename)
  1. 第一个传输参数:AVFormatContext结构体指针的指针,是存储音视频封装格式中包含的信息的结构体,所有对文件的封装、编码都是从这个结构体开始。
  2. 第二个传输参数:AVOutputFormat的结构体指针,它主要存储复合流信息的常规配置,默认为设置NULL。
  3. 第三个传输参数:format_name指的是复合流的格式,比方说:flv、ts、mp4等等
  4. 第四个传输参数:filename是输出地址,输出地址可以是本地文件(如:xxx.mp4、xxx.ts等等)。也可以是网络流地址(如:rtmp://xxx.xxx.xxx.xxx:1935/live/01)

2.2配置推流器编码参数和AVStream结构体

AVStream主要是存储流信息结构体,这个流信息包含音频流和视频流。

创建的API是avformat_new_stream如下

 函数原型:

AVStream *avformat_new_stream(AVFormatContext *s, const AVCodec *c)
  • 参数1:AVFormatContext的结构体指针
  • 参数2:AVDictionary结构体指针的指针
  • 返回值:成功返回AVStream结构体指针,失败返回NULL

创建输出码流AVStream, AVStream是存储每一个视频/音频流信息的结构体


 2.3找到编码器avcodec_find_encoder

找到对应的编码ID

函数原型:

AVCodec *avcodec_find_encoder(AVCodecID id)

 

  •  参数1:请求编码器的编号
  • 返回值:返回查找到的解码器(没有找到就返回NULL

 2.4 avcodec_alloc_context3分配AVCodecContext结构体

函数原型:

AVCodecContext *avcodec_alloc_context3(const AVCodec *codec)

功能

  • 分配一个AVCodecContext,并将其字段设置为默认值。
  • 结果由avcodec_free_context()释放

参数

  • 如果非null,分配私有数据并初始化给定编解码器的默认值。使用不同的编解码器调用avcodec_open2()是非法的。
  • 如果为NULL,那么特定于编解码器的默认值将不会被初始化,这可能会导致默认设置不理想(这对于编码器(例如libx264)尤为重要)。

返回值

  • 一个用默认值填充的AVCodecContext,失败时为NULL。

 2.5 设置AVCodecContext的参数

 

FFMPEG的视频编码参数如:分辨率(WIDTHHEIGHT)、时间基(time_base)、 帧率(r_frame_rate)、GOP_SIZE等都需要和右边VENC的参数要一一对应起来。其中time_base的值要和视频帧率必须要一致。

FFMPEG的音频编码参数如:音频采样格式(sample_fmt)、音频码率(bit_rate)、 音频采样率(sample_rate)、 音频布局(channel_layout)、 音频通道(channels)、 时间基(time_base)等都需要和右边AENC的参数一一对应。其中time_base的值必须要和音频采样率要一致。

 

AV_CODEC_FLAG_GLOBAL_HEADER发送视频数据的时候都会在关键帧前面添加SPS/PPS,这个标识符在FFMPEG初始化的时候都需要添加。

 2.6 使能推流编码器

这里的open_videoopen_audio就是使能推流编码器,其中avcodec_open2让编码器和编码器上下文进行关联。并用avcodec_parameters_from_context把刚初始化的上下文传输到AVStream的编解码器。

 2.7打开IO文件操作

使用avio_open打开对应的文件,注意这里的文件不仅是指本地的文件也指的是网络流媒体文件,下面是avio_open的定义。

int avio_open(AVIOContext **s, const char *url, int flags);
  1. 第一个参数:AVIOContext的结构体指针,它主要是管理数据输入输出的结构体
  2. 第二个参数: url地址,这个URL地址既包括本地文件如(xxx.ts、xxx.mp4),也可以是网络流媒体地址,如(rtmp://192.168.22.22:1935/live/01)等
  3. 第三个参数:flags标识符

#define AVIO_FLAG_READ  1                                      /**< read-only */

#define AVIO_FLAG_WRITE 2                                      /**< write-only */

#define AVIO_FLAG_READ_WRITE (AVIO_FLAG_READ|AVIO_FLAG_WRITE)  /**< read-write pseudo flag */

2.8  avformat_write_header对头部进行初始化

int avformat_write_header(AVFormatContext *s, AVDictionary **options);
  • 第一个参数:传递AVFormatContext结构体指针
  • 第二个参数:传递AVDictionary结构体指针的指针

 

  • 22
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
以下是使用FFmpeg库进行初始化代码示例: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <errno.h> #include <signal.h> #include <sys/time.h> #include <sys/select.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <pthread.h> #include <libavcodec/avcodec.h> #include <libavformat/avformat.h> #include <libavutil/avutil.h> #include <libavutil/pixdesc.h> #include <libavutil/time.h> #include <libswscale/swscale.h> int main(int argc, char *argv[]) { AVFormatContext *fmt_ctx = NULL; AVOutputFormat *out_fmt = NULL; AVStream *video_st = NULL; AVCodecContext *codec_ctx = NULL; AVCodec *codec = NULL; int ret; // 初始化FFmpeg库 av_register_all(); avformat_network_init(); // 打开输出文件 ret = avformat_alloc_output_context2(&fmt_ctx, NULL, "flv", "rtmp://127.0.0.1/live/stream"); if (ret < 0) { printf("Could not allocate output format context\n"); return -1; } out_fmt = fmt_ctx->oformat; // 添加音视频 video_st = avformat_new_stream(fmt_ctx, NULL); if (!video_st) { printf("Could not create video stream\n"); return -1; } codec_ctx = video_st->codec; // 设置编码器参数 codec_ctx->codec_id = out_fmt->video_codec; codec_ctx->codec_type = AVMEDIA_TYPE_VIDEO; codec_ctx->width = 640; codec_ctx->height = 480; codec_ctx->time_base.num = 1; codec_ctx->time_base.den = 25; codec = avcodec_find_encoder(codec_ctx->codec_id); if (!codec) { printf("Could not find encoder\n"); return -1; } // 打开编码器 ret = avcodec_open2(codec_ctx, codec, NULL); if (ret < 0) { printf("Could not open encoder\n"); return -1; } // 打开输出 ret = avio_open(&fmt_ctx->pb, fmt_ctx->filename, AVIO_FLAG_WRITE); if (ret < 0) { printf("Could not open output file\n"); return -1; } // 写文件头 ret = avformat_write_header(fmt_ctx, NULL); if (ret < 0) { printf("Error occurred when writing file header\n"); return -1; } // AVFrame *frame = av_frame_alloc(); AVPacket pkt; int i; for (i = 0; i < 100; i++) { av_init_packet(&pkt); pkt.data = NULL; pkt.size = 0; // 生成测试图像 uint8_t *data[1]; int linesize[1]; int height = codec_ctx->height; int width = codec_ctx->width; data[0] = (uint8_t*)malloc(width * height * 3); int y; for (y = 0; y < height; y++) { memset(data[0] + y * width * 3, y + i * 3, width * 3); } linesize[0] = width * 3; // 填充AVFrame frame->format = codec_ctx->pix_fmt; frame->width = codec_ctx->width; frame->height = codec_ctx->height; av_image_fill_arrays(frame->data, frame->linesize, data[0], codec_ctx->pix_fmt, codec_ctx->width, codec_ctx->height, 1); // 编码并发送数据 int got_picture = 0; ret = avcodec_encode_video2(codec_ctx, &pkt, frame, &got_picture); if (ret < 0) { printf("Error encoding video frame\n"); return -1; } if (got_picture) { pkt.stream_index = video_st->index; ret = av_write_frame(fmt_ctx, &pkt); if (ret < 0) { printf("Error writing video frame\n"); return -1; } } av_free_packet(&pkt); av_freep(&data[0]); } // 写文件尾 av_write_trailer(fmt_ctx); // 释放资源 avio_close(fmt_ctx->pb); avcodec_close(codec_ctx); avformat_free_context(fmt_ctx); av_frame_free(&frame); return 0; } ``` 这段代码将以FLV格式将一组测试图像到`rtmp://127.0.0.1/live/stream`位置。你可以根据需要修改地址等参数

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

@ChenPi

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

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

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

打赏作者

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

抵扣说明:

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

余额充值