Linux-Nginx+rtmp+ffmpeg搭建流媒體服務器


Nginx+rtmp+ffmpeg搭建流媒體服務器

說明:

  nginx搭建流媒體服務需要用到 nginx-rtmp-module 模塊

具體操作步驟:

安裝nginx

(1)下載第三方擴展模塊nginx-rtmp-module

# mkdir module && cd module          //創建一個存放模塊的目錄
# wget https://github.com/arut/nginx-rtmp-module/archive/master.zip        //下載模塊
# unzip master.zip                //解壓
# ls nginx-rtmp-module-master/         //查看模塊目錄

(2)編譯安裝nginx說明:此處由於我這邊已有lnmp運行項目的環境,直接動態添加的 nginx-rtmp-module模塊; 動態參加可參考這里

# yum -y install pcre-devel openssl openssl-devel        //安裝依賴
# wget http://nginx.org/download/nginx-1.12.2.tar.gz        //下載nginx包
# tar xf nginx-1.12.2.tar.gz
# ./configure --prefix=/opt/nginx-1.9.5 --add-module=/root/module/nginx-rtmp-module-master --with-http_ssl_module    //編譯安裝nginx,並指定上面下載的模塊路徑
# make        
# make install

(3)修改nginx配置文件,添加如下內容並重新載入配置文件

# vim nginx.conf
rtmp {  
    server {  
        listen 1935;      #監聽的端口號
        application myapp {     #自定義的名字
            live on;  
       }  
        application hls {  
            live on;  
            hls on;  
            hls_path /tmp/hls;   
            hls_fragment 1s;
            hls_playlist_length 3s;  
       }  
    } 
}
# /etc/init.d/nginx reload
user  root;
worker_processes  4;
worker_cpu_affinity 1000 0100 0010 0001;
worker_rlimit_nofile 30000;


#error_log  logs/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  30000;
    use epoll;
}

rtmp {  
    server {  
        listen 1935;  
        application myapp {  
            live on;  
       }  
       application hls {  
         live on;  
             hls on;  
             hls_path /tmp/rtmp/hls;  
             hls_fragment 1s;
         hls_playlist_length 3s;  
       }  
    } 
}


http {

    limit_req_zone  $binary_remote_addr  zone=req_one:10m rate=1r/s;

    autoindex off;
    include       /opt/nginx-1.9.5/conf/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" $http_x_forwarded_for $request_length $request_time "$upstream_response_time" "$upstream_addr" $host $server_addr';
                     
                      

    #access_log  logs/access.log  main;
    server_tokens off;
    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    gzip  on;
    gzip_min_length  1K;
    gzip_buffers   4 16K;
    gzip_comp_level   8;
    gzip_http_version 1.0;
    gzip_types    application/json  text/javascript  text/plain application/x-javascript text/css application/xml  text/xml;
    #gzip_types    application/json  text/javascript  text/plain application/x-javascript text/css application/xml  text/xml image/jpeg image/gif image/png;
    gzip_disable "MSIE [1-6]\.";
    client_max_body_size 500M;


    #server {
    #    listen 80 default;
    #    return 500;
    #}

    include /opt/nginx-1.9.5/conf/conf.d/*.conf;

    
}
完整nginx.conf

安裝ffmpeg

ffmpeg命令參考這位朋友的文章:https://www.jianshu.com/p/049d03705a81

(1)安裝依賴

# yum install yasm -y

(2)下載ffmpeg並安裝

# git clone https://git.ffmpeg.org/ffmpeg.git ffmpeg    //下載ffmpeg
# cd ffmpeg
# ./configure --prefix=/usr/local/ffmpeg
# make
# make install

(3)拷貝命令到/usr/bin(方便后面調用)

# ls /usr/local/ffmpeg/        //查看安裝目錄生成的文件
bin  include  lib  share
# cp /usr/local/ffmpeg/bin/* /usr/bin/

測試

(1)啟用ffmpeg進行推流

# ffmpeg -i rtsp://192.168.1.175:554/11 -acodec aac -strict experimental -ar 44100 -ac 2 -b:a 96k -r 25 -b:v 500k -s 640*480 -f flv rtmp://192.168.1.11:1935/myapp/23
-i 要處理視頻文件的路徑,此處地址是一個監控攝像頭
-s 像素
rtmp://192.168.1.11:1935/myapp/23  說明:rtmp://IP:PORT/ myapp指nginx配置文件中自定義的,22指輸出文件的名字
-f 強迫采用flv格式
別的參數參考上面那位老兄的

 (2)打開VLC 媒體——>流——>網絡

 

(3)進入服務器查看輸出的位置可以發現已生成文件

# ll /tmp/hls/
total 1636
-rw-r--r-- 1 root root 500644 Mar 28 17:05 22-1955.ts
-rw-r--r-- 1 root root 384460 Mar 28 17:05 22-1956.ts
-rw-r--r-- 1 root root 413036 Mar 28 17:05 22-1957.ts
-rw-r--r-- 1 root root 366036 Mar 28 17:05 22-1958.ts
-rw-r--r-- 1 root root    154 Mar 28 17:05 22.m3u8

(4)還可以使用瀏覽器這樣訪問 http://192.168.1.11/hls/22.m3u8

 

至此就搭建完成了,至於別的操作后續再寫

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM