參考來源:https://github.com/denji/home...
相關文章:H5視頻直播原理html
1.安裝Homebrew:
須要先在應用商店手動安裝Xcode,再進行如下操做nginx
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
安裝過程當中會提示須要安裝xcode command line tool,但Mac最新場景下安裝Xcode時已經沒有Command Line了,須要單獨安裝。根據提示在使用命令xcode-select --install 安裝時最後結果是不能安裝該軟件。git
解決方式github
2.執行命令:segmentfault
brew tap denji/nginx
3.執行命令:windows
brew install nginx-full --with-rtmp-module
查看nginx安裝信息:xcode
brew info nginx-full
可看到以下信息:瀏覽器
Docroot is: /usr/local/var/www The default port has been set in /usr/local/etc/nginx/nginx.conf to 8080 so that nginx can run without sudo. nginx will load all files in /usr/local/etc/nginx/servers/. - Tips - Run port 80: $ sudo chown root:wheel /usr/local/Cellar/nginx-full/1.15.6/bin/nginx $ sudo chmod u+s /usr/local/Cellar/nginx-full/1.15.6/bin/nginx Reload config: $ nginx -s reload Reopen Logfile: $ nginx -s reopen Stop process: $ nginx -s stop Waiting on exit process $ nginx -s quit
nginx安裝位置:緩存
/usr/local/Cellar/nginx-full/
nginx配置文件位置:ruby
/usr/local/etc/nginx/nginx.conf
nginx服務器根目錄位置:
/usr/local/var/www
驗證是否安裝成功,執行命令:
nginx
而後瀏覽器中輸入http://localhost:8080,出現如下界面,證實安裝成功
1.安裝ffmpeg,執行命令:
brew install ffmpeg
windows下安裝ffmpeg:https://ffmpeg.zeranoe.com/bu...
安裝過程須要一段時間。
2.驗證是否安裝成功,執行命令:
ffmpeg
顯示信息以下:
ffmpeg version 4.1 Copyright (c) 2000-2018 the FFmpeg developers built with Apple LLVM version 10.0.0 (clang-1000.11.45.5) configuration: --prefix=/usr/local/Cellar/ffmpeg/4.1 --enable-shared --enable-pthreads --enable-version3 --enable-hardcoded-tables --enable-avresample --cc=clang --host-cflags= --host-ldflags= --enable-ffplay --enable-gpl --enable-libmp3lame --enable-libopus --enable-libsnappy --enable-libtheora --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libx265 --enable-libxvid --enable-lzma --enable-opencl --enable-videotoolbox libavutil 56. 22.100 / 56. 22.100 libavcodec 58. 35.100 / 58. 35.100 libavformat 58. 20.100 / 58. 20.100 libavdevice 58. 5.100 / 58. 5.100 libavfilter 7. 40.101 / 7. 40.101 libavresample 4. 0. 0 / 4. 0. 0 libswscale 5. 3.100 / 5. 3.100 libswresample 3. 3.100 / 3. 3.100 libpostproc 55. 3.100 / 55. 3.100 Hyper fast Audio and Video encoder usage: ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfile}...
用IDE打開nginx.conf文件,文件路徑:
/usr/local/etc/nginx/
有atom編輯器的話能夠執行如下命令打開:
open nginx.conf -a atom
編輯內容:
1.在http節點後面加上rtmp配置
http { ... } #在http節點後面加上rtmp配置 rtmp { server { # 監聽端口 listen 1935; # 分塊大小 chunk_size 4000; # RTMP 直播流配置 application rtmplive { # 開啓直播的模式 live on; # 設置最大鏈接數 max_connections 1024; } # hls 直播流配置 application hls { live on; hls on; # 分割文件的存儲位置 hls_path /usr/local/var/www/hls; # hls分片大小 hls_fragment 5s; } } }
2.在http節點內的server節點內增長配置項:
http { ... server { ... location / { root html; index index.html index.htm; } location /hls { # 響應類型 types { application/vnd.apple.mpegurl m3u8; video/mp2t ts; } root /usr/local/var/www; # 不要緩存 add_header Cache-Control no-cache; } ... } ... }
到這裏爲止,安裝和配置工做已經完成,而後下一步操做。
1.重啓nginx服務,執行命令:
先關閉nginx
nginx -s stop
再打開:
nginx
或者直接執行reload:
nginx -s reload
2.推流
準備一個視頻文件(如mp4文件)來測試推流,
在視頻文件所在目錄下執行如下命令模擬rtmp推流:
ffmpeg -re -i test.mp4 -vcodec libx264 -acodec aac -f flv rtmp://localhost:1935/rtmplive/rtmp
能夠安裝一個支持rtmp協議的視頻播放器,在Mac下能夠用VLC播放器,在VLC的「File--Open Network--URL」裏輸入「rtmp://localhost:1935/rtmplive/rtmp」,而後點擊open選項就能夠播放
要進行hls推流的話只須要以上命令稍做修改:
ffmpeg -re -i test.mp4 -vcodec libx264 -acodec aac -f flv rtmp://localhost:1935/hls/stream
其中最後的stream是你本身取的名字,隨便寫
而後在sarafi瀏覽器裏輸入http://localhost:8080/hls/stream.m3u8 就能正常播放直播流了
也能夠用VLC播放器輸入「rtmp://localhost:1935/hls/stream」播放
帶server的代碼https://github.com/HughieSong...,裏面有個名爲「server」的二進制文件。
輸入如下命令啓動:
open server
而後會看到
SungdeMacBook-Pro:~ admin$ /Users/admin/Downloads/project/github/h5live/server/server ; exit; 2018/12/08 14:58:37 main.go:106: start livego, version 0.0.4 2018/12/08 14:58:37 main.go:40: HLS listen On :7002 2018/12/08 14:58:37 main.go:58: RTMP Listen On :1935 2018/12/08 14:58:37 main.go:75: HTTP-FLV listen On :7001
這說明服務被啓用了,這時候爲了不未來出現問題,咱們先運行如下命令中止nginx服務:
nginx -s stop
從新啓動server:
open server
這時候看到如下內容:
SungdeMacBook-Pro:~ admin$ /Users/admin/Downloads/project/github/h5live/server/server ; exit; 2018/12/08 15:22:51 main.go:106: start livego, version 0.0.4 2018/12/08 15:22:51 main.go:30: listen tcp :7002: bind: address already in use logout Saving session... ...copying shared history... ...saving history...truncating history files... ...completed. Deleting expired sessions...96 completed. [進程已完成]
而後在h5live目錄下執行推流命令:
ffmpeg -re -i test.mp4 -c copy -f flv rtmp://localhost:1935/live/movie
進程此時就跑起來了,打開VLC播放器-->File-->Open Network-->URL中輸入rtmp://localhost:1935/live/movie-->open就能夠播放了
或者sarafi瀏覽器中輸入http://127.0.0.1:7002/live/movie.m3u8 或者http://localhost:7002/live/movie.m3u8 來驗證