Ubuntu中使用nginx-rtmp-module实现视频点播
Ubuntu下安装nginx-rtmp-module
1下载源代码
1.1 下载nginx-rtmp-module源码
首先建立一个目录
# 切换到用户主目录
cd ~
# 创建新目录
mkdif nginx
下载nginx-rtmp-module源码
git clone https://github.com/arut/nginx-rtmp-module.git
1.2 下载nginx源码
网上大部分资料是1.13.4的版本,编译时可能报错
src/os/unix/ngx_user.c:36:7: error: ‘struct crypt_data’ has no member named ‘current_salt
改为最新的1.29.1版本,可以避免报错:
# 下载nginx
wget https://nginx.org/download/nginx-1.29.1.tar.gz
# 解压缩
tar -zxvf nginx-1.29.1.tar.gz
2 安装nginx
2.1 配置
# 切换至nginx目录
cd nginx-1.29.1
# 配置编译信息
./configure --add-module=../nginx-rtmp-module --with-http_ssl_module
需要依赖包,根据提示信息分别执行相应的命令安装依赖包:
- gcc
./configure: error: C compiler cc is not found
sudo apt-get install gcc
- openssl
./configure: error: SSL modules require the OpenSSL library.
You can either do not enable the modules, or install the OpenSSL library
into the system, or build the OpenSSL library statically from the source
with nginx by using --with-openssl=option.
sudo apt-get install openssl libssl-dev
- gzip
./configure: error: the HTTP gzip module requires the zlib library.
You can either disable the module by using --without-http_gzip_module
option, or install the zlib library into the system, or build the zlib library
statically from the source with nginx by using --with-zlib=option.
sudo apt-get install zlib1g-dev
2.2 编译
依次运行以下命令
make
make install
若未安装make,可能提示
makeCommand ‘make’ not found
运行以下命令安装相应工具
sudo apt-get install build-essential
3 启动nginx
命令行输入
# 切换至nginx目录
cd /usr/local/nginx/sbin
# 启动nginx
sudo ./nginx
在浏览器中输入http://localhost,看到启动画面,启动成功。
4 配置rtmp
打开nginx.conf配置文件
# 切换目录
cd /usr/local/nginx/conf
# 备份配置文件
sodu cp nginx.conf nginx.conf.bak
# vim编辑配置
sudo vim nginx.conf
进入vim编辑界面后,按i键进入编辑模式,通过键盘操作,在配置文件的events和http项之间增加rtmp项:
events {worker_connections 1024;
}rtmp { #RTMP服务server {listen 1935; # 服务端口chunk_size 4096; # 数据传输块的大小application vod {play /opt/video/vod; # 视频文件存放位置}}
}http {include mime.types;default_type application/octet-stream;
编辑完成后按ESC,再输入
:wq!
回车后即保存了修改内容并退出vim界面。
保存后,可在命令行输入:
sudo /usr/local/nginx/sbin/nginx -t
测试配置文件。测试成功后,重启nginx:
sudo ./sbin/nginx -s reload
5 准备视频文件和播放器
5.1 视频文件
在配置文件中设置了一个路径/opt/video/vod用于存放要点播的视频文件(不确定是否只能是这个路径,测试/home下的目录点播时都提示找不到视频),首先创建这个路径
# 添加目录
sudo mkdir /opt/video
sudo mkdir /opt/video/vod
# 修改目录权限
sudo chmod 777 /opt/video/vod
准备一个视频文件如.mp4拷贝至/opt/video/vod下。
5.2 准备流媒体播放器
一般使用VLC,可以直接通过源安装
sudo apt install vlc
安装完成后,在Ubuntu左下角的Show Apps图标点击后找到VLC打开
点击菜单Media-Open Network Stream,在输入URL的文本框中输入rtmp://localhost:1935/vod/1.mp4
rtmp://localhost:1935/vod/1.mp4
其中localhost是默认本地的地址,1935是在配置文件中listen项设置的端口,vod对应配置文件中application vod的应用名称,1.mp4是视频文件的名称。
在对话框中点击Play,能够看到视频,说明配置成功。