本来主要讲述如何利用ffmpeg将输入视频流通过转码的方式转成m3u8文件。如何通过http的方法将切边推送给客户端,不在本文中讲述。
输入视频流可以是rtsp流,也可以是http,还可以是文件等等。转码的基本流程如下图所示:
图1. 生产hls视频流
视频流解复用可以获得packet,对应的实现方法是av_read_frame。
下面给出代码:
1. 初始化ffmpeg
void Init()
{
av_register_all();
avfilter_register_all();
avformat_network_init();
av_log_set_level(AV_LOG_ERROR);
}
初始化ffmepg是必须的,否则调用相关的ffmpeg会返回错误。
2. 打开视频流
int OpenInput(char *fileName)
{
context = avformat_alloc_context();
context->interrupt_callback.callback = interrupt_cb;
int ret = avformat_open_input(&context, fileName, nullptr,nullptr);
if(ret < 0)
{
return ret;
}
ret = avformat_find_stream_info(context,nullptr);
av_dump_format(context, 0, fileName, 0);
if(ret >= 0)
{
cout <<"open input stream successfully" << endl;
}
return ret;
}
3.创建hls输出上下文
int OpenOutput(char *fileName)
{
int ret = 0;
ret = avformat_alloc_output_context2(&outputContext, nullptr, "hls", fileName);
if(ret < 0)
{
goto Error;
}
ret = avio_open2(&outputContext->pb, fileName, AVIO_FLAG_READ_WRITE,nullptr, nullptr);
if(ret < 0)
{
goto Error;
}
av_opt_set(outputContext->priv_data, "hls_time" ,"5" , AV_OPT_SEARCH_CHILDREN);
//av_opt_set(outputContext->priv_data, "hls_list_size" ,"10" , AV_OPT_SEARCH_CHILDREN);
av_opt_set(outputContext->priv_data, "hls_wrap" ,"5" , AV_OPT_SEARCH_CHILDREN);
for(int i = 0; i < context->nb_streams; i++)
{
AVStream * stream = avformat_new_stream(outputContext, context->streams[i]->codec->codec);
ret = avcodec_copy_context(stream->codec, context->streams[i]->codec);
//stream->codec->codec_tag = 0;
//stream->index = 0;
if(ret < 0)
{
goto Error;
}
}
av_dump_format(outputContext, 0, fileName, 1);
ret = avformat_write_header(outputContext, nullptr);
if(ret < 0)
{
goto Error;
}
if(ret >= 0)
cout <<"open output stream successfully" << endl;
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 ;
}
4.解复用
AVPacket *ReadPacketFromSource()
{
std::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());
lastFrameRealtime = av_gettime();
int ret = av_read_frame(context, packet.get());
if(ret >= 0)
{
return packet.get();
}
else
{
return nullptr;
}
}
av_read_frame返回packet,packet是经过解复用得到的裸码流.
5. 写packet到输出context
av_write_frame(outputContext, packet);
demo
int _tmain(int argc, _TCHAR* argv[])
{
string fileInput= "D:\\record\\langxi\\langxi.ts";
string fileOutput="D:\\test\\file\\live\\wgg2\\test.m3u8";
Init();
if(OpenInput((char *)fileInput.c_str()) < 0)
{
cout << "Open file Input failed!" << endl;
this_thread::sleep_for(chrono::seconds(10));
return 0;
}
if(OpenOutput((char *)fileOutput.c_str()) < 0)
{
cout << "Open file Output failed!" << endl;
this_thread::sleep_for(chrono::seconds(10));
return 0;
}
auto timebase = av_q2d(context->streams[0]->time_base);
int count = 0;
auto in_stream = context->streams[0];
auto out_stream = outputContext->streams[0];
while(true)
{
AVPacket *packet = ReadPacketFromSource();
if(packet)
{
packet->pts = av_rescale_q_rnd(packet->pts, in_stream->time_base, out_stream->time_base,
AVRounding(AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));
packet->dts = av_rescale_q_rnd(packet->dts, in_stream->time_base, out_stream->time_base, AVRounding(AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));
packet->duration = av_rescale_q(packet->duration, in_stream->time_base, out_stream->time_base);
packet->pos = -1;
int ret = av_write_frame(outputContext, packet);
}
else
{
cout <<"write packet end!"<< endl;
break;
}
}
CloseInput();
CloseOutput();
cout <<"Transcode file end!" << endl;
this_thread::sleep_for(chrono::hours(10));
return 0;
}
如有问题,加群流媒体/Ffmpeg/音视频 127903734交流,群里有demo源码.