FFmpeg from beginner to proficient
Article directory
- 3.3 Convert video files to M3U8
3.3 Convert video files to M3U8
3.3.1 Introduction to M3U8 format standard
M3U8 is a common streaming media format that mainly exists in the form of a file list. It supports both live broadcast and on-demand playback. It is especially commonly used on platforms such as Android and iOS. Let’s take a look at the simplest example of M3U8:
From this example, you can see the following fields, whose meanings are as follows.
EXTM3U
M3U8 files must contain a tag, and it must be on the first line of the file. All M3U8 files must contain this tag.
EXT-X-VERSION
The most common version of M3U8 files is 3. In fact, the version has developed a lot. As of the deadline, it has been released to version 7. After so many versions, many marks have been added and deleted during the period.
For example, version 2 and later supports the
EXT- 5. Later, the format description KEYFORMAT and KEYFORMATVERSION tags of EXT-X-KEY and the EXT-X-MAP tag will be supported. After version 6, the EXT-X-MAP tag will not contain the EXT-XI-FRAMES-ONLY tag. Of course, some tags have also been deleted in some versions. For example, the EXT-X-STREAM-INF tag and EXT-XI-FRAME-STREAM-INF tag have been deleted in version 6, and the EXT-X- ALLOW-CACHE label and other information.
EXT-X-TARGETDURATION
Each shard will have its own duration. This label is the rounded integer value of the floating point number of the largest shard, for example, 1.02, the rounded integer is 1, 2.568, the rounded integer is 3, if it is on M3U8 The maximum duration value in the fragment list is 5.001, so the EXT-X-TARGETDURATION value is 5.
EXT-X-MEDIA-SEQUENCE
M3U8 live broadcast slice sequence during live broadcast. When M3U8 is turned on for playback, the value of this tag is used as a reference to play the slice with the corresponding sequence number. Of course, there are more details about the standards for client playback of M3U8. Let’s introduce them one by one below.
The shards must be dynamically changing, the sequences cannot be the same, and the sequences must be in increasing order.
When the EXT-X-ENDLIST tag does not appear in the M3U8 list, no matter how many
fragments there are in the M3U8, the fragments will be played from the third to last fragment. If there are less than three fragments, it should not be played. Of course, if some players are specially customized, this principle does not need to be followed.
If there is a discontinuity between the previous segment and the next segment, an error may occur during playback. You need to use the EXT-X-DISCONTINUITY tag to resolve this error.
Refresh the M3U8 list with the duration of playing the current segment, and then perform the corresponding loading action.
If the playlist is the same as the previous one after the refresh, then it is refreshed again half the duration of the current segment.
EXTINF
EXTINF is the duration of each fragment in the M3U8 list. For example, the duration of the first fragment in the output information of the above example is 4.120000 seconds. In addition to the duration value, the EXTINF tag can also contain optional description information, mainly to mark the slices. Information, separated by commas.
The information below EXTINF is specific fragmentation information. The fragment storage path can be a relative path, an absolute path, or an Internet URL link address.
In addition to the above tags, there are some tags that are also commonly used tags, as follows.
EXT-X-ENDLIST
If the EXT-X-ENDLIST tag appears, it means that the M3U8 file will not generate more slices. It can be understood that M3U8 has stopped updating, and the playback of the slices will end after reaching this tag. M3U8 can be used not only as a live broadcast, but also as an on-demand video. All slice information is retained in the M3U8 file and ends with EXT-X-ENDLIST. This M3U8 is an on-demand M3U8.
EXT-X-STREAM-INF
When the EXT-X-STREAM-INF tag appears in M3U8, it mainly appears in multi-level M3U8 files, such as when M3U8 contains a sub-M3U8 list, or when the main M3U8 contains multi-code rate M3U8; this tag needs to be followed by some Properties, these properties will be explained one by one below.
BANDWIDTH
The value of BANDWIDTH is the maximum bit rate value, which is the maximum bit rate occupied when playing the corresponding M3U8 under EXT-X-STREAM-INF. This parameter is an attribute that must be included in the EXT-X-STREAM-INF tag
.
AVERAGE-BANDWIDTH
The value of AVERAGE-BANDWIDTH is the average bit rate value, which is the average bit rate occupied when playing the corresponding M3U8 under EXT-X-STREAM-INF. This parameter is an optional parameter.
CODECS
The value of CODECS is used to declare that the following EXT-X-STREAM-INF corresponds to
the audio . For example, if the audio and video of AAC-LC are H.264 Main Profile and Level3.0, then The CODECS value is "mp4a.40.2,avc 1.4d401e". This attribute should appear in the EXT-X-STREAM-INF tag, but not all M3U8 can see it. It is for reference only.
RESOLUTION
Describes the width and height information of the video in M3U8. This attribute is an optional attribute.
FRAME-RATE
Video frame rate in sub-M3U8, this attribute is still an optional attribute.
EXT-X-STREAM-INF Example
Here is a practical example for EXT-X-STREAM-INF:
In this M3U8 file, 4 EXT-X-STREAM-INF tags are used to mark the attributes of the sub-M3U8:
- M3U8 with a maximum bit rate of 1.28M and an average bit rate of 1M,
- M3U8 with a maximum bit rate of 2.56M and an average bit rate of 2M,
- M3U8 with a maximum bit rate of 7.68M and an average bit rate of 6M,
- Only 65K audio encoded by M3U8.
3.3.2 FFmpeg to HLS parameters
FFmpeg comes with HLS encapsulation parameters. You can use HLS format to encapsulate HLS. However, there are various parameters for reference when generating HLS, such as setting the pre-path of slices in the HLS list and setting when generating TS slices of HLS. TS fragmentation parameters, setting the number of TS saved in the M3U8 list when generating HLS, etc. For detailed parameters, please refer to Table 3-29.
3.3.3 Example of converting FFmpeg to HLS
When converting HLS live broadcast from file, the parameters used are as follows:
./ffmpeg -re -i input.mp4 copy -f hls -bsf:v h264_mp4toannexb output.m3u8
The output content is as follows:
Parameter introduction (I won’t list the specific content, there are too many, and I am exhausted from reading it. Please read the pdf document by yourself if you need it)
Please refer to: Introduction to ffmpeg to HLS parameters (ffmpeg to m3u8 parameters)
0. -bsf:v h264_mp4toannexb
Convert H.264 data in MP4 to H.264 AnnexB standard encoding
1. start number
The start number parameter is used to set the sequence number of the first slice in the M3U8 list.
2. hls_time
The hls_time parameter is used to set the duration of the slices in the M3U8 list
3. hls_list_size
The hls_list_size parameter is used to set the number of TS slices in the M3U8 list
4. hls_wrap
The hls_wrap parameter is used to set refresh rollback parameters for TS in the M3U8 list.
5. hls_base_url
The his_ base url parameter is used to set the preceding base path parameter for the file path in the M3U8 list.
6. hls_segment_filename
Set rule template parameters for slice file names for M3U8 lists
7. hls_flags
delete_segments
Use the delete_segments parameter to delete old files that no longer exist in the M3U8 list
round durations
Implement the duration of slice information as an integer
discont start
When generating M3U8, insert the discontinuity tag in front of the slice information to make a special statement when the slice is discontinuous.
omit endlist
At the end of generating M3U8, if it is not at the end of the file, the endlist tag will not be appended.
split_by_time
When generating M3U8, the TS is sliced based on the value set by the his_time parameter as a reference for seconds, and does not necessarily need to encounter key frames (this parameter setting may cause screen distortion at the beginning, so it is not set unless it is special)
8. use_localtime
Use the local system time as the slice file name
9. method
Used to set up HLS to upload M3U8 and TS files to HTTP server
(This is more important. It turns out that streaming is done in this way, which is to let ffmpeg generate m3u8 files into the http proxy directory)