1、安装FFmpeg
tar zxvf ffmpeg-4.1.tar.gz
cd ffmpeg-4.1/
./configure
sudo make install
2、安装Nginx
(1) 安装相关的依赖包
yum install gcc
sudo apt-get install libpcre3 libpcre3-dev
sudo apt-get install zlib1g-dev
sudo apt-get install openssl libssl-dev
(2)下载安装nginx
wget http://nginx.org/download/nginx-1.13.10.tar.gz
#解压
tar -zxvf nginx-1.13.10.tar.gz
#下载RTMP(到HOME目录)
git clone https://github.com/arut/nginx-rtmp-module.git
#进入NGINX解压目录
cd nginx-1.13.10
#配置
./configure --prefix=/usr/local/nginx --add-module=~/nginx-rtmp-module --with-http_ssl_module
#编译
make
#安装
sudo make install
(3)查看是否安装成功
cd /usr/local/nginx/sbin
sudo ./nginx -t
(4)成功之后配置
#打开nginx配置文件
cd /usr/local/nginx/conf/
sudo gedit nginx.conf
打开文档下拉至末尾:
rtmp{
server{
listen 1935;
application myapp{
live on;
record off;
}
application hls{
live on;
hls on;
hls_path nginx-rtmp-module/hls;
hls_cleanup off;
}
}
}
3、使用ffmpeg进行转码
#默认为UDP协议传输转码
ffmpeg -i "rtsp://xxxxxxx" -f flv -r 25 -s 1080*720 -an "rtmp://localhost:1935/hls/mystream"
#TCP协议转码
ffmpeg -re -rtsp_transport tcp -i "rtsp://xxxxxxxxxx" -f flv -r 25 -s 1080*720 -an "rtmp://localhost:1935/hls/mystream"
以上两个可能再播放的时候流畅度上不太好,画质也不好
#tcp协议转码,插件解码,但是插件需要安装
ffmpeg -re -rtsp_transport tcp -i "rtsp://xxxxxxxxxxxx" -vcodec libx264 -vprofile baseline -acodec aac -ar 44100 -strict -2 -ac 1 -f flv -s 1280x720 -q 10 "rtmp://localhost:1935/myapp/mystream"
3、可以用VLC播放器测试rtmp是否转码成功
转码地址为:
rtmp://localhost:1935/hls/mystream
rtmp://localhost:1935/myapp/mystream
ps:以上两个路径上面的hls 和 myapp都是在nginx配置的,路径后面的可以随意的改动,它的作用只是在为码流指定方向
4、网页播放
将上面得到的rtmp路径填写到html文件中,就可以播放视频了。不过最好用google浏览器。videojs-flash.js和videojs-contrib-hls.js有的时候,链接访问不到,所以需要自己去github上查找,并下载下来,加入本地或者使用一个可以使用的远程 链接
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>video.js</title>
<link href="https://unpkg.com/video.js@6.11.0/dist/video-js.min.css" rel="stylesheet">
<script src="https://unpkg.com/video.js@6.11.0/dist/video.min.js"></script>
<script src="https://unpkg.com/videojs-flash/dist/videojs-flash.js"></script>
<script src="https://unpkg.com/videojs-contrib-hls/dist/videojs-contrib-hls.js"></script>
</head>
<body>
<video id="my-player" class="video-js" controls>
<source src="rtmp://localhost:1935/myapp/stream-name" type="rtmp/flv">
<p class="vjs-no-js">
not support
</p>
</video>
<script type="text/javascript">
var player = videojs('my-player',{
width:400,
heigh:200
});
</script>
</body>
</html>