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-26 13:22:40 +02:00
|
|
|
|
|
|
|
#set a channel-dependent bitrate for every audio stream
|
|
|
|
command=$(
|
|
|
|
ffprobe "$file" -v 0 -show_entries stream=channels,bit_rate -select_streams a -of compact=p=0:nk=1:s="\ " |
|
|
|
|
awk -v file="$file" -v file_output="${file%.*}.opus" '
|
|
|
|
BEGIN{
|
|
|
|
ORS=" ";
|
2023-05-18 15:18:10 +02:00
|
|
|
print "ffmpeg -i \""file"\" -map 0:a -c:a libopus"
|
2023-03-26 13:22:40 +02:00
|
|
|
}
|
|
|
|
{ # https://wiki.xiph.org/Opus_Recommended_Settings
|
2023-07-04 15:48:03 +02:00
|
|
|
if($1==1) print $2>=64000 ? "-b:a:"NR-1" 64k" : "-b:a:"NR-1" "$2
|
|
|
|
else if($1==2) print $2>=128000 ? "-b:a:"NR-1" 128k" : "-b:a:"NR-1" "$2
|
|
|
|
else if($1<=6) print $2>=256000 ? "-b:a:"NR-1" 256k" : "-b:a:"NR-1" "$2
|
|
|
|
else print $2>=450000 ? "-b:a:"NR-1" 450k" : "-b:a:"NR-1" "$2
|
2023-03-26 13:22:40 +02:00
|
|
|
}
|
|
|
|
END{
|
2023-05-18 16:38:30 +02:00
|
|
|
print "\""file_output"\" -v info -hide_banner"
|
2023-03-26 13:22:40 +02:00
|
|
|
}')
|
|
|
|
|
|
|
|
|
2023-05-18 16:06:52 +02:00
|
|
|
is_video=$(ffprobe -i Hair.m4v -v fatal -select_streams V -show_entries stream=codec_type)
|
2023-05-18 15:43:17 +02:00
|
|
|
if [[ -n "$is_video" ]]; then
|
2023-05-18 16:00:37 +02:00
|
|
|
echo "$file is a video file and will not be converted"
|
2023-05-18 16:38:30 +02:00
|
|
|
|
|
|
|
#an Example for converting the audiostreams in a container while copying subtitles and videostreams.
|
|
|
|
#ffmpeg -i Hair.m4v -map 0:a -map 0:s -map 0:v -c:a libopus -c:v copy -c:s copy Hair.mkv -hide_banner -v info
|
|
|
|
|
|
|
|
#for extracting the audiostreams of a video file just use "eval "$command"".
|
|
|
|
#it is recommended to check the result before deleting the original file.
|
2023-05-18 15:43:17 +02:00
|
|
|
else
|
|
|
|
eval "$command" && rm "$file"
|
|
|
|
fi
|
2023-03-24 16:40:26 +01:00
|
|
|
|
2023-03-24 15:14:15 +01:00
|
|
|
done
|
2023-03-24 11:16:18 +01:00
|
|
|
|
|
|
|
exit 0
|