Add scripts to normalize video audio and to convert avi to mp4

This commit is contained in:
Michael Campagnaro 2021-05-27 17:37:29 -04:00
parent 40b663b0ea
commit 75c1f344fa
3 changed files with 122 additions and 2 deletions

51
bin/avi-to-mp4 Normal file
View File

@ -0,0 +1,51 @@
#!/usr/bin/env bash
# This is for reencoding an AVI video. I wasn't able to play the encoded videos
# on my phone when using libx264 and libx265 encoders, but mpeg4 works. Also
# I'm using the aac codec because some old avi's were using a codec that's not
# supported in the mp4 container.
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 == "" || $2 == "" ]]; then
printf "${BOLD}${RED}Format: $0 <filename> <bitrate, e.g. \"4000k\"> <optional output name>${NORMAL}\n"
exit 1
fi
filename=$(basename -- "$1")
extension="${filename##*.}"
filename="${filename%.*}"
bitrate="$2"
output_name="$3"
if [[ $output_name == "" ]]; then
output="${filename}_CONVERTED-${bitrate}.mp4"
else
output="${output_name}.mp4"
fi
printf "\n${YELLOW}${BOLD}Encoding '$filename.$extension' with target bitrate $bitrate | output: $output${NORMAL}\n"
ffmpeg -i "$filename.$extension" -c:a aac -c:v mpeg4 -b:v $bitrate "$output"
printf "\n${GREEN}${BOLD}Done encoding '$filename.$extension' to '$output'${NORMAL}\n\n"

View File

@ -51,8 +51,7 @@ function encode() {
ffmpeg -i "$filename.$extension" -c:v libx264 -crf $crf -preset veryfast -profile:v baseline -level 3.0 -strict -2 "$output" ffmpeg -i "$filename.$extension" -c:v libx264 -crf $crf -preset veryfast -profile:v baseline -level 3.0 -strict -2 "$output"
else else
# File will be slightly larger than CPU encoding, but it's much faster to transcode and doesn't max out the CPU cores. # File will be slightly larger than CPU encoding, but it's much faster to transcode and doesn't max out the CPU cores.
ffmpeg -y -vsync 0 -hwaccel cuvid -c:v h264_cuvid -i "$filename.$extension" -c:a copy -c:v h264_nvenc \ ffmpeg -y -vsync 0 -hwaccel cuvid -c:v h264_cuvid -i "$filename.$extension" -c:a copy -c:v h264_nvenc -profile:v high -rc:v vbr_hq -cq:v $crf -b:v 5M -maxrate 5M -max_muxing_queue_size 9999 "$output"
-profile:v high -rc:v vbr_hq -cq:v $crf -b:v 5M -maxrate 5M -max_muxing_queue_size 9999 "$output"
fi fi
printf "\n${GREEN}${BOLD}Done encoding '$filename.$extension' with CRF $crf${NORMAL}\n\n" printf "\n${GREEN}${BOLD}Done encoding '$filename.$extension' with CRF $crf${NORMAL}\n\n"
} }

View File

@ -0,0 +1,70 @@
#!/usr/bin/env bash
# Use this to normalize the audio of a video using the average loudness, or RMS-based normalization.
# This does not re-encode the video.
#
# 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 "\n${BOLD}Usage: $0 <filename> <optional output name>${NORMAL}\n"
exit 1
fi
filename=$(basename -- "$1")
extension="${filename##*.}"
filename="${filename%.*}"
output_name="$2"
if [[ $output_name == "" ]]; then
output="${filename}_normalized_audio.$extension"
else
output="${output_name}.$extension"
fi
printf "\n${YELLOW}${BOLD}Normalizing audio in $filename.$extension | output: $output${NORMAL}\n"
# This is done in two passes. The first pass will compute the mean loudness and
# the second pass will normalize the audio using the mean as the target.
# -vn, -sn, and -dn tells ffmpeg to ignore non-audio streams during the analysis. This speeds things up.
ffmpeg -i "$filename.$extension" -af "volumedetect" -vn -sn -dn -f null /dev/null
#ffmpeg -i "$filename.$extension" -c:v copy -ac 1 "$output"
printf "\n${GREEN}${BOLD}Done normalizing audio in $filename.$extension | output: $output${NORMAL}\n\n"
#---------------------------------------
# This seems better. 2 pass using loudnorm filter.
# 1st pass: ffmpeg -i "$filename.$extension" -pass 1 -filter:a loudnorm=print_format=json -vn -sn -dn -f null /dev/null
# 2nd pass: ffmpeg -i "$filename.$extension" -c:v copy -pass 2 -filter:a loudnorm=linear=true:measured_I=$input_i:measured_LRA=$input_lra:measured_tp=$input_tp:measured_thresh=$input_thresh "$output"
# TODO: extract the $input_i, input_lra, etc from the 1st pass output so that this can be automated.
# TODO: stackoverflow said if there are subtitles or multiple vid streams then add "-map 0" before the output name. Test this.
# TODO: disable the log file or just delete it after normalizing.