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搭建流媒體服務器

需要C/C++ Linux服務器開發學習資料私信“資料”(資料包括C/C++,Linux,golang技術,Nginx,ZeroMQ,MySQL,Redis,fastdfs,MongoDB,ZK,流媒體,CDN,P2P,K8S,Docker,TCP/IP,協程,DPDK,ffmpeg等),免費分享


分享到:


相關文章: