搭建Nginx+RTMP/HLS視頻直播服務(wù)器

安裝環(huán)境,macos-10.13.4
nginx-1.12.2

1. 安裝(可以用 brew 安裝)

 sudo brew install nginx

2. 查看 nginx 版本

 nginx -v

3. 啟動 nginx

 sudo nginx  

也可以使用下面的命令啟動,但是配置文件nginx.conf修改后用這個命令執(zhí)行不生效,故不建議使用:

 sudo brew services start nginx

4. 查看 nginx 是否啟動成功

在瀏覽器中訪問 http://localhost:8080,如果出現(xiàn)如下界面,則說明啟動成功.

這里寫圖片描述

備注:端口號是在配置文件 nginx.conf 里面配置的,默認(rèn)端口是 8080 ,配置文件的位置 /usr/local/etc/nginx

5. 關(guān)閉nginx

 sudo nginx -s stop

也可以使用下面的命令啟動,但是配置文件nginx.conf修改后用這個命令執(zhí)行不生效,故不建議使用:

sudo brew services stop nginx

6. 重新加載nginx

sudo nginx -s reload

7. 可能遇到的問題

  • 端口被占用

    nginx: [emerg] bind() to 0.0.0.0:80 failed (48: Address already in use)

    解決方法:修改 nginx.conf 文件里的端口號

  • 權(quán)限不夠

    nginx: [alert] could not open error log file: open() “/usr/local/var/log/nginx/error.log” failed (13: Permission denied)

    解決方法:在命令前加上 sudo,這時可能會要求輸入密碼,密碼就是電腦的開機(jī)密碼啦~

補充

安裝 homebrew ,將以上命令粘貼至terminal,然后回車即可

 /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

一些問題的處理

如果你輸入http://localhost:8080后能看到nginx配置的首頁那說明OK。

或nginx -v看下版本號。都能確保裝上.

下面是在掛裝rtmp模塊時出現(xiàn)的問題處理。

安裝命令見:https://blog.csdn.net/leonpengweicn/article/details/44917943

安裝tap時:brew tap homebrew/nginx 報was deprecated.

報下面這個錯

Updating Homebrew...

==> **Auto-updated Homebrew!**

Updated 1 tap (homebrew/core).

==> **Updated Formulae**

abyss               docker2aci          libhttpseverywhere  subversion

amqp-cpp            emscripten          mypy                tup

chronograf          grpc                packmol             znapzend

dnscrypt-proxy      influxdb            rancher-cli

Error: homebrew/nginx was deprecated. This tap is now empty as all its formulae were migrated.

并不那么順利,根查原因是因為homebrew/nginx的git路徑變了。因此后面找到一個denji/nginx的。

把命令改為 brew tap denji/nginx

fsh-mac--00-24-D6-72-E4-6A:~ fengsh$ brew tap denji/nginx

Updating Homebrew...

==> **Tapping denji/nginx**

Cloning into '/usr/local/Homebrew/Library/Taps/denji/homebrew-nginx'...

remote: Counting objects: 72, done.

remote: Compressing objects: 100% (72/72), done.

remote: Total 72 (delta 1), reused 28 (delta 0), pack-reused 0

Unpacking objects: 100% (72/72), done.

Tapped 62 formulae (162 files, 130.2KB)

裝好這個本以為就可以直接裝rtmp模塊了。

直接裝rtmp命令 : brew install nginx-full --with-rtmp-module

fsh-mac--00-24-D6-72-E4-6A:~ fengsh$ brew install nginx-full --with-rtmp-module

==> **Installing nginx-full from denji/nginx**

Error: Cannot install denji/nginx/nginx-full because conflicting formulae are installed.

  nginx: because nginx-full symlink with the name for compatibility with nginx

Please `brew unlink nginx` before continuing.

Unlinking removes a formula's symlinks from /usr/local. You can

link the formula again after the install finishes. You can --force this

