ffmpeg可以实现这个功能。ffmpeg支持rtsp协议,也支持rtmp。在这个案例中rtsp是输入,
rtmp是输出,ffmpeg实现了转码的功能。下面可出一个整体思路流程图。
如图1所示:在获取都rtsp流以后,解复用(demux)获取ES流packet,最后将ES流封装成rtmp格式并发送
到服务端。 基本思路完毕,下面上代码。
一:初始化ffmpeg库
void Init()
{
av_register_all();
avfilter_register_all();
avformat_network_init();
av_log_set_level(AV_LOG_ERROR);
}
二. 打开rtmp视频流
int OpenInput(char *fileName)
{
AVDictionary *opts = 0;
av_dict_set(&opts, "rtsp_transport", "tcp", 0);
int ret = avformat_open_input(&inf_context, in_fileName, nullptr, &opts);
if (ret < 0)
{
cout << "can not open input file" << endl;
return ret;
}
ret = avformat_find_stream_info(inf_context, nullptr);
if (ret< 0 )
{
cout << "Failed to retrieve input stream information" << endl;
}
av_dump_format(inf_context, 0, in_fileName, 0);
auto codecContext = inf_context->streams[0]->codec;
ret = avcodec_open2(codecContext, avcodec_find_decoder(codecContext->codec_id), nullptr);
return ret;
}
返回值小于0 表示打开流失败。
三. 创建输出流
int OpenOutput(char *fileName)
{
int ret = 0;
ret = avformat_alloc_output_context2(&outputContext, nullptr, "flv", fileName);
if(ret < 0)
{
goto Error;
}
ret = avio_open2(&outputContext->pb, fileName, AVIO_FLAG_READ_WRITE,nullptr, nullptr);
if(ret < 0)
{
goto Error;
}
for(int i = 0; i < context->nb_streams; i++)
{
AVStream * stream = avformat_new_stream(outputContext, nullptr);
ret = avcodec_copy_context(stream->codec, context->streams[i]->codec); if(ret < 0)
{
goto Error;
}
}
ret = avformat_write_header(outputContext, nullptr);
if(ret < 0)
{
goto Error;
}
return ret ;
Error:
if(outputContext)
{
for(int i = 0; i < outputContext->nb_streams; i++)
{
avcodec_close(outputContext->streams[i]->codec);
}
avformat_close_input(&outputContext);
}
return ret ;
}
返回值小于0,创建输出流失败,以rtmp格式发送视频流一定要以flv格式初始化output context。
四. 读取Packet
shared_ptr<AVPacket> ReadPacketFromSource()
{
shared_ptr<AVPacket> packet(static_cast<AVPacket*>(av_malloc(sizeof(AVPacket))), [&](AVPacket *p) { av_free_packet(p); av_freep(&p);});
av_init_packet(packet.get());
int ret = av_read_frame(context, packet.get());
if(ret >= 0)
{
return packet;
}
else
{
return nullptr;
}
}
五. 写Packet到服务端
1.av_interleaved_write_frame(outputContext, packet.get());
六. 简单demo
int _tmain(int argc, _TCHAR* argv[])
{
string fileInput= "rtsp://admin:admin@172.29.183.103/media/video1/multicast";
string fileOutput="rtmp://127.0.0.1/live/mystream";
Init();
if(OpenInput((char *)fileInput.c_str()) < 0)
{
cout << "Open file Input failed!" << endl;
this_thread::sleep_for(chrono::seconds(1000));
return 0;
}
if(OpenOutput((char *)fileOutput.c_str()) < 0)
{
cout << "Open file Output failed!" << endl;
this_thread::sleep_for(chrono::seconds(1000));
return 0;
}
auto timebase = av_q2d(context->streams[0]->time_base);
while(true)
{
auto packet = ReadPacketFromSource();
if(packet)
{
int ret = av_interleaved_write_frame(outputContext, packet.get());
}
else
{
cout <<"write packet end!"<< endl;
break;
}
}
CloseInput();
CloseOutput();
cout <<"Transcode file end!" << endl;
this_thread::sleep_for(chrono::seconds(1000));
return 0;
}
七. 小结:
经测试,内网rtmp直播延时在1秒以内,公网延时在3,4秒左右。