如果已經完成FFMPEG錄制視訊儲存到本地的功能,完成RTMP推流隻需要修改幾行代碼即可完成。
推流到RTMP伺服器與儲存到本地的代碼基本相同,主要是輸出位址不一樣。儲存到本地就是本地檔案名稱,推流到RTMP伺服器,就将檔案名稱換成RTMP伺服器位址即可。
完整項目代碼下載下傳位址(下載下傳即可編譯運作,不懂可以私信):
https://download.csdn.net/download/xiaolong1126626497/19323232 主要修改的代碼:filename="rtmp://js.live-send.acg.tv/live-js/?xxxxxxxx"
avformat_alloc_output_context2(&oc,nullptr,"flv",filename); //檔案名稱替換成網絡位址
//指定編碼器
fmt->video_codec=AV_CODEC_ID_H264;
fmt->audio_codec=AV_CODEC_ID_AAC;
FFMPEG儲存視訊到本地的文章:
https://blog.csdn.net/xiaolong1126626497/article/details/104858499video_audio_encode.cpp的完整代碼:
#include "video_audio_encode.h"
class VideoAudioEncode videoaudioencode;
Thread_VideoAudioEncode thread_VideoenCode; //視訊音頻編碼的線程
char audio_buffer[AUDIO_BUFFER_MAX_SIZE]; //音頻緩存
int audio_buffer_r_count=0;
int audio_buffer_w_count=0;
//音頻相關參數設定
#define AUDIO_RATE_SET 44100 //音頻采樣率
#define AUDIO_BIT_RATE_SET 64000 //設定碼率
#define AUDIO_CHANNEL_SET AV_CH_LAYOUT_MONO //AV_CH_LAYOUT_MONO 單聲道 AV_CH_LAYOUT_STEREO 立體聲
#define STREAM_DURATION 60*10.0 //錄制時間秒機關
#define STREAM_FRAME_RATE 30 /* 30 images/s 圖檔是多少幀1秒*/
#define STREAM_PIX_FMT AV_PIX_FMT_YUV420P /* default pix_fmt 視訊圖像格式 */
#define SCALE_FLAGS SWS_BICUBIC
// 單個輸出AVStream的包裝器
typedef struct OutputStream {
AVStream *st;
AVCodecContext *enc;
/* 下一幀的點數*/
int64_t next_pts;
int samples_count;
AVFrame *frame;
AVFrame *tmp_frame;
float t, tincr, tincr2;
struct SwsContext *sws_ctx;
struct SwrContext *swr_ctx;
} OutputStream;
static int write_frame(AVFormatContext *fmt_ctx, const AVRational *time_base, AVStream *st, AVPacket *pkt)
{
/*将輸出資料包時間戳值從編解碼器重新調整為流時基 */
av_packet_rescale_ts(pkt, *time_base, st->time_base);
pkt->stream_index = st->index;
/*将壓縮的幀寫入媒體檔案*/
return av_interleaved_write_frame(fmt_ctx, pkt);
}
/* 添加輸出流。 */
static void add_stream(OutputStream *ost, AVFormatContext *oc,
AVCodec **codec,
enum AVCodecID codec_id)
{
AVCodecContext *c;
int i;
/* find the encoder */
*codec = avcodec_find_encoder(codec_id);
if (!(*codec)) {
qDebug("Could not find encoder for '%s'\n",avcodec_get_name(codec_id));
exit(1);
}
ost->st = avformat_new_stream(oc, nullptr);
if (!ost->st) {
qDebug("Could not allocate stream\n");
exit(1);
}
ost->st->id = oc->nb_streams-1;
c = avcodec_alloc_context3(*codec);
if (!c) {
qDebug("Could not alloc an encoding context\n");
exit(1);
}
ost->enc = c;
switch ((*codec)->type) {
case AVMEDIA_TYPE_AUDIO:
//設定資料格式
//c->sample_fmt = (*codec)->sample_fmts ? (*codec)->sample_fmts[0] : AV_SAMPLE_FMT_FLTP;
c->sample_fmt = AV_SAMPLE_FMT_FLTP;
c->bit_rate = AUDIO_BIT_RATE_SET; //設定碼率
c->sample_rate = AUDIO_RATE_SET; //音頻采樣率
//編碼器支援的采樣率
if ((*codec)->supported_samplerates)
{
c->sample_rate = (*codec)->supported_samplerates[0];
for (i = 0; (*codec)->supported_samplerates[i]; i++)
{
//判斷編碼器是否支援
if ((*codec)->supported_samplerates[i] == AUDIO_RATE_SET)
{
c->sample_rate = AUDIO_RATE_SET;
}
}
}
//設定采樣通道
c->channels= av_get_channel_layout_nb_channels(c->channel_layout);
c->channel_layout = AUDIO_CHANNEL_SET; //AV_CH_LAYOUT_MONO 單聲道 AV_CH_LAYOUT_STEREO 立體聲
if ((*codec)->channel_layouts)
{
c->channel_layout = (*codec)->channel_layouts[0];
for (i = 0; (*codec)->channel_layouts[i]; i++)
{
if ((*codec)->channel_layouts[i] == AUDIO_CHANNEL_SET)
{
c->channel_layout = AUDIO_CHANNEL_SET;
}
}
}
c->channels = av_get_channel_layout_nb_channels(c->channel_layout);
ost->st->time_base = (AVRational){ 1, c->sample_rate };
break;
case AVMEDIA_TYPE_VIDEO:
c->codec_id = codec_id;
//碼率:影響體積,與體積成正比:碼率越大,體積越大;碼率越小,體積越小。
c->bit_rate = 400000; //設定碼率 400kps
/*分辨率必須是2的倍數。 */
c->width = 640;
c->height = 480;
/*時基:這是基本的時間機關(以秒為機關)
*表示其中的幀時間戳。 對于固定fps内容,
*時基應為1 / framerate,時間戳增量應為
*等于1。*/
ost->st->time_base = (AVRational){1,STREAM_FRAME_RATE};
c->time_base = ost->st->time_base;
c->gop_size = 12; /* 最多每十二幀發射一幀内幀 */
c->pix_fmt = STREAM_PIX_FMT;
if(c->codec_id == AV_CODEC_ID_MPEG2VIDEO)
{
/* 隻是為了測試,添加了B幀 */
c->max_b_frames = 2;
}
if (c->codec_id == AV_CODEC_ID_MPEG1VIDEO) {
/*需要避免使用其中一些系數溢出的宏塊。
*普通視訊不會發生這種情況,因為
*色度平面的運動與亮度平面不比對。 */
c->mb_decision = 2;
}
break;
default:
break;
}
/* 某些格式希望流頭分開。 */
if (oc->oformat->flags & AVFMT_GLOBALHEADER)
c->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
}
/**************************************************************/
/* audio output */
static AVFrame *alloc_audio_frame(enum AVSampleFormat sample_fmt,
uint64_t channel_layout,
int sample_rate, int nb_samples)
{
AVFrame *frame = av_frame_alloc();
int ret;
if (!frame) {
qDebug("Error allocating an audio frame\n");
exit(1);
}
frame->format = sample_fmt;
frame->channel_layout = channel_layout;
frame->sample_rate = sample_rate;
frame->nb_samples = nb_samples;
if (nb_samples) {
ret = av_frame_get_buffer(frame, 0);
if (ret < 0) {
qDebug("Error allocating an audio buffer\n");
exit(1);
}
}
return frame;
}
static void open_audio(AVFormatContext *oc, AVCodec *codec, OutputStream *ost, AVDictionary *opt_arg)
{
AVCodecContext *c;
int nb_samples;
int ret;
AVDictionary *opt = nullptr;
c = ost->enc;
/* open it */
av_dict_copy(&opt, opt_arg, 0);
ret = avcodec_open2(c, codec, &opt);
av_dict_free(&opt);
if (ret < 0) {
qDebug("無法打開音頻編解碼器\n");
exit(1);
}
/* 初始化信号發生器 */
ost->t = 0;
ost->tincr = 2 * M_PI * 110.0 / c->sample_rate;
/* 每秒增加110 Hz的頻率 */
ost->tincr2 = 2 * M_PI * 110.0 / c->sample_rate / c->sample_rate;
if (c->codec->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE)
nb_samples = 10000;
else
nb_samples = c->frame_size;
ost->frame = alloc_audio_frame(c->sample_fmt, c->channel_layout,
c->sample_rate, nb_samples);
ost->tmp_frame = alloc_audio_frame(AV_SAMPLE_FMT_S16, c->channel_layout,
c->sample_rate, nb_samples);
/*将流參數複制到多路複用器 */
ret = avcodec_parameters_from_context(ost->st->codecpar, c);
if (ret < 0) {
qDebug("無法複制流參數\n");
exit(1);
}
/* 建立重采樣器上下文 */
ost->swr_ctx = swr_alloc();
if(!ost->swr_ctx)
{
qDebug("無法配置設定重采樣器上下文\n");
exit(1);
}
/* 設定選項 */
av_opt_set_int (ost->swr_ctx, "in_channel_count", c->channels, 0);
av_opt_set_int (ost->swr_ctx, "in_sample_rate", c->sample_rate, 0);
av_opt_set_sample_fmt(ost->swr_ctx, "in_sample_fmt", AV_SAMPLE_FMT_S16, 0);//帶符号16bit
av_opt_set_int (ost->swr_ctx, "out_channel_count", c->channels, 0);
av_opt_set_int (ost->swr_ctx, "out_sample_rate", c->sample_rate, 0);
av_opt_set_sample_fmt(ost->swr_ctx, "out_sample_fmt", c->sample_fmt, 0);
qDebug("音頻通道數=%d\n",c->channels);
qDebug("音頻采樣率=%d\n",c->sample_rate);
/* 初始化重采樣上下文 */
if ((ret = swr_init(ost->swr_ctx)) < 0) {
qDebug("無法初始化重采樣上下文\n");
exit(1);
}
}
/*準備一個'frame_size'樣本的16位虛拟音頻幀,然後'nb_channels'頻道。 */
static AVFrame *get_audio_frame(OutputStream *ost)
{
AVFrame *frame = ost->tmp_frame;
int j, i, v;
int16_t *q = (int16_t*)frame->data[0];
/* 檢查我們是否要生成更多幀----用于判斷是否結束*/
if(av_compare_ts(ost->next_pts, ost->enc->time_base,
STREAM_DURATION, (AVRational){ 1, 1 }) >= 0)
return nullptr;
// qDebug("frame->nb_samples=%d\n",frame->nb_samples); //1024
// qDebug("ost->enc->channels=%d\n",ost->enc->channels);
//消費者
// videoaudioencode.audio_encode_mutex.lock();
// videoaudioencode.audio_encode_Condition.wait(&videoaudioencode.audio_encode_mutex);
// memcpy(audio_buffer_temp,audio_buffer,sizeof(audio_buffer));
// videoaudioencode.audio_encode_mutex.unlock();
#if 1
if(audio_buffer_r_count>=AUDIO_BUFFER_MAX_SIZE)audio_buffer_r_count=0;
//音頻資料指派
for(j = 0; j<frame->nb_samples; j++) //nb_samples: 此幀描述的音頻樣本數(每個通道)
{
for(i=0;i<ost->enc->channels;i++) //channels:音頻通道數
{
*q++ = audio_buffer[j+audio_buffer_r_count]; //音頻資料
}
ost->t += ost->tincr;
ost->tincr += ost->tincr2;
}
frame->pts = ost->next_pts;
ost->next_pts += frame->nb_samples;
//qDebug()<<"audio_buffer_r_count="<<audio_buffer_r_count;
audio_buffer_r_count+=1024;
#else
for(j = 0; j<frame->nb_samples; j++) //nb_samples: 此幀描述的音頻樣本數(每個通道)
{
v=(int)(sin(ost->t) * 1000);
for(i=0;i<ost->enc->channels;i++) //channels:音頻通道數
{
*q++ = v; //音頻資料
}
ost->t += ost->tincr;
ost->tincr += ost->tincr2;
}
frame->pts = ost->next_pts;
ost->next_pts += frame->nb_samples;
#endif
return frame;
}
/*
*編碼一個音頻幀并将其發送到多路複用器
*編碼完成後傳回1,否則傳回0
*/
static int write_audio_frame(AVFormatContext *oc, OutputStream *ost)
{
AVCodecContext *c;
AVPacket pkt = { 0 }; // data and size must be 0;
AVFrame *frame;
int ret;
int got_packet;
int dst_nb_samples;
av_init_packet(&pkt);
c = ost->enc;
frame = get_audio_frame(ost);
if(frame)
{
/*使用重采樣器将樣本從本機格式轉換為目标編解碼器格式*/
/*計算樣本的目标數量*/
dst_nb_samples = av_rescale_rnd(swr_get_delay(ost->swr_ctx, c->sample_rate) + frame->nb_samples,
c->sample_rate, c->sample_rate, AV_ROUND_UP);
av_assert0(dst_nb_samples == frame->nb_samples);
/*當我們将幀傳遞給編碼器時,它可能會保留對它的引用
*内部;
*確定我們不會在這裡覆寫它
*/
ret = av_frame_make_writable(ost->frame);
if (ret < 0)
exit(1);
/*轉換為目标格式 */
ret = swr_convert(ost->swr_ctx,
ost->frame->data, dst_nb_samples,
(const uint8_t **)frame->data, frame->nb_samples);
if (ret < 0) {
qDebug("Error while converting\n");
exit(1);
}
frame = ost->frame;
frame->pts = av_rescale_q(ost->samples_count, (AVRational){1, c->sample_rate}, c->time_base);
ost->samples_count += dst_nb_samples;
}
ret = avcodec_encode_audio2(c, &pkt, frame, &got_packet);
if (ret < 0) {
qDebug("Error encoding audio frame\n");
exit(1);
}
if (got_packet) {
ret = write_frame(oc, &c->time_base, ost->st, &pkt);
if (ret < 0) {
qDebug("Error while writing audio frame\n");
exit(1);
}
}
return (frame || got_packet) ? 0 : 1;
}
/**************************************************************/
/* video output */
static AVFrame *alloc_picture(enum AVPixelFormat pix_fmt, int width, int height)
{
AVFrame *picture;
int ret;
picture = av_frame_alloc();
if (!picture)
return nullptr;
picture->format = pix_fmt;
picture->width = width;
picture->height = height;
/* allocate the buffers for the frame data */
ret = av_frame_get_buffer(picture, 32);
if(ret < 0)
{
qDebug("Could not allocate frame data.\n");
exit(1);
}
return picture;
}
static void open_video(AVFormatContext *oc, AVCodec *codec, OutputStream *ost, AVDictionary *opt_arg)
{
int ret;
AVCodecContext *c = ost->enc;
AVDictionary *opt = nullptr;
av_dict_copy(&opt, opt_arg, 0);
/* open the codec */
ret = avcodec_open2(c, codec, &opt);
av_dict_free(&opt);
if (ret < 0) {
qDebug("Could not open video codec\n");
exit(1);
}
/* allocate and init a re-usable frame */
ost->frame = alloc_picture(c->pix_fmt, c->width, c->height);
if (!ost->frame) {
qDebug("Could not allocate video frame\n");
exit(1);
}
ost->tmp_frame = nullptr;
/* 将流參數複制到多路複用器 */
ret = avcodec_parameters_from_context(ost->st->codecpar, c);
if (ret < 0) {
qDebug("Could not copy the stream parameters\n");
exit(1);
}
}
/*
準備圖像資料
YUV422占用記憶體空間 = w * h * 2
YUV420占用記憶體空間 = width*height*3/2
*/
static int fill_yuv_image(AVFrame *pict, int frame_index,int width, int height)
{
unsigned int y_size=width*height;
//消費者
while(videoaudioencode.void_data_queue.isEmpty())
{
QThread::msleep(10);
if(videoaudioencode.run_flag==0) //停止編碼
{
return -1;
}
}
videoaudioencode.video_encode_mutex.lock();
QByteArray byte=videoaudioencode.void_data_queue.dequeue();
//qDebug()<<"out="<<videoaudioencode.void_data_queue.size();
videoaudioencode.video_encode_mutex.unlock();
//将YUV資料拷貝到緩沖區 y_size=wXh
memcpy(pict->data[0],byte.data(),y_size);
memcpy(pict->data[1],byte.data()+y_size,y_size/4);
memcpy(pict->data[2],byte.data()+y_size+y_size/4,y_size/4);
return 0;
}
static AVFrame *get_video_frame(OutputStream *ost)
{
AVCodecContext *c = ost->enc;
/* 檢查我們是否要生成更多幀---判斷是否結束錄制 */
if(av_compare_ts(ost->next_pts, c->time_base,STREAM_DURATION, (AVRational){ 1, 1 }) >= 0)
return nullptr;
/*當我們将幀傳遞給編碼器時,它可能會保留對它的引用
*内部; 確定我們在這裡不覆寫它*/
if (av_frame_make_writable(ost->frame) < 0)
exit(1);
//制作虛拟圖像
//DTS(解碼時間戳)和PTS(顯示時間戳)
int err=fill_yuv_image(ost->frame, ost->next_pts, c->width, c->height);
if(err)return nullptr;
ost->frame->pts = ost->next_pts++;
return ost->frame;
}
/*
*編碼一個視訊幀并将其發送到多路複用器
*編碼完成後傳回1,否則傳回0
*/
static int write_video_frame(AVFormatContext *oc, OutputStream *ost)
{
int ret;
AVCodecContext *c;
AVFrame *frame;
int got_packet = 0;
AVPacket pkt = {0};
c=ost->enc;
//擷取一幀資料
frame = get_video_frame(ost);
if(frame==nullptr)return 1;
av_init_packet(&pkt);
/* 編碼圖像 */
ret=avcodec_encode_video2(c, &pkt, frame, &got_packet);
if(ret < 0)
{
qDebug("Error encoding video frame\n");
exit(1);
}
if(got_packet)
{
ret=write_frame(oc, &c->time_base, ost->st, &pkt);
}
else
{
ret = 0;
}
if(ret < 0)
{
qDebug("Error while writing video frame\n");
exit(1);
}
return (frame || got_packet) ? 0 : 1;
}
static void close_stream(AVFormatContext *oc, OutputStream *ost)
{
avcodec_free_context(&ost->enc);
av_frame_free(&ost->frame);
av_frame_free(&ost->tmp_frame);
sws_freeContext(ost->sws_ctx);
swr_free(&ost->swr_ctx);
}
int Thread_VideoAudioEncode::StartUp_VideoAudioEncode()
{
OutputStream video_st = {0}, audio_st = { 0 };
AVOutputFormat *fmt;
AVFormatContext *oc;
AVCodec *audio_codec, *video_codec;
int ret;
int have_video = 0, have_audio = 0;
int encode_video = 0, encode_audio = 0;
AVDictionary *opt = nullptr;
QDateTime dateTime(QDateTime::currentDateTime());
//時間效果: 2020-03-05 16:25::04 周四
QString qStr="";
qStr+=SAVE_FILE_PATH; //Android 手機的照相機檔案夾
qStr+=dateTime.toString("yyyy-MM-dd-hh-mm-ss");
qStr+=".mp4";
qStr="rtmp://js.live-send.acg.tv/live-js/?streamname=live_68130189_71037877&key=b95d4cfda0c196518f104839fe5e7573";
char filename[500];
strcpy(filename,qStr.toLatin1().data());
emit LogSend(tr("目前的檔案名稱:%1\n").arg(filename));
/* 配置設定輸出環境 */
//avformat_alloc_output_context2(&oc,nullptr,nullptr,filename); //存放到檔案
avformat_alloc_output_context2(&oc,nullptr,"flv",filename); //釋出到網絡
if(!oc)
{
emit LogSend("code error.\n");
return -1;
}
fmt=oc->oformat;
fmt->video_codec=AV_CODEC_ID_H264;
fmt->audio_codec=AV_CODEC_ID_AAC;
/*使用預設格式的編解碼器添加音頻和視訊流,初始化編解碼器。 */
if(fmt->video_codec != AV_CODEC_ID_NONE)
{
add_stream(&video_st,oc,&video_codec,fmt->video_codec);
have_video = 1;
encode_video = 1;
}
if(fmt->audio_codec != AV_CODEC_ID_NONE)
{
add_stream(&audio_st, oc, &audio_codec, fmt->audio_codec);
have_audio = 1;
encode_audio = 1;
}
/*現在已經設定了所有參數,可以打開音頻視訊編解碼器,并配置設定必要的編碼緩沖區。 */
if (have_video)
open_video(oc, video_codec, &video_st, opt);
if (have_audio)
open_audio(oc, audio_codec, &audio_st, opt);
av_dump_format(oc, 0, filename, 1);
/* 打開輸出檔案(如果需要) */
if(!(fmt->flags & AVFMT_NOFILE))
{
qDebug()<<"存放視訊到檔案.\n";
ret = avio_open(&oc->pb, filename, AVIO_FLAG_WRITE);
if (ret < 0)
{
qDebug("Could not open '%s'\n",filename);
return -1;
}
}
/* 編寫流頭(如果有)*/
ret=avformat_write_header(oc,&opt);
if(ret<0)
{
qDebug("Error occurred when opening output file\n");
return -1;
}
while(encode_video || encode_audio)
{
/* 選擇要編碼的流*/
if(encode_video &&(!encode_audio || av_compare_ts(video_st.next_pts, video_st.enc->time_base,audio_st.next_pts, audio_st.enc->time_base) <= 0))
{
QElapsedTimer mstimer;
mstimer.start();
encode_video = !write_video_frame(oc,&video_st);
double time = (double)mstimer.nsecsElapsed()/(double)1000000;
qDebug() <<"video_encode:"<< time<<"ms";
}
else
{
QElapsedTimer mstimer;
mstimer.start();
encode_audio = !write_audio_frame(oc,&audio_st);
double time = (double)mstimer.nsecsElapsed()/(double)1000000;
qDebug() <<"audio_encode:"<< time<<"ms";
}
}
/*編寫預告片(如果有)。 預告片必須在之前寫好
*關閉在編寫标頭時打開的CodecContext; 除此以外
* av_write_trailer()可能會嘗試使用已釋放的記憶體
* av_codec_close()。 */
av_write_trailer(oc);
/* Close each codec. */
if (have_video)
close_stream(oc, &video_st);
if (have_audio)
close_stream(oc, &audio_st);
if (!(fmt->flags & AVFMT_NOFILE))
/* Close the output file. */
avio_closep(&oc->pb);
/* free the stream */
avformat_free_context(oc);
qDebug("編碼完成.線程退出.\n");
return 0;
}
//編碼
void Thread_VideoAudioEncode ::run()
{
while(1)
{
qDebug()<<"編碼線程開始運作.";
audio_buffer_r_count=0;
audio_buffer_w_count=0;
StartUp_VideoAudioEncode(); //啟動視訊音頻編碼
if(videoaudioencode.run_flag==0) //判斷是否停止編碼
{
break;
}
}
}