ffmpeg+nginx建立rtmp服务器,播放摄像头流

好文章,http://blog.fs-linux.org/?p=137

先转载,然后再动手实践看看

live555可以建立rtsp这个大伙都知道,ffserver+ffmpeg也可以建立一个实时rtsp服务,那么rtmp呢?好多人会首选red5,al'right,这里用的是nginx,下面记录一下使用nginx建立rtmp的全过程

首先下载一份nginx的rtmp的module

?
1
git clone https: //github.com/arut/nginx-rtmp-module.git

下载好nginx-rtmp-module以后,需要将ginx-rtmp编译进nginx

?
1
2
3
4
cd nginx
./configure --prefix=/usr/local/nginx/ --add-module=/root/nginx-rtmp-module --with-http_ssl_module
make
make install

这样,就将nginx-rtmp-module编译进nginx里面,并安装到/usr/local/nginx里面了
接下来配置一下nginx的配置文件既可
在nginx-rtmp-module中可以看到nginx配置文件的参考

?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
[root@CM nginx-rtmp-module] # ls
AUTHORS                   ngx_rtmp_auto_push_module.c  ngx_rtmp_control_module.c  ngx_rtmp_handler.c       ngx_rtmp_netcall_module.c  ngx_rtmp_relay_module.c  test
config                    ngx_rtmp_bandwidth.c         ngx_rtmp_core_module.c     ngx_rtmp_handshake.c     ngx_rtmp_netcall_module.h  ngx_rtmp_relay_module.h  TODO
doc                       ngx_rtmp_bandwidth.h         ngx_rtmp_enotify_module.c  ngx_rtmp_init.c          ngx_rtmp_notify_module.c   ngx_rtmp_send.c
hls                       ngx_rtmp.c                   ngx_rtmp_eval.c            ngx_rtmp_limit_module.c  ngx_rtmp_play_module.c     ngx_rtmp_shared.c
LICENSE                   ngx_rtmp_cmd_module.c        ngx_rtmp_eval.h            ngx_rtmp_live_module.c   ngx_rtmp_play_module.h     ngx_rtmp_stat_module.c
ngx_rtmp_access_module.c  ngx_rtmp_cmd_module.h        ngx_rtmp_exec_module.c     ngx_rtmp_live_module.h   ngx_rtmp_receive.c         ngx_rtmp_streams.h
ngx_rtmp_amf.c            ngx_rtmp_codec_module.c      ngx_rtmp_flv_module.c      ngx_rtmp_log_module.c    ngx_rtmp_record_module.c   README.md
ngx_rtmp_amf.h            ngx_rtmp_codec_module.h      ngx_rtmp.h                 ngx_rtmp_mp4_module.c    ngx_rtmp_record_module.h   stat.xsl
[root@CM nginx-rtmp-module] # ls test/
dump.sh  ffstream.sh  nginx.conf  play.sh  README.md  rtmp-publisher  www
[root@CM nginx-rtmp-module] # ls test/rtmp-publisher/
player.html  publisher.html  README.md  RtmpPlayer.mxml  RtmpPlayer.swf  RtmpPublisher.mxml  RtmpPublisher.swf  swfobject.js
[root@CM nginx-rtmp-module] # ls test/www
bg .jpg  index.html  jwplayer  jwplayer_old  record.html
[root@CM nginx-rtmp-module] #

当然,如果只为了测试rtmp,完全可以将test中的rtmp放在/usr/local/nginx/conf/中,记得要备份/usr/local/nginx/conf下的nginx.conf
将nginx.conf中的内容修改如下:

?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
worker_processes  1;
 
error_log  logs/error.log debug;
 
events {
     worker_connections  1024;
}
 
rtmp {
     server {
         listen 1935;
         application myapp {
             live on;
         }
     }
}
 
http {
     server {
         listen      80;
         location /stat {
             rtmp_stat all;
             rtmp_stat_stylesheet stat.xsl;
         }
 
         location /stat.xsl {
             root /usr/local/nginx/html/nginx-rtmp-module/;
         }
 
         location /control {
             rtmp_control all;
         }
 
         location /rtmp-publisher {
             root /usr/local/nginx/html/nginx-rtmp-module/test;
         }
 
         location / {
             root /usr/local/nginx/html/nginx-rtmp-module/test/www;
         }
     }
}

