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-rtmp-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端口

Linux下如何用nginx+ffmpeg搭建流媒体服务器

使用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

流媒体播放器:

Linux下如何用nginx+ffmpeg搭建流媒体服务器

VLC播放hls流:

Linux下如何用nginx+ffmpeg搭建流媒体服务器

您可能还会对下面的文章感兴趣: