1 . 安装nginx
参考:https://blog.csdn.net/TH_NUM/article/details/103199376
2. 下载安装 ffmpeg
去ffpmpeg 下载官网的源代码:
http://ffmpeg.org/
安装ffmpeg
./configure
make
make install
RTMP 通过FFmpeg+nginx发布成rtmp和http-flv
编译nginx需要加入(nginx-http-flv-module)模块(参考:https://blog.csdn.net/TH_NUM/article/details/103199376),执行configure的时候要加入–add-module=path/lib/nginx-http-flv-module选项。
nginx的具体编译过程不详介绍。
./configure --sbin-path=/usr/local/nginx/nginx \
--conf-path=/usr/local/nginx/nginx.conf \
--pid-path=/usr/local/nginx/nginx.pid \
--with-http_ssl_module \
--with-pcre=/home/dlr/wuyongyu/nginx/pcre-8.43 \
--add-module=/home/dlr/wuyongyu/nginx/nginx-http-flv-module \
--with-openssl=/home/dlr/wuyongyu/nginx/openssl-1.0.1t
重新编译nginx是为了集成nginx-http-flv-module这个模块,在发布rtmp的时候可以转化成支持HTTP方式的FLV。
配置nginx.conf
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location /live {
flv_live on;
chunked_transfer_encoding on; #open 'Transfer-Encoding: chunked' response
add_header 'Access-Control-Allow-Credentials' 'true'; #add additional HTTP header
add_header 'Access-Control-Allow-Origin' '*'; #add additional HTTP header
add_header Access-Control-Allow-Headers X-Requested-With;
add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
add_header 'Cache-Control' 'no-cache';
}
location /hls {
types {
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
root /tmp;
add_header 'Cache-Control' 'no-cache';
}
location /dash {
root /tmp;
add_header 'Cache-Control' 'no-cache';
}
location /stat {
#configuration of push & pull status
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}
location /stat.xsl {
root /var/www/rtmp; #specify in where stat.xsl located
}
location /control {
rtmp_control all; #configuration of control module of rtmp
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
rtmp_auto_push on;
rtmp_auto_push_reconnect 1s;
rtmp_socket_dir /tmp;
rtmp {
out_queue 4096;
out_cork 8;
max_streams 128;
timeout 15s;
drop_idle_publisher 15s;
log_interval 5s; #interval used by log module to log in access.log, it is very useful for debug
log_size 1m; #buffer size used by log module to log in access.log
server {
listen 1935;
server_name localhost; #for suffix wildcard matching of virtual host name
application live {
live on;
gop_cache on; #open GOP cache for reducing the wating time for the first picture of video
}
application hls {
live on;
hls on;
hls_path /tmp/hls;
}
application dash {
live on;
dash on;
dash_path /tmp/dash;
}
}
}
配置好nginx.conf,在cmd下启动nginx服务
第二步:编写网页端显示的代码
使用video-js
1.进入到 nginx/html 文件夹,可以看到有两个.html文件,index.html就是Welcome to nginx!这个页面
先把它备份一份,再自己写个index.html,用来显示视频流。内容如下:
需要从:https://download.csdn.net/download/TH_NUM/11990337 下载
video-js.swf文件从压缩包中解压出来放到html 文件夹下。
PS: video-js.css和video.js文件也在这个压缩包中,也可以把它们都解压出来放到html 文件夹下
index.html
RTMP Sample Player Videojs
RTMP Player Videojs
ffmpeg 推流:
ffmpeg -re -i ~/wuyongyu/video/demo.mp4 -vcodec copy -acodec copy -f flv rtmp://localhost:1935/live/mystream
或则推流摄像头:
ffmpeg -f v4l2 -video_size 640x480 -framerate 30 -i /dev/video0 -vcodec libx264 -preset veryfast -f flv rtmp://localhost:1935/live/mystream
其他:
ffmpeg 拍照:
ffmpeg -f video4linux2 -i /dev/video0 -vframes 1 /usr/local/nginx/html/test2.jpeg -y
在浏览器地址栏输入localhost:80 或者http://localhost:80/live?port=1935&app=live&stream=mystream