用nginx搭建http/rtmp/hls協議的MP4/FLV流媒體服務器

前先後後搭建了兩三個星期,終於能夠告一段落,nginx實在是有點強大。寫一篇筆記來記錄一下這個過程當中的思路和解決方案。html

一.搭建nginx平臺:
基本是基於http://blog.csdn.net/xiaoliouc/article/details/8363984 一步步安裝nginx搭建流媒體服務器 這篇博客來搭建。
個人ubuntu是14.04LTS。各類包的版本是:
nginx-1.9.9
nginx_mod_h264_streaming-2.2.7.tar.gz
openssl-0.9.8zh.tar.gz
pcre-8.12.tar.gz
zlib-1.2.7.tar.gz
爲了支持rtmp,還下載了一個模塊nginx-rtmp-module-master,沒有版本的區別。
在安裝過程當中的注意事項:
1.上述無非是./configure make make install.
2.記住若是要修改nginx的./configure的話只用make無需install由於不是覆蓋安裝。
3.要在sbin的目錄下檢查./nginx -V 才能看到nginx的配置。
4.nginx在make的時候注意把objs裏面的Makefile的權限改爲a+w,而且將-Werror刪掉,這樣就不會把warning當作error來對待。修改makefile是在configure以後make以前。
二.測試http、rtmp、hls的功能
1.配置文件(僅支持http和rtmp)
user  www www;  ##管理用戶

worker_processes 30;    ##後臺進程

error_log  /usr/local/nginx/logs/error.log;

##nginx錯誤日誌存放路徑

pid        /usr/local/nginx/logs/nginx.pid;

events {

        use epoll;

##輪訓方式

        worker_connections      65535;

##容許的最大鏈接數

        }

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  /usr/local/nginx/logs/access.log;



    sendfile        on;

    tcp_nopush     on;



    #keepalive_timeout  0;

    keepalive_timeout  65;



    gzip on;
    gzip_min_length 1000;
    gzip_buffers 4 8k;
    gzip_types text/plain;



    server {

        listen       80;

        server_name  192.168.16.69;
     #root html;
        root /usr/local/nginx/html/flv_file;
    

        #charset koi8-r;

        limit_rate_after 5m;

        limit_rate  512k;
     index  index.html;
         charset utf-8;

     #   access_log  /usr/local/nginx/logs/host.access.log  main;



    #    location / {

        #    root   /usr/local/nginx/html/flv_file;

   #            index  index.html;

           # limit_rate_after 5m;

           # limit_rate  512k;

       

        #error_page  404              /404.html;

       location ~ \.flv$ {    
     #     root /var/www/flv;
                       flv;
                        }

       location ~ \.mp4$ {
     #          root /var/www/mp4;
                        mp4;
                        }
}
}
rtmp {
        server {
            listen 1935;
            chunk_size 4000;

            # video on demand
            application vod {
                play /usr/local/nginx/html/flv_file;
            }

            # HLS
            # HLS requires libavformat & should be configured as a separate
            # NGINX module in addition to nginx-rtmp-module:
            # ./configure … –add-module=/path/to/nginx-rtmp-module/hls …
            # For HLS to work please create a directory in tmpfs (/tmp/app here)
            # for the fragments. The directory contents is served via HTTP (see
            # http{} section in config)
            #
            # Incoming stream must be in H264/AAC/MP3. For iPhones use baseline #H264
            # profile (see ffmpeg example).
            # This example creates RTMP stream from movie ready for HLS:
            #
            # ffmpeg -loglevel verbose -re -i movie.avi  -vcodec libx264
            #    -vprofile baseline -acodec libmp3lame -ar 44100 -ac 1
            #    -f flv rtmp://localhost:1935/hls/movie
            #
            # If you need to transcode live stream use ‘exec’ feature.
            #
            application hls {
                hls on;
                hls_path /usr/local/nginx/html/flv_file;
                hls_fragment 10s;
            }
        }
    }
2.配置文件(可支持http,rtmp,hls)
  #filename:nginx.conf

    #user  nobody;
    worker_processes  1;

    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;
    }

    rtmp {
        server {
            listen 1935;
            chunk_size 4000;

            # video on demand
            application vod {
                play /usr/local/nginx/html/flv_file;
            }

            # HLS
            # HLS requires libavformat & should be configured as a separate
            # NGINX module in addition to nginx-rtmp-module:
            # ./configure … –add-module=/path/to/nginx-rtmp-module/hls …
            # For HLS to work please create a directory in tmpfs (/tmp/app here)
            # for the fragments. The directory contents is served via HTTP (see
            # http{} section in config)
            #
            # Incoming stream must be in H264/AAC/MP3. For iPhones use baseline #H264
            # profile (see ffmpeg example).
            # This example creates RTMP stream from movie ready for HLS:
            #
            # ffmpeg -loglevel verbose -re -i movie.avi  -vcodec libx264
            #    -vprofile baseline -acodec libmp3lame -ar 44100 -ac 1
            #    -f flv rtmp://localhost:1935/hls/movie
            #
            # If you need to transcode live stream use ‘exec’ feature.
            #
            application hls {
                hls on;
                hls_path /usr/local/nginx/html/flv_file;
                hls_fragment 10s;
            }
        }
    }

    http {

        include mime.types;
        default_type application/octet-stream;
        sendfile on;
        keepalive_timeout 65;
        gzip on;
       
         #log format

        log_format  access  ‘$remote_addr – $remote_user [$time_local] 「$request」 ‘
                 ‘$status $body_bytes_sent 「$http_referer」 ‘
                 ‘」$http_user_agent」 $http_x_forwarded_for’;   
       
         #定義一個名爲addr的limit_zone,大小10M內存來存儲session
        limit_conn_zone $binary_remote_addr zone=addr:10m;    

        server {
            listen 8080;
             server_name localhost;

             # HTTP can be used for accessing RTMP stats
            # This URL provides RTMP statistics in XML
            location /stat {
                rtmp_stat all;
                rtmp_stat_stylesheet stat.xsl;
            }
            location /stat.xsl {
                root /home/nairely/Documents/nginxserver/nginx-rtmp-module-master;
            }
             location /control {
                rtmp_control all;
            }
            location / {
                root /home/nairely/Documents/nginxserver/nginx-rtmp-module-master/test/rtmp-publisher;
            }
        }
       
         server {
            listen 80;
             server_name localhost;
            
            location / {
                    root /usr/local/nginx/html/flv_file;
                    index index.html;
                  }        

             location ~ \.flv$ {
                   root /usr/local/nginx/html/flv_file;
                 flv;
                 limit_conn addr 20;
                 limit_rate 200k;
            }
            location ~ \.mp4$ {
                 root /usr/local/nginx/html/flv_file;
                 mp4;
                 limit_conn addr 20;
                 limit_rate 200k;
            }

             location /hls {
                # Serve HLS fragments
                alias /usr/local/nginx/html/flv_file;
            }

             access_log  logs/nginxflv_access.log access;
        }      
    }nginx

