Video-Streaming-Notes.md 4.6 KB

Video Streaming Notes

Install dependencies and create /dev/video1:

apt-get install -y motion v4l2loopback-utils v4l2loopback-source
modprobe v4l2loopback

Update motion.conf:

$ mkdir -p $HOME/log
$ cat <<EOF > $HOME/.motion/motion.conf
stream_port 8080
stream_quality 98
stream_maxrate 25
framerate 10
stream_localhost off
output_pictures off
framerate 30
ffmpeg_video_code mpeg4
width 320
height 240
auto_brightness off
contrast 0
saturation 0
text_right ''
videodevice /dev/video1
log_file /home/pi/log/motion.log
EOF

Motion will, by default, look in ~/.motin/motion.conf for the config file.

A simple video passthrough can be used to test to make sure everything is working:

ffmpeg -re -f v4l2 -i /dev/video0 -r 11 -q 3 -f avi - | ffmpeg -re -f avi -i - -f v4l2 /dev/video1

After starting motion, and pointing a browser to the motion port that's been setup (8080), a 320x240 video feed should be seen.


Once the simple test has been done, a more complex example can be done with OpenCV that puts an overlay onto the image.

Here is a simple OpenCV Python program to put some text and an alpha blended box into the middle of the stream. Call it ocv2-overlay-cap.py:

#!/usr/bin/python
#
import sys
import cv2
import numpy as np
foreground = np.ones((100,100,3),dtype='uint8')*255
cap = cv2.VideoCapture(0)
#cap.set(cv2.cv.CV_CAP_PROP_FRAME_WIDTH, 320)
#cap.set(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT, 240)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 320)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 240)
alpha = 0.4
while True:
  ret, background = cap.read()
  #background = cv2.flip(background,1)
  added_image = cv2.addWeighted(background[100:200,100:200,:],alpha,foreground[0:100,0:100,:],1-alpha,0)
  background[100:200,100:200] = added_image
  font = cv2.FONT_HERSHEY_SIMPLEX
  #cv2.putText(background,'alpha:{}'.format(alpha),(10,30), font, 1,(255,255,255),2,cv2.CV_AA)
  cv2.putText(background,'alpha:{}'.format(alpha),(10,30), font, 1,(255,255,255),2,cv2.LINE_AA)
  sys.stdout.write( background.tostring() )
cap.release()
cv2.destroyAllWindows()

Now to run:

python ./ocv2-overlay-cap.py | \
  ffmpeg -f rawvideo -pixel_format bgr24 -video_size 320x240 -i - -f avi -speed 10 -q 3 -r 11  -fflags nobuffer - | \
  ffmpeg -f avi -i - -f v4l2 /dev/video1

This will create a 320x240 feed with a simple overlay of text and a box.

Run motion and then point your browser to localhost:8080.

Misc.

I was getting some errors when trying to load cv2 from python. The following solved the issue:

apt update
apt upgrade
reboot

Different qualities can be fed in to try and speed up the video with the -q, -r and -speed parameters. Some have been chosen above but they can be altered depending on what quality of video and what playback speed is desired.


The motion parameters in motion.conf can be added to save to output files:

output_pictures on
picture_output on

The motion parameter, stream_localhost, in motion.conf should be set to off to ensure only localhost streaming can occur:

stream_localhost on

References