-
Notifications
You must be signed in to change notification settings - Fork 0
/
ntrac
executable file
·690 lines (629 loc) · 25.5 KB
/
ntrac
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
#!/bin/bash
#
# nTRAC: n-Track Recording Audio transCription
# converts two legs of a call or any multi-track recording into one mono-mixdown and generates a transcript
#
# For important setup instructions see readme.md
#
# based on
# - 2wav2mp3 - 2005 05 23 dietmar zlabinger http://www.zlabinger.at/asterisk
# - Asterisk voicemail attachment conversion script - Jason Klein, Ward Mundy & Associates LLC, et al
#
# Revision history :
# 03. Feb 2019 - v0.9 - Creation by Thomas Reintjes
# 04. Aug 2019 - v0.95 - support direct upload to Auphonic
#
##############################################################################################################################################
####################### settings #########################################################
. "$(dirname "${BASH_SOURCE[0]}")/ntrac.config"
# don't care if strings in "case" or [[...]] are uppercase or lowercase
# shopt -s nocasematch
####################### debug ############################################################
if [ "$debug_mode" = true ] ; then
set -x
exec 3>&1 4>&2
trap 'exec 2>&4 1>&3' 0 1 2 3
exec 1>"$log_file" 2>&1
echo "\nUser: "$USER" \n"
echo "\nHOME: "$HOME" \n"
if [ "$(id -u)" = "0" ]; then
echo "\nUser seems to be root \n"
fi
if [[ $EUID -eq 0 ]]; then
echo "\nUser seems to be root \n"
fi
fi
####################### functions ########################################################
# help and exit
help_and_exit() {
local retval=${1:-1}
cat <<EOF
${0##*/} [ -a | --auphonic ] [ -g | --google ] [ -l < en-US | en-GB | de-DE | ...> | --language < en-US | en-GB | de-DE |...> | --en-US | --en-GB | --de-DE... ] [ delete=< true | false > ] [ dest=< /path/to/dir | RcloneRemote1:path/to/dir | dir > ] /path/to/outgoing.wav /path/to/incoming.wav /path/to/output.wav
If no parameters are given, only a mono-mix file is generated.
EOF
exit "$retval"
}
# function to move files to output location
# output <copy|move> file
output () {
if [[ $destination == /** ]] ; then
local newfile="$destination/$(basename -- "$2")"
if [ "$newfile" != "$2" ] ; then
if [[ "$1" == copy ]] ; then
mkdir -p "$destination" && cp "$2" "$newfile"
elif [[ "$1" == move ]] ; then
mkdir -p "$destination" && mv "$2" "$newfile"
else
return 1 # something's wrong
fi
else
return 0 # all good
fi
else
rclone -vv --config="$rcloneconfig" "$1" "$2" "$destination"
fi
}
# cleanup
cleanup() {
if [[ "$1" == google ]] ; then
rclone --config="$rcloneconfig" delete "$google_rclone/$basename-nchannel.flac"
fi
if [[ "$1" == auphonic ]] && [ "$auphonic_rclone" != "$destination" ] && [ "$auphonic_in" != "$auphonic_out" ] ; then
for filename in "${input_filenames[@]}" ; do
rclone --config="$rcloneconfig" delete "$auphonic_rclone/$filename"
done
fi
if [[ "$1" == all ]] && [ "$delete_input_files" = true ] && [[ "$destination" != "$output_dir" ]] ; then
for inputfile in "${input_files[@]}" ; do
rm "$inputfile" ;
done
fi
}
####################### variables ########################################################
# command line variables
input_files=( )
while (( $# )); do
case $1 in
-a|--auphonic) auphonic=true ;;
-g|--google) google=true ;;
-l|--language) language=$2; shift ;;
--[abcdefghijklmnopqstuvwxyz][abcdefghijklmnopqstuvwxyz]-[ABCDEFGHIJKLMNOPQRSTUVWXYZ][ABCDEFGHIJKLMNOPQRSTUVWXYZ]) language=${1:2} ;;
--yue-Hant-HK) language="yue-Hant-HK" ;;
--zh) language="zh" ;;
delete=true) delete_input_files=true ;;
delete=false) delete_input_files=false ;;
dest=/**) destination="${1#*=}" ;;
dest=[a-zA-Z]*:) destination="${1#*=}" ;;
dest=[a-zA-Z]*:[a-zA-Z]*/**) destination="${1#*=}" ;;
dest=dir) destination="${1#*=}" ;;
-h|--help) help_and_exit 0 ;;
-*) printf 'Unknown option: %q\n\n' "$1"
help_and_exit 1 ;;
*) input_files+=( "$1" ) ;;
esac
shift
done
# if input file is sln16, convert to wav
for i in "${!input_files[@]}" ; do
inputfile="${input_files[$i]}"
if [ "${inputfile##*.}" = "sln16" ] ; then
# output file doesn't exist yet, hence a test is needed
test -f "$inputfile" && "$sox" -t raw -r 16k -e signed-integer -b 16 -c 1 "$inputfile" "${inputfile%.*}.wav"
test -f "${inputfile%.*}.wav" && rm "$inputfile"
input_files[$i]="${inputfile%.*}.wav"
fi
done
# get variables
tracks=$((${#input_files[@]}-1)) # number of tracks (out/local/host + in/remotes/guests)
mix="${input_files[@]: -1}" # last file = output file
unset "input_files[${#input_files[@]}-1]" # remove mix from array
output_dir="${mix%/*}" # e.g. /var/spool/asterisk/monitor/year/month/day
filename="${mix##*/}" # e.g. recording1234.wav
basename="${filename%.*}" # e.g. recording1234
extension="${filename##*.}" # e.g. wav
# all filenames
input_filenames=( )
for inputfile in "${input_files[@]}" ; do
input_filenames+=( "$(basename -- "$inputfile")" )
done
# if last file was passed without a path, try to fix it
if [[ $output_dir != /** ]] ; then
if [[ $destination == /** ]] ; then
output_dir="$destination"
else # if all input files are in the same directory, use that directory
equalinputfolders=true
inputfolder="$(dirname -- "${input_files[0]}")"
for inputfile in "${input_files[@]}"; do
if [[ "$inputfolder" != "$(dirname -- "$inputfile")" ]] ; then
equalinputfolders=false
break
fi
done
if [ "$equalinputfolders" = true ] ; then
output_dir="$(dirname -- "${input_files[0]}")"
mix="$output_dir/$filename"
else
printf 'No output location specified or invalid\n\n'
help_and_exit 1
fi
fi
fi
if [[ -z $destination && -n $auphonic && -n $auphonic_out ]] ; then
auphonic_out_use="true"
else
auphonic_out_use=""
fi
if [[ $destination == dir || $destination == "" ]] ; then
destination="$output_dir"
else
# remove trailing slash
destination="${destination%/}"
fi
####################### sox ##############################################################
# copy input files to output destination
for inputfile in "${input_files[@]}" ; do
output copy "$inputfile"
done
if [ $tracks -gt 1 ] ; then
# create mono mixdown
"$sox" -m "${input_files[@]}" "$mix"
# rename mono-mix and move to destination
mix_tmp="$output_dir/$basename-mix.$extension"
cp "$mix" "$mix_tmp" && output move "$mix_tmp"
else
cp "${input_files[0]}" "$mix" && output copy "$mix"
fi
# make mono mix available to Freepbx
if [ "$freepbx" = true ] ; then
if [ "$(dirname -- "${input_files[0]}")" != "$output_dir" ] ; then
mv "$mix" "$(dirname -- "${input_files[0]}")/$filename"
fi
else
# if not for Freepbx, mono-mix is not needed anymore
rm "$mix"
fi
####################### Google transcription #############################################
if $google ; then
transcript="$output_dir/$basename-$language.txt"
mix_nchannel="$output_dir/$basename-nchannel.flac"
if [ "$tracks" -gt 1 ] ; then
googlefiles=()
for i in "${!input_files[@]}" ; do
# create mono versions of input files
"$sox" "${input_files[$i]}" --channels 1 "$output_dir/googlemono$i.flac"
googlefiles[$i]="$output_dir/googlemono$i.flac"
done
# google requires a multi-channel file with all input files combined
"$sox" -M "${googlefiles[@]}" "$mix_nchannel"
for googlefile in "${googlefiles[@]}" ; do
# delete mono versions
rm "$googlefile"
done
else
# create mono-version of only input file
"$sox" "${input_files[0]}" --channels 1 "$mix_nchannel"
fi
samplerate=$(soxi -r "$mix_nchannel")
printf "Google upload in progress.\n"
test -r "$mix_nchannel" && rclone --config="$rcloneconfig" move "$mix_nchannel" "$google_rclone"
google_bucket="${google_rclone#*:}"
json=$(curl -s -X POST \
-H "Content-Type: application/json; charset=utf-8" \
--data "{
'config': {
'encoding': 'flac',
'sample_rate_hertz': '$samplerate',
'languageCode': '$language',
'audioChannelCount': '$tracks',
'enableSeparateRecognitionPerChannel': true,
'alternativeLanguageCodes': [$language_alt],
'enableAutomaticPunctuation': true
},
'audio': {
'uri': 'gs://$google_bucket/$basename-nchannel.flac'
}
}" "https://speech.googleapis.com/v1p1beta1/speech:longrunningrecognize?key=$google_key")
# extract operation ID from json
if [[ $json == *"error"* ]] ; then
printf "%s" "$json" > "$transcript"
output move "$transcript"
printf 'Google transcription produced an error.\n\n'
help_and_exit 1
else
google_operation=$( grep name -m 1 <<< "$json" | sed 's#^.*"name": "##g' | sed 's#"$##g')
fi
# check once every 30 seconds, if operation is done. Abort after checking set number of times
timer=$(($timeout * 2))
until [[ $json == *"\"done\": true"* ]] ; do
if [ $timer -gt 0 ]; then
printf 'Google transcription in progress. Will check again in 30 seconds.\n'
sleep 30
json=$(curl -s -H "Content-Type: application/json; charset=utf-8" "https://speech.googleapis.com/v1/operations/$google_operation?key=$google_key")
let "timer-=1"
else
printf 'Google transcription timed out.\n\n'
cleanup google
help_and_exit 1
break
fi
done
printf 'Google transcription done.\n'
# extract transcript from json
# 1. swap transcript and channelTag sections
json=$( echo "$json" | grep -E 'transcript|channelTag' )
# ...but only if channelTag exists in file, i.e. if it's a multitrack production
if [[ $json == *"channelTag"* ]] ; then
json=$( echo "$json" | sed "N
s#\(.*transcript.*\)\n\(.*channelTag.*\)#\2\
\1#" )
# 2. check if consecutive channelTags are equal, only keep the first one
sedmagic="sed"
for (( i=1; i <= $tracks; i++ )) ; do
sedmagic+=" -e '
/channelTag"'"'": "$i"/{
x
/channelTag"'"'": "$i"/!{
x
h
b
}
x
d
}
'"
done
json=$( echo "$json" | eval "$sedmagic" )
# 3. replace channelTags with names
if [ "$tracks" -eq 2 ] ; then
json=$( echo "$json" | sed "s# *.channelTag.: 2.#REMOTE:#g" )
else
for (( i=2; i <= $tracks; i++ )) ; do
json=$( echo "$json" | sed "s# *.channelTag.: $i.#REMOTE $((i - 1)):#g" )
done
fi
fi
json=$( echo "$json" | sed -e 's/ *.channelTag.: 1,/LOCAL:/g' -e 's/ *.transcript.: . */\
/g' -e 's/\",//g' )
echo "$json" > "$transcript"
output move "$transcript"
cleanup google
fi
# done Google transcription
####################### Auphonic transcription ###########################################
if $auphonic ; then
auphonic_error_check() {
if [[ $json == *"\"status_string\": \"Error\""* || $json == *"doctype html"* ]] ; then
echo "$json" > "$output_dir/$basename-error.txt"
output move "$output_dir/$basename-error.txt"
printf "Auphonic transcription produced an error\nSee %s-error.txt for details.\n\n" "$basename"
cleanup auphonic
help_and_exit 1
else
return 0
fi
}
auphonic_add_tracks() {
auphonic_tracks=" {
\"type\": \"multitrack\", \"id\": \"${track_1_parameters[4]}\",
\"algorithms\": {\"hipfilter\": ${track_1_parameters[0]}, \"backforeground\": \"${track_1_parameters[1]}\", \"denoise\": ${track_1_parameters[2]}, \"denoiseamount\": ${track_1_parameters[3]}}"
if [ -n "$auphonic_in" ] ; then # add incoming file service if configured
auphonic_tracks+=",
\"service\": \"$auphonic_in\",
\"input_file\": \"${input_filenames[0]}\""
fi
for (( i=1; i < ${tracks}; i++ )); do
if [ $tracks -eq 2 ]; then
auphonic_tracks+="
},
{
\"type\": \"multitrack\", \"id\": \"${track_2_parameters[4]}\","
else
auphonic_tracks+="
},
{
\"type\": \"multitrack\", \"id\": \"${track_2_parameters[4]} $i\","
fi
auphonic_tracks+="
\"algorithms\": {\"hipfilter\": ${track_2_parameters[0]}, \"backforeground\": \"${track_2_parameters[1]}\", \"denoise\": ${track_2_parameters[2]}, \"denoiseamount\": ${track_2_parameters[3]}}"
if [ -n "$auphonic_in" ] ; then # add incoming file service if configured
auphonic_tracks+=",
\"service\": \"$auphonic_in\",
\"input_file\": \"${input_filenames[$i]}\""
fi
done
auphonic_tracks+="
}"
auphonic_tracks=$( sed 's#\: " *#\: "#g' <<< "$auphonic_tracks" | sed 's# *",#",#g' )
}
auphonic_upload() {
printf "Auphonic upload in progress: %s\n" "$2"
json=$(curl -s -X POST https://auphonic.com/api/production/$uuid/upload.json \
-u $auphonic_user:"$auphonic_pass" \
-F ''"$1"'=@"'"$2"'"')
auphonic_error_check
}
json=""
basename_auphonic="$basename-$language"
if [ "$tracks" -eq 1 ] ; then
if [ -n "$auphonic_preset_single" ] ; then
# query Auphonic for preset details
json=$(curl -s https://auphonic.com/api/preset/$auphonic_preset_single.json -u $auphonic_user:$auphonic_pass)
# check if outgoing services are defined or if results should be downloaded later
download_flag="$(grep 'outgoing_services' <<< "$json" | grep '\]')" # if closing bracket ] is on the same line, no service is configured
# check if basename is set
basename_preset="$(grep 'output_basename' <<< "$json" | sed 's#^.*"output_basename": "##g' | sed 's#",* *$##g')"
if [ -n "$basename_preset" ] ; then
basename_auphonic="$basename_preset"
fi
# create production using preset
if [ -n "$auphonic_in" ] ; then # incoming file service configured
json=$(curl -s -X POST -H "Content-Type: application/json" \
https://auphonic.com/api/productions.json \
-u $auphonic_user:$auphonic_pass \
-d '{
"preset": "'"$auphonic_preset_single"'",
"metadata": { "title": "'"$basename"'" },
"input_file": "'"${input_filenames[0]}"'",
"service": "'"$auphonic_in"'",
"output_basename": "'"$basename_auphonic"'"
}')
else # no incoming file service
json=$(curl -s -X POST -H "Content-Type: application/json" \
https://auphonic.com/api/productions.json \
-u $auphonic_user:$auphonic_pass \
-d '{
"preset": "'"$auphonic_preset_single"'",
"metadata": { "title": "'"$basename"'" },
"output_basename": "'"$basename_auphonic"'"
}')
fi
elif [ -n "$auphonic_in" ] ; then # no preset, but incoming file service configured
basename_auphonic="$basename"
json=$(curl -s -X POST -H "Content-Type: application/json" \
https://auphonic.com/api/productions.json \
-u $auphonic_user:$auphonic_pass \
-d '{
"metadata": { "title": "'"$basename"'"},
"output_basename": "'"$basename_auphonic"'",
"input_file": "'"${input_filenames[0]}"'",
"service": "'"$auphonic_in"'",
"algorithms": {
"hipfilter": false, "leveler": false,
"normloudness": false, "denoise": false
}
}')
else # no preset, no incoming file service
basename_auphonic="$basename"
json=$(curl -s -X POST -H "Content-Type: application/json" \
https://auphonic.com/api/productions.json \
-u $auphonic_user:$auphonic_pass \
-d '{
"metadata": { "title": "'"$basename"'"},
"output_basename": "'"$basename_auphonic"'",
"algorithms": {
"hipfilter": false, "leveler": false,
"normloudness": false, "denoise": false
}
}')
fi
else
# tracks > 1
# arrays for Auphonic production parameters
declare -a track_1_parameters=(false auto false 0 LOCAL)
# array: 0:hipfilter 1:backforeground 2:denoise 3:denoiseamount 4:id
declare -a track_2_parameters=(false auto false 0 REMOTE)
if [ -n "$auphonic_preset_multi" ] ; then
# query Auphonic for preset details
json=$(curl -s https://auphonic.com/api/preset/$auphonic_preset_multi.json -u $auphonic_user:$auphonic_pass)
# check if outgoing services are defined or if results should be downloaded later
download_flag="$(grep 'outgoing_services' <<< "$json" | grep '\]')"
# check if basename is set
basename_preset="$(grep 'output_basename' <<< "$json" | sed 's#^.*"output_basename": "##g' | sed 's#",* *$##g')"
if [ -n "$basename_preset" ] ; then
basename_auphonic="$basename_preset"
fi
if [ "$tracks" -eq 2 ] ; then
# extract track IDs (names) from preset
track_1_parameters[4]="$(grep '"id":' -m 1 <<< "$json" | sed 's#^.*"id": "##g' | sed 's#",* *$##g')"
track_2_parameters[4]="$(grep '"id":' -m 2 <<< "$json" | tail -n1 | sed 's#^.*"id": "##g' | sed 's#",* *$##g')"
if [ -n "$auphonic_in" ] ; then # incoming file service configured
json=$(curl -s -X POST -H "Content-Type: application/json" \
https://auphonic.com/api/productions.json \
-u $auphonic_user:$auphonic_pass \
-d '{
"preset": "'"$auphonic_preset_multi"'",
"metadata": { "title": "'"$basename"'" },
"output_basename": "'"$basename_auphonic"'",
"multi_input_files": [
{
"type": "multitrack", "id": "'"${track_1_parameters[4]}"'",
"service": "'"$auphonic_in"'",
"input_file": "'"${input_filenames[0]}"'"
},
{
"type": "multitrack", "id": "'"${track_2_parameters[4]}"'",
"service": "'"$auphonic_in"'",
"input_file": "'"${input_filenames[1]}"'"
}
]
}')
else # no incoming file service
json=$(curl -s -X POST -H "Content-Type: application/json" \
https://auphonic.com/api/productions.json \
-u $auphonic_user:$auphonic_pass \
-d '{
"preset": "'"$auphonic_preset_multi"'",
"metadata": { "title": "'"$basename"'" },
"output_basename": "'"$basename_auphonic"'",
"is_multitrack": true
}')
fi
else
# tracks > 2
# build a template for tracks from preset
tracknumber=0
openbrackets=0
track_template=( )
while IFS='' read -r line || [[ -n "$line" ]] ; do
if [[ -n $(grep '{' <<< "$line") ]] ; then
let "openbrackets+=1"
elif [[ -n $(grep '}' <<< "$line") ]] ; then
let "openbrackets-=1"
fi
if [ "$tracknumber" = 0 ] && [[ -n $(grep 'multi_input_files' <<< "$line") ]] ; then
tracknumber=1
elif [ "$tracknumber" -gt 0 ] && [ "$tracknumber" -le 2 ] && [ "$openbrackets" -ge 4 ] ; then
track_template[$tracknumber]+="$line""
"
elif [ "$tracknumber" -gt 0 ] && [ "$tracknumber" -le 2 ] && [ "$openbrackets" -lt 4 ] ; then
# last line of track
track_template[$tracknumber]+="$(sed 's#},#}#g' <<< $line)""
"
let "tracknumber+=1"
fi
done <<< "$json"
j=0
for parameter in "hipfilter" "backforeground" "denoise" "denoiseamount" "id" ; do
# getting the actual parameters for track 1 and 2
track_1_parameters[$j]=$(grep -m 1 $parameter <<< "${track_template[1]}" | sed 's#.*"'"$parameter"'": "*##g' | sed 's#"*,* *$##g' )
track_2_parameters[$j]=$(grep -m 1 $parameter <<< "${track_template[2]}" | sed 's#.*"'"$parameter"'": "*##g' | sed 's#"*,* *$##g' )
let "j+=1"
done
auphonic_add_tracks
json=$(curl -s -X POST -H "Content-Type: application/json" \
https://auphonic.com/api/productions.json \
-u $auphonic_user:$auphonic_pass \
-d '{
"preset": "'"$auphonic_preset_multi"'",
"metadata": { "title": "'"$basename"'" },
"output_basename": "'"$basename_auphonic"'",
"multi_input_files": [
'"$auphonic_tracks"'
]
}')
fi # <-- tracks =2 / >2
else # no multi preset
auphonic_add_tracks
# create production using standard parameters
basename_auphonic="$basename"
json=$(curl -s -X POST -H "Content-Type: application/json" \
https://auphonic.com/api/productions.json \
-u $auphonic_user:$auphonic_pass \
-d '{
"metadata": { "title": "'"$basename"'" },
"output_basename": "'"$basename_auphonic"'",
"multi_input_files": [
'"$auphonic_tracks"'
],
"algorithms": {
"leveler": false,
"crossgate": true,
"gate": false
},
"is_multitrack": true
}')
fi # <-- preset exists
fi # <-- tracks > 1
auphonic_error_check
# extract production ID from response
uuid=$(grep uuid -m 1 <<< "$json" | sed 's#^.*"uuid": "##g' | sed 's#", $##g')
# upload files
if [ -n "$auphonic_in" ] ; then # incoming file service is configured
for inputfile in "${input_files[@]}" ; do
printf "Auphonic upload in progress (via Rclone %s): %s\n" "$auphonic_rclone" "$inputfile"
rclone --config="$rcloneconfig" copy "$inputfile" "$auphonic_rclone"
done
elif [ "$tracks" -eq 1 ] ; then # no incoming file service -> direct upload to Auphonic
auphonic_upload "input_file" "${input_files[0]}"
else
auphonic_upload "${track_1_parameters[4]}" "${input_files[0]}"
if [ "$tracks" -eq 2 ] ; then
auphonic_upload "${track_2_parameters[4]}" "${input_files[1]}"
else
for (( i=1; i < ${tracks}; i++ )); do
auphonic_upload "${track_2_parameters[4]} $i" "${input_files[$i]}" # added counter for track id
done
fi
fi
# manage speech recognition and file output for no-preset productions
if [[ "$tracks" -eq 1 && -z "$auphonic_preset_single" ]] || [[ "$tracks" -gt 1 && -z "$auphonic_preset_multi" ]] ; then
# add speech recognition
curl -X POST -H "Content-Type: application/json" \
https://auphonic.com/api/production/$uuid/speech_recognition.json \
-u $auphonic_user:$auphonic_pass \
-d '{"uuid": "'"$auphonic_stt"'", "language":"'"$language"'"}' > /dev/null 2>&1
# add output files
curl -s -H "Content-Type: application/json" -X POST \
https://auphonic.com/api/production/$uuid/output_files.json \
-u $auphonic_user:$auphonic_pass \
-d '[
{"format":"wav", "suffix":"-auphonic"},
{"format":"transcript", "ending":"html", "suffix":"'"-$language"'"},
{"format":"speech", "ending":"json", "suffix":"'"-$language"'"},
{"format":"subtitle", "ending":"vtt", "suffix":"'"-$language"'"}
]' > /dev/null 2>&1
if [ -n "$auphonic_out_use" ] ; then
# if set, tell Auphonic to move output files to destination
curl -s -H "Content-Type: application/json" -X POST \
https://auphonic.com/api/production/$uuid/outgoing_services.json \
-u $auphonic_user:$auphonic_pass \
-d '[{"uuid": "'"$auphonic_out"'"}]' > /dev/null 2>&1
download_flag=""
if [ "$auphonic_in" != "$auphonic_out" ] ; then
# if different from input destination, tell Auphonic to copy input files to output
if [ "$tracks" -gt 1 ] ; then
curl -s -H "Content-Type: application/json" -X POST \
https://auphonic.com/api/production/$uuid/output_files.json \
-u $auphonic_user:$auphonic_pass \
-d '[{"format":"tracks","ending":"wav.zip","suffix":".unprocessed"}]' > /dev/null 2>&1
else
curl -s -H "Content-Type: application/json" -X POST \
https://auphonic.com/api/production/$uuid/output_files.json \
-u $auphonic_user:$auphonic_pass \
-d '[{"format":"input"}]' > /dev/null 2>&1
fi
fi
else
download_flag="download files"
fi
fi
# start production
json=$(curl -s -X POST https://auphonic.com/api/production/$uuid/start.json -u $auphonic_user:$auphonic_pass)
auphonic_error_check
# check once every 30 seconds, if production is done. Abort after checking set number of times
timer=$(($timeout * 2))
while [[ $json != *"\"status_string\": \"Done\""* ]] ; do
printf 'Auphonic transcription in progress. Will check again in 30 seconds.\n'
sleep 30
json=$(curl -s https://auphonic.com/api/production/$uuid.json -u $auphonic_user:$auphonic_pass)
auphonic_error_check
if [ $timer -eq 0 ] ; then
printf 'Auphonic transcription timed out\n\n'
cleanup auphonic
help_and_exit 1
fi
let "timer-=1"
done
printf 'Auphonic transcription done.\n'
# download the results from Auphonic
if [ -n "$download_flag" ] || [[ -z $auphonic_out_use && "$destination" == /** ]] ; then
urls=$( grep 'download_url' <<< "$json" | sed 's#^.*"download_url": "##g' | sed 's#",*##g' )
# turn this into proper array:
IFS=$'\n' urls=(${urls})
# download and move
for url in "${urls[@]}" ; do
# trim leading and trailing spaces, just to be sure
url="${url#"${url%%[![:blank:]]*}"}"
url="${url%"${url##*[![:blank:]]}"}"
url_filename="${url##*/}"
printf "Auphonic download in progress: %s\n" "$url_filename"
( cd "$output_dir" ; curl -s -O "$url" -u $auphonic_user:$auphonic_pass ; output move "$output_dir/$url_filename" )
done
else
printf "Auphonic has copied the transcript to the remote output destination.\n"
fi
cleanup auphonic
fi
# done Auphonic transcription
cleanup all
# eof