Repository navigation
Expand file tree
/
Copy pathsilentpcmrm
More file actions
executable file
·107 lines (104 loc) · 3.45 KB
/
Copy pathsilentpcmrm
File metadata and controls
executable file
·107 lines (104 loc) · 3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/bin/bash
# usage: silentpcmrm [-d] [-v] [-l] [-t THRESHOLD] INPUT
# -d debug mode: actions are listed but not carried out
# -v verbose mode: actions are carried out and listed
# -l list mode: files that will be deleted are listed, surrounded by single quotes. This allows the list to sent to `xargs` for example, to open in an audio editor to double check if there's anything worth keeping.
# -n no colours: don't use colours when printing to the screen
# -t THRESHOLD set the threshold below which a PCM file is considered silent
# INPUT list of files to check for silence
if ! command -v sox &> /dev/null; then
echo "sox could not be found, and is required";
exit;
fi
cols=$(tput cols)
threshold=0.0001;
while getopts ":dlnt:v" opt; do
case $opt in
d) debug=yes;;
l) list=yes;;
n) nocolours=yes;;
t) threshold=$OPTARG;;
v) verbose=yes;;
\?) echo "Invalid option: -$OPTARG" >&2; exit 1;;
:) echo "Option -$OPTARG needs an argument" >&2; exit 1;;
esac
done
shift $(( OPTIND - 1 ))
if [ "${nocolours}" != 'yes' ]; then
bold=$(tput bold);
green=$(tput setaf 2);
grey=$(tput dim)$(tput setaf 7);
lblack=$(tput bold)$(tput setaf 0);
lred=$(tput bold)$(tput setaf 1);
red=$(tput setaf 1);
reset=$(tput sgr0);
white=$(tput setaf 7);
yellow=$(tput setaf 3);
fi
# alert debug mode
if [ "${debug}" == 'yes' ]; then
echo "${green}debug mode";
fi
# set print mode
if [[ "${verbose}" == 'yes' || "${debug}" == 'yes' ]]; then
print=yes;
fi
# print threshold
if [ "${print}" == 'yes' ]; then
echo "${green}using ${yellow}${threshold}${green} as threshold below which a file is considered 'silent'${reset}"; echo;
padlimit=${cols};
pad=$(printf '%*s' "$padlimit");
pad=${pad// /.};
fi
for i in "${@}"; do
# print file name
if [ "${print}" == 'yes' ]; then
printf '%s' "${i}";
fi
# check for zero length file
if [ ! -s "${i}" ]; then
if [ "${print}" = 'yes' ]; then
echo " ${yellow}${i}${reset} is ${blue}zero-length${reset}";
fi
if [ "${debug}" == 'yes' ]; then
printf '%s\n' " ${green}debug mode: ${red}remove ${bold}${white}${i}${reset} ";
else
if [ "${verbose}" == 'yes' ]; then
printf '%s\n' " ${red}Deleting ${bold}${white}${i}${reset} ";
fi
rm -f "${i}";
fi
else
# read amplitude of file
amplitude=$(sox "${i}" -n stat 2>&1 | grep "Maximum amplitude" | cut -d ":" -f 2 | sed 's/ //g');
# check if amplitude is below threshold
if [[ $(echo "if (${amplitude} > ${threshold}) 1 else 0" | bc) -eq 0 ]]; then
# print amplitude
if [ "${print}" == 'yes' ]; then
amp="${reset}Max amplitude: ${red}${amplitude}${reset}";
printf ' %*.*s %s\n' 0 $(( ${cols} - ${#i} - ${#amp} - 2 + ${#reset} * 5 )) "${lblack}${pad}${reset}" "${amp}";
fi
if [ "${debug}" == 'yes' ]; then
# debug mode
printf '%s' " ${green}debug mode: ${red}remove ${bold}${white}${i}${reset} ";
printf '%s\n' "[${yellow} $(du -h "${i}" | cut -f1) ${reset}]";
elif [ "${list}" == 'yes' ]; then
# list mode
echo "'${i}'";
else
if [ "${verbose}" == 'yes' ]; then
printf '%s' " ${red}Deleting ${bold}${white}${i}${reset} ";
printf '%s\n' "[${yellow} $(du -h "${i}" | cut -f1) ${reset}]";
# echo " ${red}Deleting ${bold}${white}${i}${reset}";
fi
rm -f "${i}";
fi
else
# print amplitude
if [ "${print}" == 'yes' ]; then
amp="${reset}Max amplitude: ${green}${amplitude}${reset}";
printf ' %*.*s %s\n' 0 $(( ${cols} - ${#i} - ${#amp} - 2 + ${#reset} * 5 )) "${lblack}${pad}${reset}" "${amp}";
fi
fi
fi
done