1. 程式人生 > >在ubuntu16.04上搭建hls流媒體服務器

在ubuntu16.04上搭建hls流媒體服務器

nts ges 本地服務器 put tel sta .tar.gz 備份 reset

1.Distributor ID: Ubuntu
Description: Ubuntu 16.04.3 LTS
Release: 16.04
Codename: xenial

2.Linux 4.4.0-89-generic #112-Ubuntu SMP Mon Jul 31 19:38:41 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux

mkdir ~/working

切換到~/working目錄下

cd ~/working

獲取nginx源碼:

wget http://nginx.org/download/nginx-1.13.4.tar.gz

解壓

tar xvf nginx-1.13.4.tar.gz

獲取最新的nginx-rtmp源碼

git clone https://github.com/arut/nginx-rtmp-module.git

切換到nginx目錄

cd nginx-1.13.4

配置

./configure --with-http_ssl_module --with-http_stub_status_module --add-module=../nginx-rtmp-module

編譯和安裝配置有nginx-rtmp模塊的nginx

make

sudo make install

安裝nginx初始化腳本

獲取nginx初始化腳本

wget https://raw.github.com/JasonGiedymin/nginx-init-ubuntu/master/nginx

將腳本復制到/etc/init.d/目錄下

sudo cp nginx /etc/init.d/

修改權限

sudo chmod +x /etc/init.d/nginx

使用update-rc.d進行啟動項管理

sudo update-rc.d nginx defaults

創建目錄結構

sudo mkdir /HLS

sudo mkdir /HLS/mobile

sudo mkdir /HLS/live

sudo mkdir /video_recordings

sudo chmod -R 777 /video_recordings

配置nginx

備份原來的nginx配置文件

sudo cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.back

sudo gedit /usr/local/nginx/conf/nginx.conf

往/usr/local/nginx/conf/nginx.conf添加以下內容

worker_processes 1;
error_log logs/error.log debug;
events {
worker_connections 1024;
}
rtmp {
server {
listen 1935;
allow play all;

#creates our "live" full-resolution HLS videostream from our incoming encoder stream and tells where to put the HLS video manifest and video fragments
application live {
allow play all;
live on;
record all;
record_path /video_recordings;
record_unique on;
hls on;
hls_nested on;
hls_path /HLS/live;
hls_fragment 10s;

#creates the downsampled or "trans-rated" mobile video stream as a 400kbps, 480x360 sized video
exec ffmpeg -i rtmp://192.168.1.104:1935/$app/$name -acodec copy -c:v libx264 -preset veryfast -profile:v baseline -vsync cfr -s 480x360 -b:v 400k -maxrate 400k -bufsize 400k -threads 0 -r 30 -f flv rtmp://192.168.1.104:1935/mobile/$;
}

#creates our "mobile" lower-resolution HLS videostream from the ffmpeg-created stream and tells where to put the HLS video manifest and video fragments
application mobile {
allow play all;
live on;
hls on;
hls_nested on;
hls_path /HLS/mobile;
hls_fragment 10s;
}

#allows you to play your recordings of your live streams using a URL like "rtmp://my-ip:1935/vod/filename.flv"
application vod {
play /video_recordings;
}
}
}


http {
include mime.types;
default_type application/octet-stream;

server {
listen 80;
server_name 192.168.1.104;

#creates the http-location for our full-resolution (desktop) HLS stream - "http://my-ip/live/my-stream-key/index.m3u8"
location /live {
types {
application/vnd.apple.mpegurl m3u8;
}
alias /HLS/live;
add_header Cache-Control no-cache;
}

#creates the http-location for our mobile-device HLS stream - "http://my-ip/mobile/my-stream-key/index.m3u8"
location /mobile {
types {
application/vnd.apple.mpegurl m3u8;
}
alias /HLS/mobile;
add_header Cache-Control no-cache;
}

#allows us to see how stats on viewers on our Nginx site using a URL like: "http://my-ip/stats"
location /stats {
stub_status;
}

#allows us to host some webpages which can show our videos: "http://my-ip/my-page.html"
location / {
root html;
index index.html index.htm;
}
}
}

!!記得將本地內網地址換成你實際使用的哦!!

啟動nginx服務

sudo service nginx start

安裝ffmpeg

sudo apt-get install ffmpeg

推流開始

ffmpeg -re -i test.mp4 -vcodec libx264 -acodec aac -f flv rtmp://localhost/mobile/haha /*將本地視頻文件test.mp4轉碼推流到本地服務器*/

在本機端打開vlc播放器

輸入流地址rtmp://localhost/mobile/haha即可觀看視頻,不過本地播放有些卡頓,網絡播放更卡,等待優化。。。(未完待續)

在ubuntu16.04上搭建hls流媒體服務器