-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathflac_to_mp3.sh
executable file
·246 lines (214 loc) · 5.96 KB
/
flac_to_mp3.sh
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#!/bin/bash
# flac2mp3.sh: Transcode FLAC audio files to MP3 audio files
# James A. Hillyerd
# $Revision: 24 $, $Date: 2006-10-04 19:10:00 -0700 (Wed, 04 Oct 2006) $
# Release: 1.1
#
# Usage: flac2mp3 <input-dir> <output-dir>
### Begin Configuration
# The values below will be overridden by those in ~/.flac2mp3.conf if it
# exists
# Copy artwork (*.jpg) files? Set to "true" or "false"
COPY_ARTWORK="true"
# Where we store working data (including the decoded wav file)
TMPDIR="/tmp"
# Path to flac binary
FLAC="$(which flac)"
# Path to metaflac binary (part of flac distribution)
METAFLAC="$(which metaflac)"
# Path to lame binary
LAME="$(which lame)"
# Options to pass to lame during encoding
LAME_OPTS="--vbr-new -V 1 --nohist -b 192"
# Multiple instances of this running on the same output-dir will be more
# reliable with locking available.
DOTLOCKFILE="$(which dotlockfile)"
# Note that INND's shlock isn't reliable, but we don't have a good way
# to tell whether this is that shlock or Erik Fair's shlock(1) which
# comes with several BSDs because both use the same command args.
SHLOCK="$(which shlock)"
# Maximum number of times we allow flac decode to fail per file
# before aborting
MAXFAILS=5
### End Configuration
### Read User Configuration
[[ -f ~/.flac2mp3.conf ]] && source ~/.flac2mp3.conf
# Check that we have all the required binaries
if [ ! -x "$FLAC" ]; then
echo "ERROR: flac not installed" >&2
exit 1
fi
if [ ! -x "$METAFLAC" ]; then
echo "ERROR: metaflac not installed" >&2
exit 1
fi
if [ ! -x "$LAME" ]; then
echo "ERROR: lame not installed" >&2
exit 1
fi
# Were we called correctly?
if [ -z "$1" -o -z "$2" ]; then
ME=$(basename "$0")
echo "Usage: $ME <input-dir> <output-dir>" >&2
exit 1
fi
WORK="$TMPDIR/flac2mp3.$$"
ERR="$WORK/stderr.txt"
WAV="$WORK/track.wav"
TAGS="$WORK/track.tags"
# Get absolute directory paths
INPUTDIR="$1"
OUTPUTDIR="$2"
cd "$INPUTDIR"
INPUTDIR="$PWD"
cd "$OLDPWD"
cd "$OUTPUTDIR"
OUTPUTDIR="$PWD"
# Setup work directory
rm -rf "$WORK"
if [ -e "$WORK" ]; then
echo "Couldn't delete $WORK"
exit 1
fi
mkdir "$WORK"
if [ ! -d "$WORK" ]; then
echo "Couldn't create $WORK"
exit 1
fi
# Get input file list
cd "$INPUTDIR"
OLDIFS="$IFS"
IFS=$'\n'
for filepath in $(find . -type f -name '*.flac' -print \
| sort | sed -e 's/^\.\///' -e 's/\.flac$//'); do
IFS="$OLDIFS"
filedir="$(dirname "$filepath")"
filename="$(basename "$filepath")"
if [ ! -d "$OUTPUTDIR/$filedir" ]; then
mkdir -p "$OUTPUTDIR/$filedir"
fi
if [ ! -f "$OUTPUTDIR/$filepath.mp3" ]; then
# Lock the working file
if [ -x "$DOTLOCKFILE" ]; then
if ! "$DOTLOCKFILE" -p -r 0 -l "$OUTPUTDIR/$filepath".lock; then
# Could not get a lock, so skip to next file
continue
fi
elif [ -x "$SHLOCK" ]; then
if ! "$SHLOCK" -f "$OUTPUTDIR/$filepath".lock -p $$; then
# Could not get a lock, so skip to next file
continue
fi
fi
# Clear tags
rm -f "$TAGS"
ARTIST=
ALBUM=
TITLE=
DATE=
GENRE=
TRACKNUMBER=
# Get tags
$METAFLAC --export-tags-to="$TAGS" "$filepath.flac"
sed -i -e 'h
s/^[^=]*=//
s/"/\\"/g
s/\$/\\$/g
s/^.*$/"&"/
x
s/=.*$//
y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/
G
s/\n/=/' "$TAGS"
source "$TAGS"
echo "$ARTIST / $ALBUM ($DATE) / $TRACKNUMBER - $TITLE"
# Decode FLAC (loop until success or MAXFAILS reached)
attempt=0
success=0
while (( ! $success )) && (( $attempt < $MAXFAILS )); do
let attempt++
rm -f "$ERR" "$WAV"
echo -n "Decoding attempt $attempt: "
$FLAC -sd -o "$WAV" "$filepath.flac" 2> "$ERR"
if (( ! $? )) && [ ! -s "$ERR" ]; then
success=1
echo "OK"
else
echo "FAILED"
# Waiting seems to help
sleep 2
fi
done
if (( $attempt > $MAXFAILS )); then
echo "$MAXFAILS decode attempts failed, aborting"
cat "$ERR"
exit 1
fi
# Encode MP3
$LAME $LAME_OPTS --tt "$TITLE" --ta "$ARTIST" --tl "$ALBUM" \
--tn "$TRACKNUMBER" --tg "$GENRE" --ty "$DATE" \
"$WAV" "$OUTPUTDIR/$filepath.mp3"
if (( $? )); then
echo ""
echo "Encode failed, retrying sans genre"
$LAME $LAME_OPTS --tt "$TITLE" --ta "$ARTIST" --tl "$ALBUM" \
--tn "$TRACKNUMBER" --ty "$DATE" \
"$WAV" "$OUTPUTDIR/$filepath.mp3"
if (( $? )); then
echo ""
echo "Encode failed, aborting"
exit 1
fi
fi
echo ""
echo ""
# Remove the lock file
if [ -x "$DOTLOCKFILE" ]; then
"$DOTLOCKFILE" -u "$OUTPUTDIR/$filepath".lock
elif [ -x "$SHLOCK" ]; then
rm -f "$OUTPUTDIR/$filepath".lock
fi
fi
done
# Copy artwork
if [ "$COPY_ARTWORK" == "true" ]; then
echo -n "Copying artwork: "
OLDIFS="$IFS"
IFS=$'\n'
for filepath in $(find . -type f -name '*.jpg' -print \
| sort | sed -e 's/^\.\///'); do
IFS="$OLDIFS"
filedir="$(dirname "$filepath")"
filename="$(basename "$filepath")"
if [ ! -d "$OUTPUTDIR/$filedir" ]; then
mkdir -p "$OUTPUTDIR/$filedir"
fi
# If the input is newer than the output, or the output does not exist
if [ "$filepath" -nt "$OUTPUTDIR/$filepath" ]; then
# Lock the working file
if [ -x "$DOTLOCKFILE" ]; then
if ! "$DOTLOCKFILE" -p -r 0 -l "$OUTPUTDIR/$filepath".lock; then
# Could not get a lock, so skip to next file
continue
fi
elif [ -x "$SHLOCK" ]; then
if ! "$SHLOCK" -f "$OUTPUTDIR/$filepath".lock -p $$; then
# Could not get a lock, so skip to next file
continue
fi
fi
cp -f "$filepath" "$OUTPUTDIR/$filepath"
echo -n "."
# Remove the lock file
if [ -x "$DOTLOCKFILE" ]; then
"$DOTLOCKFILE" -u "$OUTPUTDIR/$filepath".lock
elif [ -x "$SHLOCK" ]; then
rm -f "$OUTPUTDIR/$filepath".lock
fi
fi
done
echo " Done!"
fi
# Clean up
rm -rf "$WORK"
exit