Kliver Max
Kliver Max

Reputation: 5299

How to play HLS stream with nginx?

I have hginx with rtmp module. rtmp stream works fine, now i want to try hls stream.
My config:

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;
           }
      }
 }

Try to open url http://my-ip:8080/hls/mystream.m3u8 in vlc player but got error that can't open source.

What can i miss?

UPDATE

I tried to edit config based on miknik answer.

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 *;
            }
        }
    }

And try to open http://my-ip:8080/hls/mystream.m3u8 but still got same error.

And here error from nginx log:

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"

Upvotes: 1

Views: 4697

Answers (1)

miknik
miknik

Reputation: 5951

First thing, both your rtmp applications are called myapp. Call the second one hls.

In your myapp block you need to (optionally convert and) push the stream to the hls block. So you need a directive something like this:

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;

Then in your http block you want location blocks something like this:

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 *;
}

It's worth including the stats locations, as it can be very useful to figure out why something isn't working as you expect it to.

Upvotes: 1

Related Questions