Files
video-processing/capture_video.sh

126 lines
3.1 KiB
Bash
Executable File

#!/bin/bash
if [[ "$1" == *help ]]; then
echo "Example usage: ${0} [-duration 10] [-sound plughw:1,0]"
echo "optional -duration is in seconds"
echo "optional -sound values: [none, anullsrc, plughw:1,0]"
echo "default -sound is none i.e. no 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="none"
PEERTUBE_LIVE="rtmp://peertube.virtonline.eu:1935/live/LIVE-STREAM-KEY"
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}" = "none" ]; then
# ffmpeg params without audio
ffmpeg -hide_banner \
-thread_queue_size 1024 \
-t ${VIDEO_DURATION} \
-i pipe:0 \
-c:v copy \
-f tee -map 0:0 -flags +global_header -flvflags no_duration_filesize \
"[f=mp4:movflags=+faststart:onfail=ignore]${VIDEO_FILE}|[f=fifo:fifo_format=flv:drop_pkts_on_overflow=1:attempt_recovery=1:recovery_wait_time=1:use_wallclock_as_timestamps=1]${PEERTUBE_LIVE}" \
&
elif [ "${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