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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qianbo_insist

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值