[FFmpeg-devel] [PATCH] tools: update normalize script example to use loudnorm

Kyle Swanson k at ylo.ph
Mon Jun 13 05:44:15 CEST 2016


Signed-off-by: Kyle Swanson <k at ylo.ph>
---
 tools/normalize.py | 33 ------------------------------
 tools/normalize.rb | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 60 insertions(+), 33 deletions(-)
 delete mode 100755 tools/normalize.py
 create mode 100644 tools/normalize.rb

diff --git a/tools/normalize.py b/tools/normalize.py
deleted file mode 100755
index 7d87c5e..0000000
--- a/tools/normalize.py
+++ /dev/null
@@ -1,33 +0,0 @@
-#!/usr/bin/env python2
-
-import sys, subprocess
-
-if len(sys.argv) > 2:
-    ifile  = sys.argv[1]
-    encopt = sys.argv[2:-1]
-    ofile  = sys.argv[-1]
-else:
-    print 'usage: %s <input> [encode_options] <output>' % sys.argv[0]
-    sys.exit(1)
-
-analysis_cmd  = 'ffprobe -v error -of compact=p=0:nk=1 '
-analysis_cmd += '-show_entries frame_tags=lavfi.r128.I -f lavfi '
-analysis_cmd += "amovie='%s',ebur128=metadata=1" % ifile
-try:
-    probe_out = subprocess.check_output(analysis_cmd, shell=True)
-except subprocess.CalledProcessError, e:
-    sys.exit(e.returncode)
-loudness = ref = -23
-for line in probe_out.splitlines():
-    sline = line.rstrip()
-    if sline:
-        loudness = sline
-adjust = ref - float(loudness)
-if abs(adjust) < 0.0001:
-    print 'No normalization needed for ' + ifile
-else:
-    print "Adjust %s by %.1fdB" % (ifile, adjust)
-    norm_cmd  = ['ffmpeg', '-i', ifile, '-af', 'volume=%fdB' % adjust]
-    norm_cmd += encopt + [ofile]
-    print ' => %s' % ' '.join(norm_cmd)
-    subprocess.call(norm_cmd)
diff --git a/tools/normalize.rb b/tools/normalize.rb
new file mode 100644
index 0000000..53ea058
--- /dev/null
+++ b/tools/normalize.rb
@@ -0,0 +1,60 @@
+#!/usr/bin/env ruby
+
+require 'open3'
+require 'json'
+
+ffmpeg_bin = '/usr/local/bin/ffmpeg'
+target_il  = -24.0
+target_lra = +11.0
+target_tp  = -2.0
+samplerate = '48k'
+
+if ARGF.argv.count != 2
+  puts "Usage: #{$PROGRAM_NAME} input.wav output.wav"
+  exit 1
+end
+
+ff_string  = "#{ffmpeg_bin} -hide_banner "
+ff_string += "-i #{ARGF.argv[0]} "
+ff_string += '-af loudnorm='
+ff_string += "I=#{target_il}:"
+ff_string += "LRA=#{target_lra}:"
+ff_string += "tp=#{target_tp}:"
+ff_string += 'print_format=json '
+ff_string += '-f null -'
+
+_stdin, _stdout, stderr, wait_thr = Open3.popen3(ff_string)
+
+if wait_thr.value.success?
+  stats = JSON.parse(stderr.read.lines[-12, 12].join)
+  loudnorm_string  = '-af loudnorm='
+  loudnorm_string += 'print_format=summary:'
+  loudnorm_string += 'linear=true:'
+  loudnorm_string += "I=#{target_il}:"
+  loudnorm_string += "LRA=#{target_lra}:"
+  loudnorm_string += "tp=#{target_tp}:"
+  loudnorm_string += "measured_I=#{stats['input_i']}:"
+  loudnorm_string += "measured_LRA=#{stats['input_lra']}:"
+  loudnorm_string += "measured_tp=#{stats['input_tp']}:"
+  loudnorm_string += "measured_thresh=#{stats['input_thresh']}:"
+  loudnorm_string += "offset=#{stats['target_offset']}"
+else
+  puts stderr.read
+  exit 1
+end
+
+ff_string  = "#{ffmpeg_bin} -y -hide_banner "
+ff_string += "-i #{ARGF.argv[0]} "
+ff_string += "#{loudnorm_string} "
+ff_string += "-ar #{samplerate} "
+ff_string += ARGF.argv[1].to_s
+
+_stdin, _stdout, stderr, wait_thr = Open3.popen3(ff_string)
+
+if wait_thr.value.success?
+  puts stderr.read.lines[-12, 12].join
+  exit 0
+else
+  puts stderr.read
+  exit 1
+end
-- 
2.5.4 (Apple Git-61)



More information about the ffmpeg-devel mailing list