From 649e886428f9bbec49d258bc32542f7f5cd26084 Mon Sep 17 00:00:00 2001 From: JLKwong Date: Wed, 24 Mar 2021 14:08:47 -0700 Subject: [PATCH 1/6] add camera convert (fixes #2116) --- modules/camera.sh | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/modules/camera.sh b/modules/camera.sh index b5a1f4e743..ed4ef259df 100644 --- a/modules/camera.sh +++ b/modules/camera.sh @@ -55,6 +55,36 @@ function camera { fi ;; + "convert") + local frames percent status + checkargn $# 4 + inputFile="$1" + outputFile="$2" + if [ -e "$inputFile" ] && [[ "$outputFile" != "" ]]; then + frames=$(ffprobe -v error -select_streams v:0 -show_entries stream=nb_frames -of default=nokey=1:noprint_wrappers=1 $inputFile) + if ! [[ $frames =~ ^[0-9]+$ ]]; then + frames="" + fi + while read -r line || { status=$line && break; }; do + if [ -z $frames ]; then + echo -ne "conversion running"\\r + else + percent=$(bc <<< "scale=2; ($line / $frames) * 100") + echo -ne " [$percent%] completed"\\r + fi + done < <(ffmpeg -y -i $inputFile $outputFile -loglevel error -hide_banner -max_error_rate 0.0 -progress - -nostats | grep -oP --line-buffered '(?<=frame=)[0-9]+'; printf "${PIPESTATUS[0]}") + # be careful not to delete line-buffered + if [ "$status" = 0 ]; then + echo "$inputFile has been successfully converted to $outputFile" + else + echo "conversion unsuccessful" + fi + else + log_and_exit1 "Error: invalid arguments" + fi + } + ;; + "record") mkdir -p ${viddir} if ! grep -q "start_x=1" ${config} ; then @@ -122,7 +152,7 @@ function camera { function camera_help { echo - echo "Usage: $BASENAME camera [on|off|detect|capture|record]" + echo "Usage: $BASENAME camera [on|off|detect|capture|convert|record]" echo echo "Example:" echo " $BASENAME camera" @@ -142,6 +172,12 @@ function camera_help { echo " $BASENAME camera capture" echo " Camera is capturing and storing a time-stamped photo in ${directory}." echo + echo " $BASENAME camera convert " + echo + echo " Example:" + echo " $BASENAME convert video.mp4 video.mp3" + echo " convert video mp4 file to mp3 format" + echo echo " $BASENAME camera record" echo " Camera is recording ${length} seconds of video and storing a time-stamped ${vidtype} video in ${viddir}." echo From c7eafa45f8dc5a1c3c0f105c63269db0e04cfd9a Mon Sep 17 00:00:00 2001 From: JLKwong Date: Wed, 24 Mar 2021 14:25:28 -0700 Subject: [PATCH 2/6] add camera convert (fixes #2116) --- modules/camera.sh | 45 ++++++++++++++++++++++----------------------- 1 file changed, 22 insertions(+), 23 deletions(-) diff --git a/modules/camera.sh b/modules/camera.sh index ed4ef259df..58d2fe5273 100644 --- a/modules/camera.sh +++ b/modules/camera.sh @@ -56,33 +56,32 @@ function camera { ;; "convert") - local frames percent status - checkargn $# 4 - inputFile="$1" - outputFile="$2" - if [ -e "$inputFile" ] && [[ "$outputFile" != "" ]]; then - frames=$(ffprobe -v error -select_streams v:0 -show_entries stream=nb_frames -of default=nokey=1:noprint_wrappers=1 $inputFile) - if ! [[ $frames =~ ^[0-9]+$ ]]; then - frames="" - fi - while read -r line || { status=$line && break; }; do - if [ -z $frames ]; then - echo -ne "conversion running"\\r - else - percent=$(bc <<< "scale=2; ($line / $frames) * 100") - echo -ne " [$percent%] completed"\\r - fi - done < <(ffmpeg -y -i $inputFile $outputFile -loglevel error -hide_banner -max_error_rate 0.0 -progress - -nostats | grep -oP --line-buffered '(?<=frame=)[0-9]+'; printf "${PIPESTATUS[0]}") - # be careful not to delete line-buffered - if [ "$status" = 0 ]; then - echo "$inputFile has been successfully converted to $outputFile" + local frames percent status + checkargn $# 4 + inputFile="$1" + outputFile="$2" + if [ -e "$inputFile" ] && [[ "$outputFile" != "" ]]; then + frames=$(ffprobe -v error -select_streams v:0 -show_entries stream=nb_frames -of default=nokey=1:noprint_wrappers=1 $inputFile) + if ! [[ $frames =~ ^[0-9]+$ ]]; then + frames="" + fi + while read -r line || { status=$line && break; }; do + if [ -z $frames ]; then + echo -ne "conversion running"\\r else - echo "conversion unsuccessful" + percent=$(bc <<< "scale=2; ($line / $frames) * 100") + echo -ne " [$percent%] completed"\\r fi + done < <(ffmpeg -y -i $inputFile $outputFile -loglevel error -hide_banner -max_error_rate 0.0 -progress - -nostats | grep -oP --line-buffered '(?<=frame=)[0-9]+'; printf "${PIPESTATUS[0]}") + # be careful not to delete line-buffered + if [ "$status" = 0 ]; then + echo "$inputFile has been successfully converted to $outputFile" else - log_and_exit1 "Error: invalid arguments" + echo "conversion unsuccessful" fi - } + else + log_and_exit1 "Error: invalid arguments" + fi ;; "record") From d95d3fdf4869358c273b3c5f27938a75b6cbb567 Mon Sep 17 00:00:00 2001 From: JLKwong Date: Wed, 24 Mar 2021 14:30:20 -0700 Subject: [PATCH 3/6] add camera convert (fixes #2116) --- modules/camera.sh | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/modules/camera.sh b/modules/camera.sh index 58d2fe5273..0ff5417d8e 100644 --- a/modules/camera.sh +++ b/modules/camera.sh @@ -1,7 +1,7 @@ function camera { local directory timestamp config configtemp savetype checkrpi - checkargn $# 2 + checkargn $# 4 directory="/home/pi/Pictures/" viddir="/home/pi/Videos/" timestamp=$(date +"%Y%m%d-%H%M%S") @@ -13,6 +13,7 @@ function camera { case "$1" in "") + checkargn $# 2 if grep -q "start_x=1" ${config} ; then echo "Config file has Camera settings which are currently enabled. Use \"$BASENAME help camera\" for more commands." else @@ -21,6 +22,7 @@ function camera { ;; "on") + checkargn $# 2 if ! grep -q "start_x=1" ${config} ; then raspi-config nonint do_camera 0 echo "Camera settings have been enabled. A reboot is needed in order to use the camera." @@ -34,6 +36,7 @@ function camera { ;; "off") + checkargn $# 2 if grep -q "start_x=1" ${config} ; then raspi-config nonint do_camera 1 echo "Camera has been disabled. A reboot is needed in order to use the camera." @@ -46,6 +49,7 @@ function camera { ;; "capture") + checkargn $# 2 mkdir -p ${directory} if ! grep -q "start_x=1" ${config} ; then log_and_exit1 "Error: you need to enable AND reboot first in order to take pictures." @@ -57,7 +61,6 @@ function camera { "convert") local frames percent status - checkargn $# 4 inputFile="$1" outputFile="$2" if [ -e "$inputFile" ] && [[ "$outputFile" != "" ]]; then @@ -85,6 +88,7 @@ function camera { ;; "record") + checkargn $# 2 mkdir -p ${viddir} if ! grep -q "start_x=1" ${config} ; then log_and_exit1 "Error: you need to enable AND reboot first in order to take pictures." @@ -113,6 +117,7 @@ function camera { ;; "detect") + checkargn $# 2 mkdir -p ${directory} if ! grep -q "start_x=1" ${config} ; then log_and_exit1 "Error: you need to enable AND reboot first in order to take pictures." @@ -143,6 +148,7 @@ function camera { ;; *) + checkargn $# 2 echo "Error: The only supported options are 'on', 'off', 'detect, 'capture', and 'record'." camera_help ;; From f210152246e0182069e15df2a7033965abc2822a Mon Sep 17 00:00:00 2001 From: JLKwong Date: Wed, 24 Mar 2021 14:39:01 -0700 Subject: [PATCH 4/6] add camera convert (fixes #2116) --- README.md | 5 +++-- _treehouses | 3 ++- modules/convert.sh | 38 -------------------------------------- modules/help.sh | 5 +++-- 4 files changed, 8 insertions(+), 43 deletions(-) delete mode 100644 modules/convert.sh diff --git a/README.md b/README.md index 21b732a74f..edf2c4552a 100644 --- a/README.md +++ b/README.md @@ -140,7 +140,9 @@ memory [total|used|free] [gb|mb] displays the total memory of the devic storage [total|used|free] [gb|mb|kb] displays the total storage of the device, the storage used as well as the available free storage temperature [celsius|fahrenheit|kelvin] displays raspberry pi's CPU temperature speedtest tests internet download and upload speed -camera [on|off|detect|capture|record] enables and disables camera, detects camera module version, captures photo, records video +camera [on|off|detect|capture|record] enables and disables camera, detects camera + [convert] module version, captures photo, records video, + and converts video and audio files picture [file|url] displays a picture in the terminal cron [list|add|delete|deleteall] adds, deletes a custom cron job or deletes, lists all cron jobs [0W|tor|timestamp] adds premade cron job (or removes it if already active) @@ -154,7 +156,6 @@ log <0|1|2|3|4|show|max> gets/sets log level and shows log blocker <0|1|2|3|4||max> website blocking levels using /etc/hosts sdbench displays read and write speed of micro SD card inspire [fact|joke|qotd|random] displays quote based on user input -convert converts video and audio files gpio displays raspberry pi model and corresponding GPIO ports changelog [view|compare] displays the most recent changes to treehouses magazines downloads specific magazine issue as a pdf based on user input diff --git a/_treehouses b/_treehouses index 38d8a2feb9..925b1a0c91 100644 --- a/_treehouses +++ b/_treehouses @@ -40,9 +40,11 @@ treehouses button bluetooth treehouses button off treehouses camera treehouses camera capture +treehouses camera convert treehouses camera detect treehouses camera off treehouses camera on +treehouses camera record treehouses changelog treehouses changelog compare treehouses changelog view @@ -57,7 +59,6 @@ treehouses container treehouses container balena treehouses container docker treehouses container none -treehouses convert treehouses coralenv demo-always-on treehouses coralenv demo-off treehouses coralenv demo-on diff --git a/modules/convert.sh b/modules/convert.sh deleted file mode 100644 index 4b6662a5a4..0000000000 --- a/modules/convert.sh +++ /dev/null @@ -1,38 +0,0 @@ -function convert { - local frames percent status - checkargn $# 2 - inputFile="$1" - outputFile="$2" - if [ -e "$inputFile" ] && [[ "$outputFile" != "" ]]; then - frames=$(ffprobe -v error -select_streams v:0 -show_entries stream=nb_frames -of default=nokey=1:noprint_wrappers=1 $inputFile) - if ! [[ $frames =~ ^[0-9]+$ ]]; then - frames="" - fi - while read -r line || { status=$line && break; }; do - if [ -z $frames ]; then - echo -ne "conversion running"\\r - else - percent=$(bc <<< "scale=2; ($line / $frames) * 100") - echo -ne " [$percent%] completed"\\r - fi - done < <(ffmpeg -y -i $inputFile $outputFile -loglevel error -hide_banner -max_error_rate 0.0 -progress - -nostats | grep -oP --line-buffered '(?<=frame=)[0-9]+'; printf "${PIPESTATUS[0]}") - # be careful not to delete line-buffered - if [ "$status" = 0 ]; then - echo "$inputFile has been successfully converted to $outputFile" - else - echo "conversion unsuccessful" - fi - else - log_and_exit1 "Error: invalid arguments" - fi -} - -function convert_help { - echo - echo "Usage: $BASENAME convert " - echo - echo "Example:" - echo " $BASENAME convert video.mp4 video.mp3" - echo " convert video mp4 file to mp3 format" -} - diff --git a/modules/help.sh b/modules/help.sh index f3ee201f0e..8e70fea0f0 100644 --- a/modules/help.sh +++ b/modules/help.sh @@ -116,7 +116,9 @@ memory [total|used|free] [gb|mb] displays the total memory of the devic storage [total|used|free] [gb|mb|kb] displays the total storage of the device, the storage used as well as the available free storage temperature [celsius|fahrenheit|kelvin] displays raspberry pi's CPU temperature speedtest tests internet download and upload speed -camera [on|off|detect|capture|record] enables and disables camera, detects camera module version, captures photo, records video +camera [on|off|detect|capture|record] enables and disables camera, detects camera + [convert] module version, captures photo, records video, + and converts video and audio files picture [file|url] displays a picture in the terminal cron [list|add|delete|deleteall] adds, deletes a custom cron job or deletes, lists all cron jobs [0W|tor|timestamp] adds premade cron job (or removes it if already active) @@ -130,7 +132,6 @@ log <0|1|2|3|4|show|max> gets/sets log level and shows log blocker <0|1|2|3|4||max> website blocking levels using /etc/hosts sdbench displays read and write speed of micro SD card inspire [fact|joke|qotd|random] displays quote based on user input -convert converts video and audio files gpio displays raspberry pi model and corresponding GPIO ports changelog [view|compare] displays the most recent changes to treehouses magazines downloads specific magazine issue as a pdf based on user input From b05782412b816a34331ce888558fe81246dd27ca Mon Sep 17 00:00:00 2001 From: JLKwong Date: Wed, 24 Mar 2021 15:31:18 -0700 Subject: [PATCH 5/6] fix code climate errors (fixes #2116) --- modules/camera.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/modules/camera.sh b/modules/camera.sh index 0ff5417d8e..ee4b5c81b6 100644 --- a/modules/camera.sh +++ b/modules/camera.sh @@ -75,7 +75,7 @@ function camera { percent=$(bc <<< "scale=2; ($line / $frames) * 100") echo -ne " [$percent%] completed"\\r fi - done < <(ffmpeg -y -i $inputFile $outputFile -loglevel error -hide_banner -max_error_rate 0.0 -progress - -nostats | grep -oP --line-buffered '(?<=frame=)[0-9]+'; printf "${PIPESTATUS[0]}") + done < <(ffmpeg -y -i $inputFile $outputFile -loglevel error -hide_banner -max_error_rate 0.0 -progress - -nostats | grep -oP --line-buffered '(?<=frame=)[0-9]+'; printf "" "${PIPESTATUS[0]}") # be careful not to delete line-buffered if [ "$status" = 0 ]; then echo "$inputFile has been successfully converted to $outputFile" @@ -130,7 +130,7 @@ function camera { echo "Camera Module v1 detected." rm ${directory}$BASENAME-${timestamp}.png elif file ${directory}$BASENAME-${timestamp}.png | grep -q "2582 x 1933" ; then - echo "Coral Camera Module detected." + echo "Coral Camera Module detected." rm ${directory}$BASENAME-${timestamp}.png elif file ${directory}$BASENAME-${timestamp}.png | grep -q "3280 x 2464" ; then echo "Camera Module v2 detected." @@ -140,7 +140,7 @@ function camera { rm ${directory}$BASENAME-${timestamp}.png else echo "Unknown Camera detected. Something went wrong!" - file ${directory}$BASENAME-${timestamp}.png + file ${directory}$BASENAME-${timestamp}.png rm ${directory}$BASENAME-${timestamp}.png fi fi From 03e61736162f0c9f27821109b404ff05e3cd7e58 Mon Sep 17 00:00:00 2001 From: JLKwong Date: Wed, 24 Mar 2021 15:40:43 -0700 Subject: [PATCH 6/6] fix code climate errors (fixes #2116) --- modules/camera.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/camera.sh b/modules/camera.sh index ee4b5c81b6..e7ef9bda3b 100644 --- a/modules/camera.sh +++ b/modules/camera.sh @@ -75,7 +75,7 @@ function camera { percent=$(bc <<< "scale=2; ($line / $frames) * 100") echo -ne " [$percent%] completed"\\r fi - done < <(ffmpeg -y -i $inputFile $outputFile -loglevel error -hide_banner -max_error_rate 0.0 -progress - -nostats | grep -oP --line-buffered '(?<=frame=)[0-9]+'; printf "" "${PIPESTATUS[0]}") + done < <(ffmpeg -y -i $inputFile $outputFile -loglevel error -hide_banner -max_error_rate 0.0 -progress - -nostats | grep -oP --line-buffered '(?<=frame=)[0-9]+'; printf "${PIPESTATUS[0]}") # be careful not to delete line-buffered if [ "$status" = 0 ]; then echo "$inputFile has been successfully converted to $outputFile"