Use FFmpeg+go to build m3u8 on-demand server

I. Introduction

        HLS (HTTP Live Streaming) is an HTTP-based streaming media transmission protocol proposed by Apple. Its working principle is to divide the entire file into small files (usually TS format files), and the client downloads and plays them through HTTP. When preparing to play an m3u8 media source, a .m3u8 index file containing metadata information will be downloaded first, and media stream files will be downloaded sequentially according to the information of the .m3u8 index file.

        This article will use FFmpeg+go to build an m3u8 on-demand server, where FFmpeg is used to segment media files such as mp4, and go is used to build a web resource server.

2. Segment media files

ffmpeg -i lantingxv.mp4 -vbsf h264_mp4toannexb -vcodec libx264 -acodec aac -strict 2 -s 1280x720 -r 30 -g 150 -hls_time 5 -hls_list_size 0 -start_number 0 -f hls -y lantingxv.m3u8

        Prepare a media file, and then execute the above command. After the command is executed, a .m3u8 file (index file) and several .ts files (media files) will be generated in the local directory. Each .ts file can be played separately, and each The duration of the .ts file is about 5s.

        The content of the m3u8 playback index file is roughly as follows. The beginning of #EXT is the m3u8 Tag, and the meaning of each Tag is explained below.  

#EXTM3U
#EXT-X-VERSION:3
#EXT-X-TARGETDURATION:5
#EXT-X-MEDIA-SEQUENCE:0
#EXTINF:5.000000,
lantingxv0.ts
#EXTINF:5.000000,
lantingxv1.ts
#EXTINF:5.000000,
lantingxv2.ts
#EXTINF:5.000000,
lantingxv3.ts
#EXTINF:5.000000,
lantingxv4.ts
#EXTINF:5.000000,
lantingxv5.ts
#EXTINF:5.000000,
lantingxv6.ts
#EXTINF:5.000000,
lantingxv7.ts
#EXTINF:5.000000,
lantingxv8.ts
#EXTINF:5.000000,
lantingxv9.ts
#EXT-X-ENDLIST

#EXTM3U must be in the first line of the file, indicating that this is an M3U Playlist file.

#EXT-X-VERSION indicates the version.

#EXT-X-TARGETDURATION indicates the duration of the largest media segment file, as above, the longest TS file is 5s.

#EXT-X-MEDIA-SEQUENCE indicates the sequence number of the first media segment file name, as above, it starts from lantingxv0.ts, so it is 0.

#EXTINF indicates the duration of the following media segment file, which is 5s as above.

#EXT-X-ENDLIST means the end of the Playlist. The m3u8 on-demand index file has this TAG, but the m3u8 live index file does not have this TAG. The live index file is constantly updated dynamically.

3. Use go to build a static resource server

        After we split the media files into .m3u8 index files and .ts file fragments, they need to be exposed through HTTP, so that the client can obtain the index files and media files through the HTTP protocol. We use go to build a static resource server. The code is as follows .

package main

import "net/http"

func main() {
	http.Handle("/", http.FileServer(http.Dir("file")))
	http.ListenAndServe(":8080", nil)
}

First create a file directory under the same directory as the code, then copy all the .m3u8 files and corresponding .ts files to this directory, and then run the code.

4. Use VLC to play the specified m3u8 source

         Start VLC, click to open the network, enter the m3u8 source and click to play, the playback effect is as follows.

Guess you like

Origin blog.csdn.net/weixin_38102771/article/details/128445859