dotfiles/dotfiles/bin/change-volume

60 lines
1.7 KiB
Bash

#!/usr/bin/env bash
# Re-encodes the audio, modify the volume based on the supplied db delta. If
# given a video file then the file is copied as-is. If you want to bring the
# max_volume to 0 db then call analyze-volume prior to this and pass a delta
# based on the reported max_volume.
#
# Inspired by https://superuser.com/a/323127 and https://superuser.com/a/1312885
if which tput >/dev/null 2>&1; then
ncolors=$(tput colors)
fi
if [ -t 1 ] && [ -n "$ncolors" ] && [ "$ncolors" -ge 8 ]; then
RED="$(tput setaf 1)"
GREEN="$(tput setaf 2)"
YELLOW="$(tput setaf 3)"
BLUE="$(tput setaf 4)"
MAGENTA="$(tput setaf 5)"
CYAN="$(tput setaf 6)"
BOLD="$(tput bold)"
NORMAL="$(tput sgr0)"
else
RED=""
GREEN=""
YELLOW=""
BLUE=""
MAGENTA=""
CYAN=""
BOLD=""
NORMAL=""
fi
if [[ $1 == "" ]]; then
printf "${BOLD}${RED}Usage: change-volume <video or audio filename> <output name> <volume delta in db>${NORMAL}\n"
exit 1
fi
filename=$(basename -- "$1")
extension="${filename##*.}"
filename="${filename%.*}"
output_name="$2"
delta_db="$3"
if [[ $output_name == "" ]]; then
output="${filename}_normalized_audio.$extension"
else
output="${output_name}.$extension"
fi
printf "\n${YELLOW}${BOLD}Modifying audio volume in $filename.$extension | output: $output | delta: $delta_db${NORMAL}\n"
# Since we're re-encoding the audio we have to specify a codec to use.
cmd="ffmpeg -y -stats -loglevel level+error -i \"$filename.$extension\" -af \"volume=${delta_db}dB\" -c:v copy -c:a aac \"$output\""
printf "\n${BOLD}Running: $cmd\n\n${NORMAL}"
eval $cmd
printf "\n${GREEN}${BOLD}Done modifying volume in $filename.$extension | output: $output | delta: $delta_db${NORMAL}\n"