由于工作开发需求,我们很多时候需要有自己的测试流媒体服务器,个人简单的整理了下ubuntu下面搭建自动流媒体测试服务器;
创建工作目录:
1、 创建目录获取nginx源码:
mkdir hls_test
cd hls_test
wget http://nginx.org/download/nginx-1.13.4.tar.gz
2、 解压:
tar xvf nginx-1.13.4.tar.gz
3、获取最新的nginx-rtmp源码:
git clone https://github.com/arut/nginx-rtmp-module.git
4、切换到nginx目录进行相关配置:
./configure --with-http_ssl_module --with-http_stub_status_module --add-module=../nginx-rtmp-module
可以看到有报错信息:
然后我们尝试安装:
sudo apt-get install libssl-dev
后就可以开始编译和安装配置有nginx-rtmp模块的nginx:
5、编译
make
sudo make install
6、安装nginx初始化脚本、获取nginx初始化脚本,由于网络原因可能会失败,多试几次:
wget https://raw.github.com/JasonGiedymin/nginx-init-ubuntu/master/nginx
7、将脚本复制到/etc/init.d/目录下:
sudo cp nginx /etc/init.d/
修改权限:
sudo chmod +x /etc/init.d/nginx
8、使用update-rc.d进行启动项管理:
sudo update-rc.d nginx defaults
9、创建目录结构
sudo mkdir /HLS
sudo mkdir /HLS/mobile
sudo mkdir /HLS/live
sudo mkdir /video_recordings
sudo chmod -R 777 /video_recordings
10、配置nginx、备份原来的nginx配置文件
sudo cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.back
sudo vi /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://116.62.57.209: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://116.62.57.209: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 116.62.57.209;
#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;
}
}
}
需要注意:记得修改localhost为你当前服务器的IP地址;
11、启动nginx服务:
sudo service nginx start
12、安装ffmpeg
sudo apt-get install ffmpeg
13、开始推流:
sudo ffmpeg -re -i test.mp4 -vcodec libx264 -acodec aac -strict -2 -f flv rtmp://116.62.57.209:1935/mobile/haha2
14、 如果是用的阿里云可能需要开启端口号,建议进出都修改下规则
15、PC端打开vlc测试视频效果
输入流地址rtmp://116.62.57.209:1935/mobile/haha2即可观看视频:
http的也可以参考对应的配置;