esche
d575ec0a3e
added mkv support added support for multiple audio streams added support for thumbnails that are in a separate video stream added support for movies
80 lines
2.6 KiB
Bash
80 lines
2.6 KiB
Bash
#!/bin/bash
|
|
|
|
# set shopt-options used by this shell-script
|
|
# Note, 0 (true) from shopt -q is "false" in a math context.
|
|
shopt -q globstar; globstar_set=$?
|
|
((globstar_set)) && shopt -s globstar
|
|
shopt -q extglob; extglob_set=$?
|
|
((extglob_set)) && shopt -s extglob
|
|
|
|
# return default shopt-options
|
|
function finish {
|
|
((globstar_set)) && shopt -u globstar
|
|
((extglob_set)) && shopt -u extglob
|
|
}
|
|
trap finish EXIT
|
|
|
|
print_help() {
|
|
printf "Help: not implemented yet"
|
|
}
|
|
|
|
while getopts 'e:' flag; do
|
|
case "${flag}" in
|
|
e) extension+=("$OPTARG");;
|
|
h) print_help
|
|
exit 0;;
|
|
*) exit 1 ;;
|
|
esac
|
|
done
|
|
|
|
# write array into string
|
|
extension="${extension[*]}"
|
|
# remove leading space
|
|
extension="${extension##+([[:space:]])}"
|
|
# change separator
|
|
extension=${extension// /|}
|
|
|
|
for file in **/*.@($extension); do
|
|
|
|
# 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\""
|
|
|
|
# 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
|