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

來源:互聯網
上載者:User

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

 

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即可觀看視頻,不過本地播放有些卡頓,網路播放更卡,等待最佳化。。。(未完待續)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.