3.若是視頻不能播放頗有多是權限的問題,設置成644. root(read and write),group(read only)user(read only)
4.nginx做爲一個後臺服務器,用戶從html/flv_file的文件夾中取出視頻用戶點播。測試的播放器用的是vlc。
http的地址是http://192.168.16.46/yequ.flv
rtmp的地址是rtmp://192.168.16.46:1935/vod/yequ.flv
hls的地址是http://192.168.16.46/hls/sample.m3u8(在segment命令將output的URL設置爲http://192.168.16.46/hls)將sample.m3u8文件放在html/flv_file文件夾下,固然這取決與segment語句在哪一個地址下進行。
 
5.如何啓動nginx:
cd /usr/local/nginx/sbin
./nginx -t 檢查配置文件的語法問題
若是發現配置文件的路徑不對
./nginx -c 路徑
./nginx -s reload從新加載配置文件
killall -9 nginx 殺掉全部進程從頭開始,若是遇到端口被佔用的問題的話。
./nginx 啓動,注意在殺掉進程以後必定要從新啓動。
service nginx start
 
 
三.配置hls+ffmpeg的環境
在配置http和rtmp都比較簡單。在搭建nginx平臺的前提下就能夠實現點播。配置hls出現的問題都比較簡單。只是會花一段時間。
 
主要的配置仍是根據這篇來的http://www.lc365.net/blog/b/31519/ 基於HTTP Live Streaming(HLS) 搭建在線點播系統
這些包實在太多了,我簡直屬於無聊的階段。
1.faac的編譯問題
http://zhidao.baidu.com/link?url=ASyVwiBE-01ox_O0QascgPdqNNRlXpHCfI6cXyg71JIboOK5MTj3NLfHUPC31HH5b0FiE3tbWUetUfKL29HAzzXu4q0p75Iveu05HPp_ST3
2.在編譯ffmpeg的時候發現x264notfound的解決方法
在x264的./configure --enable-static --enable-shared --enable-visualize --system-libx264 --enable-gprof --enable-strip --enable-pic --enable-debug
就是把因此的開關都打開。這簡直是誤打誤撞。
3. m3u8configure的時候老是會遇到error的問題,能夠看到這些錯誤都是什麼東西過期的啥的,直接換ffmpeg。換ffmpeg版本到最新。ffmpeg-2.8.4
git clone https://github.com/johnf/m3u8-segmenter
cd m3u8-segmenter
而後configure的時候
gcc -Wall -g m3u8-segmenter.c -o segmenter -lavformat -lavcodec -lavutil
cp segmenter /usr/bin/ 
在html/flv_file路徑下
ffmpeg -i /var/flvs/baluobu.flv  -f mpegts -acodec libmp3lame -ar 48000 -ab 128k -vcodec libx264 -b 96k -flags +loop -cmp +chroma -partitions +parti4x4+partp8x8+partb8x8 -subq 5 -trellis 1 -refs 1 -coder 0 -me_range 16 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 -bt 200k -maxrate 96k -bufsize 96k -rc_eq 'blurCplx^(1-qComp)' -qcomp 0.6 -qmin 10 -qmax 51 -qdiff 4 -level 30 -aspect 320:240 -g 30 -async 2 /var/app/baluobu/baluobu.ts
上述是把flv文件轉換成ts文件
下面用segmenter命令將ts轉換成10秒一個ts切片,segmenter_test是ts小切片的前綴,-u制定URL地址。
segmenter -i vp5.ts -d 10 -p segmenter_test -m vp5.m3u8 -u  http://192.168.16.46/hls/
在segmenter的時候有一個紅色的ignore,以前一直認爲是一個error,我去才發現是一個不用管的東西,而後在vlc上用http://192.168.16.46/hls/vp5.m3u8放,其實相似一個直播的功能,播完了就沒有ts文件了。
再來一次測試就得再來一遍ffmpeg和segmenter.
總結到這兒。
相關文章
相關標籤/搜索