Complete rewrite

added mkv support
added support for multiple audio streams
added support for thumbnails that are in a separate video stream
added support for movies
This commit is contained in:
esche 2023-07-06 23:06:41 +02:00
parent 4905ad5154
commit d575ec0a3e

View file

@ -36,44 +36,45 @@ extension=${extension// /|}
for file in **/*.@($extension); do
#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=" ";
print "ffmpeg -i \""file"\" -map 0:a -c:a libopus"
}
{ # https://wiki.xiph.org/Opus_Recommended_Settings
if($1==1) {
if($2>=64000) print "-b:a:"NR-1" 64k"
}
else if($1==2) {
if($2>=128000) print "-b:a:"NR-1" 128k"
}
else if($1<=6) {
if ($2>=256000) print "-b:a:"NR-1" 256k"
}
else if ($2>=450000) print "-b:a:"NR-1" 450k"
}
END{
print "\""file_output"\" -v info -hide_banner"
}')
# array over the number of channels of all streams
readarray -t channels <<<"$(ffprobe -v error -select_streams a -show_entries stream=channels -of compact=p=0:nk=1 "$file")"
# array over the bitrates of all streams
readarray -t bitrate <<<"$(ffprobe -v error -select_streams a -show_entries stream=bit_rate -of compact=p=0:nk=1 "$file")"
# number of audiostreams
audiostreams=${#channels[@]}
# check if it contains video streams (Thumbnails etc. will be ignored)
isvideo=$(ffprobe -v error -select_streams V -show_entries stream=codec_type "$file")
use_mkv="false"
command="ffmpeg -i \"$file\""
is_video=$(ffprobe -i Hair.m4v -v fatal -select_streams V -show_entries stream=codec_type)
if [[ -n "$is_video" ]]; then
echo "$file contains a video stream and will not be converted"
#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.
else
eval "$command" #&& rm "$file"
# use mkv, if multiple audiostreams exists or it is a videofile
if [[ ${#channels[@]} -gt 1 || -n "$is_video" ]]; then
command="$command -map 0 -map -0:d -c copy -c:a libopus"
use_mkv="true"
fi
# set a channel dependend bitrate for every audio stream
# https://wiki.xiph.org/Opus_Recommended_Settings
# to use ffmpegs default just comment out this block
for ((index=0; index<audiostreams;index++)); do
if [[ ${channels[index]} -eq 1 ]]; then
if [[ $bitrate == 'N/A' || ${bitrate[index]} -gt 64000 ]]; then command="$command -b:a:$index 64k"; fi
elif [[ ${channels[index]} -eq 2 ]]; then
if [[ $bitrate == 'N/A' || ${bitrate[index]} -gt 128000 ]]; then command="$command -b:a:$index 128k"; fi
elif [[ ${channels[index]} -lt 6 ]]; then
if [[ $bitrate == 'N/A' || ${bitrate[index]} -gt 256000 ]]; then command="$command -b:a:$index 256k"; fi
elif [[ $bitrate == 'N/A' || ${bitrate[index]} -gt 450000 ]]; then command="$command -b:a:$index 450k"; fi
done
echo "$use_mkv"
if [[ $use_mkv = "true" ]]; then
command="$command -v info -hide_banner \"${file%.*}.mkv\""
else
command="$command -v info -hide_banner \"${file%.*}.opus\""
fi
eval "$command" #&& rm "$file"
done
exit 0