Skip to content

Commit

Permalink
Consolidate into single script
Browse files Browse the repository at this point in the history
  • Loading branch information
Erudition committed Mar 7, 2024
1 parent d369b66 commit 8a2b82e
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 131 deletions.
2 changes: 1 addition & 1 deletion tidy-recordings/DOCS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## What it does

See the [whole shell script](https://github.com/Erudition/home-assistant-addons/blob/main/tidy-recordings/rootfs/usr/bin/trimcompress.bash).
See the [whole shell script](https://github.com/Erudition/home-assistant-addons/blob/main/tidy-recordings/rootfs/etc/services.d/example/run).


## How to use
Expand Down
2 changes: 1 addition & 1 deletion tidy-recordings/config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# https://developers.home-assistant.io/docs/add-ons/configuration#add-on-config
name: Tidy Recordings
version: "1.972"
version: "1.98"
slug: tidy-recordings
description: Auto compress, remove silence, and organize the Assist Pipeline recordings
url: "https://github.com/Erudition/home-assistant-addons/tree/main/tidy-recordings"
Expand Down
125 changes: 122 additions & 3 deletions tidy-recordings/rootfs/etc/services.d/example/run
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,134 @@
# ==============================================================================


## Run your program
share_folder="/share"
recording_folder="$share_folder/assist_pipeline"
processed_recording_folder="$share_folder/assist_pipeline/$(bashio::config 'processed_recording_folder')"
processing_folder="$share_folder/assist_pipeline/processing"
max_overall_disk_usage_percent=$(bashio::config 'max_overall_disk_usage_percent')
max_folder_size_megabytes=$(bashio::config 'max_folder_size_megabytes')
output_segment_size=$(bashio::config 'output_segment_size') #seconds

/usr/bin/trimcompress.bash $(bashio::config 'processed_recording_folder') $(bashio::config 'max_overall_disk_usage_percent') $(bashio::config 'max_folder_size_megabytes') $(bashio::config 'output_segment_size')
trap "exit" INT # allow quit with Ctrl+C

shopt -s lastpipe # allow while loop to run in current shell so we get access to counters


export FFREPORT=file=$processing_folder/%t.log:level=32 # save ffmpeg output

function getTotalDiskUsage {
# https://stackoverflow.com/a/45667842
overall_disk_usage_percent=$(df -Ph $recording_folder | awk 'NR == 2{print $5+0}')
# https://superuser.com/a/1044209
recording_folder_size_megabytes=$(du -sxbm $recording_folder | awk '{print $1}')
recording_folder_size_human_units=$(du -sxbh $recording_folder | awk '{print $1}')

if [ $overall_disk_usage_percent -gt $max_overall_disk_usage_percent ]
then
bashio::log.warning "Total disk usage is $overall_disk_usage_percent% which is over the max you set, $max_overall_disk_usage_percent%!"
else
bashio::log.info "Total disk usage is $overall_disk_usage_percent% which is under the max you set, $max_overall_disk_usage_percent%"
fi

if [ $recording_folder_size_megabytes -gt $max_folder_size_megabytes ]
then
bashio::log.warning "Recording folder size is $recording_folder_size_human_units which is over $max_folder_size_megabytes megabytes!"
else
bashio::log.info "Recording folder size is $recording_folder_size_human_units which is under the max you set, $max_folder_size_megabytes MB"
fi
}


function deleteOldestFilesUntilUnderLimit {
deleted_count=0
#echo "Deleting oldest .wav and .opus files until overall disk usage is under the limit."
find $recording_folder -type f \( -iname "*.wav" -o -iname "*.opus" \) -print0 | xargs -0 ls -1rt | while read -r file; do
if [ $(df -Ph $recording_folder | awk 'NR == 2{print $5+0}') -ge $max_overall_disk_usage_percent ] || [ $(du -sxbm $recording_folder | awk '{print $1}') -ge $max_folder_size_megabytes ]
then
rm -f "$file"
((deleted_count++))
else
bashio::log.green "Deleted $deleted_count oldest recordings. Folder is small enough to continue."
break
fi
done
}


# TODO: segment timestamps currently assume no silence removed
function recordingConvertTrimCompress {
file_basename=$(basename "$1" .wav)
file_date=$(date -r "$1" '+%s') # unix time
wav_size_megabytes=$(du -sxbm "$1" | awk '{print $1}')
wav_size_kilobytes=$(du -sxb "$1" | awk '{print $1}')
echo
echo "Processing $wav_size_megabytes MB recording: $1"
ffprobe "$1" 2>&1 | grep "Duration"

/usr/bin/time -f "Processed in %E, CPU: %P (out of $(nproc)00 %% )" \
ffmpeg -loglevel error -hide_banner -y -i "$1" -nostdin -c:a libopus -ac 1 -b:a 24K -af silenceremove=stop_duration=1:window=0:detection=peak:stop_mode=all:start_mode=all:stop_periods=-1:stop_threshold=-30dB -f segment -segment_time $output_segment_size -reset_timestamps 1 "$processing_folder/%d.opus"

cd $processing_folder
all_segments_size_megabytes=$(du -sxbm "$processing_folder" | awk '{print $1}')
all_segments_size_kilobytes=$(du -sxb "$processing_folder" | awk '{print $1}')
saved_megabytes=$(( wav_size_megabytes - all_segments_size_megabytes))
saved_kilobytes=$(( wav_size_kilobytes - all_segments_size_kilobytes))
# using bc allows floating point division and prevents div by zero crash
saved_percent=$( echo "scale=2; x=$saved_kilobytes / $wav_size_kilobytes; scale=0; 100 - (x * 100)" | bc)
echo "Done. New Total: $all_segments_size_megabytes MB, Saved: $saved_megabytes MB ($saved_percent% smaller)"
for processed_filename in *.opus
do
segment_number=$(basename "$processed_filename" .opus)
#echo "Moving finished $processed_filename (segment #$segment_number) with $file_date"
segment_timestamp=$(( (output_segment_size * segment_number) + file_date))
new_timestamp=$(date -d@"$segment_timestamp" '+%F %T')
date_only=$(date -d@"$segment_timestamp" '+%Y-%m-%d')
#echo "Segment #$segment_number has date $new_timestamp"
mkdir -p "$processed_recording_folder/$date_only"
mv $processed_filename "$processed_recording_folder/$date_only/$new_timestamp-$file_basename.opus"
done
rm -f "$1"
#echo "Compressed, trimmed silence, and split into the the recordings folder, and deleted original: $1"
}

function convertAllWaveFiles {
# https://stackoverflow.com/a/26349346
find $recording_folder -name '*.wav' -print0 | while IFS= read -r -d '' file; do recordingConvertTrimCompress "$file"; done
}




# START ---------------------------------


mkdir -p $processed_recording_folder
mkdir -p $processing_folder
rm -rf $processing_folder/*

getTotalDiskUsage
before_recording_folder_size_human_units=$recording_folder_size_human_units

deleteOldestFilesUntilUnderLimit
convertAllWaveFiles


# FINISH --------------------------------

#getTotalDiskUsage
echo
bashio::log.green "Tidy Complete. Recording folder went from " $before_recording_folder_size_human_units " to " $recording_folder_size_human_units

# Delete all empty folders
find $recording_folder -type d -empty -delete

# Delete processing folder to show we're not running anymore
rm -rf $processing_folder


if bashio::config.false 'should_rerun' ;
then
bashio::log.info "Addon run complete. Stopping now, since you have not configured automatic re-run."
echo "${APP_EXIT_CODE}" > /run/s6-linux-init-container-results/exitcode # guess we have to do this or it won't actually stop
exec /run/s6/basedir/bin/halt
else
bashio::log.info "Addon run complete. Sleeping for $(bashio::config 'sleep_duration')."
Expand Down
126 changes: 0 additions & 126 deletions tidy-recordings/rootfs/usr/bin/trimcompress.bash

This file was deleted.

0 comments on commit 8a2b82e

Please sign in to comment.