FFMPEG对HLS切片的支持

Apple HTTP Live Streaming muxer that segments MPEG-TS according to the HTTP Live Streaming (HLS) specification.

It creates a playlist file, and one or more segment files. The output filename specifies the playlist filename.

By default, the muxer creates a file for each segment produced. These files have the same name as the playlist, followed by a sequential number and a .ts extension.

For example, to convert an input file with ffmpeg:

ffmpeg -i in.nut out.m3u8

This example will produce the playlist, out.m3u8, and segment files: out0.tsout1.tsout2.ts, etc.

See also the segment muxer, which provides a more generic and flexible implementation of a segmenter, and can be used to perform HLS segmentation.

4.10.1 Options

This muxer supports the following options:

hls_time seconds

Set the target segment length in seconds. Default value is 2. Segment will be cut on the next key frame after this time has passed.

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_ts_options options_list

Set output format options using a :-separated list of key=value parameters. Values containing : special characters must be escaped.

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 to wrap.

start_number number

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

hls_allow_cache allowcache

Explicitly set whether the client MAY (1) or MUST NOT (0) cache media segments.

hls_base_url baseurl

Append baseurl to every entry in the playlist. Useful to generate playlists with absolute paths.

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 thewrap option is specified.

hls_segment_filename filename

Set the segment filename. Unless hls_flags single_file is set, filename is used as a string format with the segment number:

ffmpeg in.nut -hls_segment_filename 'file%03d.ts' out.m3u8

This example will produce the playlist, out.m3u8, and segment files: file000.tsfile001.tsfile002.ts, etc.

use_localtime

Use strftime on filename to expand the segment filename with localtime. The segment number (%d) is not available in this mode.

ffmpeg in.nut -use_localtime 1 -hls_segment_filename 'file-%Y%m%d-%s.ts' out.m3u8

This example will produce the playlist, out.m3u8, and segment files: file-20160215-1455569023.tsfile-20160215-1455569024.ts, etc.

use_localtime_mkdir

Used together with -use_localtime, it will create up to one subdirectory which is expanded in filename.

ffmpeg in.nut -use_localtime 1 -use_localtime_mkdir 1 -hls_segment_filename '%Y%m%d/file-%Y%m%d-%s.ts' out.m3u8

This example will create a directory 201560215 (if it does not exist), and then produce the playlist, out.m3u8, and segment files: 201560215/file-20160215-1455569023.ts,201560215/file-20160215-1455569024.ts, etc.

hls_key_info_file key_info_file

Use the information in key_info_file for segment encryption. The first line of key_info_file specifies the key URI written to the playlist. The key URL is used to access the encryption key during playback. The second line specifies the path to the key file used to obtain the key during the encryption process. The key file is read as a single packed array of 16 octets in binary format. The optional third line specifies the initialization vector (IV) as a hexadecimal string to be used instead of the segment sequence number (default) for encryption. Changes tokey_info_file will result in segment encryption with the new key/IV and an entry in the playlist for the new key URI/IV.

Key info file format:

key URI
key file path
IV (optional)

Example key URIs:

http://server/file.key
/path/to/file.key
file.key

Example key file paths:

file.key
/path/to/file.key

Example IV:

0123456789ABCDEF0123456789ABCDEF

Key info file example:

http://server/file.key
/path/to/file.key
0123456789ABCDEF0123456789ABCDEF

Example shell script:

#!/bin/sh
BASE_URL=${1:-'.'}
openssl rand 16 > file.key
echo $BASE_URL/file.key > file.keyinfo
echo file.key >> file.keyinfo
echo $(openssl rand -hex 16) >> file.keyinfo
ffmpeg -f lavfi -re -i testsrc -c:v h264 -hls_flags delete_segments \
  -hls_key_info_file file.keyinfo out.m3u8
hls_flags single_file

If this flag is set, the muxer will store all segments in a single MPEG-TS file, and will use byte ranges in the playlist. HLS playlists generated with this way will have the version number 4. For example:

ffmpeg -i in.nut -hls_flags single_file out.m3u8

Will produce the playlist, out.m3u8, and a single segment file, out.ts.

hls_flags delete_segments

Segment files removed from the playlist are deleted after a period of time equal to the duration of the segment plus the duration of the playlist.

hls_flags round_durations

Round the duration info in the playlist file segment info to integer values, instead of using floating point.

hls_flags discont_starts

Add the #EXT-X-DISCONTINUITY tag to the playlist, before the first segment’s information.

hls_flags omit_endlist

Do not append the EXT-X-ENDLIST tag at the end of the playlist.

hls_flags split_by_time

Allow segments to start on frames other than keyframes. This improves behavior on some players when the time between keyframes is inconsistent, but may make things worse on others, and can cause some oddities during seeking. This flag should be used with the hls_time option.

hls_playlist_type event

Emit #EXT-X-PLAYLIST-TYPE:EVENT in the m3u8 header. Forces hls_list_size to 0; the playlist can only be appended to.

hls_playlist_type vod

Emit #EXT-X-PLAYLIST-TYPE:VOD in the m3u8 header. Forces hls_list_size to 0; the playlist must not change.

method

Use the given HTTP method to create the hls files.

ffmpeg -re -i in.ts -f hls -method PUT http://example.com/live/out.m3u8

This example will upload all the mpegts segment files to the HTTP server using the HTTP PUT method, and update the m3u8 files every refresh times using the same method. Note that the HTTP server must support the given method for uploading files.

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
FFmpeg是一款强大的多媒体处理工具,可以用来完成各种视频音频处理任务,其中包括将RTSP流转换成HLS流。 RTSP(实时流媒体传输协议)是一种用于实时数据传输的网络协议,常用于视频流的传输。HLS(HTTP Live Streaming)是一种流媒体传输协议,可以将视频切分为多个小文件,并通过HTTP协议分段传输。 要实现RTSP转HLS,首先需要安装FFmpeg工具。在安装完毕后,可以使用以下命令行参数来进行转换: ```shell ffmpeg -i rtsp://source_url -c:v copy -c:a copy -hls_segment_type mpegts -hls_time 10 -hls_list_size 0 -f hls output.m3u8 ``` 上述命令中,使用`-i`参数指定RTSP输入源的URL,`-c:v copy`和`-c:a copy`表示不对视频和音频进行编码,直接复制到HLS输出中。 `-hls_segment_type mpegts`指定输出的HLS段文件类型为`mpegts`,`-hls_time 10`表示每个HLS段的时长为10秒,`-hls_list_size 0`表示不限制目标播放列表的最大文件数量。 最后使用`-f hls`参数指定输出格式为HLS,并指定输出文件名为`output.m3u8`。 执行上述命令后,FFmpeg会将RTSP流转换成HLS流。输出的`output.m3u8`文件是一个HLS播放列表文件,其中包含了各个切片文件的URL地址。 通过将`output.m3u8`和切片文件(.ts文件)部署在HTTP服务器上,即可使用支持HLS协议的播放器进行在线播放。 需要注意的是,转换过程中可能会出现一些兼容性或格式方面的问题。在使用FFmpeg进行RTSP转HLS时,可以根据具体需求调整命令行参数以达到最佳效果。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值