forked from markjdb/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert-albums
executable file
·127 lines (106 loc) · 2.73 KB
/
convert-albums
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
#!/bin/sh
usage()
{
cat >&2 <<__EOF__
Usage: $(basename $0) [ -j numjobs ] [ album-dir ]
Options:
-j numjobs -- Maximum number of conversion processes that will be run
concurrently. Must be 1 or larger. Defaults to the number
of CPUs installed.
album-dir -- Name of the target directory. Defaults to
${HOME}/media/music/classical.
__EOF__
exit ${1-1}
}
log()
{
echo "$(basename $0): $1" >&2
}
trap 'log "cleaning up.."; wait ; exit 0' SIGHUP SIGINT SIGTERM
while [ $# -gt 0 ]; do
case $1 in
-h|--help)
usage 0
;;
-j)
shift; jobs="$1"
;;
*)
break
;;
esac
shift
done
: ${jobs=$(sysctl -n hw.ncpu)}
if [ $# -eq 1 ]; then
albumdir="$1"
elif [ $# -eq 0 ]; then
albumdir=${HOME}/media/music/classical
else
usage
fi
if ! which -s oggenc; then
log "oggenc isn't installed, exiting"
exit 1
elif ! mount | awk '{print $1}' | egrep -q '^procfs$'; then
log "procfs isn't mounted, exiting"
exit 1
fi
[ ${#jobs} -gt 0 -a $(expr "$jobs" : [1-9][0-9]*) -eq ${#jobs} ] || usage
if ! cd "$albumdir" >/dev/null 2>&1; then
log "couldn't cd to $albumdir"
exit 1
fi
count=$(find . -name '*.flac' | wc -l | awk '{print $1}')
nproc=0
n=0
OLDIFS=$IFS
IFS='
'
for file in $(find . -name '*.flac'); do
basefile=$(basename "${file%.flac}")
basedir=${file%/flac/*.flac}
n=$((n + 1))
# Check to see if a converted version already exists.
if [ -f "${basedir}/ogg/${basefile}.ogg" -o -f "${basedir}/flac/${basefile}.ogg" ]; then
log "($n of $count) skipping $file"
continue
fi
log "($n of $count) converting $file"
oggenc -b 160 $file >/dev/null 2>&1 &
pid=$! # PID of oggenc
pidq="$pidq $pid"
nproc=$((nproc + 1))
while [ $nproc -ge $jobs ]; do
sleep 1
unset tpidq
IFS=$OLDIFS
for pid in ${pidq}; do
piddir=/proc/$pid
status=${piddir}/status
if [ -d $piddir -a -r $status -a "$(awk '{print $3}' $status 2>/dev/null)" = $$ ]; then
tpidq="$tpidq $pid"
else
# The oggenc run finished, so remove the PID.
nproc=$((nproc - 1))
fi
done
OLDIFS=$IFS
IFS='
'
pidq=$tpidq
done
done
wait
for file in $(find . -name '*.flac'); do
basefile=$(basename ${file%.flac})
basedir=${file%/flac/*.flac}
if [ ! -d "${basedir}/ogg" ]; then
log "${basedir}/ogg doesn't exist - creating"
mkdir "${basedir}/ogg"
fi
if [ -f ${basedir}/flac/${basefile}.ogg ]; then
mv -f "${basedir}/flac/${basefile}.ogg" "${basedir}/ogg/${basefile}.ogg"
fi
done
exit 0