ffmepg将mp4视频切割成ts文件从而形成m3u8文件
切片生成m3u8列表命令:
ffmpeg -i input.mp4 -c:v libx264 -c:a aac -f hls output.m3u8
此转换命令默认的每片(即一个ts文件)时长是2s,m3u8列表文件中默认只保存最后的5条片信息,也就是播放器在拉流时只能播最后的那5条ts视频文件。这肯定不是你想要的,那么幸运的是ffmpeg提供了更改这些相关设置的参数,如下:
相关设置参数:
hls_time seconds’:Set the segment length in seconds. Default value is 2.(设置每片的长度,默认值为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.(设置播放列表保存的最多条目,设置为0会保存有所片信息,默认值为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.(设置多少片之后开始覆盖,如果设置为0则不会覆盖,默认值为0.这个选项能够避免在磁盘上存储过多的片,而且能够限制写入磁盘的最多的片的数量)
start_number number’:Start the playlist sequence number from number. Default value is 0.(设置播放列表中sequence number的值为number,默认值为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.(提示:播放列表的sequence number 对每个segment来说都必须是唯一的,而且它不能和片的文件名混淆,因为在,如果指定了“wrap”选项文件名会出现重复使用。)
使用示例:
ffmpeg -i 1.mp4 -c:v libx264 -c:a copy -f hls -threads 8 -hls_time 5 -hls_list_size 12 index.m3u8