normalize.py 1018 B

123456789101112131415161718192021222324252627282930313233
  1. #!/usr/bin/env python2
  2. import sys, subprocess
  3. if len(sys.argv) > 2:
  4. ifile = sys.argv[1]
  5. encopt = sys.argv[2:-1]
  6. ofile = sys.argv[-1]
  7. else:
  8. print 'usage: %s <input> [encode_options] <output>' % sys.argv[0]
  9. sys.exit(1)
  10. analysis_cmd = 'ffprobe -v error -of compact=p=0:nk=1 '
  11. analysis_cmd += '-show_entries frame_tags=lavfi.r128.I -f lavfi '
  12. analysis_cmd += "amovie='%s',ebur128=metadata=1" % ifile
  13. try:
  14. probe_out = subprocess.check_output(analysis_cmd, shell=True)
  15. except subprocess.CalledProcessError, e:
  16. sys.exit(e.returncode)
  17. loudness = ref = -23
  18. for line in probe_out.splitlines():
  19. sline = line.rstrip()
  20. if sline:
  21. loudness = sline
  22. adjust = ref - float(loudness)
  23. if abs(adjust) < 0.0001:
  24. print 'No normalization needed for ' + ifile
  25. else:
  26. print "Adjust %s by %.1fdB" % (ifile, adjust)
  27. norm_cmd = ['ffmpeg', '-i', ifile, '-af', 'volume=%fdB' % adjust]
  28. norm_cmd += encopt + [ofile]
  29. print ' => %s' % ' '.join(norm_cmd)
  30. subprocess.call(norm_cmd)