首页 文章

如何使用nginx播放HLS流?

提问于
浏览
1

我有rtmp模块的hginx . rtmp流工作正常,现在我想尝试hls流 .
我的配置:

rtmp {
    server {
        listen 1935;
        application myapp {
            live on;
            exec_pull /usr/bin/ffmpeg -i  http://url-to-remote-webcam
-map 0 -codec:v copy rtmp://my-ip:1935/myapp/mystream.flv  2>>/tmp/ffmpeg-$name.log;
        }

        application myapp {
           live on;
           hls on;
           hls_path /tmp/hls;
           hls_fragment 5s;
        }
    }
 }

 http {
      server {
           listen 8080;
           location /hls {
                 root /tmp;
           }
      }
 }

尝试在vlc播放器中打开url http://my-ip:8080/hls/mystream.m3u8但收到无法打开源的错误 .

我能错过什么?

UPDATE

我试图根据miknik答案编辑配置 .

rtmp {
    server {
        listen 1935;
        application myapp {
            live on;
            exec_push ffmpeg -i http://url-to-remote-webcam -acodec copy -vcodec libx264 -vprofile baseline -g 10 -r 15 -s 640x360 -f flv rtmp://my-ip:1935/hls/mystream;
         }

         application hls {
            live on;
            hls on;
            hls_path /tmp/hls;
            hls_fragment 5s;
          }
     }
  }

  http {
       server {
             listen 8080;
             location /hls {
                  types {
                        application/vnd.apple.mpegurl m3u8;
                  }
                  root /tmp;
                  add_header Cache-Control no-cache;
                  add_header Access-Control-Allow-Origin *;
            }
        }
    }

并尝试打开 http://my-ip:8080/hls/mystream.m3u8 但仍然有相同的错误 .

这里来自nginx日志的错误:

2018/09/20 15:50:29 [error] 11406#11406: *2 open() "/tmp/hls/mystream.m3u8" failed (2: No such file or directory), client: client-ip, server: , request: "GET /hls/mystream.m3u8 HTTP/1.0", host: "server-ip:8080"

1 回答

  • 1

    首先,你的rtmp应用程序都被称为 myapp . 拨打第二个 hls .

    在myapp块中,您需要(可选地转换并)将流推送到hls块 . 所以你需要这样的指令:

    exec_push ffmpeg -i rtmp://127.0.0.1:1935/stream/$name -acodec copy -vcodec libx264 -vprofile baseline -g 10 -r 15 -s 640x360 -f flv rtmp://127.0.0.1:1935/hls/$name;
    

    然后在你的http块你想要的位置块像这样:

    location / {
      try_files $uri $uri/ =404;
    }
    
    location /stat {
      rtmp_stat all;
      rtmp_stat_stylesheet stat.xsl;
    }
    
    location /stat.xsl {
      root /var/www/test/stat;
    }
    
    location /hls {
      types {
        application/vnd.apple.mpegurl m3u8;
        video/mp2t ts;
      }
      root /tmp;
      add_header Cache-Control no-cache;
      index index.m3u8 index.ts;
      add_header Access-Control-Allow-Origin *;
    }
    

    值得包括统计数据位置,因为它可以非常有用地找出为什么某些东西没有像你期望的那样工作 .

相关问题