Files
video-processing/capture_video.sh
2024-02-06 22:00:48 +01:00

114 lines
2.5 KiB
Bash
Executable File

#!/bin/bash
if [[ "$1" == *help ]]; then
echo "Usage: ${0} [-duration 10] [-sound plughw:1,0]"
echo "default duration (in seconds) is infinite"
echo "default sound is anullsrc i.e. silent sound"
exit 1
fi
# available options
# 640x480/60
# 1296x972/43
# 1920x1080/30
# 2592x1944/15
#VIDEO_RES_HEIGHT="1080"
#VIDEO_RES_WIDTH="1920"
#VIDEO_RES_HEIGHT="972"
#VIDEO_RES_WIDTH="1296"
VIDEO_BITRATE="700000"
VIDEO_RES_HEIGHT="480"
VIDEO_RES_WIDTH="640"
VIDEO_FRAMERATE="10"
VIDEO_FILE="camera_$(date +%Y-%m-%d_%H-%M-%S).mp4"
VIDEO_DURATION="1000000000"
SOUND_SOURCE="anullsrc"
while [[ $# -gt 0 ]]; do
case $1 in
-sound)
if [ -n "$2" ]; then
SOUND_SOURCE="$2"
shift
fi
;;
-duration)
if [ -n "$2" ]; then
VIDEO_DURATION="$2"
shift
fi
;;
*)
echo "Unknown parameter $1"
exit 1
esac
shift
done
# Set up a trap to check for SIGINT
trap "pkill rpicam-vid; exit 130" SIGINT
# Start the pipeline
rpicam-vid --inline \
-n \
-t 0 \
--width ${VIDEO_RES_WIDTH} \
--height ${VIDEO_RES_HEIGHT} \
--nopreview \
--exposure long \
--sharpness 1.2 \
--contrast 1.4 \
--brightness 0.2 \
--saturation 1.0 \
--awb auto \
--denoise auto \
--rotation 0 \
--autofocus-mode auto \
--codec h264 \
--framerate ${VIDEO_FRAMERATE} \
-b ${VIDEO_BITRATE} \
-o - | \
if [ "${SOUND_SOURCE}" = "anullsrc" ]; then
# ffmpeg params with silent audio
ffmpeg -hide_banner \
-f lavfi \
-i anullsrc=channel_layout=stereo:sample_rate=44100 \
-thread_queue_size 1024 \
-use_wallclock_as_timestamps 1 \
-i pipe:0 \
-c:v copy \
-c:a aac \
-preset fast \
-strict experimental \
-f mp4 \
-t ${VIDEO_DURATION} \
${VIDEO_FILE} &
else
# ffmpeg params with audio
ffmpeg -hide_banner \
-f alsa \
-thread_queue_size 1024 \
-ac 2 \
-i ${SOUND_SOURCE} \
-thread_queue_size 1024 \
-use_wallclock_as_timestamps 1 \
-i pipe:0 \
-c:v copy \
-c:a aac \
-b:a 32k \
-ar 44100 \
-f mp4 \
-t ${VIDEO_DURATION} \
${VIDEO_FILE} &
fi
# Get process ID
pid=$!
# Wait for process to finish or get terminated signal
wait || pkill rpicam-vid; exit 130