Setup Guide: Raspberry Pi | Recording Triggered by Events
shomar80 edited this page Jun 2, 2018
·
5 revisions
You can use CNCjs Events to trigger all kinds of things.
For this demonstration we are going to start & stop a video recording on job start & stop.
This is done by calling ~/record.sh start
& ~/record.sh stop
(as seen in the top of the screenshot)
- First, you will want to follow Setup Guide: Raspberry Pi | MJPEG Streamer Install & Setup, and FFMpeg Recording
- After you have MJPEG Streamer & FFMpeg setup and working. Configure and place the script below to
~/record.sh
. - set the ~/record.sh to be executable 'chmod +x ~/record.sh'
- Setup the CNCjs Events as show in the screenshot above.
- Event to start recording on Job Start
- Event to stop recording on Job Stop
#!/bin/bash
# MJPEG Streamer Recorder w/ FFMpeg - v1
# chmod +x record.sh
# sh ~/record.sh start
FFMPEG_STREAMER_BIN="$(which ffmpeg)"
INPUT_OPTIONS="-y -f mjpeg -re"
SOURCE_STREAM="http://localhost:8080/?action=stream"
OUTPUT_OPTIONS="-q:v 10"
DESTINATION_DIRECTORY="/home/pi/Videos"
DESTINATION_FILE="xcarve-recording_$(date +'%Y%m%d_%H%M%S').mpeg"
# ==========================================================
function running() {
if ps aux | grep ${FFMPEG_STREAMER_BIN} | grep "${SOURCE_STREAM}" >/dev/null 2>&1; then
return 0
else
return 1
fi
}
function start() {
if running; then
echo "already started"
return 1
fi
export LD_LIBRARY_PATH="$(dirname $FFMPEG_STREAMER_BIN):."
echo "Starting: ${FFMPEG_STREAMER_BIN} ${INPUT_OPTIONS} -i \"${SOURCE_STREAM}\" ${OUTPUT_OPTIONS} \"${DESTINATION_DIRECTORY}/${DESTINATION_FILE}\""
#${FFMPEG_STREAMER_BIN} ${INPUT_OPTIONS} -i "${SOURCE_STREAM}" ${OUTPUT_OPTIONS} "${DESTINATION_DIRECTORY}/${DESTINATION_FILE}" </dev/null >/dev/null 2>record_ffmpeg.log & # https://trac.ffmpeg.org/wiki/PHP
${FFMPEG_STREAMER_BIN} ${INPUT_OPTIONS} -i "${SOURCE_STREAM}" ${OUTPUT_OPTIONS} "${DESTINATION_DIRECTORY}/${DESTINATION_FILE}" >/dev/null 2>/dev/null & # No Log File
sleep 1
if running; then
# if [ "$1" != "nocheck" ]; then
# check_running & > /dev/null 2>&1 # start the running checking task
# check_hanging & > /dev/null 2>&1 # start the hanging checking task
# fi
echo "started"
return 0
else
echo "failed to start"
return 1
fi
}
function stop() {
if ! running; then
echo "not running"
return 1
fi
own_pid=$$
if [ "$1" != "nocheck" ]; then
# stop the script running check task
ps aux | grep $0 | grep start | tr -s ' ' | cut -d ' ' -f 2 | grep -v ${own_pid} | xargs -r kill
sleep 0.5
fi
# stop the process
ps aux | grep ${FFMPEG_STREAMER_BIN} | grep "${SOURCE_STREAM}" | tr -s ' ' | cut -d ' ' -f 2 | grep -v ${own_pid} | xargs -r kill
echo "stopped"
return 0
}
function check_running() {
echo "starting running check task" >> ${MJPG_STREAMER_LOG_FILE}
while true; do
sleep ${RUNNING_CHECK_INTERVAL}
if ! running; then
echo "server stopped, starting" >> ${MJPG_STREAMER_LOG_FILE}
start nocheck
fi
done
}
function check_hanging() {
echo "starting hanging check task" >> ${MJPG_STREAMER_LOG_FILE}
while true; do
sleep ${HANGING_CHECK_INTERVAL}
# treat the "error grabbing frames" case
if tail -n2 ${MJPG_STREAMER_LOG_FILE} | grep -i "error grabbing frames" > /dev/null; then
echo "server is hanging, killing" >> ${MJPG_STREAMER_LOG_FILE}
stop nocheck
fi
done
}
function help() {
echo "Usage: $0 [start|stop|restart|status]"
return 0
}
if [ "$1" == "start" ]; then
start && exit 0 || exit -1
elif [ "$1" == "stop" ]; then
stop && exit 0 || exit -1
elif [ "$1" == "restart" ]; then
stop && sleep 1
start && exit 0 || exit -1
elif [ "$1" == "status" ]; then
if running; then
echo "running"
exit 0
else
echo "stopped"
exit 1
fi
else
help
fi
Join CNCjs Users Group on Facebook to share your experiences with other people.
Site: https://cnc.js.org
- Introduction
- Installation
- Using Pendants
-
Raspberry Pi Setup Guide
- System Setup & Preparation
- Install Node.js & CNCjs
- Install Node.js via Package Manager (Recommended)
- Install Node.js via Node Version Manager (NVM) (Alternative)
- Install Node.js Manually (Alternative)
- Use PM2 to auto-start CNCjs (Optional)
- Use Port 80 (Optional)
- Maintain
- Additional Setup Options:
- CNCjs UI on Pi - Adventures in PiLand
- Pi with Arduino Due
- Electron App
- User Guide
- Tool Change
- Troubleshooting
-
FAQ
- Forgot your password?
- How can I enable WebGL in my browser?
- Raspberry Pi: Error opening serial port "ttyAMA0"
- Webcam Streaming with Raspberry Pi
- Restream RTSP to M-JPEG
- Connect to an Arduino using WiFi
- Install Native Addons with Node.js v4
- Install Serialport on OS X El Capitan
- Kernel panic issue on macOS Sierra for devices using the CH340G, CH34G or CH34X chipset
- Testing without Arduino board
- Development
- API
- Wiki History
- Resources
- About