讓nginx支持HLS

準(zhǔn)備工作:

1.安裝nginx和rtmp模塊

2.安裝ffmepg(用來(lái)推流)

以上準(zhǔn)備工作參見(jiàn)這篇博客:http://www.itdecent.cn/p/99f7b4581f8b

1.配置nginx

用記事本工具打開(kāi)

/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 修改下面這個(gè)地方

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.進(jìn)行推流測(cè)試

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

然后你就可以在這個(gè)目錄
/usr/local/var/www/hls
看到生成一個(gè)個(gè)ts的文件,還會(huì)生成一個(gè)movie.m3u8的文件

在Safari里輸入地址查看視頻(需要等movie.m3u8文件生成后),也可以用iPad或者iPhone上的Safari來(lái)訪問(wèn)(其他設(shè)備記得需要把localhost改為nginx的ip地址)
http://localhost:8080/hls/movie.m3u8

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容