手把手教你,ubuntu 搭建实时流媒体服务器

   由于工作开发需求,我们很多时候需要有自己的测试流媒体服务器,个人简单的整理了下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的也可以参考对应的配置;

  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值