配置完成之后,可以启动nginx

?
1
/usr/local/nginx/sbin/nginx

nginx从此启动完成
接下来试一下

?
1
ffmpeg  -f video4linux2  -s 1280x720 -i /dev/video0 -vcodec libx264 -preset ultrafast -vprofile baseline -vlevel 1.0  -s 640x480 -b:v 800k -r 25  -pix_fmt yuv420p -f flv rtmp: //192.168.0.194/myapp/mystream

然后ffmpeg的进程会显示如下

?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
ffmpeg version N-54918-g11cb697 Copyright (c) 2000-2013 the FFmpeg developers
   built on Jun 19 2013 08:54:07 with gcc 4.4.7 (GCC) 20120313 (Red Hat 4.4.7-3)
   configuration: --disable-yasm --enable-libx264 --enable-gpl
   libavutil      52. 40.100 / 52. 40.100
   libavcodec     55. 19.100 / 55. 19.100
   libavformat    55. 12.102 / 55. 12.102
   libavdevice    55.  3.100 / 55.  3.100
   libavfilter     3. 81.102 /  3. 81.102
   libswscale      2.  4.100 /  2.  4.100
   libswresample   0. 17.103 /  0. 17.103
   libpostproc    52.  3.100 / 52.  3.100
Input #0, video4linux2,v4l2, from '/dev/video0' :
   Duration: N/A, start: 686.347441, bitrate: 147456 kb/s
     Stream #0:0: Video: rawvideo (YUY2 / 0x32595559), yuyv422, 1280x720, 147456 kb/s, 10 fps, 10 tbr, 1000k tbn, 1000k tbc
[libx264 @ 0x2e63660] frame MB size (40x30) > level limit (99)
[libx264 @ 0x2e63660] DPB size (1 frames, 1200 mbs) > level limit (0 frames, 396 mbs)
[libx264 @ 0x2e63660] MB rate (30000) > level limit (1485)
[libx264 @ 0x2e63660] using cpu capabilities: none!
[libx264 @ 0x2e63660] profile Constrained Baseline, level 1.0
[libx264 @ 0x2e63660] 264 - core 133 r2334M a3ac64b - H.264/MPEG-4 AVC codec - Copyleft 2003-2013 - http: //www.videolan.org/x264.html - options: cabac=0 ref=1 deblock=0:0:0 analyse=0:0 me=dia subme=0 psy=1 psy_rd=1.00:0.00 mixed_ref=0 me_range=16 chroma_me=1 trellis=0 8x8dct=0 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=0 threads=3 lookahead_threads=1 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=0 weightp=0 keyint=250 keyint_min=25 scenecut=0 intra_refresh=0 rc=abr mbtree=0 bitrate=800 ratetol=1.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=0
Output #0, flv, to 'rtmp://192.168.0.194/myapp/mystream' :
   Metadata:
     encoder         : Lavf55.12.102
     Stream #0:0: Video: h264 (libx264) ([7][0][0][0] / 0x0007), yuv420p, 640x480, q=-1--1, 800 kb/s, 1k tbn, 25 tbc
Stream mapping:
   Stream #0:0 -> #0:0 (rawvideo -> libx264)
Press [q] to stop, [?] for help
frame= 1469 fps=9.7 q=30.0 size=   14868kB time=00:02:31.44 bitrate= 804.3kbits/s   

将nginx配置文件中需要的对应的目录放在nginx下面

?
1
2
3
mkdir /usr/local/nginx/html/nginx-rtmp-module
cp /root/nginx-rtmp-module/test /usr/local/nginx/html/nginx-rtmp-module/
cp /root/nginx-rtmp-module/stat.xsl /usr/local/nginx/html/nginx-rtmp-module/
?
1
2
ls /root/nginx-rtmp-module/test/www/
bg.jpg        index.html    jwplayer/     jwplayer_old/ record.html  

可以看到,在nginx-rtmp-module/test/www/下面使用的是jwplayer,这个player是一个AS建立出来的flash player

然后访问地址http://192.168.0.194/rtmp-publisher/player.html,就可以播放了

你可能感兴趣的:(ffmpeg+nginx建立rtmp服务器,播放摄像头流)