blob: 4923baa16094a8020e31db71742cabef6f4dd738 (
plain)
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
|
#!/usr/bin/env zsh
# concatenate multiple audio files into one
source "$HOME/.config/init/helpers" || exit 1
local callstr=$0
usage() {
echo "Usage: $callstr [OPTIONS...] <infile>..."
echo "Convert multiple audio files to a new format"
echo ""
echo " \e[1mdescription opt longform arg default\e[0m"
echo " output codec -c --codec <codec> opus"
echo " output bitrate -b --bitrate <int>k (opus:35k, vorbis:50k, mp3:65k, flac:N/A)"
echo " output quality -q --quality <int> N/A"
echo " output directory -d --dest <dir> ."
echo " copy metadata -m --metadata <bool> false"
echo " background job count -j --jobs <int> 4"
echo " print this help -h --help"
exit 0
}
local fifoname
local fifomade=0
local pid
trap_abort() {
echo ""
print-error "process interrupted"
# race condition-y, but eh
repeat 5; do
[[ -z $pid ]] && sleep 0.1 || break
done
[[ -z $pid ]] || kill -s SIGKILL $pid
[[ -p $fifoname ]] && [[ $fifomade -eq 1 ]] && 2>/dev/null rm $fifoname
exit 1
}
trap 'trap_abort' SIGABRT SIGHUP SIGINT SIGQUIT SIGTERM
local codecpat='^(opus|mp3|vorbis|flac)$'
local bitratepat='^[1-9][0-9]*k$'
local metadatapat='^(true|false)$'
local jobcountpat='^[1-9][0-9]*$'
local qpatvorbis='^([0-9]|10)$'
local qpatmp3='^[0-9]$'
local codecstr
local codecprintstr
local extstr
local bitratestr
local bitrateset
local metastr
local dirstr
local qstr
local jobcount
local parseerr=$(2>&1 zparseopts -D -E -M -A args \
c: -codec:=c \
b: -bitrate:=b \
q: -quality:=q \
d: -dest:=d -destination:=d \
j: -jobs:=j \
m:: -metadata::=m \
h -help=h | cut -d ' ' -f 2-)
[[ -z $parseerr ]] || abort $parseerr
zparseopts -D -E -M -A args \
c: -codec:=c \
b: -bitrate:=b \
q: -quality:=q \
d: -dest:=d -destination:=d \
j: -jobs:=j \
m:: -metadata::=m \
h -help=h
local optarg
local opt
for opt in ${(@k)args}; do
unset optarg
[[ -z $args[$opt] ]] || optarg=$args[$opt]
case $opt in
-c) [[ $optarg =~ $codecpat ]] || \
abort "codec must be one of \`opus\`, \`mp3\`, \`flac\`, or \`vorbis\`"
[[ ${optarg} == 'opus' ]] \
&& codecprintstr=${optarg} \
&& codecstr='libopus' && extstr='opus' \
&& [[ -z $bitratestr ]] && bitratestr='35k'
[[ ${optarg} == 'mp3' ]] \
&& codecprintstr=${optarg} \
&& codecstr='libmp3lame' && extstr='mp3' \
&& [[ -z $bitratestr ]] && bitratestr='65k'
[[ ${optarg} == 'vorbis' ]] \
&& codecprintstr=${optarg} \
&& codecstr='libvorbis' && extstr='ogg' \
&& [[ -z $bitratestr ]] && bitratestr='50k'
[[ ${optarg} == 'flac' ]] \
&& codecprintstr=${optarg} \
&& codecstr='flac' && extstr='flac' \
&& [[ -z $bitratestr ]] && bitratestr=''
;;
-b) [[ $optarg =~ $bitratepat ]] || \
abort "bitrate must positive integer + k, for kbps"
bitratestr=$optarg
bitrateset=1
;;
-q) qstr=$optarg ;;
-d) dirstr=$optarg ;;
-j) [[ $optarg =~ $jobcountpat ]] || \
abort "job count must be a non-negative integer"
jobcount=$optarg
;;
-m) if [[ $optarg ]]; then
[[ $optarg =~ $metadatapat ]] || \
abort "metadata must be either \`true\` or \`false\`"
[[ $optarg == "false" ]] && metastr="" \
|| metastr=("-map_metadata" "-1")
else
metastr=("-map_metadata" "-1")
fi
;;
-h) usage ;;
esac
done
[[ -z $codecstr ]] && codecstr='libopus'
[[ -z $bitratestr ]] && bitratestr='35k'
[[ -z $codecprintstr ]] && codecprintstr='opus'
[[ -z $extstr ]] && extstr='opus'
[[ -z $jobcount ]] && jobcount=4
[[ -z $dirstr ]] && dirstr='.'
[[ -z $metastr ]] && metastr=("-map_metadata" "-1")
if [[ ! -z $qstr ]]; then
[[ -z $bitratset ]] || abort "-b and -q are not compatible"
[[ $codecstr == "libopus" ]] && abort "-q cannot be used with opus"
[[ $codecstr == "libflac" ]] && abort "-q cannot be used with flac"
[[ $codecstr == "libvorbis" ]] && [[ ! $qstr =~ $qpatvorbis ]] \
&& abort "for vorbis, -q must be in the range [0-10]"
[[ $codecstr == "libmp3lame" ]] && [[ ! $qstr =~ $qpatmp3 ]] \
&& abort "for mp3, -q must be in the range [0-9]"
else
fi
[[ $codecstr == "flac" ]] && bitratestr=''
[[ $#@ -eq 0 ]] && abort "no input files specified"
local f
for f in $@; do
[[ -f $f ]] || abort "could not read file \`$f\`"
done
2>/dev/null mkdir -p $dirstr || abort "could not write to directory \`$dirstr\`"
repeat 5; do
fifoname="$dirstr/audio-convert-tmp-$RANDOM"
[[ -e $fifoname ]] && continue
2>/dev/null mkfifo $fifoname || return 1
fifomade=1
break
done
[[ $fifomade -eq 1 ]] || abort "could not make temporary fifo"
local delprompt
for f in $@; do
local dest="$dirstr/${f:t:r}.$extstr"
if [[ -e $dest ]]; then
printf "destination file \`$dest\` exists\ndelete it? [y/N]: "
read -q delprompt
print ""
[[ $delprompt = "y" ]] || return 0
2>/dev/null rm $dest || abort "could not delete file \`$dest\`"
fi
done
local totalstr="${#@} file"
[[ $#@ -ne 1 ]] && totalstr="${totalstr}s"
local jobstr=$jobcount
[[ $#@ -lt $jobcount ]] && jobstr=$#@
[[ $jobstr -gt 1 ]] && jobstr="${jobstr} jobs" || jobstr="${jobstr} job"
(
{
local joblist=()
while read -d $'\0' f; do
local dest="$dirstr/${f:t:r}.$extstr"
(
ffmpeg -loglevel -8 -i $f \
-vn -sn -c:a $codecstr \
${bitratestr/*k/-b:a} $bitratestr \
${qstr/[0-9]*/-q:a} $qstr \
$metastr \
$dest
echo "done"
) &
joblist+=$!
done
local j
for j in $joblist; do
2>/dev/null wait $j
done
} < $fifoname | {
repeat $jobcount; do
if [[ $#@ -gt 0 ]]; then
printf "%s\0" $@[1]
shift
fi
done
while read line; do
if [[ $#@ -gt 0 ]]; then
printf "%s\0" $@[1]
shift
else
break
fi
done
} > $fifoname
)&
pid=$!
wait-anim $pid "using $jobstr to convert $totalstr to $codecprintstr"
2>/dev/null rm $fifoname
|