install, but the build may fail or cause obscure side-effects in the

resulting software.

又出問題了,denji的git下沒有nginx-full?詳細(xì)看錯誤有一句 please 'brew unlink nginx ' before continuing.

因此在執(zhí)行裝rtmp時先執(zhí)行 brew unlink nginx 再來安裝,最后成功了。

fsh-mac--00-24-D6-72-E4-6A:~ fengsh$ brew unlink nginx

Unlinking /usr/local/Cellar/nginx/1.13.12... 2 symlinks removed

fsh-mac--00-24-D6-72-E4-6A:~ fengsh$ brew install nginx-full --with-rtmp-module

Updating Homebrew...

==> **Installing nginx-full from denji/nginx**

==> **Installing dependencies for denji/nginx/nginx-full: ****rtmp-nginx-module**

==> **Installing denji/nginx/nginx-full dependency: ****rtmp-nginx-module**

==> **Downloading https://github.com/sergey-dryabzhinsky/nginx-rtmp-module/archive**

==> **Downloading from https://codeload.github.com/sergey-dryabzhinsky/nginx-rtmp-**

######################################################################## 100.0%

==> **Downloading https://github.com/sergey-dryabzhinsky/nginx-rtmp-module/compare**

######################################################################## 100.0%

==> **Patching**

==> **Applying v1.1.7.10...d25c56f.diff**

patching file .gitignore

patching file README.md

patching file config

patching file dash/ngx_rtmp_dash_module.c

patching file doc/README.md

patching file doc/directives.md

patching file hls/ngx_rtmp_hls_module.c

patching file hls/ngx_rtmp_mpegts.c

patching file hls/ngx_rtmp_mpegts.h

patching file hls/ngx_rtmp_mpegts_crc.c

patching file hls/ngx_rtmp_mpegts_crc.h

patching file ngx_rtmp.c

patching file ngx_rtmp_auto_push_module.c

patching file ngx_rtmp_cmd_module.h

patching file ngx_rtmp_codec_module.c

patching file ngx_rtmp_core_module.c

patching file ngx_rtmp_exec_module.c

patching file ngx_rtmp_handshake.c

patching file ngx_rtmp_live_module.c

patching file ngx_rtmp_live_module.h

patching file ngx_rtmp_netcall_module.c

patching file ngx_rtmp_notify_module.c

patching file ngx_rtmp_record_module.c

patching file ngx_rtmp_record_module.h

patching file ngx_rtmp_shared.c

patching file ngx_rtmp_version.h

配置文件 open /usr/local/etc/nginx 下的nginx.conf

