Nginx+rtmp+ffmpeg搭建流媒体服务器
说明:
nginx搭建流媒体服务需要用到 nginx-rtmp-module 模块
具体操作步骤:
安装nginx
(1)下载第三方扩展模块nginx-rtmp-module
# mkdir module && cd module //创建一个存放模块的目录 # wget https://github.com/arut/nginx-rtmp-module/archive/master.zip //下载模块 # unzip master.zip //解压 # ls nginx-rtmp-module-master/ //查看模块目录
(2)编译安装nginx(说明:此处由于我这边已有lnmp运行项目的环境,直接动态添加的 nginx-rtmp-module模块; 动态参加可参考:这里)
# yum -y install pcre-devel openssl openssl-devel //安装依赖 # wget http://nginx.org/download/nginx-1.12.2.tar.gz //下载nginx包 # tar xf nginx-1.12.2.tar.gz # ./configure --prefix=/opt/nginx-1.9.5 --add-module=/root/module/nginx-rtmp-module-master --with-http_ssl_module //编译安装nginx,并指定上面下载的模块路径 # make # make install
(3)修改nginx配置文件,添加如下内容并重新载入配置文件
# vim nginx.conf rtmp { server { listen 1935; #监听的端口号 application myapp { #自定义的名字 live on; } application hls { live on; hls on; hls_path /tmp/hls; hls_fragment 1s; hls_playlist_length 3s; } } } # /etc/init.d/nginx reload
user root; worker_processes 4; worker_cpu_affinity 1000 0100 0010 0001; worker_rlimit_nofile 30000; #error_log logs/error.log warn; pid /var/run/nginx.pid; events { worker_connections 30000; use epoll; } rtmp { server { listen 1935; application myapp { live on; } application hls { live on; hls on; hls_path /tmp/rtmp/hls; hls_fragment 1s; hls_playlist_length 3s; } } } http { limit_req_zone $binary_remote_addr zone=req_one:10m rate=1r/s; autoindex off; include /opt/nginx-1.9.5/conf/mime.types; default_type application/octet-stream; log_format main '$remote_addr [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" $http_x_forwarded_for $request_length $request_time "$upstream_response_time" "$upstream_addr" $host $server_addr'; #access_log logs/access.log main; server_tokens off; sendfile on; #tcp_nopush on; keepalive_timeout 65; gzip on; gzip_min_length 1K; gzip_buffers 4 16K; gzip_comp_level 8; gzip_http_version 1.0; gzip_types application/json text/javascript text/plain application/x-javascript text/css application/xml text/xml; #gzip_types application/json text/javascript text/plain application/x-javascript text/css application/xml text/xml image/jpeg image/gif image/png; gzip_disable "MSIE [1-6]."; client_max_body_size 500M; #server { # listen 80 default; # return 500; #} include /opt/nginx-1.9.5/conf/conf.d/*.conf; }
安装ffmpeg
ffmpeg命令参考这位朋友的文章:https://www.jianshu.com/p/049d03705a81
(1)安装依赖
# yum install yasm -y
(2)下载ffmpeg并安装
# git clone https://git.ffmpeg.org/ffmpeg.git ffmpeg //下载ffmpeg # cd ffmpeg # ./configure --prefix=/usr/local/ffmpeg # make # make install
(3)拷贝命令到/usr/bin(方便后面调用)
# ls /usr/local/ffmpeg/ //查看安装目录生成的文件 bin include lib share # cp /usr/local/ffmpeg/bin/* /usr/bin/
测试
(1)启用ffmpeg进行推流
# ffmpeg -i rtsp://192.168.1.175:554/11 -acodec aac -strict experimental -ar 44100 -ac 2 -b:a 96k -r 25 -b:v 500k -s 640*480 -f flv rtmp://192.168.1.11:1935/myapp/23 -i 要处理视频文件的路径,此处地址是一个监控摄像头 -s 像素 rtmp://192.168.1.11:1935/myapp/23 说明:rtmp://IP:PORT/ myapp指nginx配置文件中自定义的,22指输出文件的名字 -f 强迫采用flv格式 别的参数参考上面那位老兄的
(2)打开VLC 媒体——>流——>网络
(3)进入服务器查看输出的位置可以发现已生成文件
# ll /tmp/hls/ total 1636 -rw-r--r-- 1 root root 500644 Mar 28 17:05 22-1955.ts -rw-r--r-- 1 root root 384460 Mar 28 17:05 22-1956.ts -rw-r--r-- 1 root root 413036 Mar 28 17:05 22-1957.ts -rw-r--r-- 1 root root 366036 Mar 28 17:05 22-1958.ts -rw-r--r-- 1 root root 154 Mar 28 17:05 22.m3u8
(4)还可以使用浏览器这样访问 http://192.168.1.11/hls/22.m3u8
至此就搭建完成了,至于别的操作后续再写