Use ffmpeg to build an HLS live broadcast system

[Time: 2018-04] [Status: Open]
[Keywords: streaming, stream, HLS, ffmpeg, live, live, on-demand, nginx, ssegment]

0 Preface

This article serves as a follow-up to the HLS review .
The main purpose is to use ffmpeg to build a simple HLS on-demand and live broadcast system. Use nginx as HTTP server.

HLS is an HTTP-based file distribution system regardless of on-demand or live broadcast, so the basic idea of ​​this article is:

  1. Use nginx to build an HTTP server
  2. Use ffmpeg to achieve fragmentation of ts files and generate m3u8
  3. ffmpeg uses local files to simulate HLS live broadcast
  4. Support multi-rate HLS generation

1 Building a web server based on nginx

With the rise of big data, cloud computing and cloud services, nginx is widely used as a lightweight and high-performance web server, which is characterized by small memory footprint and strong concurrency capability. At present, domestic BAT basically uses nginx. (If you don't know anything about nginx, you might as well find some information to understand it briefly.)
Closer to home, here we will use nginx to build a web server. First explain my system environment: Ubuntu 15.04 (with various development environments)

1.1 Install nginx

The nginx installation introduced in many previous network materials is compiled from source code, but from the current nginx open source environment, if you do not modify the relevant code, you can directly download [nginx official website] (( http://nginx.org) The provided compiled executable file . (If you want to compile nginx from the source code, the official website also provides corresponding documents)
It is relatively simple for me to install nginx here, and install it directly through apt-get. The command is as follows:

sudo apt-get install nginx

Just like installing a software normally on ubuntu.
The nginx version after installation is as follows:

获取:2 http://mirrors.ustc.edu.cn/ubuntu/ precise-updates/universe nginx-common all 1.1.19-1ubuntu0.8 [17.1 kB]
获取:3 http://mirrors.ustc.edu.cn/ubuntu/ precise-updates/universe nginx-full amd64 1.1.19-1ubuntu0.8 [379 kB]
获取:4 http://mirrors.ustc.edu.cn/ubuntu/ precise-updates/universe nginx all 1.1.19-1ubuntu0.8 [5,812 B]

1.2 nginx startup and shutdown

It is very simple to start nginx, just use the following command: (After nginx is installed, it needs high permissions. During the simple period, the administrator is used to start it directly. It is not recommended for actual use)
sudo nginx

After startup, you can ps aux | grep nginxretrieve it, and the output on my host is as follows:

$ ps aux | grep nginx
root     14443  0.0  0.0 100240  3012 ?        Ss   14:49   0:00 nginx: master process nginx
www-data 14444  0.0  0.0 100544  3780 ?        S    14:49   0:00 nginx: worker process
www-data 14445  0.0  0.0 100544  3780 ?        S    14:49   0:00 nginx: worker process
www-data 14446  0.0  0.0 100544  3780 ?        S    14:49   0:00 nginx: worker process
www-data 14447  0.0  0.0 100544  3780 ?        S    14:49   0:00 nginx: worker process

After nginx is successfully started, you can enter the local IP in the browser, and you can see the page display of "Welcome to nginx!".

nginx exit can be used nginx -s stop(forced exit, equivalent to killing the process directly) or nginx -s quit(graceful shutdown, recommended).

After modifying the nginx configuration file, you can nginx -s reloadreload the configuration without restarting.

1.3 Configure nginx to support html file publishing

The nginx configuration file is located /etc/nginxunder the directory, and the log file is located /var/log/nginxunder. Here is the directory composition of the nginx configuration file:
$ ls
conf.d koi-utf mime.types naxsi.rules proxy_params sites-available uwsgi_params
fastcgi_params koi-win naxsi_core.rules nginx.conf scgi_params sites-enabled win-utf

The main one nginx.confand sites-enabled/defaulttwo files. We need to modify the latter to ensure that the network can access the resources in the specified directory.
For example, we create a hls directory, put /data/it in the directory, and create a file named index.html in the hls directory, where you can add any resources you want to add. To access the hls directory through the network, you need sites-enabled/defaultto add the following modifications in:

    location /hls/ {
        #alias /data/hls/;
        root /data;
    }

If you are interested in the nginx configuration file syntax, it is recommended to refer to the user manual on the official website.

After the modification is completed, restart nginx or reload the nginx configuration file, you can access it through the browser, the URL is: http://localhost/hls/index.html

At this point, our nginx-based network server is completed, and we can provide network services to the outside world.

1.4 References

2. Use ffmpeg to achieve file segmentation and generate m3u8

2.1 Preparations

You need to download or compile a ffmpeg that can run on your system. If you compile it yourself, make sure that all HLS-related parts are enabled.
I downloaded the running ffmpeg under ubuntu through https://johnvansickle.com/ffmpeg website.

2.2 ffmpeg slices files ①

Here, hls_muxer, which is commonly used in live broadcast, is used to segment local files. The command format is as follows:
./ffmpeg -i s1080p.mp4 -c:v copy -c:a copy -f hls -hls_time 10 -hls_playlist_type vod high.m3u8
Here hls_time indicates that the segment length is 10s, high.m3u8 is the final output m3u8 file, and the HLS type is set to VOD.
After successful operation, it can be accessed through http://localhost/hls/high.m3u8 .
For more parameter introduction of ffmpeg's HLS demuxer, please refer to hls-doc .

2.3 ffmpeg slices files ②

ffmpeg also provides segment_demuxer, which provides a more general fragmentation mechanism and parameters.
To achieve the same local file segmentation as 2.2, you can use the following commands:
./ffmpeg -i s1080p.mp4 -c:v copy -c:a copy -f ssegment -segment_format mpegts -segment_list shigh.m3u8 -segment_time 10 shigh%d.ts
segment_format indicates the output format, which is set to mpegts; segment_list indicates the file list after the output slice; segment_time indicates the length of the slice, and the last string specifies the format of the output file name .
For more parameters of ffmpeg's segment demuxer, please refer to segment-doc

2.4 References

3. ffmpeg uses local files to simulate HLS live broadcast

Now that on-demand is realized, the live broadcast is relatively simple. You can directly change the live broadcast source to realize the broadcast, and the output is still live. Of course, you can also read the data encoded output from the camera, or you can use local files to simulate live broadcast (similar to loop playback). This file simulates a live broadcast using the simplest local file. (For other cases, ffmpeg directly supports various devices and protocols, just change the -i parameter.)
For live broadcast, hls muxer is generally used. The command format is as follows:
./ffmpeg -re -i s1080p.mp4 -c:v libx264 -s 720x576 -c:a copy -f hls -hls_time 10 live.m3u8
The default output type of hls muxer is live broadcast, so the option to set hls_playlist_type is removed here; due to the general performance of my host, I will not directly transcode 1080p here, and set the output resolution to 720x576.
The main parameters of the simulated live broadcast are provided by ffmpeg re. This parameter means that the input file is read according to the actual frame rate, and most of them are used to simulate the input of the live broadcast device.

For more information, please refer to ffmpeg-StreamingGuide

4. Support multi-rate HLS generation

For on-demand, of course, you can use an independent m3u8, and then write a master playlist yourself to achieve an effect similar to HLS multi-rate. But does ffmpeg provide a mechanism similar to one-time generation of master playlist? (After all, one step less is one step, and providing efficiency is the main thing.)

If you can't find any relevant information online, just look at the HLS documentation on the ffmpeg official website. I tried various commands, and finally verified that the achievable command line is as follows:

./ffmpeg -i s1080p.mp4 -c:v:0 copy -c:v:1 libx264 -s 640x320 -c:a:0 copy -b:a:1 32k \
    -map 0:v -map 0:a -map 0:v -map 0:a -f hls  -var_stream_map "v:0,a:0 v:1,a:1" 、
    -hls_time 10 -hls_playlist_type vod -master_pl_name master.m3u8 out%v/out.m3u8`

There are several more special hls parameters here. master_pl_nameUsed to specify the output master playlist file name; var_stream_mapused to specify the program information contained in the variant list, which is separated by spaces, such as the above parameters: variant#0 contains the original 1080p video and audio, variant#1 contains 640x320 Video and 32k audio.

The format of master.m3u8 output by this command is as follows:

#EXTM3U
#EXT-X-VERSION:3
#EXT-X-STREAM-INF:BANDWIDTH=1029895,RESOLUTION=1920x1080
out0/out.m3u8

#EXT-X-STREAM-INF:BANDWIDTH=35200,RESOLUTION=640x320,CODECS="avc1.64001e,mp4a.40.2"
out1/out.m3u8

The generated ts files are located in the out0 and out1 directories, respectively.

If you need multi-bit rate live broadcast, please refer to the introduction in Section 2.2 and Section 3

5. Summary

This article first introduces how to install and configure nginx, so that the http server can be implemented, and on this basis, ffmpeg is used to implement HLS on-demand/live processing, and finally the ffmpeg command is given to implement a statement to generate the master playlist required by HLS.
In general, after doing this, I have a general understanding of the construction of the HLS system environment, which is only recorded for subsequent reference.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325122407&siteId=291194637