Mac搭建nginx+rtmp服務器,ffmpeg實現視頻推流

一、clone nginx到本地,執行

brew tap denji/homebrew-nginx

二、安裝nginx,執行

brew install nginx-full --with-rtmp-module

執行:nginx,在瀏覽器裏打開http://localhost:8080 出現Welcome to nginx! 表示安裝成功

三、查看nginx安裝到哪了

brew info nginx-full

如下, 找到nginx.conf文件所在位置

The default port has been set in /usr/local/etc/nginx/nginx.conf to 8080 so that

nginx can run without sudo.

四、修改:nginx.conf

# 在http節點後面加上rtmp配置:

rtmp {
    server {
        listen 1935;
        application rtmplive {
            live on;
            record off;
        }
    }
}

 

五、重啓nginx

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

六、推流

ffmpeg -re -i aa.mp4 -vcodec libx264 -acodec aac -strict -2 -f flv rtmp://localhost:1935/rtmplive/room

七、vlc播放

將視頻推流到服務器後,打開VLC,然後File->open network->輸入:

rtmp://localhost:1935/rtmplive/room

 

配置對HLS的支持

 

HLS 直播流配置

找到http-->server,在花括號中增加

server {
        listen       8089;   #修改8080
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }
        
        #HLS配置開始,這個配置爲了`客戶端`能夠以http協議獲取HLS的拉流
        location /hls {
            # Serve HLS fragments
            types {
                application/vnd.apple.mpegurl m3u8;
                video/mp2t ts;
            }
            root html;
            add_header Cache-Control no-cache;
        }
       #HLS配置結束

        #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下的server在花括號中增加

# 在http節點後面加上rtmp配置:
rtmp {
    server {
        listen 1935;
        application rtmplive {
            live on;
            record off;
        }

        #增加對HLS支持開始
        #live on; 開啓實時
        #hls on; 開啓hls
        #hls_path; ts文件存放路徑
            #如需要修改指定的目錄下,比如"/tmp/hls"
            #那麼,我們也需要在http-->server中對root 路徑更改爲:/tmp 。要不然,會拉不到流。
            #root html 是指使用當前nginx服務器根目錄所在位置,指向的是 /usr/local/var/www 這個目錄
        #hls_fragment 5s; 每個TS文件包含5秒的視頻內容
        application hls {
            live on;
            hls on;
            hls_path /usr/local/var/www/hls;
            hls_fragment 5s; 
        }
        #增加對HLS支持結束
    }
}

進行推流

ffmpeg推流還是和上邊的一樣,不過,我們需要推到新配置的hls中,movie 關鍵字可以任何替換

ffmpeg -re -i demo.mp4 -vcodec copy -f flv rtmp://localhost:1935/hls/movie

 

測試拉流

通過上面的配置,我們可以同時通過rtmphls兩種播放方式來看到推出來的流。注意,如果使用 http 方式,則是監聽的 8080 端口,這個是在配置文件裏寫的

(1) rtmp:(使用VLC驗證播放)

rtmp://192.168.1.100/hls/movie

(2) hls播放

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章