一:Nginx 上进行配置

1. 查找 nginx的安装目录 which nginx

[root@localhost conf]# which nginx
/usr/local/nginx/sbin/nginx
[root@localhost conf]#

2. 进入 nginx的配置目录

[root@localhost conf]# cd /usr/local/nginx/
[root@localhost nginx]# ll
总用量 36
drwx------. 2 nobody root 4096 2月  24 18:38 client_body_temp
drwxr-xr-x. 2 root   root 4096 3月  18 10:44 conf
drwx------. 2 nobody root 4096 2月  24 18:38 fastcgi_temp
drwxr-xr-x. 2 root   root 4096 2月  24 18:33 html
drwxr-xr-x. 2 root   root 4096 3月  19 11:18 logs
drwx------. 2 nobody root 4096 2月  24 18:38 proxy_temp
drwxr-xr-x. 2 root   root 4096 2月  24 18:33 sbin
drwx------. 2 nobody root 4096 2月  24 18:38 scgi_temp
drwx------. 2 nobody root 4096 2月  24 18:38 uwsgi_temp
[root@localhost nginx]#

3. 进入conf目录,修改 nginx.conf 文件内容如下

worker_processes  1;

error_log  logs/error.log debug;

events {
    worker_connections  1024;
}

rtmp {
    server {
        listen 1935;
        # RTMP 直播配置
        application rtmplive {
            live on; # 开启直播
            #hls on;# 开启HLS直播
            #hls_path /home/jack/video/m3u8File; #配置HLS m3u8 文件存放地址
            #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;
        }

        # HLS 直播流配置
        application hls{
            live on;
            hls on;
            hls_path /tmp/hls;
            hls_fragment 5s; # 一个ts文件的时长 5s
        }
    }
}

http {
    server {
        listen  80;
        location /hls {
            types {
               #m3u8 type配置
              application/vnd.apple.mpegurl m3u8;
               #ts 分片文件配置
               video/mp2t ts;
            }
            #指向访问m3u8文件目录
            #alias /home/jack/video/m3u8File;
            root /tmp;
            add_header Cache-Control no-cache;#禁止缓存
        }

        location /stat {
            rtmp_stat all;
            rtmp_stat_stylesheet stat.xsl;
        }

        location /stat.xsl {
            root /lib/nginx-rtmp-module-1.2.1;
        }

        location /control {
            rtmp_control all;
        }

        #location /publish {
        #    return 201;
        #}

        #location /play {
        #    return 202;
        #}

        #location /record_done {
        #    return 203;
        #}

        location /rtmp-publisher {
            root /lib/nginx-rtmp-module-1.2.1/test;
        }
         
        # 配置nginx访问首页
        location / {
            root html;
            index index.html index.htm;
        }
    }
}

二:HLS推流和拉流

HLS推流
zfz:测试视频 zhangfengzhou$ ffmpeg -re -i hanfu_xiaohun.mp4 -vcodec libx264 -acodec aac -f flv rtmp://192.168.0.88:1935/hls/test2
HLS拉流
zfz:~ zhangfengzhou$ ffplay -i http://192.168.0.88/hls/test2.m3u8

三:RTMP推流和拉流

RTMP推流
zfz:测试视频 zhangfengzhou$ ffmpeg -re -i hanfu_xiaohun.mp4 -vcodec libx264 -acodec aac -f flv rtmp://192.168.0.88/rtmplive/test1
RTMP拉流
zfz:~ zhangfengzhou$ ffplay -i rtmp://192.168.0.88/rtmplive/test1

四:查看推流的统计数据

在浏览器中输入 http://192.168.0.88/stat 就可以查看推流的情况

五:其他问题

在有些时候进行hls拉流的时候,会遇到 HTTP error 404 Not found, 一般问题出在 tmp/hls 目录下没有m3u8文件和ts文件,尝试多次拉流,就可以正常播放了。

Logo

AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。

更多推荐