-
Notifications
You must be signed in to change notification settings - Fork 0
/
flac2mp3.sh
executable file
·225 lines (177 loc) · 6.88 KB
/
flac2mp3.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
#! /bin/bash
scriptversion="trunk"
scriptlastedit="20150825"
scriptauthor="John Pyper"
scriptsite="https://github.com/jpyper/bash-scripts"
#############
### ABOUT ###
#############
# This is a simple script that takes input FLAC file(s) in a directory and creates
# output MP3 file(s). Commonly used to convert high quality lossless FLAC audio to
# high quality lossy MP3 audio for portable music players and devices that do not
# support the FLAC file format.
#
# Tip: Place this script in your '$USER/bin' directory, then 'chmod +x flac2mp3.sh'.
# As long as '$USER/bin' is in your '$PATH', the script can be run anywhere
# on the system.
####################
### REQUIREMENTS ###
####################
# flac, metaflac, lame, sed, grep, cut, which, find, mkdir, wc
###############
### LICENSE ###
###############
# This script is licensed under the MIT License.
# Details here: http://opensource.org/licenses/MIT
############################
### CONFIGURABLE OPTIONS ###
############################
# outdir: change this to where you want your converted files to go. default makes a
# directory called mp3 in the directory where the script is run from.
outdir="${PWD}/mp3"
# flacbin: full path location of 'flac' binary
flacbin=$(which flac)
# flacopts: decode file, include errors if present, to pipe. 'man flac' for details.
flacopts="--totally-silent --decode --decode-through-errors --stdout"
# metaflacbin: full path location of 'metaflac' binary
metaflacbin=$(which metaflac)
# lamebin: full path location of 'lame' binary
lamebin=$(which lame)
# lameopts: Joint stereo, highest quality variable bitrate stream.
# Please read the manpage 'man lame' or 'lame --longhelp' for option details.
lameopts="--quiet -m j -q 0 -V 0 --vbr-new -T -c -o -p -b 32 --noreplaygain --add-id3v2 --pad-id3v2 --ignore-tag-errors"
#########################
### MAGIC INGREDIENTS ###
#########################
f2m_check_script_requirements() {
# make sure we have the required, possibly not installed by default, utilities to do the job.
# check that flacbin points to a valid file
if [ ! -f ${flacbin} ]; then
echo ":-( This script can not locate the flac binary."
echo " Check the 'flacbin' variable at the top of this script. Make sure it is pointing to the flac binary."
echo " Install the 'flac' package provided by your distribution, or"
echo " obtain source from https://www.xiph.org/flac/ and compile a binary to use."
echo
exit 1;
fi
# check that metaflacbin points to a valid file
if [ ! -f ${metaflacbin} ]; then
echo ":-( This script can not locate the metaflac binary."
echo " Check the 'metaflacbin' variable at the top of this script. Make sure it is pointing to the metaflac binary."
echo " Install the 'flac' package provided by your distribution, or"
echo " obtain source from https://www.xiph.org/flac/ and compile a binary to use."
echo
exit 1;
fi
# check that lamebin points to a valid file
if [ ! -f ${lamebin} ]; then
echo ":-( This script can not locate the lame binary."
echo " Check the 'lamebin' variable at the top of this script. Make sure it is pointing to the lame binary."
echo " Install the 'lame' package provided by your distribution, or"
echo " obtain source from http://lame.sourceforge.net/ and compile a binary to use."
echo
exit 1;
fi
}
f2m_check_output_directory() {
# check for output directory
echo "+-------------------------------+"
echo "| Checking for output directory |"
echo "+-------------------------------+"
echo
if [ ! -d "${outdir}" ]; then
echo ":-( Can't find output directory:"
echo " ${outdir}"
echo ":-| Creating directory."
mkdir -p "${outdir}"
if [ ${?} -gt 0 ]; then
echo ":-( There was a problem creating the directory."
echo " Make sure you have write permissions to the directory specified."
echo
exit 1
fi
echo ":-) Directory successfully created."
echo
else
echo ":-) Found output directory:"
echo " ${outdir}"
echo
fi
}
f2m_test_directory_flac_files() {
echo "+-------------------------------------------+"
echo "| Testing source FLAC files. Please wait... |"
echo "+-------------------------------------------+"
# echo
${flacbin} --test ./*.[Ff][Ll][Aa][Cc]
if [ ${?} != 0 ]; then
echo
echo ":-( Testing of FLAC files encountered a problem. Please look over the results above."
echo
exit 2
fi
echo
}
f2m_flac_to_mp3_conversion() {
echo "+----------------------------------------+"
echo "| Converting FLAC to MP3. Please wait... |"
echo "+----------------------------------------+"
echo
# get number of flac files in directory. initialize counter.
filetotal=$(find . -name "*.[Ff][Ll][Aa][Cc]" | wc -l)
filecount="0"
for a in ./*.[Ff][Ll][Aa][Cc]; do
# get flac file meta info
# filebase="$(echo "$a" | sed s/.flac//g)"
filebase="${a//.flac/}"
fileartist="$(metaflac "${a}" --show-tag=ARTIST | sed s/.*=//g)"
filetitle="$(metaflac "${a}" --show-tag=TITLE | sed s/.*=//g)"
filetrack="$(metaflac "${a}" --show-tag=TRACKNUMBER | sed s/.*=//g)"
filealbum="$(metaflac "${a}" --show-tag=ALBUM | sed s/.*=//g)"
# print some artist and album info. only gets info from first file processed in the loop.
if [ ${filecount} -lt 1 ]; then
# i haven't figured out how to determine "various artists" yet.
#echo "ARTIST: ${fileartist}"
echo "ALBUM: ${filealbum}"
echo
fi
# increment file number +1 for each run through the loop
filecount=$((filecount + 1))
# show file number of total files being worked on and current file name minus extension
echo "[${filecount}/${filetotal}] ${fileartist} -- ${filetitle}"
# this is where the cooking, err, converting happens
${flacbin} ${flacopts} "${filebase}.flac" | ${lamebin} ${lameopts} --ta "${fileartist}" --tt "${filetitle}" --tl "${filealbum}" --tn "${filetrack:-0}" - "${outdir}/${filebase}.mp3"
done
echo
}
f2m_show_outdir_contents() {
echo "+--------------------------------------+"
echo "| Here are your fresh new MP3 files... |"
echo "+--------------------------------------+"
echo
ls -lh "${outdir}"
echo
}
########################
### [ MAGICAL SOUP ] ###
########################
# let's start with a blank screen
clear
# display script header
echo
echo "+-----------------------------------------------"
echo "| FLAC to MP3 Converter"
echo "| version: ${scriptversion} (${scriptlastedit})"
echo "| by: ${scriptauthor}"
echo "| web: ${scriptsite}"
echo
# step 1: check for required possibly not installed by default binaries to operate script.
f2m_check_script_requirements
# step 2: test the integrity of flac file(s) in the directory. halt script operations if flac file errors are encountered.
f2m_test_directory_flac_files
# step 3: test for output directory. create if needed.
f2m_check_output_directory
# step 4: read flac file(s) to produce new mp3 file(s).
f2m_flac_to_mp3_conversion
# step 5: show directory list of newly created mp3 file(s).
f2m_show_outdir_contents