=================================== 利用Nginx和RTMP实现流媒体点播和直播 =================================== 1. 实验环境 ----------- +----------+ +----------+ | 直播文件 | | 点播文件 | +----------+ +----------+ | | +--------+ +--------------------+ +--------------------+ +--------------------+ +--------+ | 摄像头 | --> | | | | | | --> | 显示器 | +--------+ | 推送端 (Windows) | --\ | 服务器 (Ubuntu) | --\ | 播放端 (Windows) | +--------+ +--------+ | FFmpeg | --/ | Nginx + RTMP | --/ | VLC media player | +--------+ | 麦克风 | --> | | | | | | --> | 音 箱 | +--------+ +--------------------+ +--------------------+ +--------------------+ +--------+ | +----------+ | 屏幕桌面 | +----------+ 2. 在服务器上下载并安装编译依赖 ------------------------------- $ sudo apt-get install build-essential libpcre3 libpcre3-dev libssl-dev 3. 在服务器上创建并切换工作目录 ------------------------------- $ mkdir Nginx-RTMP $ cd Nginx-RTMP 4. 在服务器上下载nginx和nginx-rtmp源代码 ---------------------------------------- $ wget http://nginx.org/download/nginx-1.7.5.tar.gz $ wget https://github.com/arut/nginx-rtmp-module/archive/master.zip 5. 在服务器上安装unzip工具 -------------------------- $ sudo apt-get install unzip 6. 在服务器上解压nginx和nginx-rtmp源代码包 ------------------------------------------ $ tar -zxvf nginx-1.7.5.tar.gz $ unzip master.zip 7. 在服务器上切换到nginx源代码目录 ---------------------------------- $ cd nginx-1.7.5 8. 在服务器上配置nginx编译选项以添加nginx-rtmp模块 -------------------------------------------------- $ ./configure --with-http_ssl_module --add-module=../nginx-rtmp-module-master 9. 在服务器上编译安装nginx -------------------------- $ make $ sudo make install 10. 在服务器上安装nginx启动脚本 ------------------------------- $ sudo wget https://raw.github.com/JasonGiedymin/nginx-init-ubuntu/master/nginx -O /etc/init.d/nginx $ sudo chmod +x /etc/init.d/nginx $ sudo update-rc.d nginx defaults 11. 在服务器上启动并停止nginx服务,生成配置文件 ----------------------------------------------- $ sudo service nginx start $ sudo service nginx stop 12. 在服务器上编辑nginx配置文件,在http部分和文件末尾添加配置信息并保存 ----------------------------------------------------------------------- $ sudo vi /usr/local/nginx/conf/nginx.conf http { ... server { ... location /stat { rtmp_stat all; rtmp_stat_stylesheet stat.xsl; } location /stat.xsl { root /home/minwei/Projects/Tarena/Nginx-RTMP/nginx-rtmp-module-master/; } location /hls { types { application/vnd.apple.mpegurl m3u8; video/mp2t ts; } root /tmp; add_header Cache-Control no-cache; } ... } ... } rtmp { server { listen 1935; chunk_size 4096; application vod { play /home/minwei/Videos; } application live { live on; record off; } application hls { live on; hls on; hls_path /tmp/hls; wait_key on; hls_fragment 10s; hls_playlist_length 60s; hls_continuous on; hls_cleanup on; hls_nested on; } } } 13. 在服务器上启动或重启nginx服务 --------------------------------- $ sudo service nginx start 或 $ sudo service nginx restart 14. 在服务器上更改防火墙,允许1935/tcp端口 ------------------------------------------ $ sudo ufw allow 1935/tcp 15. 在服务器上用netstat检查端口侦听情况 --------------------------------------- $ netstat -ltn Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 0.0.0.0:1935 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 16. 在服务器上通过浏览器检查nginx服务状态 ----------------------------------------- http://localhost:8080/stat 17. 在服务器上将视频点播文件(1.mp4 ...)拷贝到/home/minwei/Videos目录下,保证任何用户对其可读 -------------------------------------------------------------------------------------------- 18. 测试点播媒体文件 -------------------- (播放端需要先从http://www.videolan.org下载并安装VLC media player) 在播放端用VLC media player打开网络串流:rtmp://192.168.3.12/vod/1.mp4 19. 测试RTMP直播媒体文件 ------------------------ (推送端需要先从http://ffmpeg.org下载并安装FFmpeg) 在推送端用FFmpeg推送媒体流: ffmpeg -re -i "F:\Movies\1.mp4" -vcodec libx264 -vprofile baseline -acodec aac -ar 48000 -strict -2 -ac 2 -f flv -s 720x480 -q 10 rtmp://192.168.3.12/live/1 在播放端用VLC media player打开网络串流"rtmp://192.168.3.12/live/1" 20. 测试HLS直播媒体文件 ----------------------- 在推送端用FFmpeg推送媒体流: ffmpeg -re -i "F:\Movies\1.mp4" -vcodec libx264 -vprofile baseline -acodec aac -ar 48000 -strict -2 -ac 2 -f flv -s 720x480 -q 10 rtmp://192.168.3.12/hls/1 在播放端用VLC media player打开网络串流"rtmp://192.168.3.12/hls/1" 21. 测试RTMP直播摄像头 ---------------------- 在推送端用FFmpeg推送媒体流: ffmpeg -f dshow -i video="BisonCam, NB Pro" -f dshow -i audio="麦克风 (Realtek High Definition Audio)" -pix_fmt yuv420p -vcodec libx264 -vprofile baseline -acodec aac -ar 48000 -strict -2 -ac 2 -f flv -s 720x480 -q 10 rtmp://192.168.3.12/live/1 在播放端用VLC media player打开网络串流"rtmp://192.168.3.12/live/1" 22. 测试HLS直播摄像头 --------------------- 在推送端用FFmpeg推送媒体流: ffmpeg -f dshow -i video="BisonCam, NB Pro" -f dshow -i audio="麦克风 (Realtek High Definition Audio)" -pix_fmt yuv420p -vcodec libx264 -vprofile baseline -acodec aac -ar 48000 -strict -2 -ac 2 -f flv -s 720x480 -q 10 rtmp://192.168.3.12/hls/1 在播放端用VLC media player打开网络串流"rtmp://192.168.3.12/hls/1" 23. 测试RTMP直播屏幕 -------------------- (推送端需要先从http://sourceforge.net/projects/screencapturer/files下载并安装Screen Capture Recorder) 在推送端用FFmpeg推送媒体流: ffmpeg -f dshow -i video="screen-capture-recorder" -f dshow -i audio="麦克风 (Realtek High Definition Audio)" -pix_fmt yuv420p -vcodec libx264 -vprofile baseline -acodec aac -ar 48000 -strict -2 -ac 2 -f flv -s 720x480 -q 10 rtmp://192.168.3.12/live/1 在播放端用VLC media player打开网络串流"rtmp://192.168.3.12/live/1" 24. 测试HLS直播屏幕 ------------------- 在推送端用FFmpeg推送媒体流: ffmpeg -f dshow -i video="screen-capture-recorder" -f dshow -i audio="麦克风 (Realtek High Definition Audio)" -pix_fmt yuv420p -vcodec libx264 -vprofile baseline -acodec aac -ar 48000 -strict -2 -ac 2 -f flv -s 720x480 -q 10 rtmp://192.168.3.12/hls/1 在播放端用VLC media player打开网络串流"rtmp://192.168.3.12/hls/1"