2023-03-24 15:14:15 +01:00
|
|
|
#!/bin/bash
|
2023-03-24 11:16:18 +01:00
|
|
|
|
2023-03-24 15:14:15 +01:00
|
|
|
# set shopt-options used by this shell-script
|
|
|
|
# Note, 0 (true) from shopt -q is "false" in a math context.
|
2023-03-24 11:16:18 +01:00
|
|
|
shopt -q globstar; globstar_set=$?
|
|
|
|
((globstar_set)) && shopt -s globstar
|
|
|
|
shopt -q extglob; extglob_set=$?
|
|
|
|
((extglob_set)) && shopt -s extglob
|
|
|
|
|
2023-03-24 15:14:15 +01:00
|
|
|
# return default shopt-options
|
|
|
|
function finish {
|
|
|
|
((globstar_set)) && shopt -u globstar
|
|
|
|
((extglob_set)) && shopt -u extglob
|
|
|
|
}
|
|
|
|
trap finish EXIT
|
2023-03-24 11:16:18 +01:00
|
|
|
|
2023-03-24 16:40:26 +01:00
|
|
|
|
2023-03-24 15:45:41 +01:00
|
|
|
# setting default extension to mp3
|
2023-03-24 15:14:15 +01:00
|
|
|
if [ $# -eq 0 ]; then
|
2023-03-24 15:45:41 +01:00
|
|
|
extension="mp3"
|
2023-03-24 15:14:15 +01:00
|
|
|
else
|
|
|
|
extension=$@
|
2023-03-24 16:40:26 +01:00
|
|
|
extension=${extension// /|}
|
2023-03-24 15:14:15 +01:00
|
|
|
fi
|
2023-03-24 11:16:18 +01:00
|
|
|
|
2023-03-24 16:40:26 +01:00
|
|
|
|
|
|
|
for file in **/*.@($extension); do
|
2023-03-24 17:51:01 +01:00
|
|
|
# getting information about the streams
|
|
|
|
channels=$(ffprobe "$file" -show_entries stream=channels -select_streams a -of compact=p=0:nk=1 -v 0)
|
|
|
|
bitrate=$(ffprobe "$file" -show_entries stream=bit_rate -select_streams a -of compact=p=0:nk=1 -v 0)
|
2023-03-24 17:03:07 +01:00
|
|
|
streams=$(echo "$channels" | wc -l)
|
2023-03-24 17:51:01 +01:00
|
|
|
|
|
|
|
# setting bitrate
|
2023-03-24 17:03:07 +01:00
|
|
|
if [ streams -gt 1 ]; then
|
|
|
|
echo "Error: Automatic Bitrate is not supported for files with multiple audiostreams. The default bitrate of 128k will be used"
|
2023-03-24 17:51:01 +01:00
|
|
|
bitrate="128k"
|
|
|
|
elif
|
|
|
|
case $channels in
|
|
|
|
1) if [$bitrate -gt 64000]; then bitrate="64k"; fi;;
|
|
|
|
2) if [$bitrate -gt 128000]; then bitrate="128k"; fi;;
|
|
|
|
6) if [$bitrate -gt 256000]; then bitrate="256k"; fi;;
|
|
|
|
8) if [$bitrate -gt 450000]; then bitrate="450k"; fi;;
|
|
|
|
*) bitrate=128k";;
|
|
|
|
esac
|
2023-03-24 17:03:07 +01:00
|
|
|
fi
|
2023-03-24 16:40:26 +01:00
|
|
|
# https://wiki.xiph.org/Opus_Recommended_Settings
|
|
|
|
|
2023-03-24 17:03:07 +01:00
|
|
|
ffmpeg -threads 4 -v 0 -i "$file" -map 0 -c:a libopus -b:a $bitrate "${file%.*}.opus" && rm "$file"
|
2023-03-24 15:14:15 +01:00
|
|
|
done
|
2023-03-24 11:16:18 +01:00
|
|
|
|
|
|
|
exit 0
|