How to Setup an Camera for RTMP Streaming

Uses the video4linux2 (or simply v4l2) input device to capture live input such as from a webcam.

install

1
$ sudo apt install v4l-utils

To list the supported, connected capture devices you can use the v4l-ctl tool. This example shows two connected webcams: /dev/video0 and /dev/video1.

1
2
3
4
5
$ v4l2-ctl --list-devices
USB2.0 PC CAMERA (usb-0000:00:1d.7-1):
/dev/video1
UVC Camera (046d:0819) (usb-0000:00:1d.7-2):
/dev/video0

To list available formats (supported pixel formats, video formats, and frame sizes) for a particular input device:

1
2
3
4
$ ffmpeg -f v4l2 -list_formats all -i /dev/video0

[video4linux2,v4l2 @ 0xf07d80] Raw : yuyv422 : YUV 4:2:2 (YUYV) : 640x480 160x120 176x144 320x176 320x240 352x288 432x240 544x288 640x360
[video4linux2,v4l2 @ 0xf07d80] Compressed: mjpeg : MJPEG : 640x480 160x120 176x144 320x176 320x240 352x288 432x240 544x288 640x360

Alternatively you could use v4l2-ctl –list-formats-ext to list available formats.

Example to encode video from /dev/video0

1
ffmpeg -f v4l2 -framerate 25 -video_size 640x480 -i /dev/video0 output.mkv

Use LiveGo as rtmp sever. LiveGo is a simple and efficient live broadcast server.

https://github.com/gwuhaolin/livego

Directly downloading the compiled binary file or compile from source.

Download the source code git clone https://github.com/gwuhaolin/livego.git

Go to the livego directory and execute ‘go build’.

First, execute the livego binary file to start the livego service.

Get a channelkey from http://localhost:8090/control/get?room={appname} and copy data like your channelkey.

Such as you enter http://localhost:8090/control/get?room=cam and the browser return

1
{"status":200,"data":"rfBd56ti2SMtYvSgD5xAV0YU99zampta7Z7S575KLkIZ9PYk"}

The appname is cam. And channelkey is rfBd56ti2SMtYvSgD5xAV0YU99zampta7Z7S575KLkIZ9PYk.

After start the LiveGO, now You can use ffmpeg to push your camera stream.

1
ffmpeg -f v4l2 -i /dev/video0 --vcodec libx264 -f flv rtmp://localhost:1935/live/cam

Now is time to downstream playback.

FLV: http://localhost:7001/live/rfBd56ti2SMtYvSgD5xAV0YU99zampta7Z7S575KLkIZ9PYk.flv

HLS: http://localhost:7002/live/rfBd56ti2SMtYvSgD5xAV0YU99zampta7Z7S575KLkIZ9PYk.m3u8