diff --git a/termux-camera-record b/termux-camera-record new file mode 100644 index 0000000..ea9768a --- /dev/null +++ b/termux-camera-record @@ -0,0 +1,96 @@ +#!@TERMUX_PREFIX@/bin/env bash +## Example usage: 'cams.bash 0 255 16 2048 r 90 2' +## Semantic usage: 'cams.bash [0 [255 [16 [2048 [r[otate] [90 [2]]]]]]]' +## Please run 'pkg install ffmpeg imagemagick termux-api' before running this script. Also ensure that Termux-api is installed, which is available at this https://github.com/termux/termux-api/actions/workflows/debug_build.yml webpage. +## VLC APK can be downloaded from these https://www.videolan.org/vlc/download-android.html and https://get.videolan.org/vlc-android/3.3.4/ webpages. +## Options in addition to image rotation can be added by editing this script at the magick rotation command; The command line options for magick are listed at this https://imagemagick.org/script/command-line-options.php webpage. +## All script arguments are listed below, including their defaults: +set -eu +[[ -n "${1:-}" ]] && [[ "${1//-}" = [Hh]* ]] && { printf '%s\n' "Help for script '${0##*/}':" && TSFILE="($(grep '##\ ' "$0"))" && printf '%s\n%s\n' "$(for i in "${TSFILE[@]}" ; do sed -e "s/##/ /" <<< "$i" | cut -f 2 ; done | cut -d\) -f1)" "Help for script '${0##*/}': DONE" ; exit ; } +CAMID=${1:-0} ## [1] default 0: One camera 0 1 2 3 4 5 6 7 id, +FRAMECTOT=${2:-255} ## [2] default 256: Video frame count total, +FRAMERATE=${3:-16} ## [3] default 16: Video 2 4 8 16 32 frames per second rendered in the mpg file, +THRESHOLDSET=${4:-2048} ## [4] default 2048: Byte difference 64 128 256 512 1024 2048 4096 8192 16384 32768 65536 between last two picture frames taken; Used for motion detection. The greater the number, the lesser the motion sensitivity. Camera resolution also affects this argument, +_CAMS_ () { +FRAMECOUNT=0 +while [ "$FRAMECOUNT" -le "$FRAMECTOT" ] +do +FRAMENAME="$(printf '%s.%04d.jpg' "${PWD##*/}$CAMID" "$FRAMECOUNT")" +printf '%s\n%s\n' "I $((FRAMECOUNT + 1))/$((FRAMECTOT + 1)) frame count: ${THRESHOLDSET:-} threshold set" "I $CAMID camid taking picture $FRAMENAME" +sleep 0.0"$(shuf -i 101-420 -n 1)" +"${PREFIX:-/data/data/com.termux/files/usr}"/libexec/termux-api CameraPhoto --es camera "$CAMID" --es file "$PWD/$FRAMENAME" +_ISZERO_ "$@" +done +} +_CHECKMOTIONDIFF_() { +if [ "$FRAMECOUNT" -ne 0 ] +then +THRESHOLD="$((LASTZERO - ISZERO))" +THRESHOLD="${THRESHOLD//-}" +if [ "$THRESHOLD" -le "$THRESHOLDSET" ] +then +printf '%s\n%s\n' "D $THRESHOLD threshold: deleting file $FRAMENAME" "I frame $FRAMENAME: Threshold set to $THRESHOLDSET" +rm -f "$FRAMENAME" +else +printf '%s\n' "S $THRESHOLD threshold: saving file $FRAMENAME" +FRAMECOUNT="$((FRAMECOUNT + 1))" +fi +else +FRAMECOUNT="$((FRAMECOUNT + 1))" +fi +} +_ISZERO_ () { +if [ -n "${ISZERO:-}" ] +then +LASTZERO="$ISZERO" +fi +sleep 0.0"$(shuf -i 420-640 -n 1)" +ISZERO="$(find . -type f -name "$FRAMENAME" -printf "%s" || ls -al "$FRAMENAME" | awk '{print $5}')" +if [ "$ISZERO" -eq 0 ] +then +if [ "$FRAMECOUNT" -eq 0 ] +then +printf '%s\n%s\n' "E could not begin shoot: ERROR" "Please check for issues and run '${0##*/}' again: Exiting..." +rm -f "$FRAMENAME" +exit 1 +else +printf '%s\n' "W deleting zero size file $FRAMENAME" +rm -f "$FRAMENAME" +fi +else +_CHECKMOTIONDIFF_ +fi +_MAGICKCK_ "$@" +} +_MAGICKCK_ () { +if [ -e "$FRAMENAME" ] +then +printf '%s' "I checking file $FRAMENAME for errors: " +MAGICKCK="$(nice -n 20 magick identify "$FRAMENAME" 2>&1 ||:)" +if grep -i error <<< "$MAGICKCK" +then +rm -f "$FRAMENAME" +FRAMECOUNT="$((FRAMECOUNT - 1))" +printf '%s\n%s\n%s\n' "DONE" "D deleted file $FRAMENAME: ERROR" "I redoing file $FRAMENAME: ERROR" +else +printf '%s\n' "DONE" +if [ -n "${5:-}" ] +then +if [[ "${5//-}" = [Rr]* ]] ## [5] default no rotation: R|r[otate]: you can use R or r to activate rotation which is preset to 90° rotation. The rotation option can also be applied as a 90° rotation preset in arguments 1 and 2 in which case their default values will become presets, +then +printf '%s' "I rotating file $FRAMENAME by ${6:-90}°: " ## [6] default 90°: Enter desired picture rotation angle in digits if you do not want to use the 90° default rotation, +nice -n 20 magick "$FRAMENAME" -rotate "${6:-90}" "$FRAMENAME".jpg +mv "$FRAMENAME".jpg "$FRAMENAME" +printf '%s\n' "DONE" +fi +fi +fi +fi +} +_MEFFMPEG_ () { printf '%s\n' "I making $VIDEOPREFIX$TIMESTAMP.mp4: This job will complete in the background..." && nice -n 20 ffmpeg -framerate "$FRAMERATE" -i "$VIDEOPREFIX"%04d.jpg "$VIDEOPREFIX$TIMESTAMP".mp4 && { ls -al "$VIDEOPREFIX$TIMESTAMP".mp4 && printf '%s\n' "I done making $VIDEOPREFIX$TIMESTAMP.mp4: DONE" ; } || printf '%s\n' "E creating file $VIDEOPREFIX$TIMESTAMP.mp4: ERROR" ; } +_CAMS_ "$@" +VIDEOPREFIX="${FRAMENAME%%.*}." +TIMESTAMP="$(date +%Y%m%d%H%M%S)" +_MEFFMPEG_ & +sleep "${7:-2}.$(shuf -i 420-640 -n 1)" ## [7] default ~2 seconds: Time before script exits; Program ffmpeg will continue to run on in the background until its job of producing an mp4 file ends. This sleep is so the jpg files can be read by ffmpeg when this script is used within a loop. +# termux-camera-record EOF