11 The command uses ffprobe to analyze an input file with the ebur128
12 filter, and finally run ffmpeg to normalize the input depending on the
15 ffmpeg encoding arguments can be passed through the extra arguments
16 after options, for example as in:
17 normalize.py --input input.mp3 --output output.mp3 -- -loglevel debug -y
20 logging.basicConfig(format=
'normalize|%(levelname)s> %(message)s', level=logging.INFO)
21 log = logging.getLogger()
25 argparse.ArgumentDefaultsHelpFormatter, argparse.RawDescriptionHelpFormatter
31 parser = argparse.ArgumentParser(description=HELP, formatter_class=Formatter)
32 parser.add_argument(
'--input',
'-i', required=
True, help=
'specify input file')
33 parser.add_argument(
'--output',
'-o', required=
True, help=
'specify output file')
34 parser.add_argument(
'--dry-run',
'-n', help=
'simulate commands', action=
'store_true')
35 parser.add_argument(
'encode_arguments', nargs=
'*', help=
'specify encode options used for the actual encoding')
37 args = parser.parse_args()
40 'ffprobe',
'-v',
'error',
'-of',
'compact=p=0:nk=1',
41 '-show_entries',
'frame_tags=lavfi.r128.I',
'-f',
'lavfi',
42 f
"amovie='{args.input}',ebur128=metadata=1"
45 def _run_command(cmd, dry_run=False):
46 log.info(f
"Running command:\n$ {shlex.join(cmd)}")
48 result = subprocess.run(cmd, check=
True, stdout=subprocess.PIPE)
51 result = _run_command(analysis_cmd)
54 for line
in result.stdout.splitlines():
59 adjust = ref -
float(loudness)
60 if abs(adjust) < 0.0001:
61 logging.info(f
"No normalization needed for '{args.input}'")
64 logging.info(f
"Adjusting '{args.input}' by {adjust:.2f}dB...")
66 'ffmpeg',
'-i', args.input,
'-af', f
'volume={adjust:.2f}dB'
67 ] + args.encode_arguments + [args.output]
69 _run_command(normalize_cmd, args.dry_run)
72 if __name__ ==
'__main__':