centos 利用nginx-rtmp和ffmpeg 搭建rtmp推流,hls播放,录制mp4并回放 - Go语言中文社区

centos 利用nginx-rtmp和ffmpeg 搭建rtmp推流,hls播放,录制mp4并回放


1.首先安装相关依赖
yum -y install gcc glibc glibc-devel make nasm pkgconfig lib-devel openssl-devel expat-devel gettext-devel libtool mhash.x86_64 perl-Digest-SHA1.x86_64 gcc-c++ pcre-devel

2.编译nginx
下载相关所需模块并解压, configure选项设置好相关模块,并进行编译 。(或者直接下载所有所需文件点击下载
./configure --prefix=/usr/local/nginx/ --add-module=…/nginx-rtmp-module-master --with-http_mp4_module --add-module=…/ngx_cache_purge-master --add-module=…/nginx_mod_h264_streaming-2.2.7 --with-http_stub_status_module --with-http_ssl_module --with-http_sub_module --with-http_gzip_static_module --with-http_flv_module --with-openssl=…/openssl-1.0.2h

3.修改nginx相关配置如下(或者直接用上述链接资源里的nginx.conf 替换)

  rtmp {
        server {
            listen 1935;
            chunk_size 4000;

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

            # 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 live  {
                live on;
                hls on;
                hls_path /usr/local/nginx/media/app;
                hls_fragment 10s;
               
               record all;
           record_path /usr/local/nginx/media/vod;
           #record_unique on;
           #record_interval 30s;
           exec_record_done '/usr/local/bin/ffmpeg' -y -i $path  -vcodec libx264 -f mp4 $dirname/$basename.mp4 2>>/usr/local/nginx/media/vod/test_record.log;
}

   }
 
}
    http {

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

        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 /usr/local/soft/nginx-rtmp-module-master;
            }
             location /control {
                rtmp_control all;
            }
            location / {
                root /usr/local/soft/nginx-rtmp-module-master/test/rtmp-publisher;
            }
        }
        
         server {
            listen 80;
             server_name localhost;
             
            location / {
                    root /usr/local/wwwroot;
                    index index.html;
                  }         

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

             location /hls {
                # Serve HLS fragments
                alias  /usr/local/nginx/media/app;
            }

             access_log  logs/nginxflv_access.log access;
        }
        
           
    }

4.启动nginx
首先在nginx下创建media文件夹,在media目录下再创建app和vod文件夹。app是hls的文件存放路径,vod是录制文件的存放路径。往服务器上推送的视频须为h264,音频必须为aac。
1 启动服务/opt/nginx/sbin/
#/opt/nginx/sbin/nginx -c /opt/nginx/conf/nginx.conf
2、停止服务
#/opt/nginx/sbin/nginx -s stop
如果出现服务器无法访问等问题,查一下是否是端口占用或者防火墙的问题。
推流测试可以使用ffmpeg,例如:
ffmpeg -re -i video.mp4 -acodec aac -vcodec libx264 -f flv rtmp://192.168.113.185/live/teststream8
或者使用OBS推流,客户端可以使用vlc进行播放

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/zhangxm006/article/details/104312873
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2023-01-01 21:24:43
  • 阅读 ( 114 )
  • 分类:Go Web框架

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