让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

你可能感兴趣的:(让nginx支持HLS)