**[plain]** [view plain](https://blog.csdn.net/fengsh998/article/details/79942775# "view plain")[copy](https://blog.csdn.net/fengsh998/article/details/79942775# "copy")

<embed id="ZeroClipboardMovie_1" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_1" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=1&amp;width=16&amp;height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;">

<embed id="ZeroClipboardMovie_2" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="16" height="16" name="ZeroClipboardMovie_2" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=2&amp;width=16&amp;height=16" wmode="transparent" style="box-sizing: border-box; outline: 0px; word-break: break-all;"> 

1.  #user  nobody;  
2.  worker_processes  1;  

4.  events {  
5.  worker_connections  1024;  
6.  }  

9.  http {  
10.  include       mime.types;  
11.  default_type  application/octet-stream;  

13.  sendfile        on;  

15.  keepalive_timeout  65;  
16.  server {  
17.  listen       8080;  
18.  server_name  localhost;  

20.  #charset koi8-r;  

22.  #access_log  logs/host.access.log  main;  

24.  location / {  
25.  root   html;  
26.  index  index.html index.htm;  
27.  }  

29.  #配置rtmp stat  
30.  location /stat {  
31.  rtmp_stat all;  
32.  rtmp_stat_stylesheet stat.xsl;  
33.  }  
34.  location /stat.xsl {  
35.  root /usr/local/Cellar/rtmp-nginx-module/1.1.7.11-dev_2/share/rtmp-nginx-module;  
36.  }  

38.  location /control {  
39.  rtmp_control all;  
40.  }  

42.  #HLS配置開始,這個配置為了`客戶端`能夠以http協(xié)議獲取HLS的拉流  
43.  location /hls {  
44.  # Serve HLS fragments  
45.  types {  
46.  application/vnd.apple.mpegurl m3u8;  
47.  video/mp2t ts;  
48.  }  
49.  root html;  
50.  add_header Cache-Control no-cache;  
51.  }  
52.  #HLS配置結(jié)束  

54.  #error_page  404              /404.html;  

56.  # redirect server error pages to the static page /50x.html  
57.  #  
58.  error_page   500 502 503 504  /50x.html;  
59.  location = /50x.html {  
60.  root   html;  
61.  }  
62.  }  

66.  include servers/*;  
67.  }  

69.  rtmp {  
70.  server {  
71.  listen 1935;  
72.  ping 30s;  
73.  notify_method get;  

75.  application myapp {  
76.  live on;  
77.  record off;  
78.  max_connections 1024;  
79.  }  

81.  #增加對HLS支持開始  
82.  application hls {  
83.  live on;  
84.  hls on;  
85.  hls_path /usr/local/var/www/hls;  
86.  hls_fragment 5s;  
87.  }  
88.  #增加對HLS支持結(jié)束  
89.  }  
90.  }  

有關(guān)rtmp的配置項看這個:https://www.cnblogs.com/lidabo/p/7099501.html

配置好之后要記得 nginx -s reload 重新加載配置。

之后就可以使用ffmpeg+vlc 進(jìn)行推流測試了。(經(jīng)試推流過一段時間會有Broken pipe)還在百度中,希望能找到吧。

安裝FFmpeg

1)利用上面的homebrew安裝FFmpeg:

brew install ffmpeg

2)當(dāng)命令結(jié)束之后,輸入以下命令(查看你的安裝ffmpeg的信息):

brew info ffmpeg

輸出的結(jié)果如下:

image.png

圖片中有好多庫,例如ffac,fontconfig,freetype等等,有紅叉的代表是沒有這個庫,有綠色的對勾的表示此庫已經(jīng)安裝上了。想要知道怎么安裝其他的依賴庫嗎?就看下面命令行。

更新ffmpeg,輸入以下命令行:

brew update 

或者:

brew upgrade ffmpeg

安裝某一個特定的庫如下格式:

brew install [FORMULA...]

例如:安裝openssl 庫輸入以下命令:

brew install openssl

image.png

卸載某個特定的庫如下格式:

brew uninstall [FORMULA...]

例如:卸載openssl庫輸入以下命令:

brew uninstall openssl

推流測試

二、FFmpeg推流

1.推流MP4文件

  • 視頻文件地址:/Users/xu/Desktop/bangbangbang.mp4

  • 推流拉流地址:rtmp://localhost:1935/rtmplive/home

  • acc:RTMP的音頻格式

  • flv: RTMP的視頻格式

    ffmpeg -re -i /Users/xu/Desktop/bangbangbang.mp4 -vcodec libx264 -acodec aac -f flv rtmp://localhost:1935/rtmplive/home
    
image

輸入命令行后,暫時先不要點回車,等設(shè)置好本地拉流后,再進(jìn)行推流。

2.本地拉流MP4文件

  • 打開VLC播放器
image
  • 設(shè)置播放地址
image
  • 設(shè)置拉流地址

    rtmp://localhost:1935/rtmplive/home
    
    image
  • 開始推流,點擊open后開始播放。

image

推流成功!

三、用ffmpeg推流桌面以及推流攝像頭進(jìn)行直播

1.如果希望將桌面錄制或者分享,可以使用命令行如下:

ffmpeg -f avfoundation -i "1" -vcodec libx264 -preset ultrafast -acodec libfaac -f flv rtmp://localhost:1935/rtmplive/home
  • 這個只能夠推桌面。

2.如果需要桌面+麥克風(fēng),比如一般做遠(yuǎn)程教育分享 命令行如下:

ffmpeg -f avfoundation -i "1:0" -vcodec libx264 -preset ultrafast -acodec libmp3lame -ar 44100 -ac 1 -f flv rtmp://localhost:1935/rtmplive/home
  • 這個可以推桌面+麥克風(fēng)。

3.如果需要桌面+麥克風(fēng),并且還要攝像頭拍攝到自己,比如一般用于互動主播,游戲主播,命令行如下

ffmpeg -f avfoundation -framerate 30 -i "1:0" \-f avfoundation -framerate 30 -video_size 640x480 -i "0" \-c:v libx264 -preset ultrafast \-filter_complex 'overlay=main_w-overlay_w-10:main_h-overlay_h-10' -acodec libmp3lame -ar 44100 -ac 1  -f flv rtmp://localhost:1935/rtmplive/home
  • 這個可以推桌面+麥克風(fēng),并且攝像頭把人頭放在界面下面
image

四、FFmpeg常用基本命令

1.分離視頻音頻流

ffmpeg -i input_file -vcodec copy -an output_file_video  //分離視頻流
ffmpeg -i input_file -acodec copy -vn output_file_audio  //分離音頻流

2.視頻解復(fù)用

ffmpeg –i test.mp4 –vcodec copy –an –f m4v test.264ffmpeg –i test.avi –vcodec copy –an –f m4v test.264

3.視頻轉(zhuǎn)碼

ffmpeg –i test.mp4 –vcodec h264 –s 352*278 –an –f m4v test.264 //轉(zhuǎn)碼為碼流原始文件
ffmpeg –i test.mp4 –vcodec h264 –bf 0 –g 25 –s 352*278 –an –f m4v test.264 //轉(zhuǎn)碼為碼流原始文件
ffmpeg –i test.avi -vcodec mpeg4 –vtag xvid –qsame test_xvid.avi //轉(zhuǎn)碼為封裝文件
  • -bf B幀數(shù)目控制
  • -g 關(guān)鍵幀間隔控制
  • -s 分辨率控制

4.視頻封裝

ffmpeg –i video_file –i audio_file –vcodec copy –acodec copy output_file

5.視頻剪切

ffmpeg –i test.avi –r 1 –f image2 image-%3d.jpeg //提取圖片
ffmpeg -ss 0:1:30 -t 0:0:20 -i input.avi -vcodec copy -acodec copy output.avi //剪切視頻
  • -r 提取圖像的頻率
  • -ss 開始時間
  • -t 持續(xù)時間

6.視頻錄制

ffmpeg –i rtsp://192.168.3.205:5555/test –vcodec copy out.avi

7.YUV序列播放

ffplay -f rawvideo -video_size 1920x1080 input.yuv

8.YUV序列轉(zhuǎn)AVI

ffmpeg –s w*h –pix_fmt yuv420p –i input.yuv –vcodec mpeg4 output.avi

9.常用參數(shù)說明:

主要參數(shù):
i 設(shè)定輸入流
f 設(shè)定輸出格式
ss 開始時間
視頻參數(shù):
b 設(shè)定視頻流量,默認(rèn)為200Kbit/s-r 設(shè)定幀速率,默認(rèn)為25
s 設(shè)定畫面的寬與高-aspect 設(shè)定畫面的比例
vn 不處理視頻-vcodec 設(shè)定視頻編解碼器,未設(shè)定時則使用與輸入流相同的編解碼器
音頻參數(shù):
ar 設(shè)定采樣率
ac 設(shè)定聲音的Channel數(shù)
acodec 設(shè)定聲音編解碼器,未設(shè)定時則使用與輸入流相同的編解碼器an 不處理音頻

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

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

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