1. 购买云服务器

申请成功后,会收到邮件。

您新购买的云服务器(共1台)已分配成功(订单号:20180108126856),感谢您对腾讯云的支持!
服务器操作系统为 Ubuntu Server 16.04.1 LTS 64位 ,默认账户为 ubuntu ,初始密码为 9WHGBwKUSa3EHUSi
服务器名称 云主机ID 所在网络ID 内网IP 公网IP
u672au547du540d ins-qgchmd9i 基础网络 10.135.148.23 139.199.66.124
用终端登录
The authenticity of host '139.199.66.124 (139.199.66.124)' can't be established.
ECDSA key fingerprint is SHA256:P0XOcgjXyTikxaGKzmWsALKUYZPqTftP0KTFoTJnFvs.
Are you sure you want to continue connecting (yes/no)?

在控制台点登录或者在终端输入ssh 用户名@服务器IP地址
登录到服务器


第一次用ssh连接会提示验证主机

服务器查看自己的ECDSA key fingerprint
ssh-keygen -l -f /etc/ssh/ssh_host_ecdsa_key.pub

原来的密码太长,使用命令password
更改密码为8*
2.设置无密码登录(可跳过次步骤)
复制本机的~/.ssh/id_rsa.pub
,到云主机控制台创建密钥,密钥名称可以随便输入,公钥就是刚刚复制的内容

创建完成后,加载密钥



重启云服务器,现在可以无密码登录了

3.搭建nginx服务器
开发环境
Ubuntu 16.04 server
nginx的服务器的搭建
下载nginx
mkdir ~/code
cd ~/code
wget http://nginx.org/download/nginx-1.8.1.tar.gz
解压
tar -zxf nginx-1.8.1.tar.gz -C ~/code/
下载nginx-rtmp-module
git clone https://github.com/arut/nginx-rtmp-module.git

安装nginx的依赖库
sudo apt-get update
sudo apt-get install libpcre3 libpcre3-dev
sudo apt-get install openssl libssl-dev
配置并编译nginx
使用nginx的默认配置,添加nginx的rtmp模块。

cd ~/code/nginx-1.8.1/
./configure --add-module=../nginx-rtmp-module
make
sudo make install
运行测试nginx
进入安装目录
cd /usr/local/nginx
运行命令
sudo ./sbin/nginx
注意:以后所有的命令都在
/usr/local/nginx
目录运行,也是nginx配置文件的相对目录。
打开浏览器在地址栏输入:139.199.66.124
。如果,如下图显示那样就证明您的nginx服务器搭建成功了。

