天天看點

Nginx支援HLS配置

準備工作:

1.安裝nginx和rtmp子產品

2.安裝ffmepg(用來推流)

以上準備工作參見這篇部落格:http://www.jianshu.com/p/99f7b4581f8b

1.配置nginx

用記事本工具打開

/usr/local/etc/nginx/nginx.conf

找到server 修改

server {

listen 8080;

server_name localhost;

#charset koi8-r;

   #access_log  logs/host.access.log  main;

   location / {
       root   html;
       index  index.html index.htm;
   }

   location /hls {
       # Serve HLS fragments
       types {
           application/vnd.apple.mpegurl m3u8;
           video/mp2t ts;
       }
       root html;
       add_header Cache-Control no-cache;
   }

   location /dash {
       # Serve DASH fragments
       root /tmp;
       add_header Cache-Control no-cache;
   }


   #error_page  404              /404.html;

   # redirect server error pages to the static page /50x.html
   #
   error_page   500 502 503 504  /50x.html;
   location = /50x.html {
       root   html;
   }

   # proxy the PHP scripts to Apache listening on 127.0.0.1:80
   #
   #location ~ \.php$ {
   #    proxy_pass   http://127.0.0.1;
   #}

   # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
   #
   #location ~ \.php$ {
   #    root           html;
   #    fastcgi_pass   127.0.0.1:9000;
   #    fastcgi_index  index.php;
   #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
   #    include        fastcgi_params;
   #}

   # deny access to .htaccess files, if Apache's document root
   # concurs with nginx's one
   #
   #location ~ /\.ht {
   #    deny  all;
   #}
           

}

找到rtmp 修改下面這個地方

rtmp {

server {

listen 1935;

application live1 {

live on;

record off;

}

# HLS

    # For HLS to work please create a directory in tmpfs (/tmp/hls here)
    # for the fragments. The directory contents is served via HTTP (see
    # http{} section in config)
    #
    # Incoming stream must be in H264/AAC. For iPhones use baseline H264
    # profile (see ffmpeg example).
    # This example creates RTMP stream from movie ready for HLS:
    #
    # ffmpeg -loglevel verbose -re -i movie.avi  -vcodec libx264
    #    -vprofile baseline -acodec libmp3lame -ar 44100 -ac 1
    #    -f flv rtmp://localhost:1935/hls/movie
    #
    # If you need to transcode live stream use 'exec' feature.
    #
    application hls {
        live on;
        hls on;
        hls_path /usr/local/var/www/hls;
    }

    # MPEG-DASH is similar to HLS

    application dash {
        live on;
        dash on;
        dash_path /tmp/dash;
    }
}
           

}

儲存配置檔案,重新加載nginx配置

/usr/local/Cellar/nginx-full/1.8.1/bin/nginx -s reload

2.進行推流測試

ffmpeg -loglevel verbose -re -i /Users/Rick/Movies/Demo.mov -vcodec libx264 -vprofile baseline -acodec libmp3lame -ar 44100 -ac 1 -f flv rtmp://localhost:1935/hls/movie

然後你就可以在這個目錄

/usr/local/var/www/hls

看到生成一個個ts的檔案,還會生成一個movie.m3u8的檔案

在Safari裡輸入位址檢視視訊(需要等movie.m3u8檔案生成後),也可以用iPad或者iPhone上的Safari來通路(其他裝置記得需要把localhost改為nginx的ip位址)

http://localhost:8080/hls/movie.m3u8

作者:嗷大喵

連結:https://www.jianshu.com/p/089b70f57bca

來源:簡書

簡書著作權歸作者所有,任何形式的轉載都請聯系作者獲得授權并注明出處。

繼續閱讀