ffmpeg如何存储hls文件
Posted qianbo_insist
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ffmpeg如何存储hls文件相关的知识,希望对你有一定的参考价值。
目标 生成mp4文件和hls文件
除了生成mp4,我们平台的目标还要能生成hls文件
在生成context的时候,看是否要生成hls文件,里面比较重要的是,要控制hls_time参数,以便于按照时间间隔生成ts文件
show me the code
int Mp4WrapperLocal::Create(const char *fileName, TFileFormat *param)
Close();
//video_st.dlen = 0;
//audio_st.dlen = 0;
_pts = 0;
_firstRecordPts = 0;
_sample_count = 1024; //1024 是默认值,注意后面会自动修改//param->sample_count;
//audio_st.samples_count = 0;
videofps = param->fps;
if (param->m3u8 == 1)
//ffmpeg - re - i source.mp4 - codec:v libx264 - codec : a libfaac - map 0 - f hls - hls_list_size 6 - hls_wrap 10 - hls_time 10 playlist.m3u8
avformat_alloc_output_context2(&oc, NULL, "hls", fileName);
av_opt_set(oc->priv_data, "hls_time", "5", AV_OPT_SEARCH_CHILDREN);
//av_opt_set(oc->priv_data, "hls_list_size" ,"0" , AV_OPT_SEARCH_CHILDREN);
av_opt_set(oc->priv_data, "hls_wrap", "5", AV_OPT_SEARCH_CHILDREN);
else
avformat_alloc_output_context2(&oc, NULL, NULL, fileName);
if (!oc)
return 1;
/* Add the audio and video streams using the default format codecs
* and initialize the codecs. */
_have_video = param->haveVideo;
if (_have_video == 1)
add_video_stream( oc->oformat->video_codec, param);
_have_audio = param->haveAudio;
if (_have_audio == 1)
add_audio_stream(oc->oformat->audio_codec, NULL);
else
oc->oformat->audio_codec = AV_CODEC_ID_NONE;
int ret = -1;
/* open the output file, if needed */
if (!(oc->oformat->flags & AVFMT_NOFILE))
ret = avio_open(&oc->pb, fileName, AVIO_FLAG_WRITE);
if (ret < 0)
//_log.write("Mp4File", base::LOGLEVEL_NOTICE, "%s\\n", "无法打开文件");
fprintf(stderr, "Could not open '%s': %d\\n", fileName, ret);
return -1;
/* Write the stream header, if any. */
ret = avformat_write_header(oc, NULL);
if (ret < 0)
//_log.write("Mp4File", base::LOGLEVEL_NOTICE, "%s\\n", "写入mp4头部出错");
fprintf(stderr, "Error occurred when opening output file: %d\\n", ret);
return -1;
return 0;
以上是关于ffmpeg如何存储hls文件的主要内容,如果未能解决你的问题,请参考以下文章
我们如何使用 ffmpeg 将实时 rtmp 流转码为实时 hls 流?