如何使用Nginx播放HLS流?

时间:2018-09-12 08:59:17

标签: nginx rtmp hls

我有带有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播放器中打开网址http://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"

1 个答案:

答案 0 :(得分: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 *;
}

包括统计信息位置是很值得的,因为找出某些原因无法按预期工作非常有用。