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編譯參數(shù)

./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狀態(tài)頁面

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監(jiān)聽1935端口,如果是hls的話用hls on開啟hls,并且為hls設置一個臨時文件目錄hls_path /tmp/hls; 其它更高級的配置可以參看nginx-rtmp-module的readme,里面有比較詳細的介紹其它的配置,并且它還提供了一個通過JWPlayer在網(wǎng)頁上播放的例子.

重啟nginx

nginx -t

nginx -s reload

查看nginx已經(jīng)監(jiān)聽1935端口

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

流媒體播放器:

VLC播放hls流:

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

  • 包含的重點內(nèi)容:JAVA基礎JVM 知識開源框架知識操作系統(tǒng)多線程TCP 與 HTTP架構設計與分布式算法數(shù)據(jù)庫知...
    消失er閱讀 4,565評論 1 10
  • 最全的iOS面試題及答案 iOS面試小貼士 ———————————————回答好下面的足夠了-----------...
    zweic閱讀 2,804評論 0 73
  • 多線程、特別是NSOperation 和 GCD 的內(nèi)部原理。運行時機制的原理和運用場景。SDWebImage的原...
    LZM輪回閱讀 2,132評論 0 12
  • ———————————————回答好下面的足夠了---------------------------------...
    恒愛DE問候閱讀 1,846評論 0 4
  • 背景 項目地址 etcd-v3-apietcd-web-ui
    ddu_sw閱讀 3,601評論 0 1

友情鏈接更多精彩內(nèi)容