4.点播视频服务器的配置
通过上一步nginx服务器已经搭建完成,然后我们就可以开启一个视频点播的服务了。打开配置文件nginx.conf
,添加RTMP的配置。
worker_processes 1;
events {
worker_connections 1024;
}
rtmp { #RTMP service
server {
listen 1935; #port
chunk_size 4096; #data size
application vod {
play /home/ubuntu/Videos; #video path
}
}
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
发送测试用的视频文件到服务器
rong@rong-ubuntu-07:~/Videos$ scp testfile.mp4 ubuntu@139.199.66.124:/home/ubuntu/Videos
ubuntu@139.199.66.124's password:
testfile.mp4 100% 908KB 908.4KB/s 00:00

文件放好之后,那就让我们重新启动一下nginx
sudo ./sbin/nginx -s reload
打开视频播放软件选用的是VLC media-> open network stream….
如图填写我们要点播的节目地址rtmp://139.199.66.124/vod/testfile.mp4 如图:


点击play就可以播放了。
当然点播不使用RTMP插件nginx自身也是可以实现点播服务的。那就是配置location部分,由于下面我们要配置直播和回看功能所以选用了RTMP服务。
5.直播视频服务器的配置
接着我们就在点播服务器配置文件的基础之上添加直播服务器的配置。一共2个位置,第一处就是给RTMP服务添加一个application这个名字可以任意起,也可以起多个名字,由于是直播我就叫做它live吧,如果打算弄多个频道的直播就可以live_cctv1、live_cctv2名字任意。第二处就是添加两个location字段,字段的内容请直接看文件吧。
worker_processes 1;
events {
worker_connections 1024;
}
rtmp { #RTMP service
server {
listen 1935; #port
chunk_size 4096; #data size
application vod {
play /home/ubuntu/Videos; #video path
}
application live{ #first live field
live on;
}
}
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location /stat { #first location field
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}
location /stat.xsl { #second location field
root /home/ubuntu/code/nginx-rtmp-module/;
}
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
添加完这两处之后,重新启动nginx打开浏览器看看,是否有如下图显示:

有没有看到live字样呢?如果可以显示出来,证明你的配置生效了。试一下推流到“rtmp://139.199.66.124/live/test”


播放的地址就是“rtmp://139.199.66.124/live/test”,如果您本地有支持rtmp协议的播放器就可以试试了。


6.实时回看视频服务器的配置
我们想一想如果直播服务能够把节目录制在本地,我们不就可以直接进行回看先前的节目了吗?回看一分钟、一小时甚至一天的。想想就兴奋不用写代码有现成的可以使用。怎么用呢?继续看nginx的配置吧。
worker_processes 1;
events {
worker_connections 1024;
}
rtmp {
server {
listen 1935;
chunk_size 4096;
application vod {
play /home/ubuntu/Videos;
}
application live{
live on;
hls on; #这个参数把直播服务器改造成实时回放服务器。
wait_key on; #对视频切片进行保护,这样就不会产生马赛克了。
hls_path /home/ubuntu/Videos/hls; #切片视频文件存放位置。
hls_fragment 10s; #每个视频切片的时长。
hls_playlist_length 60s; #总共可以回看的事件,这里设置的是1分钟。
hls_continuous on; #连续模式。
hls_cleanup on; #对多余的切片进行删除。
hls_nested on; #嵌套模式。
}
}
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location /stat {
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}
location /stat.xsl {
root /home/ubuntu/code/nginx-rtmp-module/;
}
location /live {
types {
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
alias /home/ubuntu/Videos/hls;
expires -1;
add_header Cache-Control no-cache;
}
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
1.添加完成后需要重新启动nginx,由于这次nginx需要向服务器写切片视频文件,但nginx我又没有给nginx指定用户名只能走默认的nobody用户和nogroup用户组,其实就是没有组。所以我对需要写入的目录做了增大权限的修改。
如下图,这样做就是为了避免由于权限问题而无法写文件。

2.如何给服务器录制视频,在上一节已经说过,这里就不再说了。
3.查看视频文件是否真的录制上没有,继续看图:

已经产生切片视频文件了。其中还有一个index.m3u8。
4.播放视频,这次可是http开头的了,“http://139.199.66.124/live/test/index.m3u8”。
5.已经可以播放了,如何回看呢?其实这个index.m3u8文件仅仅是目录。想回看那个就播放那个.ts文件就可以了。
7.录制flv视频服务器的配置
worker_processes 1;
events {
worker_connections 1024;
}
rtmp {
server {
listen 1935;
chunk_size 4096;
application vod {
play /home/ubuntu/Videos;
}
application live{
live on;
hls on;
wait_key on;
hls_path /home/ubuntu/Videos/hls;
hls_fragment 10s;
hls_playlist_length 60s;
hls_continuous on;
hls_cleanup on;
hls_nested on;
record all; #record flv
record_path /home/ubuntu/Videos/hls;
record_suffix -%d-%b-%y-%T.flv;
}
}
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location /stat {
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}
location /stat.xsl {
root /home/ubuntu/code/nginx-rtmp-module/;
}
location /live {
types {
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
alias /home/ubuntu/Videos/hls;
expires -1;
add_header Cache-Control no-cache;
}
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
查看是否保存flv文件

把文件复制到本地
scp ubuntu@139.199.66.124:/home/ubuntu/Videos/hls/test-08-Jan-18-20:35:40.flv ~/temp

播放

到此结束。
附上一些报错
重启服务器后报错
ubuntu@VM-148-23-ubuntu:/usr/local/nginx$ sudo ./sbin/nginx -s reload
nginx: [error] open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory)
ubuntu@VM-148-23-ubuntu:/usr/local/nginx$ sudo ./sbin/nginx
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()
解:
因为我装了两个nginx,要卸载掉自动安装的
sudo apt-get purge nginx
sudo apt-get autoremove
保存的ts和m3u8文件会自动删除
解:因为设置了这个hls_playlist_length 60s; #总共可以回看的事件,这里设置的是1分钟。
无法生成flv文件
查看error.log
2018/01/08 20:13:45 [crit] 31443#0: *25 record: failed to open file '/home/rong/temp/test-08-Jan-18-20:13:45.flv' (13: Permission denied), client: 192.168.3.7, server: 0.0.0.0:1935
解:修改目录权限为777.

参考
https://github.com/arut/nginx-rtmp-module/issues/332
https://github.com/arut/nginx-rtmp-module/wiki/Directives#record
网友评论