1. Chuẩn bị
Thông tin về server:
OS: Ubuntu 16.04
RAM: 2GB
Cores: 2
Bandwidth: 1Gb/s
eth0: 192.168.100.197
Gateway: 192.168.100.1
NETWORK: 192.168.100.0/24
2. Cài đặt và cấu hình
2.1 Biên dịch NIGNX-RTMP
Cài đặt các trình biên dịch:
apt-get update -y
apt-get install build-essential libpcre3 libpcre3-dev libssl-dev git wget dpkg-dev zlib1g-dev unzip -y
Tải các gói cài đặt cần thiết:
Clone nginx-rtmp-module
Install nginx dependencies
Download nginx
Latest nginx can be downloaded from this page.
for example
Latest nginx can be downloaded from this page.
for example
nginx-1.10.1
can be downloaded from this link: http://nginx.org/download/nginx-1.10.1.tar.gz
Compile nginx
- Notice the
--add-module=../nginx-rtmp-module
argument, the path must point correctly to the cloned module - (Optional) replace -j 1 with the amount of cpu's on your computer to accelerate the compilation
2. Create nginx configuration file¶
rtmp module config
An application in nginx means an rtmp endpoint
Basic uri syntax:
We will be using
Basic uri syntax:
rtmp://nginx_host[:nginx_port]/app_name/stream_name
We will be using
stream
as our stream name so our endpoint will be: rtmp://localhost/show/stream
Which will later be available as http://localhost:8080/hls/stream.m3u8
For good HLS experience we recommend using 3 seconds fragments with 60 seconds playlist.
Note that the example points
You can change this to a different directory but make sure that nginx have write permissions.
/mnt/hls/
as the target path for the hls playlist and video files.You can change this to a different directory but make sure that nginx have write permissions.
http server config
Since HLS consists of static files, a simple http server can be set up with two additions, correct MIME types and CORS headers.
the complete nginx.conf
The default location for nginx conf is
/usr/local/nginx/conf/nginx.conf
or /etc/nginx/nginx.conf
3. Start nginx¶
The nginx binary is located wherever you compiled it to -
/usr/local/nginx/sbin/nginx
by default. Change it to reflect your path.
Test the configuration file
Start nginx in the background
Start nginx in the foreground
Reload the config on the go
Kill nginx
4. Pushing live stream to nginx using rtmp¶
nginx accepts rtmp stream as input. For a proper HLS stream the video codec should be
x264
and audio codec aac
/mp3
/ac3
most commonly being aac
.Options 1: From existing rtmp stream already in h264¶
if you have an existing rtmp stream in the correct codec, you can skip ffmpeg and tell nginx to pull the stream directly. In order to do so add a
pull
directive under application
section in nginx.conf like so:
Read on more available options here
Options 2: From local webcam/different rtmp/file¶
To achieve the stream encoding and muxing we will use the almighty ffmpeg.
To install ffmpeg using PPA run these commands
There are several source from which you can produce an rtmp stream. here are couple examples: Update
localhost
to your nginx server ip/domain
Capture webcam on
/dev/video0
and stream it to nginx-re
- consume stream on media's native bitrate (and not as fast as possible)-f
- use video4linux2 plugin-i
- select physical device to capture from-vcodec
- specify video codec to output-vprofile
- use x264 baseline profile-acodec
- use aac audio codec-strict
- allow using the experimental aac codec-f
- specify format to outputrtmp://localhost/show/stream
- rtmp endpoint to stream to. if the target port is not1935
is should be included in the uri.
The last path component is the stream name - that means that multiple channels can be pushed using different names
Stream file example-vid.mp4
Stream another rtmp stream
5. Take the server for a test run!¶
Now that we are pushing our stream into nginx, a manifest file in the format
stream-name.m3u8
is created in the target folder along with the video fragments.
For our example, the manifest is available at:
http://localhost:8080/hls/stream.m3u8
.
For testing our new HLS live stream we will use videojs5.
player.html
With Peer5 plugin (get your Customer ID here):
It's alive!
Suu tam
Tags:
Stream