第7课 利用FFmpeg将摄像头画面与麦克风数据合成后推送到rtmp服务器

2024-01-01 18:58:34

上节课我们已经拿到了摄像头数据和麦克风数据,这节课我们来看一下如何将二者合并起来推送到rtmp服务器。推送音视频合成流到rtmp服务器地址的流程如下:

1.创建输出流

//初始化输出流上下文
avformat_alloc_output_context2(&outFormatCtx, NULL, "flv", outFileName);
outFormat = outFormatCtx->oformat;
outFormat->video_codec = AV_CODEC_ID_H264;
outFormat->audio_codec = AV_CODEC_ID_AAC;

2.创建视频编码器

vEncodec = avcodec_find_encoder(AV_CODEC_ID_H264);
vEncodeCtx = avcodec_alloc_context3(vEncodec);
vEncodeCtx->codec_id = vEncodec->id;
vEncodeCtx->codec_type = AVMEDIA_TYPE_VIDEO;
vEncodeCtx->bit_rate = 1000000;
vEncodeCtx->width = backWidth;
vEncodeCtx->height = backHeight;
vEncodeCtx->time_base = { 1, 25 };
vEncodeCtx->framerate = { 25, 1 };
vEncodeCtx->gop_size = 25 * 10;
vEncodeCtx->pix_fmt = *vEncodec->pix_fmts;
vEncodeCtx->max_b_frames = 0;
vEncodeCtx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
av_dict_set(&vEncodeOpts, "profile", "baseline", 0);
av_dict_set(&vEncodeOpts, "preset", "ultrafast", 0);
av_dict_set(&vEncodeOpts, "tune", "zerolatency", 0);
avcodec_open2(vEncodeCtx, vEncodec, &vEncodeOpts);

3.创建音频编码器

//音频编码器
aEncodec = avcodec_find_encoder(AV_CODEC_ID_AAC);;
aEncodeCtx = avcodec_alloc_context3(aEncodec);
aEncodeCtx->bit_rate = 64000;
aEncodeCtx->sample_rate = 44100;
aEncodeCtx->block_align = 0;
aEncodeCtx->sample_fmt = AV_SAMPLE_FMT_FLTP;
aEncodeCtx->channel_layout = AV_CH_LAYOUT_STEREO;
aEncodeCtx->channels = 2;
aEncodeCtx->time_base.num = 1;
aEncodeCtx->time_base.den = aEncodeCtx->sample_rate;

aEncodeCtx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
avcodec_open2(aEncodeCtx, aEncodec, NULL);

4.创建输出视频流

//创建输出视频流
videoStream = avformat_new_stream(outFormatCtx, vEncodec);
videoStream->id = outFormatCtx->nb_streams - 1;
videoStream->codecpar->codec_tag = 0;
avcodec_parameters_from_context(videoStream->codecpar, vEncodeCtx);

5.创建输出音频流

//创建输出音频流
audioStream = avformat_new_stream(outFormatCtx, NULL);
audioStream->codecpar->codec_tag = 0;
audioStream->id = outFormatCtx->nb_streams - 1;
avcodec_parameters_from_context(audioStream->codecpar, aEncodeCtx);

6.打开输出流并写入文件头

//打开输出流
av_dump_format(outFormatCtx, 0, outFileName, 1);
ret = avio_open2(&outFormatCtx->pb, outFileName, AVIO_FLAG_READ_WRITE, nullptr, nullptr);

//写文件头
ret = avformat_write_header(outFormatCtx, NULL);

7.分别封装音视频帧并送入编码器

ret = avcodec_send_frame(vEncodeCtx, deVideoFrame);
ret = avcodec_receive_packet(vEncodeCtx, &enVideoPacket);

ret = avcodec_send_frame(aEncodeCtx, deAudioFrame);
ret = avcodec_receive_packet(aEncodeCtx, &enAudioPacket);

8.分别推送音视频流

if (enVideoPacket.size > 0){
 ret = av_interleaved_write_frame(outFormatCtx, &enVideoPacket);				
}

if (enAudioPacket.size > 0){
 ret = av_interleaved_write_frame(outFormatCtx, &enAudioPacket);				
}

9.播放测试rtmp流

打开vlc或第一章写好的rtmp播放器测试,对着麦克风说话如果能从播放器听到声音并看到图像说明成功。

文章来源:https://blog.csdn.net/XiBuQiuChong/article/details/135325730
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。