Build your own live server on Mac

Install nginx on Mac

Why install nginx on Mac because there are modules that support the RTMP protocol on the nginx, which makes it easier to implement the RTMP protocol. Other services can be implemented as long as the RTMP protocol, but because nginx is easy to implement, nginx is used here as a live broadcast service.

We use the brew command to install nginx, please ensure that the homebrew is already installed on your Mac, if not installed can access the homebrew website to download and install https://brew.sh/index_zh-cn.html, you can run the following command in the terminal installation:

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

After you install homebrew, run the following command in the terminal and install nginx.

Nginx will be downloaded to the local brew / tap homebrew/nginx
Install the nginx server brew install nginx-full / / --with-rtmp-module
/ / open nginx server

Configuring the nginx service

/ / open VIM configuration file VIM /usr/local/etc/nginx/nginx.conf
RTMP {server {listen 1935; / / set the corresponding port of the application Mylive on record {live; off;}}} / / not record data
You add information to / / reload file to nginx -s reload

Install FFmpeg

In fact, after the above steps, live broadcast service has been set up, we are following the operation to verify our results, we have to install a FFmpeg third party library for the realization of the push stream.

Install the ffmpeg brew install ffmpeg / /

Realize the live streaming of video files

Ffmpeg, -re, -i, /Users/liuxiaoliang/Desktop/720.m4v, -vcodec, libx264, -acodec, AAC, -strict, -2,, -f, flv, rtmp://localhost:1935/mylive/room

You can also abbreviate the parameters inside

Ffmpeg, -re, -i, /Users/liuxiaoliang/Desktop/720.m4v, -c:v, libx264, -c:a, AAC, -strict, -2,, -f, flv, rtmp://localhost:1935/mylive/room

The command /Users/liuxiaoliang/Desktop/720.m4v above is the path to your video file. Change according to the location of your video file. Finally, the following rtmp://localhost:1935/mylive/room is my push stream address. Now, when you run the command, if the information of the packet stream appears in the following figure, the push flow will be successful.

Build your own live server on Mac
computer screenshot

Achieve pull flow

In order to facilitate testing, we use VLC player to achieve the pull stream, Mac side can download the Mac side player in VLC’s official website, for the iOS side, you can download the VLC player on the AppStore of the iOS mobile phone.
mobile phone as an example, the input current pulling address in the mobile phone VLC player network streaming, and it actually push flow is the same address just here to replace localhost Mac computer IP address (the implementation can not replace the current pulling on the Mac), and to ensure that the computer and mobile phone in Mac in a local area network.

Enter the following address / / in the VLC player can see the pull of streaming video on mobile phone records, if you replace localhost Mac IP address, rtmp://localhost:1935/mylive/room
Build your own live server on Mac
screen screenshot
Build your own live server on Mac
screen screenshot
Build your own live server on Mac
screen screenshot

Desktop video live

We can also push out the computer desktop images. But first we have to get the input hardware information on the computer, and then execute the following command to get the hardware information to determine which hardware is used as the input to the video image.

Get / / input device Mac on the ffmpeg -f avfoundation -list_devices true -i list""
Build your own live server on Mac
Mac computer can input device information.Png

We see the serial number of the computer screen is 1, so we execute the following command.

/ / to the server computer desktop image information ffmpeg -f avfoundation -i "1" -vcodec libx264 -f flv rtmp://localhost:1935/mylive/room

The only method above is the image without sound. The following commands are available for the sound

Ffmpeg, -f, avfoundation, -i, 1:0, -vcodec, libx264, -acodec, libmp3lame, -ar, 44100, -ac,, rtmp://localhost:1935/rtmplive/room, -f, flv
Build your own live server on Mac
computer screenshot

Capture images with computer cameras and push streams

It doesn’t feel like a full live broadcast, does it? Since we didn’t use the camera, we used the computer camera to get the image and then push it. You can use the following command

/ / set frame rate of 30, the image size is 320x240 ffmpeg -f avfoundation -framerate 30 -video_size 320x240 -i "0" -vcodec libx264 -f flv rtmp://localhost:1935/mylive/room

The command above is silent, and here is the command with the sound added

Ffmpeg, -f, avfoundation, -framerate, 30, -video_size, 320x240, -i, 0:0, -vcodec, libx264, -acodec, libmp3lame, -ar, 44100, -ac,,, -f, flv, rtmp://localhost:1935/mylive/room
Build your own live server on Mac
screen screenshot

The computer screen and camera simultaneously push the stream

Sometimes we have to have a webcam when we’re using the computer, and you can use the following commands

The computer desktop and the camera at the same time / / ffmpeg -f avfoundation -framerate -i live 30 "1:0" -f avfoundation -framerate 30 -video_size 320x240 -i "0" -vcodec libx264 -filter_complex'overlay=main_w-overlay_w-10:main_h-overlay_h-10'-acodec libmp3lame -ar 44100 -ac 1 -f flv rtmp://localhost:1935/mylive/room
Build your own live server on Mac
screen screenshot