Detect scene changes and save h264 video clips on Raspberry Pi without decoding


Good afternoon. In this article, I’ll tell you, far from the first time, how to simultaneously detect motion and save / broadcast video in H264 format on Raspberry Pi 3 and weaker platforms. I will share with the same newcomers in the world of Raspberry Pi, as I, about what I found out in a few days, while I figured out how to solve the problem. I’ll talk about working with the Raspberry Pi camera in simple human language.


" CSI- Raspberry Pi v.1.3 2.1 720p @ 25 FPS, 1 h264, , ".


, , .


, , , ( ) , , .

RPi, , OpenCV, , - , ffmpeg, - .


OpenCV, , : " RGB/MJPEG, - H264 MP4...".


Raspberry Pi , - , datasheets- ( ), β€” , .


, Raspberry Pi . , , , 2.8-3.2 . , , CS-mount , . - 3d-, β€” , Thingverse .


Raspberry 3 / β€” . FullHD OpenMAX.


: Raspberry Pi - H264 -.


FFmpeg :


#!/bin/bash

SS=$1
FPS=$2

WIDTH=1600
HEIGHT=1200

while :
do

ffmpeg -f video4linux2 -input_format h264 -video_size ${WIDTH}x${HEIGHT} \
    -framerate $FPS -i /dev/video0 -vcodec copy -map 0 \
    -segment_time $SS -f segment \
    -strftime 1 -reset_timestamps 1 \
    -segment_format mp4 /video/file_%s.mp4
done

$SS FPS $FPS , unix-timestamp. , MP4. CPU . , FTP.


v2.1 1080p β€” ( FOV). 1600x1200 β€” FPS. , , ffmpeg raspivid (. ) . .


, , , RAM GPU Raspberry Pi, - β€” .

H264, , . : Raspberry Pi 3 FullHD OpenCV , 1280x720 @ 30 FPS .


H264? β€” H264.


Raspberry Pi (motion vectors) H264. . , .


Raspberry Pi , MV :


typedef struct
{
   signed char x_vector;
   signed char y_vector;
   short sad;
} INLINE_MOTION_VECTOR; 

{x,y}_vector β€” , sad β€” . sad vectors . sad, motion vectors.


FFmpeg motion vectors , Raspivid:


#!/bin/bash

SS=$1
FPS=$2

W=1600
H=1200

while :
do
    raspivid -pts -stm -w $W -h $H -fps $FPS -t 0 \
                    -sn $(date +%s) -sg $(($SS*1000)) \
                    -o /video/video_%d.h264 -x /video/mv_%d.txt
done

, raspivid +1, , timestamp, stat .

FFmpeg CVLC, RTSP . , β€” motion vectors .


, "", , , . . FPS=25 ( 30) , , 50Hz , .


β€” , , MV, , . . , , .

, , RPi β€” H264 , MV.


, *.h264 β€” H264, MP4, VLC, , , . Raspberry Pi gpac:


sudo apt install -y gpac

h264 mp4 :


time MP4Box -add /video/video_1588645858.h264 /video/video_1588645858.mp4
AVC-H264 import - frame size 1280 x 720 at 25.000 FPS
AVC Import results: 1560 samples - Slices: 26 I 1534 P 0 B - 0 SEI - 26 IDR
Saving to /video/video_1588645858.mp4: 0.500 secs Interleaving

real    0m11.164s
user    0m0.548s
sys 0m0.469s

11 Raspberry Pi 3, .


MV :


import os
import sys
import struct
import numpy as np

resolution = [1280, 720]

framelength = int(((resolution[0] + 16) / 16) * (resolution[1] / 16) * 4)

framedata = []

cnt = 0

while True:
    data = sys.stdin.read(framelength)

    if data == '':
      break

    cnt += 1

    framedata = struct.unpack('>%db' % framelength, data)
    vectors = np.reshape(framedata[:(resolution[1]/16 * (resolution[0]/16 + 1) * 4)], (resolution[1]/16, resolution[0]/16 + 1, 4)).astype(np.float32)
    vectors = np.multiply(vectors[:, :, 0:1], vectors[:, :, 0:1])

    print int(np.sum(vectors))

C, .


time $(cat /video/mv_1588645871.txt| python proc.py > /dev/null)

real    0m26.288s
user    0m25.005s
sys 0m0.583s

, NumPy ( C), , , , 50% , . MV, , .


, :


, , β€” . β€” . .


:


cat /video/mv_1588645871.txt | python proc.py | awk '{ if ($1 > 0) print "motion" }' | grep motion > /dev/null
echo $?
0

, . , FTP, .


, " " , , Raspberry Pi .


, , , MV . Medium, MV Python.


The article may seem commonplace for professionals, but I had to spend considerable time in order to deal with these things. I hope that for beginners starting to work with video on the Raspberry Pi platform this will be useful.


All Articles