-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathreflac
executable file
·162 lines (142 loc) · 4.3 KB
/
reflac
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/usr/bin/env bash
set -euo pipefail
shopt -s dotglob
IFS=$'\n\t'
VERSION=2.0.1
VERBOSE=0
RECURSIVE=0
NOACTION=0
SYNC=1
FLAC_LEVEL=5 # Default FLAC compression level
SELF="$(readlink -f "$0")"
OPTS=$(getopt -o 012345678hnrsvV \
-l best,fast,help,no-action,no-sync,recursive,verbose,version \
-n reflac -- "$@")
TMPDIR="$(mktemp --tmpdir -d reflac.XXXXXXXXXX)"
eval set -- "$OPTS"
trap "exit 1" INT
cleanup()
{
rmdir "$TMPDIR"
exit "$1"
}
usage()
{
cat <<EOF
‘reflac’ recompresses FLAC files while maintaining tags and file names.
Usage: reflac [OPTION]... [--] DIRECTORY...
-h --help Displays this help text
-0 --fast Use the fastest, but worst, compression possible.
-1..-7 Adjust FLAC compression between these standard ranges.
The default is -5, the same as for flac itself.
-8 --best Use the slowest, but best, compression possible.
-n --no-action Do not recompress. With --verbose, displays a list of
files that would be processed.
-r --recursive Recurse into directories.
-s --no-sync Do not synchronize file data. Will return faster, with
the potential danger to lose your files in a system crash.
-v --verbose Increases the verbosity. Use once to display the FLACs
currently being processed, use twice for the full ‘flac’
output.
-V --version Displays the version of this program
DIRECTORY should point ‘reflac’ to somewhere that contains *.flac
files. Optionally terminate the argument list with -- so that any
possible directory names don’t get misinterpreted as arguments.
EOF
cleanup 0
}
recompress()
{
for flac in *.flac; do
if [ $VERBOSE -eq 1 ]; then
echo " $flac"
fi
if [ $NOACTION -eq 0 ]; then
if [ $VERBOSE -gt 1 ]; then
flac -$FLAC_LEVEL \
--output-name="${TMPDIR}/$(basename -- "$flac")" \
--verify -- \
"$flac"
else
flac --silent -$FLAC_LEVEL \
--output-name="${TMPDIR}/$(basename -- "$flac")" \
--verify -- \
"$flac"
fi
mv -f -- "$TMPDIR/$flac" "$flac.new"
if [ $SYNC -eq 1 ]; then
sync "$flac.new"
mv -f "$flac.new" "$flac"
sync "$flac"
else
mv -f "$flac.new" "$flac"
fi
fi
done
}
while true ; do
case "$1" in
-0|-1|-2|-3|-4|-5|-6|-7|-8)
FLAC_LEVEL=${1#-}
shift ;;
--fast)
FLAC_LEVEL=0
shift ;;
--best)
FLAC_LEVEL=8
shift ;;
-h|--help)
usage ;;
-n|--no-action)
NOACTION=1
shift ;;
-r|--recursive)
RECURSIVE=1
shift ;;
-s|--no-sync)
SYNC=0
shift ;;
-v|--verbose)
VERBOSE=$((VERBOSE+1))
shift ;;
-V|--version)
echo "reflac version $VERSION"
cleanup 0 ;;
--) shift; break ;;
*) echo "$0: This should never happen!" >&2; cleanup 1 ;;
esac
done
if [ -z "$*" ]; then
usage
fi
if [ $VERBOSE -eq 0 ] && [ $NOACTION -eq 1 ]; then
cleanup 0
fi
for dir; do
REFLAC_OPTS="${FLAC_LEVEL}"
if [ $SYNC -eq 0 ]; then
REFLAC_OPTS="${REFLAC_OPTS}s"
fi
if [ $RECURSIVE -eq 1 ] && [ $NOACTION -eq 1 ]; then
find "$(readlink -f -- "$dir")" -type d -execdir "$SELF" -nv {} \;
elif [ $RECURSIVE -eq 1 ]; then
if [ $VERBOSE -eq 1 ]; then
find "$(readlink -f -- "$dir")" -type d \
-execdir "$SELF" -v"$REFLAC_OPTS" {} \;
elif [ $VERBOSE -ge 2 ]; then
find "$(readlink -f -- "$dir")" -type d \
-execdir "$SELF" -vv"$REFLAC_OPTS" {} \;
else
find "$(readlink -f -- "$dir")" -type d \
-execdir "$SELF" -"$REFLAC_OPTS" {} \;
fi
else
pushd -- "$dir" >/dev/null || cleanup $?
if [ $VERBOSE -gt 0 ]; then readlink -f .; fi
if [ -n "$(ls -- *.flac 2>/dev/null)" ]; then
recompress
fi
popd >/dev/null
fi
done
rmdir "$TMPDIR"