在mac上搭建簡單的hls推流服務器
最近公司要有在網(wǎng)頁端用hls推流來播放全景視頻的需求远寸,難點在于全景播放器的兼容性問題宾茂。目前市場上已有成熟的產(chǎn)品是krpano,但是公司希望我去調(diào)研下。常年iOS開發(fā)的我只好找了兩天的開源項目链烈,目前選定videojs來做播放器,結合公司的渲染器來完成挚躯。
為了測試demo强衡,先完成在自己的mac上搭建一個hls的推流服務器。一搜索基本全都是用ngnix搭建的码荔,根據(jù)各種教程漩勤,磕磕絆絆用了半天多時間才搞定。特地把遇到的坑做個記錄缩搅。
第一步:安裝brew
$?/usr/bin/ruby -e"$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
第二步:安裝nginx服務器
$?brew tap homebrew/nginx
報錯:Error: homebrew/nginx was deprecated. This tap is now empty as all its formulae were migrated.
隨后換了個github的項目(https://github.com/denji/homebrew-nginx)成功了:
$?brew tap denji/nginx
$?brew install nginx-full --with-upload-module
安裝成功之后可查看信息:
$?brew options nginx-full
$?brew info nginx-full
第三步:開始啟動服務器
$?sudo nginx
發(fā)生錯誤:
nginx: [emerg] bind() to 0.0.0.0:1935 failed (48: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:8080 failed (48: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:1935 failed (48: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:8080 failed (48: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:1935 failed (48: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:8080 failed (48: Address already in use)
發(fā)現(xiàn)前面有嘗試過開啟越败,雖然報錯了,端口還是被占用了
$ sudo nginx -s stop
順利開啟之后硼瓣,開始配置hls支持
第四步:配置hls
worker_processes 1;
#error_log? logs/error.log debug;
events {
? ? worker_connections? 1024;
}
rtmp {
? ? server {
? ? ? ? listen 1935;
? ? ? ? application zbcs {
? ? ? ? ? ? ? ? live on;
? ? ? ? ? ? ? ? record off;
? ? ? ? ? ? }
? ? ? ? #配置HLS支持開始
? ? ? ? application hls {
? ? ? ? ? ? live on;
? ? ? ? ? ? hls on;
? ? ? ? ? ? hls_path /usr/local/var/www/hls;
? ? ? ? ? ? hls_fragment 5s;
? ? ? ? }
? ? ? ? #配置HLS支持結束
? ? }
}
http {
? ? server {
? ? ? ? ? ? listen? ? ? 8080;
? ? ? ? ? ? server_name? localhost;
? ? ? ? ? ? #charset koi8-r;
? ? ? ? ? ? #access_log? logs/host.access.log? main;
? ? ? ? ? ? location / {
? ? ? ? ? ? ? ? root? html;
? ? ? ? ? ? ? ? index? index.html index.htm;
? ? ? ? ? ? }
? ? ? ? ? #HLS可在http下訪問
? ? ? ? ? ? location /hls {
? ? ? ? ? ? ? ? # Serve HLS fragments
? ? ? ? ? ? ? ? types {
? ? ? ? ? ? ? ? ? ? application/vnd.apple.mpegurl m3u8;
? ? ? ? ? ? ? ? ? ? video/mp2t ts;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? root /usr/local/var/www;
? ? ? ? ? ? ? ? add_header Cache-Control no-cache;
? ? ? ? ? ? }
? ? ? ? ? #HLS在http結束
? ? ? ? ? ? #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;
? ? ? ? ? ? }
? ? }
}
然后重新load配置文件
$ sudo nginx -s reload
報錯:
nginx: [error] open() "/usr/local/var/run/nginx.pid" failed (2: No such file or directory)
需要重新設置一下配置文件:
$ sudo nginx -c /usr/local/etc/nginx/nginx.conf
$?sudo nginx -s reload
$?sudo nginx //開啟服務器
還有一種錯誤:
[emerg] 31225#0: unknown directive "rtmp" in /usr/local/etc/nginx/nginx.conf:9
這是因為rtmp 模塊沒有加進去究飞,需要重新安裝一下:
$ brew uninstall nginx-full// 刪除
$ nginxbrew install nginx-full --with-rtmp-module// 安裝
終于到了第五步:利用ffmpeg推送流:
$ brew install ffmpeg // 安裝ffmpeg
$ ffmpeg -re -i 視頻的據(jù)對路徑 -vcodec copy -acodec copy -f flv rtmp://localhost:1935/hls/movie //指向在/usr/local/var/www 下建立一個可讀可寫的hls文件夾
結果ts文件雖然是有一直生成,但是不能播放:
$ ffmpeg -re -i 視頻的據(jù)對路徑?-vcodec libx264 -acodec aac -strict -2 -f flv rtmp://127.0.0.1:1935/hls/movie // 搞定
可通過直接訪問: