用ffmpeg 把mp4文件转为ts文件并生成m3u8列表

特别说明:以下文章暂未经过实验,仅作为后续开发参考

在使用hls技术播放视频流时,首先要把视频转换为ts片和一个m3u8播放列表,使用ffmpeg进行该转换时(低版本ffmpeg不支持直接转,只能现在转换成ts,再用m3u8-segmenter切片,笔者使用的是ffmpeg version-2.1.2),通常使用如下命令:

ffmpeg -i input.mp4 -c:v libx264 -c:a aac -strict -2 -f hls output.m3u8

但是在此状态下,默认的每片长度为2秒,m3u8文件中默认只保存最新的5条片的信息,导致最后播放的时候只能播最后的一小部分。

实际上还有一些其他参数可以控制这些内容:

以下为ffmpeg官网原文:

This muxer supports the following options:

‘hls_time seconds’

Set the segment length in seconds. Default value is 2.

‘hls_list_size size’

Set the maximum number of playlist entries. If set to 0 the list file will contain all the segments. Default value is 5.

‘hls_wrap wrap’

Set the number after which the segment filename number (the number specified in each segment file) wraps. If set to 0 the number will be never wrapped. Default value is 0.

This option is useful to avoid to fill the disk with many segment files, and limits the maximum number of segment files written to disk towrap.

‘start_number number’

Start the playlist sequence number from number. Default value is 0.

Note that the playlist sequence number must be unique for each segment and it is not to be confused with the segment filename sequence number which can be cyclic, for example if the ‘wrap’ option is specified.

翻译如下:

-hls_time n: 设置每片的长度,默认值为2。单位为秒

-hls_list_size n:设置播放列表保存的最多条目,设置为0会保存有所片信息,默认值为5

-hls_wrap n:设置多少片之后开始覆盖,如果设置为0则不会覆盖,默认值为0.这个选项能够避免在磁盘上存储过多的片,而且能够限制写入磁盘的最多的片的数量

-hls_start_number n:设置播放列表中sequence number的值为number,默认值为0

 

注意:播放列表的sequence number 对每个segment来说都必须是唯一的,而且它不能和片的文件名(当使用wrap选项时,文件名有可能会重复使用)混淆,加上了这些参数,小伙伴们就可以愉快地控制生成的m3u8文件啦

 

其它文章

1、将MP4转成m3u8

ffmpeg -i test.mp4 -codec copy -bsf h264_mp4toannexb test.ts

2、将ts转成m3u8

网上很多垃圾文章推荐segmenter工具,但用的时候,3.5G的ts文件丢了一半的数据,于是想到了ffmpeg转。

在国外网站找到命令,一句话搞定,没报半句错:

ffmpeg -i 12生肖.ts -c copy -map 0 -f segment -segment_list playlist.m3u8 -segment_time 10 output.ts

顺便共享给各位国内的同仁,免得深受其苦。毕竟,大家都说HLS代表future,rtsp已经是过去式了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值