Skip to content

Instantly share code, notes, and snippets.

@izderadicka
Last active November 24, 2018 11:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save izderadicka/0f66a9ee1a2594be1fd27792bbc0a18c to your computer and use it in GitHub Desktop.
Save izderadicka/0f66a9ee1a2594be1fd27792bbc0a18c to your computer and use it in GitHub Desktop.
Converts mp3 files to opus concurrently (one conversion per core)
#!/bin/bash
# Author : <Ivan Zderadicka> ivan@zderadicka.eu
# License: MIT
VERSION=0.1.1
BITRATE=48
CUTOFF=20000
APPLICATION=audio
FORMAT=opus
QUALITY=10
trap "exit 2" SIGINT
print_help () {
cat <<EOF
Converts audio files to opus encoding
Usage: $0 [options] <directory>...
-h, --help prints help and exits
-v,--version prints version and exits
-d, --delete deletes input file after conversion
-r, --recursive converts recursiverly in subdirectories
-b <bitrate>,
--bitrate <bitrate> bitrate in kilobits eg. 24, 32, 48, 64, 96 ... [default: 48]
-c <feq>,
--cutoff <freq> cutoff frequency for low-pass filter [default: 20000]
recommended values: 4000, 6000, 8000, 12000, or 20000
-a <app>,
--app <app> application type - voip, audio or lowdelay
-f <format>,
--format <format> output container format - opus, mka, webm [default: opus]
-q <quality>,
--quality <quality> encoding quality 0 fast, low quality - 10 slow, high quality
[default: 10]
EOF
}
while (( $# > 0 ))
do
case $1 in
-b|--bitrate) # bitrate in kilobits eg. 24, 32. 64 ...
BITRATE="$2"
shift
;;
-c|--cutoff) # cutoff - for low pass filter - 4000, 6000, 8000, 12000, or 20000
CUTOFF="$2"
shift
;;
-a|--app) # application type - voip, audio or lowdelay
APPLICATION="$2"
shift
;;
-f|--format) # container format ogg, mkv, webm ...
FORMAT="$2"
shift
;;
-q|--quality) # compression quality - 0 fast, low quality - 10 - low high quality
QUALITY="$2"
shift
;;
-d|--delete) # delete input file after conversion
DELETE=YES
;;
-r|--recursive) # resurse into sub directories
RECURSIVE=" "
;;
-h|--help)
print_help
exit
;;
-v|--version)
echo Version: $VERSION
exit
;;
-*=*)
echo Do not support options with =
exit 1
;;
*)
break # unknown option, possibly argument
;;
esac
shift
done
wait_proc() {
while (( $(jobs -pr | wc -l ) >= $(nproc) )); do
sleep 1
done
}
while (( $# > 0 )); do
if [[ -n "$1" && -d "$1" ]]; then
while read -d $'\0' FILE ; do
echo "Processing $FILE"
{
ffmpeg -nostdin -v error -i "$FILE" -y -map_metadata 0 -map a -vn -acodec libopus -b:a ${BITRATE}k -vbr on -compression_level $QUALITY -application $APPLICATION -cutoff $CUTOFF "${FILE%.*}.$FORMAT"
if [[ $? -ne 0 ]]; then
echo "Encoding error on $FILE ret code $?" >&2
else
echo "Finished $FILE"
if [[ -n $DELETE ]]; then
rm "$FILE"
fi
fi
} &
wait_proc
done < <(find "$1" -depth ${RECURSIVE:--maxdepth 1} -type f -name "*.mp3" -print0 | sort -z)
else
echo "$1 is not directory" >&2
fi
shift
done
wait
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment