ffmpeg linux安装_Linux下如何用nginx+ffmpeg搭建流媒体服务器

安装ffmpeg

安装过程略

安装完成后,检查是否安装成功。比如我这里采用向pili推流的方式,将本地的一个mp4视频推流到七牛pili。

ffmpeg -re -i /tmp/ffmpeg_test.mp4 -vcodec copy -acodec copy -f flv "rtmp://pili-publish.qingkang.echohu.top/qingkang/stream1?key=***"

安装nginx

安装过程略

需要注意的是:一定要添加nginx-rtmp-module模块

git clone https:// github.com/arut/nginx-r tmp-module.git
#编译的时候添加nginx-rtmp-module模块
--add-module=path_of_/nginx-rtmp-module

我的nginx编译参数

./configure --prefix=/opt/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_gzip_static_module --with-http_stub_status_module --with-pcre=/opt/software/pcre-8.35 --with-zlib=/opt/software/zlib-1.2.8 --with-openssl=/opt/software/openssl-1.0.1i --add-module=/opt/software/nginx-1.8.1/modules/nginx-rtmp-module

修改nginx配置文件nginx.conf

加入rtmp配置

#切换自动推送(多 worker 直播流)模式。默认为 off
rtmp_auto_push on;
#当 worker 被干掉时设置自动推送连接超时时间。默认为 100 毫秒
rtmp_auto_push_reconnect 1s;
rtmp {
server {
listen 1935;
#直播流配置
application myapp {
live on;
}
application hls {
live on;
hls on;
hls_path /tmp/hls;
}
application qiniu {
live on;
push 推流地址;
}
application pull {
live on;
pull 拉流地址;
}
#rtmp日志设置
access_log logs/rtmp_access.log ;
}
}

在http中增加一个location配置支持hls

location /hls {
types {
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
root /tmp;
add_header Cache-Control no-cache;
}

完整的nginx.conf如下

#user nobody;
worker_processes 2;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
use epoll;
worker_connections 1024;
}
#切换自动推送(多 worker 直播流)模式。默认为 off
rtmp_auto_push on;
#当 worker 被干掉时设置自动推送连接超时时间。默认为 100 毫秒
rtmp_auto_push_reconnect 1s;
rtmp {
server {
listen 1935;
#直播流配置
application myapp {
live on;
}
application hls {
live on;
hls on;
hls_path /tmp/hls;
}
application qiniu {
live on;
push 推流地址;
}
application pull {
live on;
pull 拉流地址;
}
#rtmp日志设置
access_log logs/rtmp_access.log ;
}
}
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 utf-8;
#access_log logs/host.access.log main;
location / {
root /opt/www/html;
index index.html index.htm;
}
#rtmp状态页面
location /stat {
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}
location /stat.xsl {
root /opt/software/nginx-rtmp-module/;
}
location /hls {
types {
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
root /tmp;
add_header Cache-Control no-cache;
}
#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;
}
}
include vhosts/*.conf;
}

这是一个最简单,最基础的配置, rtmp监听1935端口,如果是hls的话用hls on开启hls,并且为hls设置一个临时文件目录hls_path /tmp/hls; 其它更高级的配置可以参看nginx-rtmp-module的readme,里面有比较详细的介绍其它的配置,并且它还提供了一个通过JWPlayer在网页上播放的例子.

重启nginx

nginx -t
nginx -s reload

查看nginx已经监听1935端口

6c71204c33e53e20f37bef26781cf029.png

使用ffmpeg推流到nginx

推一个本地的mp4到上面配置的myapp上:

ffmpeg -re -i /tmp/ffmpeg_test.mp4 -vcodec copy -acodec copy -f flv "rtmp://127.0.0.1:1935/myapp/test1"
流播放地址为(10.0.0.6是我本地的IP):rtmp://10.0.0.6:1935/myapp/test1

推一个本地的mp4到hls上

ffmpeg -re -i /tmp/ffmpeg_test.mp4 -vcodec copy -acodec copy -f flv "rtmp://127.0.0.1:1935/hls/test2"
流播放地址为: http:// 10.0.0.6/hls/test2.m3u8

流媒体播放器:

3d01214de300e900fc3941ef2ee57534.png

VLC播放hls流:

75bd3fe461c0e9655ecb8598d65b79d1.png

需要C/C++ Linux服务器开发学习资料私信“资料”(资料包括C/C++,Linux,golang技术,Nginx,ZeroMQ,MySQL,Redis,fastdfs,MongoDB,ZK,流媒体,CDN,P2P,K8S,Docker,TCP/IP,协程,DPDK,ffmpeg等),免费分享

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值