nginx + ffmpeg

第一種方案:ffmpeg+nginxjavascript

新的ffmpeg已經支持HLS。(本人也參與了代碼供獻,給本身作個廣告:))html

點播:java

生成hls分片:linux

ffmpeg -i <媒體文件> -c:v libx264 -c:a aac -f hls /usr/local/nginx/html/test.m3u8nginx

 

直播:git

ffmpeg -i udp://@:1234 -c:v libx264 -c:a aac -f hls -hls_list_size 5 -hls_wrap 5 /usr/local/nginx/html/test.m3u8github

 

創建web服務器:web

默認配置就能夠。windows

 server {
        listen       80;
        server_name  localhost;緩存

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

}

 

啓動nginx。

客戶端訪問:http://IP/test.m3u8

在windows上能夠用vlc播放。

 

第二個方案,用nginx-rtmp-module

 

      
rtmp {

    server {

        listen 1935;

        chunk_size 4000;
      
        #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. 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 {
            live on;
            hls on;
            hls_path /tmp/app;
            hls_fragment 5s;
        }
    }
}

http {

    server {

        listen      80;
        location /hls {
            # Serve HLS fragments
            types {
                application/vnd.apple.mpegurl m3u8;
                video/mp2t ts;
            }
            alias /tmp/app;
            expires -1;
        }
    }
}

1        功能:

1.1         視頻、音頻直播流(Live streaming of video/audio)

1.2         Flv、mp4視頻需求,從本地或HTTP播放(Video ondemand FLV/MP4, playing from local filesystem or HTTP)

1.3         支持分佈式流中繼:推或拉模式(Stream relay support for distributed streaming: push & pullmodels)

1.4         錄製FLVS流(Recording streams in multiple FLVs)

1.5         支持H264/AAC

1.6         在線用ffmpeg轉碼(Online transcoding with FFmpeg)

1.7         HLS (HTTP Live Streaming) 支持

1.8         基於http的操做(發佈、播放、錄製)(HTTP callbacks (publish/play/record/update etc))

1.9         在必定的事件中執行應用程序(Running external programs on certain events (exec))

1.10      HTTP control module forrecording audio/video and dropping clients

1.11      高級內存管理技術,快速流和低內存佔用(Advanced buffering techniques to keep memory allocations at aminimum level for faster streaming and low memory footprint)

1.12      實踐證實能夠在下列產品中工做: Wirecast,FMS,Wowza, JWPlayer,FlowPlayer,StrobeMediaPlayback,ffmpeg,avconv,rtmpdump,flvstreamer and many more

1.13      Statistics in XML/XSL in machine-& human- readable form

1.14      Linux/FreeBSD/MacOS/Windows

2        代碼下載:

Nginx:

官網:http://nginx.org/

下載:hg clone http://hg.nginx.org/nginx

nginx-rtmp-module:

官網:https://github.com/arut/nginx-rtmp-module

下載:git clone https://github.com/arut/nginx-rtmp-module.git

3        編譯:

3.1         Linux下:

進入nginx源碼根目錄:

auto/configure--add-module=<path-to-nginx-rtmp-module>

make -f objs/Makefile

make -f objs/Makefile install

 

增長:http_ssl_module:

auto/configure--add-module=<path-to-nginx-rtmp-module> --with-http_ssl_module

3.2   windows下:

3.2.1 官網提供的編譯方法:

http://nginx.org/en/docs/howto_build_on_win32.html

3.2.1.1 預備:

l  Microsoft Visual C compiler.Microsoft Visual Studio® 8 and 10

l  Msys

l  Perl(若是要編譯OpenSSL而且nginx支持SLL),例如:ActivePerl 或 Strawberry Perl.

l  PCRE,zlib and OpenSSLlibraries sources.

3.2.1.2    編譯步驟:

l  設置MSYS、Perl、VC環境變量

運行vc設置環境變量腳本vcvarsall.bat(默認安裝路徑:C:\Program Files\MicrosoftVisual Studio 9.0\VC)

l  開始MSYS

l  創建編譯和庫目錄

mkdir objs

mkdir objs/lib

cd objs/lib

tar -xzf ../../pcre-8.32.tar.gz

tar -xzf ../../zlib-1.2.7.tar.gz

tar -xzf ../../openssl-1.0.1e.tar.gz

l  運行配置腳本:

auto/configure --with-cc=cl --builddir=objs--prefix= \

--conf-path=conf/nginx.conf--pid-path=logs/nginx.pid \

--http-log-path=logs/access.log--error-log-path=logs/error.log \

--sbin-path=nginx.exe--http-client-body-temp-path=temp/client_body_temp \

--http-proxy-temp-path=temp/proxy_temp \

--http-fastcgi-temp-path=temp/fastcgi_temp\

--with-cc-opt=-DFD_SETSIZE=1024--with-pcre=objs/lib/pcre-8.32 \

--with-zlib=objs/lib/zlib-1.2.7--with-openssl=objs/lib/openssl-1.0.1e \

--with-select_module --with-http_ssl_module--with-ipv6

運行腳本:

nmake –f objs/Makefile

3.2.2 Cygwin下:

linux

4        Windows下的限制:

Windows下下列功能不支持:

