nginx rtmp模块 实现hls

nginx rtmp  ffmpeg 组合模仿hls直播


前几天老总说搞了一个局域网内的直播,想到了之前提到的rtmp模块,抱着试试看的的心态 开干了


系统环境:

[root@localhost html]# uname -a 
Linux localhost.localdomain 2.6.18-194.el5 #1 SMP Fri Apr 2 14:58:35 EDT 2010 i686 athlon i386 GNU/Linux
[root@localhost html]# getconf LONG_BIT
32


NGINX环境:

1、nginx下载地址:

http://nginx.org/download/ 

本测试环境系统版本是1.4.7


2、nginx模块rtmp下载地址

https://github.com/arut/nginx-rtmp-module/archive/master.zip


3、多媒体视频处理工具→ffmpeg下载地址

http://ffmpeg.org/releases/ffmpeg-2.6.1.tar.bz2


首先解释一些东西

rtmp:RTMP(Real Time Messaging Protocol)实时消息传送协议是Adobe Systems公司为Flash播放器和服务器之间音频、视频和数据传输 开发的开放协议。

然后nginx的rtmp模块

战斗民族俄罗斯人民开发的一款NGINX的流媒体插件,除了直播发布音视频流之外具备流媒体服务器的常见功能

比如推拉流媒体资源

基于HTTP的FLV/MP4 VOD点播

HLS (HTTP Live Streaming) M3U8的支持

基于http的操作(发布、播放、录制)

可以很好的协同现有的流媒体服务器以及播放器一起工作

在线调用ffmpeg对流媒体进行转码

H264/AAC音视频编码格式的支持

linux/BSD/MAC系统的支持

(来自于网络)



安装步骤、

nginx安装

下载好rtmp模块

wget https://github.com/arut/nginx-rtmp-module/archive/master.zip
unzip  master.zip
wget http://nginx.org/download/nginx-1.4.7.tar.gz
tar xvf nginx-1.4.7.tar.gz
cd nginx-1.4.7
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module  --add-module=/usr/local/src/nginx-rtmp-module-master  --with-pcre=/usr/local/src/pcre-8.11 
make;make install


ffmpeg安装

wget http://ffmpeg.org/releases/ffmpeg-2.6.1.tar.bz2
tar xvf ffmpeg-2.6.1.tar.bz2 
cd ffmpeg-2.6.1

在这一个编译的话肯定是会报错的 具体报什么错就是说缺乏东西,需要安装yasm

wget http://www.tortall.net/projects/yasm/releases/yasm-1.2.0.tar.gz
tar xvf yasm-1.2.0.tar.gz
cd yasm-1.2.0
./configure
make;make install
cd ffmpeg-2.6.1
./configure 
make; make instal




改装的东西都装好了,现在来改改配置文件

nginx的配置文件需要换一换了 nginx rtmp的模块里面有一个nginx的主配置文件

[

root@localhost test]# pwd
/usr/local/src/nginx-rtmp-module-master/test
[root@localhost test]# ll
total 56
-rwxr-xr-x 1 root root   49 Mar 24 03:30 dump.sh
-rwxr-xr-x 1 root root   84 Mar 24 03:30 ffstream.sh
-rw-r--r-- 1 root root 1245 Mar 24 03:30 nginx.conf
-rwxr-xr-x 1 root root   59 Mar 24 03:30 play.sh
-rw-r--r-- 1 root root  499 Mar 24 03:30 README.md
drwxr-xr-x 2 root root 4096 Mar 24 03:30 rtmp-publisher
drwxr-xr-x 4 root root 4096 Mar 24 03:30 www

将上面的主配置文件拷贝到nginx目录就好


nginx主配置文件如下

worker_processes  1;
error_log  logs/error.log debug;
events {
    worker_connections  1024;
}
rtmp {
    server {
        listen 1936;
        application myapp {
            live on;
            #record keyframes;
            #record_path /tmp;
            #record_max_size 128K;
            #record_interval 30s;
            #record_suffix .this.is.flv;
            #on_publish http://localhost:8080/publish;
            #on_play http://localhost:8080/play;
            #on_record_done http://localhost:8080/record_done;
        }
    }
}
rtmp {
    server {
        listen 1935;
        chunk_size 4000;
        #HLS
        application hls {
            live on;
            hls on;
            hls_path /usr/local/nginx/html/hls;
            hls_fragment 5s;
        }
    }
}
http {
    server {
        listen  8080;
        location /hls {
            # Serve HLS fragments
            types {
                application/vnd.apple.mpegurl m3u8;
                video/mp2t ts;
            }
            root html;
            expires -1;
        }
    }
}
http {
    server {
        listen      8081;
        location /stat {
            rtmp_stat all;
            rtmp_stat_stylesheet stat.xsl;
        }
        location /stat.xsl {
            root /html/nginx-rtmp-module/;
        }
        location /control {
            rtmp_control all;
        }
        #location /publish {
        #    return 201;
        #}
        #location /play {
        #    return 202;
        #}
        #location /record_done {
        #    return 203;
        #}
        #location /rtmp-publisher {
        #    root /html/nginx-rtmp-module/test;
       # }
        location / {
            root /html/nginx-rtmp-module/test/www;
        }
    }
}


上传一个flv格式的文件到html目录

执行

ffmpeg -re -i sample.flv -vcodec copy -acodec copy -f flv rtmp://192.168.3.105/hls/mystream

然后页面观看地址

http://192.168.3.105:8080/hls/mystream.m3u8




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章