【问题标题】:How to play HLS stream with nginx?如何使用 nginx 播放 HLS 流?
【发布时间】:2018-09-12 08:59:17
【问题描述】:

我有带有 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播放器中打开urlhttp://my-ip:8080/hls/mystream.m3u8,但出现无法开源的错误。

我能错过什么?

更新

我尝试根据 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"

【问题讨论】:

  • 检查你的 nginx error.log 看看是否有任何线索。它可能会说“没有这样的文件或目录”,这表明您的位置块指向错误的路径(只是一个示例)。
  • @iangetz 是的,在 error.log 中我有这个错误,但我已经在这个文件夹上设置了权限chmod -R 755 /tmp/hls

标签: nginx rtmp http-live-streaming


【解决方案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 *;
}

包含统计信息位置是值得的,因为它可以非常有用地找出为什么某些东西没有按您期望的那样工作。

【讨论】:

  • 我试图编辑我的配置,但仍然不理解 rtmp 块。我在我的问题上添加了新配置。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-15
相关资源
最近更新 更多