-
Notifications
You must be signed in to change notification settings - Fork 27
/
fix_volume
executable file
·85 lines (75 loc) · 3.35 KB
/
fix_volume
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/bin/bash
# this script takes one argument which is a video file. It evaluates the audio of the file, determines the difference needed to change the audio to -30dB as a mean_volume and then makes that adjustment to an output MXF file. The video track is simply copied.
# version 1.1 swap from ffmbc to ffmpeg
VERSION=1.1
unset DEPENDENCIES
DEPENDENCIES=(ffmpeg ffprobe)
# local variables
SUFFIX="_voladj"
SCRIPTDIR=$(dirname "${0}")
. "${SCRIPTDIR}/mmfunctions" || { echo "Missing '${SCRIPTDIR}/mmfunctions'. Exiting." ; exit 1 ;};
_usage(){
echo
echo "$(basename "${0}") ${VERSION}"
echo "This application will use an input video file to produce an output video file where the audio is adjusted to meet an integrated loudness of -23dB. If the integrated loudness of the input is already within 1dB of the target then no change will occur. The output file will be produced in the same directory as the input but be distinguished by a SUFFIX in the filename: ${SUFFIX}."
echo "Dependencies: ${DEPENDENCIES[@]}"
echo "Usage: $(basename "${0}") file1 [ file2 ...]"
echo " -h display this help"
echo
exit
}
[ "${#}" = 0 ] && _usage
_check_dependencies "${DEPENDENCIES[@]}"
# command line arguments
OPTIND=1
while getopts ":h" OPT ; do
case "${OPT}" in
h) _usage ;;
*) echo "Invalid option: -${OPTARG}" ; _writeerrorlog "fix_volume" "You used an invalid option and the script had to exit." ; exit 1 ;;
:) echo "Option -${OPTARG} requires an argument" ; _writeerrorlog "fix_volume" "The option selected required an argument and none was provided. The script had to exit." ; exit 1 ;;
esac
done
shift $(( ${OPTIND} - 1 ))
_cleanup(){
_log -a "Process aborted"
exit 1
}
trap _cleanup SIGHUP SIGINT SIGTERM
_log -b
while [ "${*}" != "" ] ; do
SOURCEFILE="${1}"
NAME=$(basename "${SOURCEFILE}")
EXTENSION="${NAME#*.}"
_get_codectagstring
unset MIDDLEOPTIONS
MIDDLEOPTIONS+=(-map 0:v)
MIDDLEOPTIONS+=(-c:v copy)
MIDDLEOPTIONS+=(-map 0:a)
MIDDLEOPTIONS+=(-c:a pcm_s24be)
MIDDLEOPTIONS+=(-ar 48000)
MIDDLEOPTIONS+=(-ac 2)
if [ "${CODEC_TAG_STRING}" = "mpeg" ] ; then
EXTENSION="mxf"
else
EXTENSION="mov"
fi
OUTPUT_MOVIE="${SOURCEFILE%.*}${SUFFIX}.${EXTENSION}"
if [ -f "${OUTPUT_MOVIE}" ] ; then
_report -wt "The intended output of $(basename "${0}") already exists. Skipping for now. Please delete ${OUTPUT_MOVIE} and rerun or figure out why you are trying to do this."
else
_get_volume_adjustment "${SOURCEFILE}"
if [ "${VOLADJ}" ] ; then
if [ $(echo "${VOLADJ} < 2" |bc) -eq 1 -a $(echo "${VOLADJ} > -2" |bc) -eq 1 ] ; then
_report -dt "Integrated loudness for $(basename "${INPUT_MOVIE}") is ${INTEGRATED_LOUDNESS}dB. Reference is ${REFERENCE}dB. No adjustment is needed, skipping."
else
_report -dt "Integrated loudness for $(basename "${INPUT_MOVIE}") is ${INTEGRATED_LOUDNESS}dB. Reference is ${REFERENCE}dB. Will adjust by ${VOLADJ}dB."
MIDDLEOPTIONS+=(-af volume=${VOLADJ}dB)
_report -dt "Generating ${OUTPUT_MOVIE} ... using these options: ${MIDDLEOPTIONS[*]}"
ffmpeg -report -i "${SOURCEFILE}" "${MIDDLEOPTIONS[@]}" "${OUTPUT_MOVIE}"
_report -dst "Done with ${NAME}."
fi
fi
fi
shift
done
_log -e