-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecodetomp3.sh
executable file
·157 lines (127 loc) · 3.32 KB
/
recodetomp3.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
#!/usr/bin/env bash
# converts all .flac files to .mp3 and copies them to the
# OUT directory; copies all .mp3 files to the OUT directory.
LAMEOPTS="-V0 --quiet"
MAXJOBS=2
function jobidfromstring()
{
local STRING;
local RET;
STRING=$1;
RET="$(echo $STRING | sed 's/^[^0-9]*//' | sed 's/[^0-9].*$//')"
echo $RET;
}
function clearToSpawn
{
local JOBCOUNT="$(jobs | wc -l)"
if [ $JOBCOUNT -lt $MAXJOBS ] ; then
echo 1;
return 1;
fi
echo 0;
return 0;
}
if [ "$#" -ne 2 ]
then
echo "usage: $0 MUSIC_DIR OUTPUT_DIR"
exit 1
fi
IN=$1
if [ ! -d "$IN" ]
then
echo "$IN is not a directory"
exit 1
fi
OUT=$2
if [ ! -d "$OUT" ]
then
mkdir "$OUT"
fi
echo "checking mp3 links"
# first, find all .mp3s and hardlink them to the OUT dir
# unless they are already there
find "$IN" -iname "*.mp3" | while read mp3;
do
OF=`echo "$mp3" | sed s,"$IN","$OUT\/",g|sed s,\?,,g|sed s,\*,,g|sed s,:,,g`
dir=`dirname "$OF"`
if [ ! -d "$dir" ]
then
mkdir -p "$dir"
fi
if [ ! -e "$OF" ]
then
echo "linking $mp3 to $OF"
#cp "$mp3" "$OF"
ln "$mp3" "$OF"
fi
done
echo " done"
JOBLIST=""
echo "checking flac file conversions"
# now, find all .flacs and convert them to .mp3
# unless they have already been converted
find "$IN" -iname "*.flac" | while read flac;
do
OF=`echo "$flac" | sed s/\.flac/\.mp3/g | sed s,"$IN","$OUT\/",g|sed s,\?,,g|sed s,\*,,g|sed s,:,,g`
dir=`dirname "$OF"`
if [ ! -d "$dir" ]
then
mkdir -p "$dir"
fi
if [ ! -e "$OF" ]
then
# retrieve ID3 tags
ARTIST=`metaflac "$flac" --show-tag=ARTIST | sed s/.*=//g`
TITLE=`metaflac "$flac" --show-tag=TITLE | sed s/.*=//g`
ALBUM=`metaflac "$flac" --show-tag=ALBUM | sed s/.*=//g`
GENRE=`metaflac "$flac" --show-tag=GENRE | sed s/.*=//g`
TRACKNUMBER=`metaflac "$flac" --show-tag=TRACKNUMBER | sed s/.*=//g`
DATE=`metaflac "$flac" --show-tag=DATE | sed s/.*=//g`
# convert to MP3, preserving ID3 tags
echo "encoding $flac to $OF"
flac -c -dF --silent "$flac" | lame $LAMEOPTS \
--add-id3v2 --pad-id3v2 --ignore-tag-errors --tt "$TITLE" --tn "${TRACKNUMBER:-0}" --ta \
"$ARTIST" --tl "$ALBUM" --ty "$DATE" --tg "${GENRE:-12}" \
- "$OF" &
fi
while [ `clearToSpawn` -ne 1 ]; do
sleep 1
done
done
echo "done"
JOBLIST=""
echo "checking ogg file conversions"
# now, find all .ogg and convert them to .mp3
# unless they have already been converted
find "$IN" -iname "*.ogg" | while read ogg;
do
OF=`echo "$ogg" | sed s/\.ogg/\.mp3/g | sed s,"$IN","$OUT\/",g|sed s,\?,,g|sed s,\*,,g|sed s,:,,g`
dir=`dirname "$OF"`
if [ ! -d "$dir" ]
then
mkdir -p "$dir"
fi
if [ ! -e "$OF" ]
then
# retrieve ID3 tags
ARTIST=`ogginfo "$ogg" |grep -i artist|grep "artist="|grep -v album | sed s/.*=//g`
TITLE=`ogginfo "$ogg" |grep -i title | sed s/.*=//g`
ALBUM=`ogginfo "$ogg" |grep -i album|grep -v artist|grep -v musicbr | sed s/.*=//g`
#GENRE=`metaflac "$flac" --show-tag=GENRE | sed s/.*=//g`
TRACKNUMBER=`ogginfo "$ogg" |grep -i tracknumber | sed s/.*=//g`
DATE=`ogginfo "$ogg" |grep -i date| sed s/.*=//g`
# convert to MP3, preserving ID3 tags
while [ `clearToSpawn` -ne 1 ]; do
sleep 1
done
echo "encoding $ogg to $OF"
ogg123 -q -d wav -f - "$ogg" | lame $LAMEOPTS \
--add-id3v2 --pad-id3v2 --ignore-tag-errors --tt "$TITLE" --tn "${TRACKNUMBER:-0}" --ta \
"$ARTIST" --tl "$ALBUM" --ty "$DATE" \
- "$OF" &
fi
done
for JOB in $JOBLIST ; do
wait %$JOB
echo "finished"
done