l  execs

l  static pulls

l  auto_push

 

windows下用的是select模式:

events{

    worker_connections  30; #默認爲(1024),windows默認爲64,因此須要修改此值小於64

}

 

5        訪問URL格式:

rtmp://rtmp.example.com/app[/name]

app-應該是配置文件中的application塊

name-能夠爲空

6        配置:

在源碼要目下有個test目錄。其中有測試配置文件和腳本。

6.1         點播配置:

l  環境:

服務器IP爲:192.168.10.4。

安裝根路徑:/usr/local/nginx

 

在你的配置時,須要把IP換成你的服務器IP或域名。

l  創建媒體文件夾:

mkdir /usr/local/nginx/vod/flvs

l  準備媒體文件:

把媒體文件 a.flv 複製到 /usr/local/nginx/vod/flvs目錄下。

l  準備網頁

下載jwplayer:http://www.longtailvideo.com/players/jw-flv-player/

下載後,解壓到:/usr/local/nginx/html/

創建測試頁面test.html,也放到上面目錄下。

<html>

         <head>

                   <scriptsrc="/jwplayer/jwplayer.js"></script>

         </head>

         <body>

                   <divid='my-video'></div>

                   <scripttype='text/javascript'>

                            jwplayer('my-video').setup({

                                     file:'rtmp://192.168.10.4/vod/a.flv',    

                                     width:'50%',

                                     aspectratio:'3:2',

                                     fallback:'false',

                                     primary:'flash'  

                                     });

                   </script>

         </body>

</html>

l  在nginx中配置http

#配置事件工做線程數

events {

   worker_connections  30; #默認爲(1024),windows默認爲64,因此須要修改此值小於64

}

#配置http

http {

   include       mime.types;

   default_type application/octet-stream;

 

    #配置訪問web頁

   server {

       listen       80;

       server_name  localhost;

 

       #charset koi8-r;

 

       #access_log logs/host.access.log  main;

 

       location / {

           root   html;

           index  index.html index.htm;

       }

 

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

       }

   }

  

   #配置rtmp狀態頁

  server {

       listen      8080;

 

       location /stat {

           rtmp_stat all;

           rtmp_stat_stylesheet stat.xsl;

       }

 

       location /stat.xsl {

           root /usr/local/nginx/nginx-rtmp-module/;  #在nginx-rtmp-module源碼根目錄

       }

   }

}

#rtmp點播配置

rtmp {

   server {

        listen 1935;

        chunk_size 4000;

        application  vod {

                play /usr/local/nginx/vod/flvs;  #點播媒體文件存放目錄

        }

    }

}

l  啓動nginx

./nginx

l  從客戶端訪問網頁:

http://192.168.10.4/test.html

l  訪問rtmp狀態頁(非必須):

http://192.168.10.4:8080/stat

 

6.2         直播配置:

l   

l  在配置文件nginx.conf中加入

#rtmp直播配置

rtmp {

   server {

        listen 1935;

        chunk_size 4000;

        application  live {

                   live on;

        }

    }

}

 

#配置訪問web頁

http {

    include       mime.types;

   default_type application/octet-stream;

 

#配置訪問web頁

   server {

       listen       80;

       server_name  localhost;

 

       #charset koi8-r;

 

       #access_log logs/host.access.log  main;

 

       location / {

           root   html;

           index  index.html index.htm;

       }

 

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

       }

 

#配置rtmp狀態頁

   server {

       listen      8080;

 

       location /stat {

           rtmp_stat all;

           rtmp_stat_stylesheet stat.xsl;

       }

 

       location /stat.xsl {

           root /usr/local/nginx/nginx-rtmp-module/;  #在nginx-rtmp-module源碼根目錄

       }

}

}

l  訪問rtmp狀態頁(非必須):

http://192.168.10.4:8080/stat

能夠查看rtmp流的統計狀況

l  準備網頁

下載jwplayer:http://www.longtailvideo.com/players/jw-flv-player/

下載後,解壓到:/usr/local/nginx/html/

創建測試頁面test.html,也放到上面目錄下。

<html>

         <head>

                   <scriptsrc="/jwplayer/jwplayer.js"></script>

         </head>

         <body>

                   <divid='my-video'></div>

                   <scripttype='text/javascript'>

                            jwplayer('my-video').setup({

                               file:'rtmp://192.168.0.128/live/test', #live是applicatioin,test是直播緩存流文件

                                     width:'50%',

                                     aspectratio:'3:2',

                                     fallback:'false',

                                     primary:'flash'  

                                     });

                   </script>

         </body>

</html>

l  啓動服務

nginx

l  推流

用ffmpeg產生一個模擬直播源,向rtmp服務器推送

ffmpeg –i/usr/local/nginx/vod/flvs/a.flv-strict -2 -c:v libx264 -c:a aac -f flv rtmp://192.168.10.4/live/test

注意,源文件必須是H.264+AAC編碼的。

192.168.10.4是運行nginx的服務器IP,l

ive是applicatioin,

test是直播緩存流文件,須要與配置文件中的直播緩存文件名同樣。

 

l  從客戶端訪問網頁:

http://192.168.10.4/test.html

相關文章
相關標籤/搜索